-
Notifications
You must be signed in to change notification settings - Fork 3
/
lexer.nim
491 lines (428 loc) · 12.9 KB
/
lexer.nim
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import lexbase, strutils, streams, idents, keywords, razcontext, types
type
Lexer* = object of BaseLexer
context*: RazContext
nextState: LexerState
tokenStartPos: int
fileIndex*: int32
c: char
# this must be keep in sync with TokenKindToStr array
TokenKind* = enum
tkInvalid, tkEof, tkComment, tkNestedComment
tkIdent, tkNumber, tkFloat, tkString, tkCharLit
tkSemiColon, tkAccent, tkComma
tkParLe, tkParRi, tkCurlyLe, tkCurlyRi, tkBracketLe, tkBracketRi
# tkDot..tkProp must be keep in sync with
# keywords.SpecialWords
tkDot, tkDotDot, tkColon, tkColonColon
tkBang, tkChoice, tkPercent, tkAt
tkEquals, tkGreaterOrEqual, tkLessOrEqual, tkOpr
tkProgram, tkStyle, tkAlias, tkFlex, tkEvent, tkProp
TokenVal* = object {.union.}
iNumber*: uint64
fNumber*: float64
ident*: Ident
Token* {.acyclic.} = object
kind*: TokenKind
val*: TokenVal
indent*: int
literal*: string
line*, col*: int
LexerState = proc(lex: var Lexer, tok: var Token): bool {.locks: 0, gcSafe.}
const
LineEnd = {'\l', '\c', EndOfFile}
OctalDigits = {'0'..'7'}
BinaryDigits = {'0'..'1'}
OpChars = {'+', '-', '*', '/', '\\', '<', '>', '!', '?', '^', '.',
'|', '=', '%', '&', '$', '@', '~', ':', '\x80'..'\xFF'}
TokenKindToStr: array[TokenKind, string] = [
"tkInvalid", "[EOF]", "tkComment", "tkNestedComment",
"tkIdent", "tkNumber", "tkFloat", "tkString", "tkCharLit",
";", "`", ",", "(", ")", "{", "}", "[", "]",
".", "..", ":", "::", "!", "|", "%", "@", "=", ">=", "<=", "tkOpr",
"program", "style", "alias", "flex", "event", "prop"]
proc toString*(kind: TokenKind): string =
result = TokenKindToStr[kind]
const
Tabulator* = '\x09'
ESC* = '\x1B'
CR* = '\x0D'
FF* = '\x0C'
LF* = '\x0A'
BEL* = '\x07'
BACKSPACE* = '\x08'
VT* = '\x0B'
{.push gcSafe, locks: 0.}
proc stateOuterScope(lex: var Lexer, tok: var Token): bool
proc stateLineComment(lex: var Lexer, tok: var Token): bool
proc stateNestedComment(lex: var Lexer, tok: var Token): bool
proc stateIdentifier(lex: var Lexer, tok: var Token): bool
proc stateNumber(lex: var Lexer, tok: var Token): bool
proc stateString(lex: var Lexer, tok: var Token): bool
proc stateCharLit(lex: var Lexer, tok: var Token): bool
proc stateOperator(lex: var Lexer, tok: var Token): bool
{.pop.}
proc openLexer*(stream: Stream, context: RazContext, fileIndex: int32): Lexer =
result.fileIndex = fileIndex
result.open(stream)
result.nextState = stateOuterScope
result.c = result.buf[result.bufpos]
result.context = context
proc advance(lex: var Lexer, step = 1) =
lex.bufpos.inc(step)
lex.c = lex.buf[lex.bufpos]
proc nextChar(lex: Lexer): char =
result = lex.buf[lex.bufpos + 1]
proc tokenLen(lex: Lexer): int =
result = lex.bufpos - lex.tokenStartPos
proc tokenLit(lex: Lexer): string =
result = newString(lex.tokenLen)
copyMem(result.cstring, lex.buf[lex.tokenStartPos].unsafeAddr, result.len)
proc getToken*(lex: var Lexer, tok: var Token) =
while not lex.nextState(lex, tok): discard
proc error(lex: Lexer, kind: MsgKind, args: varargs[string, `$`]) =
var err = new(SourceError)
err.msg = lex.context.msgKindToString(kind, args)
err.line = lex.lineNumber
err.column = lex.getColNumber(lex.bufpos)
err.lineContent = lex.getCurrentLine(false)
err.fileIndex = lex.fileIndex
raise err
proc lexCR(lex: var Lexer) =
lex.bufpos = lex.handleCR(lex.bufpos)
lex.c = lex.buf[lex.bufpos]
proc lexLF(lex: var Lexer) =
lex.bufpos = lex.handleLF(lex.bufpos)
lex.c = lex.buf[lex.bufpos]
template singleToken(tokKind: TokenKind) =
tok.col = lex.getColNumber(lex.bufpos)
tok.kind = tokKind
tok.literal.add lex.c
lex.advance
return true
#[template doubleToken(kindSingle: TokenKind, secondChar: char, kindDouble: TokenKind) =
tok.col = lex.getColNumber(lex.bufpos)
tok.literal.add lex.c
if lex.nextChar == secondChar:
lex.advance
tok.kind = kindDouble
tok.literal.add lex.c
else:
tok.kind = kindSingle
lex.advance
return true
]#
template tokenNextState(tokKind: TokenKind, nextStateProc: typed) =
tok.col = lex.getColNumber(lex.bufpos)
tok.kind = tokKind
lex.nextState = nextStateProc
return false
proc stateOuterScope(lex: var Lexer, tok: var Token): bool =
let lineStart = lex.getColNumber(lex.bufpos) == 0
while lex.c == ' ':
lex.advance
tok.line = lex.lineNumber
if lineStart:
tok.indent = lex.getColNumber(lex.bufpos)
case lex.c
of '#':
tok.col = lex.getColNumber(lex.bufpos)
if lex.nextChar == '[':
tok.kind = tkNestedComment
lex.nextState = stateNestedComment
else:
tok.kind = tkComment
lex.nextState = stateLineComment
return false
of '\t':
lex.error(errTabsAreNotAllowed)
of '\c':
lex.lexCR
return false
of '\l':
lex.lexLF
return false
of IdentStartChars: tokenNextState(tkIdent, stateIdentifier)
of Digits: tokenNextState(tkNumber, stateNumber)
of '"': tokenNextState(tkString, stateString)
of '\'': tokenNextState(tkCharLit, stateCharLit)
of EndOfFile:
tok.indent = 0
singleToken(tkEof)
of ';': singleToken(tkSemiColon)
of '`': singleToken(tkAccent)
of '(': singleToken(tkParLe)
of ')': singleToken(tkParRi)
of '{': singleToken(tkCurlyLe)
of '}': singleToken(tkCurlyRi)
of '[': singleToken(tkBracketLe)
of ']': singleToken(tkBracketRi)
of ',': singleToken(tkComma)
else:
if lex.c in OpChars: tokenNextstate(tkOpr, stateOperator)
else: lex.error(errInvalidToken, lex.c)
result = true
proc stateLineComment(lex: var Lexer, tok: var Token): bool =
while lex.c notin LineEnd:
lex.advance
lex.nextState = stateOuterScope
result = true
proc stateNestedComment(lex: var Lexer, tok: var Token): bool =
lex.advance # skip '['
var level = 1
while true:
case lex.c
of '\c': lex.lexCR
of '\l': lex.lexLF
of '#':
if lex.nextChar == '[':
lex.advance
inc(level)
lex.advance
of ']':
if lex.nextChar == '#':
lex.advance
dec(level)
if level == 0:
lex.advance
lex.nextState = stateOuterScope
return true
lex.advance
of EndOfFile:
lex.error(errUnexpectedEOLinMultiLineComment)
else:
lex.advance
proc stateIdentifier(lex: var Lexer, tok: var Token): bool =
lex.tokenStartPos = lex.bufpos
while lex.c in IdentChars:
lex.advance
tok.literal = lex.tokenLit
tok.val.ident = lex.context.getIdent(tok.literal)
if tok.val.ident.id > ord(wProgram) and tok.val.ident.id < ord(wThis):
let id = tok.val.ident.id - ord(wProgram)
tok.kind = TokenKind(id + ord(tkProgram))
lex.nextState = stateOuterScope
result = true
proc charToInt(c: char): int =
case c
of {'0'..'9'}: result = ord(c) - ord('0')
of {'a'..'f'}: result = ord(c) - ord('a') + 10
of {'A'..'F'}: result = ord(c) - ord('A') + 10
else: result = 0
proc parseBoundedInt(lex: var Lexer, val: string, base: int, maxVal: uint64, start = 0): uint64 =
result = 0
for i in start.. <val.len:
result = result * uint64(base)
let c = uint64(charToInt(val[i]))
let ov = maxVal - result
if c > ov:
lex.error(errNumberOverflow)
else: result += c
proc stateNumber(lex: var Lexer, tok: var Token): bool =
type
NumberType = enum
UnknownNumber
OrdinaryNumber
HexNumber
OctalNumber
BinaryNumber
FloatNumber
template matchChars(lex: var Lexer, validRange, wideRange: set[char]) =
while true:
while lex.c in wideRange:
if lex.c notin validRange:
lex.error(errInvalidNumberRange, lex.c)
tok.literal.add lex.c
lex.advance
if lex.c == '_':
lex.advance
if lex.c notin wideRange:
lex.error(errInvalidToken, "_")
if lex.c notin wideRange:
break
var numberType = UnknownNumber
if lex.c == '0':
let nc = lex.nextChar
tok.literal.add lex.c
lex.advance
case nc
of 'x', 'X':
tok.literal.add lex.c
lex.advance
lex.matchChars(HexDigits, Letters + Digits)
numberType = HexNumber
of 'c', 'o', 'C', 'O':
tok.literal.add lex.c
lex.advance
lex.matchChars(OctalDigits, Digits)
numberType = OctalNumber
of 'b', 'B':
tok.literal.add lex.c
lex.advance
lex.matchChars(BinaryDigits, Digits)
numberType = BinaryNumber
else:
numberType = OrdinaryNumber
else:
numberType = OrdinaryNumber
if numberType == OrdinaryNumber:
lex.matchChars(Digits, Digits)
if lex.c == '.':
tok.literal.add lex.c
lex.advance
tok.kind = tkFloat
numberType = FloatNumber
if tok.kind == tkFloat:
lex.matchChars(Digits, Digits)
if lex.c in {'e', 'E'}:
tok.literal.add lex.c
lex.advance
if lex.c in {'+', '-'}:
tok.literal.add lex.c
lex.advance
while lex.c in Digits:
tok.literal.add lex.c
lex.advance
when defined(cpu64):
const maxVal = 0xFFFFFFFF_FFFFFFFF'u64
else:
const maxVal = 0xFFFFFFFF'u32
case numberType
of OrdinaryNumber: tok.val.iNumber = lex.parseBoundedInt(tok.literal, 10, maxVal)
of HexNumber: tok.val.iNumber = lex.parseBoundedInt(tok.literal, 16, maxVal, 2)
of OctalNumber: tok.val.iNumber = lex.parseBoundedInt(tok.literal, 8, maxVal, 2)
of BinaryNumber: tok.val.iNumber = lex.parseBoundedInt(tok.literal, 2, maxVal, 2)
of FloatNumber: tok.val.fNumber = parseFloat(tok.literal)
else: lex.error(errUnknownNumberType)
lex.nextState = stateOuterScope
result = true
proc handleHexChar(lex: var Lexer, xi: var int) =
case lex.c
of '0'..'9':
xi = (xi shl 4) or (ord(lex.c) - ord('0'))
lex.advance
of 'a'..'f':
xi = (xi shl 4) or (ord(lex.c) - ord('a') + 10)
lex.advance
of 'A'..'F':
xi = (xi shl 4) or (ord(lex.c) - ord('A') + 10)
lex.advance
else: discard
proc handleDecChars(lex: var Lexer, xi: var int) =
while lex.c in {'0'..'9'}:
xi = (xi * 10) + (ord(lex.c) - ord('0'))
lex.advance
template ones(n): untyped = ((1 shl n)-1) # for utf-8 conversion
proc getEscapedChar(lex: var Lexer, tok: var Token) =
lex.advance
case lex.c
of 'r', 'c', 'R', 'C':
tok.literal.add CR
lex.advance
of 'l', 'L', 'n', 'N':
tok.literal.add LF
lex.advance
of 'f', 'F':
tok.literal.add FF
lex.advance
of 't', 'T':
tok.literal.add Tabulator
lex.advance
of 'v', 'V':
tok.literal.add VT
lex.advance
of '\\', '"', '\'':
tok.literal.add lex.c
lex.advance
of 'a', 'A':
tok.literal.add BEL
lex.advance
of 'b', 'B':
tok.literal.add BACKSPACE
lex.advance
of 'e', 'E':
tok.literal.add ESC
lex.advance
of Digits:
var xi = 0
lex.handleDecChars(xi)
if (xi <= 255): add(tok.literal, chr(xi))
else: lex.error(errInvalidCharacterConstant, lex.c)
of 'x', 'X', 'u', 'U':
let tp = lex.c
lex.advance
var xi = 0
lex.handleHexChar(xi)
lex.handleHexChar(xi)
if tp in {'u', 'U'}:
lex.handleHexChar(xi)
lex.handleHexChar(xi)
# inlined toUTF-8 to avoid unicode and strutils dependencies.
if xi <=% 127:
add(tok.literal, xi.char)
elif xi <=% 0x07FF:
add(tok.literal, ((xi shr 6) or 0b110_00000).char)
add(tok.literal, ((xi and ones(6)) or 0b10_0000_00).char)
elif xi <=% 0xFFFF:
add(tok.literal, (xi shr 12 or 0b1110_0000).char)
add(tok.literal, (xi shr 6 and ones(6) or 0b10_0000_00).char)
add(tok.literal, (xi and ones(6) or 0b10_0000_00).char)
else: # value is 0xFFFF
add(tok.literal, "\xef\xbf\xbf")
else:
add(tok.literal, chr(xi))
else:
lex.error(errWrongEscapeChar, lex.c)
proc stateString(lex: var Lexer, tok: var Token): bool =
lex.advance
while true:
case lex.c
of LineEnd:
lex.error(errClosingQuoteExpected)
of '\\':
lex.getEscapedChar(tok)
of '"':
lex.advance
break
else:
tok.literal.add lex.c
lex.advance
lex.nextState = stateOuterScope
result = true
proc stateCharLit(lex: var Lexer, tok: var Token): bool =
lex.advance
case lex.c
of '\0'..pred(' '), '\'':
lex.error(errInvalidCharacterConstant, toHex(ord(lex.c), 2))
of '\\':
lex.getEscapedChar(tok)
else:
tok.literal.add lex.c
lex.advance
if lex.c != '\'':
lex.error(errMissingFinalQuote)
lex.advance
lex.nextState = stateOuterScope
result = true
proc stateOperator(lex: var Lexer, tok: var Token): bool =
while lex.c in OpChars:
tok.literal.add lex.c
lex.advance
tok.val.ident = lex.context.getIdent(tok.literal)
if tok.val.ident.id >= ord(wDot) and tok.val.ident.id < ord(wProgram):
let id = tok.val.ident.id - ord(wDot)
tok.kind = TokenKind(id + ord(tkDot))
lex.nextState = stateOuterScope
result = true
proc initToken*(): Token =
result.kind = tkInvalid
result.indent = -1
result.literal = ""
result.line = 0
result.col = 0
proc reset*(tok: var Token) =
tok.kind = tkInvalid
tok.indent = -1
tok.literal.setLen(0)
tok.line = 0
tok.col = 0