-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.go
292 lines (251 loc) · 7.1 KB
/
tokenizer.go
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
287
288
289
290
291
292
package main
import (
"fmt"
"strconv"
"strings"
)
type TokenType string
const (
TokenNewline TokenType = "Newline"
TokenIdentifier TokenType = "Identifier"
TokenNumber TokenType = "Number"
TokenString TokenType = "String"
TokenInternalLiteral TokenType = "InternalLiteral"
TokenEqualEqual TokenType = "EqualEqual"
TokenNotEqual TokenType = "NotEqual"
TokenGreaterThan TokenType = "GreaterThan"
TokenGreaterEqual TokenType = "GreaterEqual"
TokenLessThan TokenType = "LessThan"
TokenLessEqual TokenType = "LessEqual"
TokenPlus TokenType = "Plus"
TokenMinus TokenType = "Minus"
TokenAsterisk TokenType = "Asterisk"
TokenSlash TokenType = "Slash"
TokenPercent TokenType = "Percent"
TokenUnderscore TokenType = "Underscore"
TokenAmpersand TokenType = "Ampersand"
TokenPipe TokenType = "Pipe"
TokenOr TokenType = "Or"
TokenAnd TokenType = "And"
TokenBOr TokenType = "BOr"
TokenXOr TokenType = "XOr"
TokenBAnd TokenType = "BAnd"
TokenEquals TokenType = "Equals"
TokenParenOpen TokenType = "ParenOpen"
TokenParenClose TokenType = "ParenClose"
TokenBraceOpen TokenType = "BraceOpen"
TokenBraceClose TokenType = "BraceClose"
TokenBracketOpen TokenType = "BracketOpen"
TokenBracketClose TokenType = "BracketClose"
TokenComma TokenType = "Comma"
TokenIf TokenType = "If"
TokenOtherwise TokenType = "Otherwise"
TokenWhile TokenType = "While"
TokenFor TokenType = "For"
TokenExit TokenType = "Exit"
TokenFunction TokenType = "Function"
TokenLoop TokenType = "Loop"
TokenNext TokenType = "Next"
TokenIteration TokenType = "Iteration"
TokenFullStop TokenType = "FullStop"
)
type Token struct {
Type TokenType
Lexeme string
Literal any
Pos, Line, Col int
}
func (t *Token) LineCol() LineCol {
return LineCol{t.Line, t.Col}
}
var singleCharTokens = map[rune]TokenType{
'\n': TokenNewline,
'\r': TokenNewline,
'(': TokenParenOpen,
')': TokenParenClose,
'{': TokenBraceOpen,
'}': TokenBraceClose,
'[': TokenBracketOpen,
']': TokenBracketClose,
',': TokenComma,
'+': TokenPlus,
'_': TokenUnderscore,
'-': TokenMinus,
'*': TokenAsterisk,
'%': TokenPercent,
'&': TokenAmpersand,
'|': TokenPipe,
'.': TokenFullStop,
}
var keywordTokens = map[string]TokenType{
"if": TokenIf,
"otherwise": TokenOtherwise,
"while": TokenWhile,
"for": TokenFor,
"exit": TokenExit,
"function": TokenFunction,
"loop": TokenLoop,
"next": TokenNext,
"iteration": TokenIteration,
"or": TokenOr,
"and": TokenAnd,
"bor": TokenBOr,
"xor": TokenXOr,
"band": TokenBAnd,
}
func tokenize(input string) (tokens []Token, errors []error) {
addToken := func(token Token) {
tokens = append(tokens, token)
}
addError := func(err error) {
errors = append(errors, err)
}
isAlpha := func(c rune) bool { return c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') }
isAlphaNum := func(c rune) bool { return isDigit(c) || isAlpha(c) }
// col is set to 0 because we increment them right after reading
line, col := 1, 0
runes := []rune(input)
for i := 0; i < len(runes); i++ {
c := runes[i]
col += 1
tokenType, found := singleCharTokens[c]
if found {
addToken(Token{tokenType, string(c), nil, i, line, col})
if c == '\n' || c == '\r' {
line += 1
col = 0
}
continue
}
switch {
case c == ' ' || c == '\t':
break
case c == '/':
if i != len(runes)-1 && runes[i+1] == '/' {
// Comment
j := i + 1
// we don't increment col, because we don't expect any tokens (and thus errors) after //
if runes[j+1] == '\r' || runes[j+1] == '\n' {
// Commenting out the newline
i = j + 1
col = 0
line += 1
} else {
// Discard until end of line; but keep the newline
for ; j < len(runes) && (runes[j] != '\r' && runes[j] != '\n'); j++ {
}
i = j - 1
}
} else {
addToken(Token{TokenSlash, "/", nil, i, line, col})
}
case c == '=':
if i != len(runes)-1 && runes[i+1] == '=' {
i += 1
col += 1
addToken(Token{TokenEqualEqual, "==", nil, i, line, col})
} else {
addToken(Token{TokenEquals, "=", nil, i, line, col})
}
case c == '>':
if i != len(runes)-1 && runes[i+1] == '=' {
i += 1
col += 1
addToken(Token{TokenGreaterEqual, ">=", nil, i, line, col})
} else {
addToken(Token{TokenGreaterThan, ">", nil, i, line, col})
}
case c == '<':
if i != len(runes)-1 && runes[i+1] == '=' {
i += 1
col += 1
addToken(Token{TokenLessEqual, "<=", nil, i, line, col})
} else if i != len(runes)-1 && runes[i+1] == '>' {
i += 1
col += 1
addToken(Token{TokenNotEqual, "<>", nil, i, line, col})
} else {
addToken(Token{TokenLessThan, "<", nil, i, line, col})
}
case c == '"':
token, err := tokenizeString(runes[i+1:], i, line, col)
if err != nil {
addError(err)
break
}
addToken(token)
i += len(token.Lexeme) + 1
col += len(token.Lexeme) + 1
case isDigit(c):
token, err := tokenizeNumber(runes[i:], i, line, col)
if err != nil {
addError(err)
break
}
addToken(token)
i += len(token.Lexeme) - 1
col += len(token.Lexeme) - 1
case isAlpha(c):
j := i + 1
for ; j < len(runes) && isAlphaNum(runes[j]); j++ {
}
identifier := string(runes[i:j])
tokenType, found := keywordTokens[identifier]
if found {
addToken(Token{tokenType, identifier, nil, i, line, col})
} else {
addToken(Token{TokenIdentifier, identifier, nil, i, line, col})
}
newI := j - 1
col += (newI - i)
i = newI
default:
addError(fmt.Errorf("unexpected character: %c (line %d, col %d)", c, line, col))
}
}
return
}
func tokenizeString(runes []rune, pos, line, col int) (Token, error) {
i := 0
for i < len(runes) {
if runes[i] == '"' {
break
}
if i < len(runes)-4 && runes[i] == '$' && runes[i+1] == '{' && runes[i+2] == '"' && runes[i+3] == '}' {
i += 3
}
i += 1
}
if i == len(runes) || runes[i] != '"' {
return Token{}, fmt.Errorf("unterminated string at %d:%d", line, col)
}
lexeme := string(runes[0:i])
literal := strings.ReplaceAll(lexeme, "${\"}", "\"")
return Token{TokenString, lexeme, literal, pos, line, col}, nil
}
func tokenizeNumber(runes []rune, pos, line, col int) (Token, error) {
i := 0
for ; i < len(runes) && (isDigit(runes[i]) || runes[i] == '\''); i++ {
}
/* TODO: float support disabled for now lol
if len(runes[i:]) >= 2 && runes[i] == '.' && isDigit(runes[i+1]) {
i += 1 // Consume the .
for ; i < len(runes) && isDigit(runes[i]); i++ {
}
}
*/
rawLexeme := string(runes[0:i])
fixedLexeme := strings.ReplaceAll(rawLexeme, "'", "")
literal, err := strconv.Atoi(fixedLexeme)
if err != nil {
// TODO: better errors for really big numbers
return Token{}, fmt.Errorf("error converting '%s' to int: %v", fixedLexeme, err)
}
if len(fixedLexeme) > 1 && fixedLexeme[0] == '0' && fixedLexeme[1] != '.' {
return Token{}, fmt.Errorf("numbers may not start with 0")
}
return Token{TokenNumber, rawLexeme, literal, pos, line, col}, nil
}
func isDigit(c rune) bool {
return c >= '0' && c <= '9'
}