-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scanner.kt
180 lines (144 loc) · 4.67 KB
/
Scanner.kt
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
package lox
import lox.Lox.Companion.error
import lox.TokenType.*
data class Scanner(
private val source: String,
) {
companion object {
@JvmStatic val keywords: Map<String, TokenType> = HashMap<String, TokenType>().apply {
put("and", AND)
put("class", CLASS)
put("else", ELSE)
put("false", FALSE)
put("for", FOR)
put("fun", FUN)
put("if", IF)
put("nil", NIL)
put("or", OR)
put("print", PRINT)
put("return", RETURN)
put("super", SUPER)
put("this", THIS)
put("true", TRUE)
put("var", VAR)
put("while", WHILE)
}
}
private val tokens = arrayListOf<Token>()
private var start = 0
private var current = 0
private var line = 1
fun scanTokens(): List<Token> {
while (!isAtEnd()) {
// We are at the beginning of the next lexeme.
start = current
scanToken()
}
return tokens.apply {
add(Token(EOF, "", null, line))
}
}
private fun isAtEnd() = (current >= source.length)
private fun scanToken() {
val c = advance()
when (c) {
'(' -> addToken(LEFT_PAREN)
')' -> addToken(RIGHT_PAREN)
'{' -> addToken(LEFT_BRACE)
'}' -> addToken(RIGHT_BRACE)
',' -> addToken(COMMA)
'.' -> addToken(DOT)
'-' -> addToken(MINUS)
'+' -> addToken(PLUS)
';' -> addToken(SEMICOLON)
'*' -> addToken(STAR)
'!' -> addToken(if (match('=')) BANG_EQUAL else BANG)
'=' -> addToken(if (match('=')) EQUAL_EQUAL else EQUAL)
'<' -> addToken(if (match('=')) LESS_EQUAL else LESS)
'>' -> addToken(if (match('=')) GREATER_EQUAL else GREATER)
'/' -> scanTokenForwardSlash()
' ', '\r', '\t' -> {} // ignore
'\n' -> scanTokenNewLine()
'"' -> scanTokenString()
else -> scanTokenDefault(c)
}
}
private fun scanTokenForwardSlash() = if (match('/')) {
while (peek() != '\n' && !isAtEnd()) advance() // A comment goes until the end of the line.
} else {
addToken(SLASH)
}
private fun scanTokenNewLine() {
line++
}
private fun scanTokenString() {
while (peek() != '"' && !isAtEnd()) {
if (peek() == '\n') line++
advance()
}
if (isAtEnd()) {
error(line, "Unterminated string.")
return
}
// The closing ".
advance()
// Trim the surrounding quotes.
val value = source.substring(start + 1, current - 1)
addToken(STRING, value)
}
private fun scanTokenDefault(c: Char) = if (isDigit(c)) {
scanTokenDigit(c)
} else if(isAlpha(c)) {
scanTokenIdentifier(c)
} else {
error(line, "Unexpected character.")
}
private fun scanTokenDigit(c: Char) {
while (isDigit(peek())) advance();
// Look for a fractional part.
if (peek() == '.' && isDigit(peekNext())) {
// Consume the "."
advance();
while (isDigit(peek())) advance();
}
addToken(NUMBER, source.substring(start, current).toDouble());
}
private fun scanTokenIdentifier(c: Char) {
while (isAlphaNumeric(peek())) advance()
val text = source.substring(start, current)
var type = keywords[text]
if (type == null) type = IDENTIFIER
addToken(IDENTIFIER);
}
private fun advance() = source[current++]
private fun addToken(
type: TokenType,
literal: Any? = null,
) = addToken(
type,
text = source.substring(start, current),
literal = literal,
)
private fun addToken(
type: TokenType,
text: String?,
literal: Any?,
) {
tokens.add(Token(type, text, literal, line))
}
private fun match(expected: Char): Boolean {
if (isAtEnd()) return false
if (!isSourceAtCurrentEquals(expected)) return false
current++
return true
}
private fun isSourceAtCurrentEquals(expected: Char) =
source[current] == expected
private fun isAlphaNumeric(c: Char) = isDigit(c) || isAlpha(c)
private fun isDigit(c: Char) = c in '0'..'9'
private fun isAlpha(c: Char) = (c in 'a'..'z') || (c in 'A'..'Z') || c == '_';
private fun peek() = if (isAtEnd()) '\u0000' else source[current]
private fun peekNext(): Char {
return if (current + 1 >= source.length) '\u0000' else source[current + 1]
}
}