-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathlexer.go
434 lines (395 loc) · 9.56 KB
/
lexer.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package lexer
import "bytes"
import "fmt"
import "regexp"
import "strconv"
import "strings"
//var reSpaces = regexp.MustCompile(`^\s+`)
var reNewLine = regexp.MustCompile("\r\n|\n\r|\n|\r")
var reIdentifier = regexp.MustCompile(`^[_\d\w]+`)
var reNumber = regexp.MustCompile(`^0[xX][0-9a-fA-F]*(\.[0-9a-fA-F]*)?([pP][+\-]?[0-9]+)?|^[0-9]*(\.[0-9]*)?([eE][+\-]?[0-9]+)?`)
var reShortStr = regexp.MustCompile(`(?s)(^'(\\\\|\\'|\\\n|\\z\s*|[^'\n])*')|(^"(\\\\|\\"|\\\n|\\z\s*|[^"\n])*")`)
var reOpeningLongBracket = regexp.MustCompile(`^\[=*\[`)
var reDecEscapeSeq = regexp.MustCompile(`^\\[0-9]{1,3}`)
var reHexEscapeSeq = regexp.MustCompile(`^\\x[0-9a-fA-F]{2}`)
var reUnicodeEscapeSeq = regexp.MustCompile(`^\\u\{[0-9a-fA-F]+\}`)
type Lexer struct {
chunk string // source code
chunkName string // source name
line int // current line number
nextToken string
nextTokenKind int
nextTokenLine int
}
func NewLexer(chunk, chunkName string) *Lexer {
return &Lexer{chunk, chunkName, 1, "", 0, 0}
}
func (self *Lexer) Line() int {
return self.line
}
func (self *Lexer) LookAhead() int {
if self.nextTokenLine > 0 {
return self.nextTokenKind
}
currentLine := self.line
line, kind, token := self.NextToken()
self.line = currentLine
self.nextTokenLine = line
self.nextTokenKind = kind
self.nextToken = token
return kind
}
func (self *Lexer) NextIdentifier() (line int, token string) {
return self.NextTokenOfKind(TOKEN_IDENTIFIER)
}
func (self *Lexer) NextTokenOfKind(kind int) (line int, token string) {
line, _kind, token := self.NextToken()
if kind != _kind {
self.error("syntax error near '%s'", token)
}
return line, token
}
func (self *Lexer) NextToken() (line, kind int, token string) {
if self.nextTokenLine > 0 {
line = self.nextTokenLine
kind = self.nextTokenKind
token = self.nextToken
self.line = self.nextTokenLine
self.nextTokenLine = 0
return
}
self.skipWhiteSpaces()
if len(self.chunk) == 0 {
return self.line, TOKEN_EOF, "EOF"
}
switch self.chunk[0] {
case ';':
self.next(1)
return self.line, TOKEN_SEP_SEMI, ";"
case ',':
self.next(1)
return self.line, TOKEN_SEP_COMMA, ","
case '(':
self.next(1)
return self.line, TOKEN_SEP_LPAREN, "("
case ')':
self.next(1)
return self.line, TOKEN_SEP_RPAREN, ")"
case ']':
self.next(1)
return self.line, TOKEN_SEP_RBRACK, "]"
case '{':
self.next(1)
return self.line, TOKEN_SEP_LCURLY, "{"
case '}':
self.next(1)
return self.line, TOKEN_SEP_RCURLY, "}"
case '+':
self.next(1)
return self.line, TOKEN_OP_ADD, "+"
case '-':
self.next(1)
return self.line, TOKEN_OP_MINUS, "-"
case '*':
self.next(1)
return self.line, TOKEN_OP_MUL, "*"
case '^':
self.next(1)
return self.line, TOKEN_OP_POW, "^"
case '%':
self.next(1)
return self.line, TOKEN_OP_MOD, "%"
case '&':
self.next(1)
return self.line, TOKEN_OP_BAND, "&"
case '|':
self.next(1)
return self.line, TOKEN_OP_BOR, "|"
case '#':
self.next(1)
return self.line, TOKEN_OP_LEN, "#"
case ':':
if self.test("::") {
self.next(2)
return self.line, TOKEN_SEP_LABEL, "::"
} else {
self.next(1)
return self.line, TOKEN_SEP_COLON, ":"
}
case '/':
if self.test("//") {
self.next(2)
return self.line, TOKEN_OP_IDIV, "//"
} else {
self.next(1)
return self.line, TOKEN_OP_DIV, "/"
}
case '~':
if self.test("~=") {
self.next(2)
return self.line, TOKEN_OP_NE, "~="
} else {
self.next(1)
return self.line, TOKEN_OP_WAVE, "~"
}
case '=':
if self.test("==") {
self.next(2)
return self.line, TOKEN_OP_EQ, "=="
} else {
self.next(1)
return self.line, TOKEN_OP_ASSIGN, "="
}
case '<':
if self.test("<<") {
self.next(2)
return self.line, TOKEN_OP_SHL, "<<"
} else if self.test("<=") {
self.next(2)
return self.line, TOKEN_OP_LE, "<="
} else {
self.next(1)
return self.line, TOKEN_OP_LT, "<"
}
case '>':
if self.test(">>") {
self.next(2)
return self.line, TOKEN_OP_SHR, ">>"
} else if self.test(">=") {
self.next(2)
return self.line, TOKEN_OP_GE, ">="
} else {
self.next(1)
return self.line, TOKEN_OP_GT, ">"
}
case '.':
if self.test("...") {
self.next(3)
return self.line, TOKEN_VARARG, "..."
} else if self.test("..") {
self.next(2)
return self.line, TOKEN_OP_CONCAT, ".."
} else if len(self.chunk) == 1 || !isDigit(self.chunk[1]) {
self.next(1)
return self.line, TOKEN_SEP_DOT, "."
}
case '[':
if self.test("[[") || self.test("[=") {
return self.line, TOKEN_STRING, self.scanLongString()
} else {
self.next(1)
return self.line, TOKEN_SEP_LBRACK, "["
}
case '\'', '"':
return self.line, TOKEN_STRING, self.scanShortString()
}
c := self.chunk[0]
if c == '.' || isDigit(c) {
token := self.scanNumber()
return self.line, TOKEN_NUMBER, token
}
if c == '_' || isLetter(c) {
token := self.scanIdentifier()
if kind, found := keywords[token]; found {
return self.line, kind, token // keyword
} else {
return self.line, TOKEN_IDENTIFIER, token
}
}
self.error("unexpected symbol near %q", c)
return
}
func (self *Lexer) next(n int) {
self.chunk = self.chunk[n:]
}
func (self *Lexer) test(s string) bool {
return strings.HasPrefix(self.chunk, s)
}
func (self *Lexer) error(f string, a ...interface{}) {
err := fmt.Sprintf(f, a...)
err = fmt.Sprintf("%s:%d: %s", self.chunkName, self.line, err)
panic(err)
}
func (self *Lexer) skipWhiteSpaces() {
for len(self.chunk) > 0 {
if self.test("--") {
self.skipComment()
} else if self.test("\r\n") || self.test("\n\r") {
self.next(2)
self.line += 1
} else if isNewLine(self.chunk[0]) {
self.next(1)
self.line += 1
} else if isWhiteSpace(self.chunk[0]) {
self.next(1)
} else {
break
}
}
}
func (self *Lexer) skipComment() {
self.next(2) // skip --
// long comment ?
if self.test("[") {
if reOpeningLongBracket.FindString(self.chunk) != "" {
self.scanLongString()
return
}
}
// short comment
for len(self.chunk) > 0 && !isNewLine(self.chunk[0]) {
self.next(1)
}
}
func (self *Lexer) scanIdentifier() string {
return self.scan(reIdentifier)
}
func (self *Lexer) scanNumber() string {
return self.scan(reNumber)
}
func (self *Lexer) scan(re *regexp.Regexp) string {
if token := re.FindString(self.chunk); token != "" {
self.next(len(token))
return token
}
panic("unreachable!")
}
func (self *Lexer) scanLongString() string {
openingLongBracket := reOpeningLongBracket.FindString(self.chunk)
if openingLongBracket == "" {
self.error("invalid long string delimiter near '%s'",
self.chunk[0:2])
}
closingLongBracket := strings.Replace(openingLongBracket, "[", "]", -1)
closingLongBracketIdx := strings.Index(self.chunk, closingLongBracket)
if closingLongBracketIdx < 0 {
self.error("unfinished long string or comment")
}
str := self.chunk[len(openingLongBracket):closingLongBracketIdx]
self.next(closingLongBracketIdx + len(closingLongBracket))
str = reNewLine.ReplaceAllString(str, "\n")
self.line += strings.Count(str, "\n")
if len(str) > 0 && str[0] == '\n' {
str = str[1:]
}
return str
}
func (self *Lexer) scanShortString() string {
if str := reShortStr.FindString(self.chunk); str != "" {
self.next(len(str))
str = str[1 : len(str)-1]
if strings.Index(str, `\`) >= 0 {
self.line += len(reNewLine.FindAllString(str, -1))
str = self.escape(str)
}
return str
}
self.error("unfinished string")
return ""
}
func (self *Lexer) escape(str string) string {
var buf bytes.Buffer
for len(str) > 0 {
if str[0] != '\\' {
buf.WriteByte(str[0])
str = str[1:]
continue
}
if len(str) == 1 {
self.error("unfinished string")
}
switch str[1] {
case 'a':
buf.WriteByte('\a')
str = str[2:]
continue
case 'b':
buf.WriteByte('\b')
str = str[2:]
continue
case 'f':
buf.WriteByte('\f')
str = str[2:]
continue
case 'n', '\n':
buf.WriteByte('\n')
str = str[2:]
continue
case 'r':
buf.WriteByte('\r')
str = str[2:]
continue
case 't':
buf.WriteByte('\t')
str = str[2:]
continue
case 'v':
buf.WriteByte('\v')
str = str[2:]
continue
case '"':
buf.WriteByte('"')
str = str[2:]
continue
case '\'':
buf.WriteByte('\'')
str = str[2:]
continue
case '\\':
buf.WriteByte('\\')
str = str[2:]
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': // \ddd
if found := reDecEscapeSeq.FindString(str); found != "" {
d, _ := strconv.ParseInt(found[1:], 10, 32)
if d <= 0xFF {
buf.WriteByte(byte(d))
str = str[len(found):]
continue
}
self.error("decimal escape too large near '%s'", found)
}
case 'x': // \xXX
if found := reHexEscapeSeq.FindString(str); found != "" {
d, _ := strconv.ParseInt(found[2:], 16, 32)
buf.WriteByte(byte(d))
str = str[len(found):]
continue
}
case 'u': // \u{XXX}
if found := reUnicodeEscapeSeq.FindString(str); found != "" {
d, err := strconv.ParseInt(found[3:len(found)-1], 16, 32)
if err == nil && d <= 0x10FFFF {
buf.WriteRune(rune(d))
str = str[len(found):]
continue
}
self.error("UTF-8 value too large near '%s'", found)
}
case 'z':
str = str[2:]
for len(str) > 0 && isWhiteSpace(str[0]) { // todo
str = str[1:]
}
continue
}
self.error("invalid escape sequence near '\\%c'", str[1])
}
return buf.String()
}
func isWhiteSpace(c byte) bool {
switch c {
case '\t', '\n', '\v', '\f', '\r', ' ':
return true
}
return false
}
func isNewLine(c byte) bool {
return c == '\r' || c == '\n'
}
func isDigit(c byte) bool {
return c >= '0' && c <= '9'
}
func isLetter(c byte) bool {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
}