-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.java
More file actions
180 lines (155 loc) · 4.74 KB
/
Copy pathScanner.java
File metadata and controls
180 lines (155 loc) · 4.74 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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Scanner {
private final String source;
private final List<Token> tokens = new ArrayList<>();
private int start = 0;
private int current = 0;
private int line = 1;
private static final Map<String, TokenType> keywords;
static {
keywords = new HashMap<>();
keywords.put("and", TokenType.AND);
keywords.put("class", TokenType.CLASS);
keywords.put("else", TokenType.ELSE);
keywords.put("false", TokenType.FALSE);
keywords.put("for", TokenType.FOR);
keywords.put("fun", TokenType.FUN);
keywords.put("if", TokenType.IF);
keywords.put("nil", TokenType.NIL);
keywords.put("or", TokenType.OR);
keywords.put("print", TokenType.PRINT);
keywords.put("return", TokenType.RETURN);
keywords.put("super", TokenType.SUPER);
keywords.put("this", TokenType.THIS);
keywords.put("true", TokenType.TRUE);
keywords.put("var", TokenType.VAR);
keywords.put("while", TokenType.WHILE);
}
public Scanner(String source) {
this.source = source;
}
public List<Token> scanTokens() {
while (!isAtEnd()) {
start = current;
scanToken();
}
tokens.add(new Token(TokenType.EOF, "", null, line));
return tokens;
}
private void scanToken() {
char c = advance();
switch (c) {
case '(': addToken(TokenType.LEFT_PAREN); break;
case ')': addToken(TokenType.RIGHT_PAREN); break;
case '{': addToken(TokenType.LEFT_BRACE); break;
case '}': addToken(TokenType.RIGHT_BRACE); break;
case ',': addToken(TokenType.COMMA); break;
case '.': addToken(TokenType.DOT); break;
case '-': addToken(TokenType.MINUS); break;
case '+': addToken(TokenType.PLUS); break;
case ';': addToken(TokenType.SEMICOLON); break;
case '*': addToken(TokenType.STAR); break;
case '!':
addToken(match('=') ? TokenType.BANG_EQUAL : TokenType.BANG);
break;
case '=':
addToken(match('=') ? TokenType.EQUAL_EQUAL : TokenType.EQUAL);
break;
case '<':
addToken(match('=') ? TokenType.LESS_EQUAL : TokenType.LESS);
break;
case '>':
addToken(match('=') ? TokenType.GREATER_EQUAL : TokenType.GREATER);
break;
case '/':
if (match('/')) {
while (peek() != '\n' && !isAtEnd()) advance();
} else {
addToken(TokenType.SLASH);
}
break;
case ' ': case '\r': case '\t': break;
case '\n': line++; break;
case '"': string(); break;
default:
if (isDigit(c)) {
number();
} else if (isAlpha(c)) {
identifier();
} else {
Lox.error(line, "Unexpected character");
}
}
}
private void identifier() {
while (isAlphaNumeric(peek())) advance();
String text = source.substring(start, current);
TokenType type = keywords.get(text);
if (type == null) type = TokenType.IDENTIFIER;
addToken(type);
}
private void number() {
while (isDigit(peek())) advance();
if (peek() == '.' && isDigit(peekNext())) {
// Consume the .
advance();
while (isDigit(peek())) advance();
}
addToken(TokenType.NUMBER, Double.parseDouble(source.substring(start, current)));
}
private void string() {
while (peek() != '"' && !isAtEnd()) {
if (peek() == '\n') line++;
advance();
}
if (isAtEnd()) {
Lox.error(line, "Unterminated string");
return;
}
// Consume the "
advance();
String value = source.substring(start + 1, current - 1);
addToken(TokenType.STRING, value);
}
private boolean match(char expected) {
if (isAtEnd()) return false;
if (source.charAt(current) != expected) return false;
current++;
return true;
}
private char peek() {
if (isAtEnd()) return '\0';
return source.charAt(current);
}
private char peekNext() {
if (current + 1 >= source.length()) return '\0';
return source.charAt(current + 1);
}
private boolean isAlpha(char c) {
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '_';
}
private boolean isAlphaNumeric(char c) {
return isAlpha(c) || isDigit(c);
}
private boolean isDigit(char c) {
return c >= '0' && c <= '9';
}
private boolean isAtEnd() {
return current >= source.length();
}
private char advance() {
return source.charAt(current++);
}
private void addToken(TokenType type) {
addToken(type, null);
}
private void addToken(TokenType type, Object literal) {
String text = source.substring(start, current);
tokens.add(new Token(type, text, literal, line));
}
}