-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lexer.java
220 lines (140 loc) · 4.91 KB
/
Lexer.java
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package net.zerobone.grammax.lexer;
import net.zerobone.grammax.lexer.tokens.CodeToken;
import net.zerobone.grammax.lexer.tokens.IdToken;
import net.zerobone.grammax.lexer.tokens.Token;
import net.zerobone.grammax.parser.GrxParser;
import java.io.IOException;
import java.io.InputStream;
public class Lexer {
private static final int EOF = -1;
private final InputStream stream;
private int line = 1;
private int current;
private boolean peeking = false;
public Lexer(InputStream stream) {
this.stream = stream;
}
private void readChar() throws IOException {
if (peeking) {
peeking = false;
return;
}
current = stream.read();
if (current == '\n') {
line++;
}
}
private void peekChar() throws IOException {
// assert !peeking;
peeking = true;
current = stream.read();
if (current == '\n') {
line++;
}
}
private void advancePeek() {
assert peeking;
peeking = false;
}
private Token constructPrimitiveToken(int type) {
return new Token(line, type);
}
public Token lex() throws IOException, LexerException {
for (;;) {
readChar();
if (current == -1) {
return constructPrimitiveToken(GrxParser.T_EOF);
}
if (current == ' ' || current == '\n' || current == '\t' || current == '\r') {
continue;
}
else if (current == '/') {
peekChar();
if (current == '/') {
advancePeek();
// single-line comment start
do {
readChar();
} while (current != '\n' && current != EOF);
}
continue;
}
break;
}
switch (current) {
case '=':
return constructPrimitiveToken(GrxParser.T_ASSIGN);
case ';':
return constructPrimitiveToken(GrxParser.T_SEMICOLON);
case '(':
return constructPrimitiveToken(GrxParser.T_LEFT_PAREN);
case ')':
return constructPrimitiveToken(GrxParser.T_RIGHT_PAREN);
case '{': {
// code block
int nestingLevel = 0;
StringBuilder sb = new StringBuilder();
for (;;) {
readChar();
if (current == EOF) {
break;
}
if (current == '{') {
nestingLevel++;
}
else if (current == '}') {
if (nestingLevel == 0) {
return new CodeToken(line, sb.toString());
}
nestingLevel--;
}
sb.append((char)current);
}
throw new LexerException("Reached end of input while scanning code block.", line);
}
case '%': {
readChar();
if (current == EOF) {
throw new LexerException("Unexpected end of file while reading directive header.", line);
}
if (!Character.isLetter(current)) {
throw new LexerException("Expected identifier after directive begin.", line);
}
// return new IdToken(line, readIndentifier());
String id = readIndentifier();
if (id.equals("type")) {
return constructPrimitiveToken(GrxParser.T_TYPE);
}
else if (id.equals("top")) {
return constructPrimitiveToken(GrxParser.T_TOP);
}
else if (id.equals("name")) {
return constructPrimitiveToken(GrxParser.T_NAME);
}
else if (id.equals("algo")) {
return constructPrimitiveToken(GrxParser.T_ALGO);
}
else {
throw new LexerException("Unknown directive '" + id + "'", line);
}
}
default:
break;
}
if (Character.isLetter(current)) {
return new IdToken(line, readIndentifier());
}
throw new LexerException("Invalid start of lexeme '" + (char)current + "'", line);
}
private String readIndentifier() throws IOException {
StringBuilder sb = new StringBuilder();
do {
sb.append((char)current);
peekChar();
if (current == EOF) {
break;
}
} while (Character.isLetterOrDigit((char)current) || current == '_');
return sb.toString();
}
}