-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexer.java
More file actions
286 lines (214 loc) · 6.81 KB
/
Lexer.java
File metadata and controls
286 lines (214 loc) · 6.81 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package plc.project;
import jdk.nashorn.internal.codegen.types.Type;
import java.util.ArrayList;
import java.util.List;
import static plc.project.Token.Type.*;
/**
* The lexer works through three main functions:
*
* - {@link #lex()}, which repeatedly calls lexToken() and skips whitespace
* - {@link #lexToken()}, which lexes the next token
* - {@link CharStream}, which manages the state of the lexer and literals
*
* If the lexer fails to parse something (such as an unterminated string) you
* should throw a {@link ParseException} with an index at the character which is
* invalid or missing.
*
* The {@link #peek(String...)} and {@link #match(String...)} functions are
* helpers you need to use, they will make the implementation a lot easier.
*/
public final class Lexer {
private final CharStream chars;
public Lexer(String input) {
chars = new CharStream(input);
}
/**
* Repeatedly lexes the input using {@link #lexToken()}, also skipping over
* whitespace where appropriate.
*/
public List<Token> lex() {
List<Token> tokens = new ArrayList<Token>();
while (chars.has(0)) {
while (peek(" ") || peek("\b") || peek("\n") || peek("\r") || peek("\t")) {
lexEscape();
}
if (chars.has(0)) {
chars.skip();
tokens.add(lexToken());
}
}
return tokens;
}
/**
* This method determines the type of the next token, delegating to the
* appropriate lex method. As such, it is best for this method to not change
* the state of the char stream (thus, use peek not match).
*
* The next character should start a valid token since whitespace is handled
* by {@link #lex()}
*/
public Token lexToken() {
if (peek("[A-Za-z_]")) {
return lexIdentifier();
}
else if (peek("[+-]", "[0-9]") || peek("[0-9]")) {
return lexNumber();
}
else if (peek("'")) {
return lexCharacter();
}
else if (peek("\"")) {
return lexString();
}
else {
return lexOperator();
}
}
public Token lexIdentifier() {
match("[A-Za-z_]");
while (match("[A-Za-z0-9_-]")) {
}
return chars.emit(IDENTIFIER);
}
public Token lexNumber() {
match("[+-]");
while (match("[0-9]")) {
}
if (peek("\\.", "[0-9]")) {
match("\\.");
while (match("[0-9]")) {
}
return chars.emit(DECIMAL);
}
else {
return chars.emit(INTEGER);
}
}
public Token lexCharacter() {
match("'");
if (match("[^'\\n\\r\\\\]")) {
if (match("'")) {
return chars.emit(CHARACTER);
}
else {
throw new ParseException("Error: Invalid/missing character!", chars.getIndex());
}
}
else {
if (match("\\\\", "[bnrt'\"\\\\]")) {
if (match("'")) {
return chars.emit(CHARACTER);
}
else {
throw new ParseException("Error: Invalid/missing character!", chars.getIndex());
}
}
else {
throw new ParseException("Error: Invalid/missing character!", chars.getIndex());
}
}
}
public Token lexString() {
match("\"");
if (match("\"")) {
return chars.emit(STRING);
}
if (!peek("[^\"\\n\\r\\\\]") && !peek("\\\\", "[bnrt'\"\\\\]")) {
if (peek("\\\\")) {
throw new ParseException("Error: Invalid string!", chars.getIndex() + 1);
}
else {
throw new ParseException("Error: Invalid string!", chars.getIndex());
}
}
while (match("[^\"\\n\\r\\\\]") || match("\\\\", "[bnrt'\"\\\\]")) {
}
if (match("\"")) {
return chars.emit(STRING);
}
else {
if (peek("\\\\")) {
throw new ParseException("Error: Invalid string!", chars.getIndex() + 1);
}
else {
throw new ParseException("Error: Invalid string!", chars.getIndex());
}
}
}
public void lexEscape() {
chars.advance();
}
public Token lexOperator() {
if (match("[<>!=]", "=")) {
return chars.emit(OPERATOR);
}
else {
match("[\\s\\S]");
return chars.emit(OPERATOR);
}
}
/**
* Returns true if the next sequence of characters match the given patterns,
* which should be a regex. For example, {@code peek("a", "b", "c")} would
* return true if the next characters are {@code 'a', 'b', 'c'}.
*/
public boolean peek(String... patterns) {
for (int i = 0; i < patterns.length; i++) {
if ( !chars.has(i) || !String.valueOf(chars.get(i)).matches(patterns[i]) ) {
return false;
}
}
return true;
}
/**
* Returns true in the same way as {@link #peek(String...)}, but also
* advances the character stream past all matched characters if peek returns
* true. Hint - it's easiest to have this method simply call peek.
*/
public boolean match(String... patterns) {
boolean peek = peek(patterns);
if (peek) {
for (int i = 0; i < patterns.length; i++){
chars.advance();
}
}
return peek;
}
/**
* A helper class maintaining the input string, current index of the char
* stream, and the current length of the token being matched.
*
* You should rely on peek/match for state management in nearly all cases.
* The only field you need to access is {@link #index} for any {@link
* ParseException} which is thrown.
*/
public static final class CharStream {
private final String input;
private int index = 0;
private int length = 0;
public CharStream(String input) {
this.input = input;
}
public boolean has(int offset) {
return index + offset < input.length();
}
public char get(int offset) {
return input.charAt(index + offset);
}
public int getIndex() {
return index;
}
public void advance() {
index++;
length++;
}
public void skip() {
length = 0;
}
public Token emit(Token.Type type) {
int start = index - length;
skip();
return new Token(type, input.substring(start, index), start);
}
}
}