-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLexer.java
More file actions
206 lines (185 loc) · 7.09 KB
/
Copy pathLexer.java
File metadata and controls
206 lines (185 loc) · 7.09 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
package com.fun;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Data
@Slf4j
public class Lexer {
private String sourceCode;
private int lineNum;
private String nextToken;
private TokenEnum nextTokenType;
private int nextTokenLineNum;
private Map<String, TokenEnum> keywords;
private Map<TokenEnum, String> tokenNameMap;
private Pattern regexName = Pattern.compile("^[_\\d\\w]+");
public Lexer(String sourceCode) {
this(sourceCode, 1, "", TokenEnum.TOKEN_EOF, 0);
}
public Lexer(String sourceCode, int lineNum, String nextToken, TokenEnum nextTokenType, int nextTokenLineNum) {
this.sourceCode = sourceCode;
this.lineNum = lineNum;
this.nextToken = nextToken;
this.nextTokenType = nextTokenType;
this.nextTokenLineNum = nextTokenLineNum;
this.keywords = new HashMap<>();
initKeyWords();
initTokenNameMap();
}
private void initKeyWords() {
this.keywords.put("print", TokenEnum.TOKEN_PRINT);
this.tokenNameMap = new HashMap<>();
}
private void initTokenNameMap() {
this.tokenNameMap.put(TokenEnum.TOKEN_EOF, "EOF");
this.tokenNameMap.put(TokenEnum.TOKEN_VAR_PREFIX, "$");
this.tokenNameMap.put(TokenEnum.TOKEN_LEFT_PAREN, "(");
this.tokenNameMap.put(TokenEnum.TOKEN_RIGHT_PAREN, ")");
this.tokenNameMap.put(TokenEnum.TOKEN_EQUAL, "=");
this.tokenNameMap.put(TokenEnum.TOKEN_QUOTE, "\"");
this.tokenNameMap.put(TokenEnum.TOKEN_DUOQUOTE,"\"\"");
this.tokenNameMap.put(TokenEnum.TOKEN_NAME, "Name");
this.tokenNameMap.put(TokenEnum.TOKEN_PRINT, "print");
this.tokenNameMap.put(TokenEnum.TOKEN_IGNORED, "Ignored");
}
private String scanName() {
return this.scan(regexName);
}
private String scan(Pattern pattern) {
Matcher m = pattern.matcher(this.sourceCode);
if (m.find()) {
String token = m.group();
if (!Objects.equals("", token)) {
this.skipSourceCode(token.length());
return token;
}
}
log.error("unreachable");
System.exit(-1);
return "";
}
public MatchedToken nextTokenIs(TokenEnum tokenEnum) {
MatchedToken matchedToken = this.getNextToken();
if (!Objects.equals(matchedToken.getTokenType(), tokenEnum)) {
log.error("next token type error");
System.exit(-1);
}
return matchedToken;
}
public TokenEnum lookAhead() {
if (this.nextTokenLineNum > 0) {
return this.nextTokenType;
}
int nowLineNum = this.lineNum;
MatchedToken matchedToken = this.getNextToken();
this.lineNum = nowLineNum;
this.nextTokenLineNum = matchedToken.getLineNum();
this.nextTokenType = matchedToken.getTokenType();
this.nextToken = matchedToken.getToken();
return matchedToken.getTokenType();
}
public void lookAheadAndSkip(TokenEnum tokenEnum) {
int nowLineNum = this.lineNum;
MatchedToken matchedToken = this.getNextToken();
if (!Objects.equals(matchedToken.getTokenType(), tokenEnum)) {
this.lineNum = nowLineNum;
this.nextTokenLineNum = matchedToken.getLineNum();
this.nextTokenType = matchedToken.getTokenType();
this.nextToken = matchedToken.getToken();
}
}
public MatchedToken getNextToken() {
if (this.nextTokenLineNum > 0) {
this.lineNum = this.nextTokenLineNum;
this.nextTokenLineNum = 0;
return new MatchedToken(this.lineNum, this.nextTokenType, this.nextToken);
}
return this.matchToken();
}
public MatchedToken matchToken() {
if (this.isIgnored()) {
return new MatchedToken(this.lineNum, TokenEnum.TOKEN_IGNORED, "Ignored");
}
if (this.sourceCode.length() == 0) {
return new MatchedToken(this.lineNum, TokenEnum.TOKEN_EOF, this.tokenNameMap.get(TokenEnum.TOKEN_EOF));
}
switch (this.sourceCode.charAt(0)) {
case '$':
this.skipSourceCode(1);
return new MatchedToken(this.lineNum, TokenEnum.TOKEN_VAR_PREFIX, "$");
case '(':
this.skipSourceCode(1);
return new MatchedToken(this.lineNum, TokenEnum.TOKEN_LEFT_PAREN, "(");
case ')':
this.skipSourceCode(1);
return new MatchedToken(this.lineNum, TokenEnum.TOKEN_RIGHT_PAREN, ")");
case '=':
this.skipSourceCode(1);
return new MatchedToken(this.lineNum, TokenEnum.TOKEN_EQUAL, "=");
case '"':
if (this.nextSourceCodeIs("\"\"")) {
this.skipSourceCode(2);
return new MatchedToken(this.lineNum, TokenEnum.TOKEN_DUOQUOTE, "\"\"");
}
this.skipSourceCode(1);
return new MatchedToken(this.lineNum, TokenEnum.TOKEN_QUOTE, "\"");
default:
break;
}
if (this.sourceCode.charAt(0) == '_' || this.isLetter(this.sourceCode.charAt(0))) {
String token = this.scanName();
return new MatchedToken(this.lineNum, this.keywords.getOrDefault(token, TokenEnum.TOKEN_NAME), token);
}
log.error(String.format("MatchToken(): unexpected symbol near '%d'.", (int) this.sourceCode.charAt(0)));
System.exit(-1);
return null;
}
private boolean isLetter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
private boolean nextSourceCodeIs(String s) {
return this.sourceCode.startsWith(s);
}
private void skipSourceCode(int i) {
this.sourceCode = this.sourceCode.substring(i);
}
private boolean isNewLine(char c) {
return c == '\r' || c == '\n';
}
private boolean isWhiteSpace(char c) {
return c == '\t' || c == '\n' || c == '\f' || c == ' ';
}
private boolean isIgnored() {
boolean result = false;
while (this.sourceCode.length() > 0) {
if (this.nextSourceCodeIs("\r\n") || this.nextSourceCodeIs("\n\r")) {
this.skipSourceCode(2);
this.lineNum += 1;
result = true;
} else if (isNewLine(this.sourceCode.charAt(0))) {
this.skipSourceCode(1);
this.lineNum += 1;
result = true;
} else if (isWhiteSpace(this.sourceCode.charAt(0))) {
this.skipSourceCode(1);
result = true;
} else {
break;
}
}
return result;
}
protected String scanBeforeToken(String token) {
String[] result = this.sourceCode.split(token);
if (result.length < 2) {
log.error("unreachable!");
System.exit(-1);
}
this.skipSourceCode(result[0].length());
return result[0];
}
}