1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
using VQDR.Expression;
namespace VQDR {
public errordomain ParserError {
NOT_READY,
INVALID_DATA;
}
enum CharType {
NULL = 0,
DIGIT,
UOP,
OP,
ALPHA,
DOT,
POP,
PCL,
COM,
UNKNOWN;
/**
* Determine character type.
* @param ch Character to be checked.
* @return The character type.
*/
CharType from_char (char ch) {
switch (ch) {
case '0': case '1':
case '2': case '3':
case '4': case '5':
case '6': case '7':
case '8': case '9':
return DIGIT;
case '+': case '-':
return UOP;
case '*': case '/':
return OP;
case '.':
return DOT;
case '(':
return POP;
case ')':
return PCL;
case ',':
return COM;
case ' ': case 0:
return NULL;
default:
if ((ch >= 'a' && ch <= 'z') || (ch>= 'A' && ch <= 'Z' )) {
return ALPHA;
}
assert_not_reached ();
}
}
}
public string parser_error_to_string (ParserError e) {
switch (e.code) {
case (ParserError.NOT_READY):
return "NOT READY";
case (ParserError.INVALID_DATA):
return "INVALID_DATA";
default:
assert_not_reached ();
}
}
class Parser {
private class Lexer {
private bool ready = false;
private string? data = null;
private size_t data_size = 0;
private Array<Token> tokens = null;
private char cur_char = 0;
private int index = -1;
public Lexer (string data) {
assert (data.length != 0);
this.data = data;
this.data_size = data.length;
this.cur_char = data[0];
this.index = 0;
tokens = new Array<Token> ();
}
public void lex () throws ParserError {
while (this.index <= this.data_size) {
}
this.ready = true;
}
private void advance () {
if (this.index < this.data_size && this.cur_char != '\0') {
this.index++;
this.cur_char = this.data[this.index];
} else {
assert_not_reached ();
}
}
public Array<Token>? get_tokens () throws ParserError {
if (!ready) {
throw (new ParserError.NOT_READY ("Lexer is not ready." +
"Needs to run Lexer.lex() first."));
}
return tokens;
}
}
}
}
|