-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.java
33 lines (30 loc) · 1000 Bytes
/
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
import java.util.ArrayList;
public class Lexer {
private ArrayList<IReadable> readers = new ArrayList<>();
public void register(IReadable reader){
readers.add(reader);
}
public ArrayList<Token> tokenize(String s){
if(readers.size() == 0) return null;
ArrayList<Token> tokens = new ArrayList<>();
int pos = 0;
while(pos < s.length()) {
int maxLength = 0;
Token maxToken = null;
for (IReadable reader : readers) {
Token token = reader.tryGetToken(s.substring(pos));
if (token != null)
if (token.getText().length() > maxLength) {
maxToken = token;
maxLength = token.getText().length();
}
}
if(maxLength != 0){
pos += maxLength;
tokens.add(maxToken);
}
else pos += 1;
}
return tokens;
}
}