This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.lua
818 lines (657 loc) · 18.6 KB
/
lexer.lua
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
--[[
Copyright 2018 kurzyx https://github.com/kurzyx
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]
-- This lexer is heavily based on the JavaScript lexer
-- from jshint. https://github.com/jshint/jshint
local Lexer = {
proto = {}
}
Lexer.proto.__index = Lexer.proto
Lexer.create = function(...)
local o = setmetatable({}, Lexer.proto)
o:__construct(...)
return o
end
-- Token types
Lexer.TokenType = {
EndOfFile = 0,
EndOfLine = 1,
Identifier = 2,
Punctuator = 3,
Keyword = 4,
Comment = 5,
NullLiteral = 6,
BooleanLiteral = 7,
NumericLiteral = 8,
StringLiteral = 9,
}
-- (reserved) keywords
Lexer.Keywords = {
'abstract',
'break',
'case', 'catch', 'class', 'continue',
'def', 'do',
'else', 'end', 'export', 'extends',
'false', 'final', 'finally', 'for',
'if', 'import',
'new', --[['nil', ]] 'null',
'object', 'override',
'package', 'private', 'protected',
'return',
'sealed', 'super',
'this', 'throw', 'trait', 'try', 'true',
'val', 'var',
'while', 'with'
}
local TokenType = Lexer.TokenType
local ContextType = Lexer.ContextType
local Keywords = Lexer.Keywords
local KeywordsAsKeys = {}
for _, keyword in ipairs(Lexer.Keywords) do
KeywordsAsKeys[keyword] = true
end
--
--
local isWhitespaceCharacter = function(c)
return c == ' ' -- space
or c == '\t' -- tab
or c == '\n'
or c == '\r'
end
local isHexDigit = function(char)
return char:match('^[0-9a-fA-F]$')
end
local isDecimalDigit = function(char)
return char:match('^[0-9]$')
end
local isOctalDigit = function(char)
return char:match('^[0-7]$')
end
local isBinaryDigit = function(char)
return char == '0' or char == '1'
end
--
--
Lexer.proto.__construct = function(self, input)
self.input = input
self.listeners = {}
end
--[[
- Subscribe to a token event.
- You can subscribe to multiple events with one call:
-
- lexer.on('identifier number', function(...)
- -- ...
- end)
]]
Lexer.proto.on = function(self, names, listener)
for name in names:gmatch('%s*(%w+)%s*') do
self.listeners[name] = self.listeners[name] or {}
table.insert(self.listeners[name], listener)
end
end
--[[
- Trigger a token event.
]]
Lexer.proto.trigger = function(self, name, ...)
for _, listener in ipairs(self.listeners[name] or {}) do
listener(...)
end
end
--
--
--[[
-
]]
Lexer.proto.start = function(self)
self:reset()
if type(self.input) ~= 'string' then
error("Input must be a string.")
end
-- Split at each new line
for line in self.input:gmatch('([^\r\n]*)[\r\n]?') do
table.insert(self.lines, line)
end
-- Pop last line (which is \0)
self.lines[#self.lines] = nil
self:nextLine()
end
--[[
- Resets the state of the lexer.
]]
Lexer.proto.reset = function(self)
self.exhausted = false
self.lines = {}
self.line = ""
self.lineNr = 0
self.charNr = 0
end
--[[
- Return the next i character without actually moving the
- char pointer.
]]
Lexer.proto.peek = function(self, i)
i = self.charNr + (i or 0)
return self.line:sub(i, i)
end
--[[
- Move the char pointer forward i (or 1) times.
]]
Lexer.proto.skip = function(self, i)
i = i or 1
self.charNr = self.charNr + i
end
--[[
- Move the char pointer to after the last character of the line.
]]
Lexer.proto.skipToEOL = function(self)
self.charNr = #self.line + 1
end
--[[
- Extract a comment out of the next sequence of characters and/or
- lines or return 'nil' if its not possible. Since comments can
- span across multiple lines this method has to move the char
- pointer.
]]
Lexer.proto.scanComments = function(self)
local char1 = self:peek()
local char2 = self:peek(1)
-- End of unbegun comment... Raise an error and skip it.
if char1 == '*' and char2 == '/' then
self:trigger('error', {
code = 'E018',
line = self.lineNr,
char = self.charNr
})
self:skip(2)
return nil
end
local startLineNr = self.lineNr
local startCharNr = self.charNr
-- Comments must start either with // or /*
if char1 ~= '/' or (char2 ~= '/' and char2 ~= '*') then
return null
end
-- One-line comment
if char2 == '/' then
local token = {
type = TokenType.Comment,
value = self.line:sub(self.charNr + 2),
line = startLineNr,
char = startCharNr,
isMultiline = false,
isMalformed = false
}
self:skipToEOL()
return token
end
-- Multi-line comment
local value = ""
self:skip(2)
-- Loop till we find the end
while true do
local pos = self.line:find('*/')
if pos ~= nil then
value = value .. self.line:sub(self.charNr, pos - 1)
self:skip(pos + 2)
break
end
-- Append remaining line
value = value .. self.line:sub(self.charNr) .. "\n"
-- If we hit EOF and our comment is still unclosed,
-- trigger an error and end the comment implicitly.
if not self:nextLine() then
self:trigger('error', {
code = 'E017',
line = startLineNr,
char = startCharNr
})
return {
type = TokenType.Comment,
value = value,
line = startLineNr,
char = startCharNr,
isMultiline = true,
isMalformed = true
}
end
end
return {
type = TokenType.Comment,
value = value,
line = startLineNr,
char = startCharNr,
isMultiline = true,
isMalformed = false
}
end
--[[
- Extract a string out of the next sequence of characters and/or
- lines or return 'nil' if its not possible. Since strings can
- span across multiple lines this method has to move the char
- pointer.
]]
Lexer.proto.scanStringLiteral = function(self)
local quote = self:peek()
-- String must start with a quote.
if quote ~= '\'' and quote ~= '"' then
return nil
end
local value = ""
local isMultiline = false
local startLineNr = self.lineNr
local startCharNr = self.charNr
self:skip()
-- Multi-line string
if self:peek() == quote and self:peek(1) == quote then
isMultiline = true
self:skip(2)
end
-- Check each character till ending
while true do
local char = self:peek()
if char == '' then -- End of string
-- If we hit EOL and the string is not multilined
-- trigger an error and end the string implicitly.
if isMultiline == false then
self:trigger('error', {
code = 'E029',
line = startLineNr,
char = startCharNr
})
return {
type = TokenType.StringLiteral,
value = value,
line = startLineNr,
char = startCharNr,
isMultiline = false,
isMalformed = true
}
end
-- If we hit EOF and our string is still unclosed,
-- trigger an error and end the string implicitly.
if not self:nextLine() then
self:trigger('error', {
code = 'E029', -- TODO: Check this error code...
line = startLineNr,
char = startCharNr
})
return {
type = TokenType.StringLiteral,
value = value,
line = startLineNr,
char = startCharNr,
isMultiline = true,
isMalformed = true
}
end
value = value .. '\n'
continue
end
-- Special treatment for some escaped characters.
if char == '\\' then
local parsed = self:scanEscapeSequence()
value = value .. parsed.char
self:skip(parsed.skip)
continue
end
-- Possible end of string
if char == quote then
if isMultiline == false then
self:skip()
break
end
-- Check for multi-line string ending
if self:peek(1) == quote and self:peek(2) == quote then
self:skip(3)
break
end
-- No end of multi-line string
end
value = value .. char
self:skip()
end
return {
type = TokenType.StringLiteral,
value = value,
line = startLineNr,
char = startCharNr,
isMultiline = isMultiline,
isMalformed = false
}
end
--[[
- Assumes previously parsed character was \ (== '\\') and was not skipped.
]]
Lexer.proto.scanEscapeSequence = function(self)
local char = self:peek(1)
if char == '' then -- End of line
return {
char = '',
skip = 1
}
end
local escpadedChars = {
['a'] = '\\a',
['b'] = '\\b',
['f'] = '\\f',
['n'] = '\\n',
['r'] = '\\r',
['t'] = '\\t',
['v'] = '\\v',
['\\'] = '\\\\',
['\''] = '\\\'',
['"'] = '\\"',
}
return {
char = escpadedChars[char] or char,
skip = 2
}
end
--[[
- Extract a punctuator out of the next sequence of characters
- or return 'nil' if its not possible.
]]
Lexer.proto.scanPunctuator = function(self)
local c1 = self:peek()
if c1 == nil then -- End of line
return nil
end
local _token = function(value)
self:skip(#value)
return {
type = TokenType.Punctuator,
value = value,
line = self.lineNr,
char = self.charNr
}
end
-- Explict single-character punctuators: ( ) [ ] { } ; , : ~ ?
if ('()[]{};,:~?'):find(c1, 1, true) then
return _token(c1)
end
-- (Possible) multiple-character punctuators
local c2 = self:peek(1)
local c3 = self:peek(2)
local c4 = self:peek(3)
if c1 == '.' then
-- Check if this is supposed to be a number
if c2:match('[0-9]') ~= nil then
return nil
end
-- ... (vararg)
if c2 == '.' and c3 == '.' then
return _token('...')
end
return _token('.')
end
-- 3-character punctuators: >>> <<= >>=
if c1 == '>' and c2 == '>' and (c3 == '>' or c3 == '=') then -- >>> >>=
return _token('>>' .. c3)
end
if c1 == '<' and c2 == '<' and c3 == '=' then -- <<=
return _token('<<=')
end
-- 2-character punctuators: <= >= == != ++ -- << >> && ||
-- += -= *= %= &= |= ^= /=
if c2 == '=' and ('<>=!+-*%&|^/'):find(c1, 1, true) then -- <= >= == != += -= *= %= &= |= ^= /=
return _token(c1 .. c2)
end
if c1 == c2 and ('+-<>&|'):find(c1, 1, true) then -- ++ -- << >> && ||
return _token(c1 .. c2)
end
-- 1-character punctuators: < > = ! + - * % & | ^ /
if ('<>=!+-*%&|^/'):find(c1, 1, true) then
return _token(c1)
end
return nil
end
--[[
- Extract a keyword out of the next sequence of characters or
- return 'nil' if its not possible.
]]
Lexer.proto.scanKeyword = function(self)
local p1, p2 = self.line:find('^[a-zA-Z_$][a-zA-Z0-9_$]*', self.charNr)
if p1 == nil then
return nil
end
local value = self.line:sub(p1, p2)
if KeywordsAsKeys[value] == nil then
return nil
end
local startCharNr = self.charNr
self:skip(p2 - p1 + 1)
return {
type = TokenType.Keyword,
value = value,
line = self.lineNr,
char = startCharNr
}
end
--[[
- Extract an identifier out of the next sequence of
- characters or return 'nil' if its not possible. In addition,
- to Identifier this method can also produce BooleanLiteral
- (true/false) and NullLiteral (null).
]]
Lexer.proto.scanIdentifier = function(self)
end
--[[
- Extract a numeric literal out of the next sequence of
- characters or return 'nil' if its not possible.
]]
Lexer.proto.scanNumericLiteral = function(self)
local char = self:peek()
-- Numbers must start either with a decimal digit or a point.
if char ~= '.' and not isDecimalDigit(char) then
return nil
end
local value = ""
local base = 10
local isAllowedDigit = isDecimalDigit
if char ~= '.' then
value = char
self:skip()
char = self:peek()
if value == '0' then
-- Base-16 numbers.
if char == 'x' or char == 'X' then
base = 16
isAllowedDigit = isHexDigit
end
-- Base-8 numbers.
if char == 'o' or char == 'O' then
base = 8
isAllowedDigit = isOctalDigit
end
-- Base-2 numbers.
if char == 'b' or char == 'B' then
base = 2
isAllowedDigit = isBinaryDigit
end
-- TODO: decimals with leading 0 is illegal
value = value .. char
self:skip()
end
-- Loop till the character is not a valid digit.
while true do
char = self:peek()
if not isAllowedDigit(char) then
break
end
value = value .. char
self:skip()
end
if base ~= 10 then
if #value == 2 then -- 0x 0o 0b
return {
type = TokenType.NumericLiteral,
value = value,
isMalformed = true
}
end
return {
type = TokenType.NumericLiteral,
value = value,
base = base,
isMalformed = false
}
end
end
-- Decimal digits
if char == '.' then
value = value .. char
self:skip()
-- Loop till the character is not a valid digit.
while true do
char = self:peek()
if not isDecimalDigit(char) then
break
end
value = value .. char
self:skip()
end
end
-- Exponent part.
if char == 'e' or char == 'E' then
value = value .. char
self:skip()
char = self:peek()
if char == '+' or char == '-' then
value = value .. char
self:skip()
end
char = self:peek()
if not isDecimalDigit(char) then -- illegal
return nil
end
value = value .. char
self:skip()
-- Loop till the character is not a valid digit.
while true do
char = self:peek()
if not isDecimalDigit(char) then
break
end
value = value .. char
self:skip()
end
end
return {
type = TokenType.NumericLiteral,
value = value,
base = base,
isMalformed = false
}
end
--[[
- Produce the next raw token or return 'nil' if no tokens can be matched.
- This method skips over all space characters.
]]
Lexer.proto.next = function(self)
local token
-- Move to the next non-whitespace character.
while isWhitespaceCharacter(self:peek()) do
self:skip()
end
-- Methods that work with multi-line structures and move the
-- character pointer.
token = nil
or self:scanComments()
or self:scanStringLiteral()
if token then
return token
end
-- Methods that don't move the character pointer.
local token = nil
or self:scanPunctuator()
or self:scanKeyword()
or self:scanIdentifier()
or self:scanNumericLiteral()
if token then
return token
end
-- No token could be matched, give up.
return nil
end
--[[
- Switch to the next line and reset all char pointers.
]]
Lexer.proto.nextLine = function(self)
if self.lineNr >= #self.lines then
return false
end
self.line = self.lines[self.lineNr + 1]
self.lineNr = self.lineNr + 1
self.charNr = 1
-- TODO: Warnings..?
return true
end
--[[
- Produce the next token. This function is called by advance() to get
- the next token.
]]
Lexer.proto.token = function(self)
-- Keep returning `nil` when we passed EOF
if self.exhausted then
return nil
end
while true do
-- End of line? Move to the next line.
if self.charNr > #self.line then
local startLineNr = self.lineNr
local startCharNr = self.charNr
if self:nextLine() then
return {
type = TokenType.EndOfLine,
value = '<eol>',
line = startLineNr,
char = startCharNr
}
end
self.exhausted = true
return {
type = TokenType.EndOfFile,
value = '<eof>',
line = self.lineNr,
char = self.charNr
}
end
local token = self:next()
-- No token? Something may have gone wrong...
if token == nil then
if self.charNr <= #self.line then
-- Unexpected character
self:trigger('error', {
code = 'E024',
line = self.lineNr,
char = self.charNr,
data = {
self:peek()
}
})
-- Skip to EOL
self:skipToEOL()
end
continue
end
--
-- Trigger token event
-- TODO
if token.type == TokenType.Comment then
self:trigger('Comment', token)
end
return token
end
end
-- export
_G.Lexer = Lexer