-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
lex.zig
1311 lines (1235 loc) · 54.6 KB
/
lex.zig
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const Allocator = std.mem.Allocator;
const AutoComptimeLookup = @import("comptime_lookup.zig").AutoComptimeLookup;
const zua = @import("zua.zig");
// Notes:
//
// In Lua's lexer, all single char tokens use their own ASCII value as their ID, and
// every other multi-character token uses ID >= 257 (see FIRST_RESERVED in llex.h).
// For now, this implementation uses a 'single_char' token as a catch-all for
// such single char tokens
//
// Lua's lexer uses a lua_State and parses strings/numbers while lexing, allocating
// strings and adding them to the lua_State's string table. This lexer, instead,
// does no allocation or parsing of strings/numbers (that will be done later). However,
// it maintains 1:1 compatibility with when Lua's lexer errors by validating
// strings/numbers at lex-time in the same way that Lua's lexer parses them.
//
// Lua's lexer skips over all comments (doesn't store them as tokens). This functionality is
// kept in this implementation.
//
// Lua's number lexing allows for using locale-specific decimal points
// TODO: ?
//
// Zua's lexer error messages are almost entirely identical to Lua's, except in a few cases:
// - malformed number in Lua prints more than where a number stops being valid, whereas Zua
// only prints up to the first malformed char (e.g. Zua will stop at 3.. whereas Lua will
// print 3.................)
// - string token context in Lua prints the parsed string rather than the source of the string
// since Lua parses and lexes at the same time. Zua will print the source of the string instead.
// (e.g. for "\v Zua will print "\v whereas Lua would print an actual vertical tab character)
// - escape sequence too large in Lua doesn't include the actual escape sequence that is too large,
// while Zua does include it (e.g. "\444" in Lua would error with: near '"', while Zua gives:
// near '"\444')
// Debug/test output
const dumpTokensDuringTests = false;
const veryVerboseLexing = false;
pub const Token = struct {
id: Id,
start: usize,
end: usize,
line_number: usize,
// for single-char tokens
// TODO: figure out something better for this (it's only used for nameForDisplay)
char: ?u8,
/// Helper for .single_char tokens
pub fn isChar(self: *const Token, expected_char: u8) bool {
return self.id == .single_char and self.char.? == expected_char;
}
pub const Keyword = struct {
pub fn idFromName(name: []const u8) ?Id {
return keywords.get(name);
}
};
const keywordMapping = .{
.{ "and", .keyword_and },
.{ "break", .keyword_break },
.{ "do", .keyword_do },
.{ "else", .keyword_else },
.{ "elseif", .keyword_elseif },
.{ "end", .keyword_end },
.{ "false", .keyword_false },
.{ "for", .keyword_for },
.{ "function", .keyword_function },
.{ "if", .keyword_if },
.{ "in", .keyword_in },
.{ "local", .keyword_local },
.{ "nil", .keyword_nil },
.{ "not", .keyword_not },
.{ "or", .keyword_or },
.{ "repeat", .keyword_repeat },
.{ "return", .keyword_return },
.{ "then", .keyword_then },
.{ "true", .keyword_true },
.{ "until", .keyword_until },
.{ "while", .keyword_while },
};
pub const keywords = std.ComptimeStringMap(Id, keywordMapping);
pub const Id = enum {
// terminal symbols denoted by reserved words
keyword_and,
keyword_break,
keyword_do,
keyword_else,
keyword_elseif,
keyword_end,
keyword_false,
keyword_for,
keyword_function,
keyword_if,
keyword_in,
keyword_local,
keyword_nil,
keyword_not,
keyword_or,
keyword_repeat,
keyword_return,
keyword_then,
keyword_true,
keyword_until,
keyword_while,
// any normal byte
single_char,
// other terminal symbols
concat,
ellipsis,
eq,
ge,
le,
ne,
number,
name,
string,
eof,
};
// A mapping of id -> name pairs as an array
const keywordNames = blk: {
// FIXME: This relies on the keyword enums starting at 0 and being contiguous
var array: [keywordMapping.len][]const u8 = undefined;
for (keywordMapping) |mapping| {
const name = mapping[0];
const id = @intFromEnum(@as(Id, mapping[1]));
array[id] = name;
}
break :blk array;
};
// buffer for nameForDisplay
var token_name_buf: [10]u8 = undefined;
/// Intended to be equivalent to Lua's luaX_token2str function
///
/// NOTE: To avoid allocation, this function uses a static buffer for the
/// name of control characters (which display as `char(15)`, etc).
/// This means that this function only works correctly if you immediately
/// print/copy the result before calling it again, as the returned
/// slice can potentially be overwritten on subsequent calls.
/// TODO: is this ok? ^
pub fn nameForDisplay(self: *const Token) []const u8 {
return switch (self.id) {
.keyword_and,
.keyword_break,
.keyword_do,
.keyword_else,
.keyword_elseif,
.keyword_end,
.keyword_false,
.keyword_for,
.keyword_function,
.keyword_if,
.keyword_in,
.keyword_local,
.keyword_nil,
.keyword_not,
.keyword_or,
.keyword_repeat,
.keyword_return,
.keyword_then,
.keyword_true,
.keyword_until,
.keyword_while,
=> keywordNames[@intFromEnum(self.id)],
.concat => "..",
.ellipsis => "...",
.eq => "==",
.ge => ">=",
.le => "<=",
.ne => "~=",
.number => "<number>",
.name => "<name>",
.string => "<string>",
.eof => "<eof>",
.single_char => blk: {
if (std.ascii.isControl(self.char.?)) {
break :blk std.fmt.bufPrint(&token_name_buf, "char({d})", .{self.char.?}) catch unreachable;
} else {
break :blk @as(*const [1]u8, &self.char.?)[0..1];
}
},
};
}
/// Intended to be equivalent to Lua's txtToken (llex.c) function
///
/// NOTE: The slice returned should be considered temporary, and either copied
/// or otherwise used immediately. See also nameForDisplay, which this function
/// can call.
pub fn nameForErrorDisplay(self: *const Token, source: []const u8) []const u8 {
return switch (self.id) {
.name, .string, .number => source[self.start..self.end],
else => self.nameForDisplay(),
};
}
};
pub const LexError = error{
UnfinishedString,
UnfinishedLongComment,
UnfinishedLongString,
InvalidLongStringDelimiter,
MalformedNumber,
EscapeSequenceTooLarge,
ChunkHasTooManyLines,
LexicalElementTooLong,
};
/// error -> msg lookup for lex errors
pub const lex_error_strings = AutoComptimeLookup(LexError, []const u8, .{
.{ LexError.UnfinishedString, "unfinished string" },
.{ LexError.UnfinishedLongComment, "unfinished long comment" },
.{ LexError.UnfinishedLongString, "unfinished long string" },
.{ LexError.InvalidLongStringDelimiter, "invalid long string delimiter" },
.{ LexError.MalformedNumber, "malformed number" },
.{ LexError.EscapeSequenceTooLarge, "escape sequence too large" },
.{ LexError.ChunkHasTooManyLines, "chunk has too many lines" },
.{ LexError.LexicalElementTooLong, "lexical element too long" },
});
pub const LexErrorContext = struct {
token: Token,
// TODO this is kinda weird, doesn't seem like it needs to be stored (maybe passed to render instead?)
err: LexError,
pub fn renderAlloc(self: *LexErrorContext, allocator: Allocator, lexer: *Lexer) ![]const u8 {
var buffer = std.ArrayList(u8).init(allocator);
errdefer buffer.deinit();
const looked_up_msg = lex_error_strings.get(self.err).?;
const error_writer = buffer.writer();
const MAXSRC = 80; // see MAXSRC in llex.c
var chunk_id_buf: [MAXSRC]u8 = undefined;
const chunk_id = zua.object.getChunkId(lexer.chunk_name, &chunk_id_buf);
try error_writer.print("{s}:{d}: {s}", .{ chunk_id, self.token.line_number, looked_up_msg });
// special case errors that shouldn't be printed with context
if (self.err != LexError.ChunkHasTooManyLines and self.err != LexError.LexicalElementTooLong) {
try error_writer.print(" near '{s}'", .{self.token.nameForErrorDisplay(lexer.buffer)});
}
return buffer.toOwnedSlice();
}
};
pub const Lexer = struct {
const Self = @This();
chunk_name: []const u8,
buffer: []const u8,
index: usize,
line_number: usize = 1,
// TODO this still feels slightly sloppy, could probably be cleaned up still.
// Would be good to revisit when adding parse error rendering
error_context: ?LexErrorContext = null,
/// In Lua 5.1 there is a bug in the lexer where check_next() accepts \0
/// since \0 is technically in the string literal passed to check_next representing
/// the set of characters to check for (since the string is null-terminated)
///
/// This affects a few things:
/// ".\0" gets lexed as ..
/// ".\0\0" and "..\0" get lexed as ...
/// the e/E in numeral exponents can be '\0'
/// the +- sign for numeral exponents can be '\0'
///
/// Note that these last two affect how numbers are ultimately parsed, since something like
/// 1\0 gets lexed as the equiv of 1e but gets parsed into 1 (since the str2d func treats it as
/// a null-terminated string). For this reason, "1\0\0" will succesfully parse into 1
/// while "1e\0002" will fail to parse (since it will treat it as "1e").
check_next_bug_compat: bool = true,
// TODO: implement or ignore this (options for handling nesting of [[]] in multiline strings)
// for now we simply allow [[ (Lua 5.1 errors by default on [[ saying that nesting is deprecated)
long_str_nesting_compat: bool = false,
/// maximum number of allowable lines in a chunk
/// Lua uses MAX_INT from llimits.h which is set to INT_MAX-2
/// This is the equivalent of that.
max_lines: usize = std.math.maxInt(i32) - 2,
/// maximum size of a lexical element
/// Lua uses MAX_SIZET/2 where MAX_SIZET is from llimits.h and is set to (~(size_t)0)-2)
/// This is the equivalent of that.
max_lexical_element_size: usize = (std.math.maxInt(usize) - 2) / 2,
pub const Error = LexError;
pub fn init(buffer: []const u8, chunk_name: []const u8) Self {
return Self{
.buffer = buffer,
.index = 0,
.chunk_name = chunk_name,
};
}
pub fn dump(self: *Self, token: *const Token) void {
std.debug.print("{s} {s}:{d}: \"{s}\"\n", .{ @tagName(token.id), token.nameForDisplay(), token.line_number, std.fmt.fmtSliceEscapeLower(self.buffer[token.start..token.end]) });
}
const State = enum {
start,
identifier,
string_literal,
string_literal_backslash,
string_literal_backslash_line_endings,
dash,
dot,
concat,
comment_start,
short_comment,
long_comment_start,
long_comment,
long_comment_possible_end,
long_string_start,
long_string,
long_string_possible_end,
number,
number_exponent_start,
number_exponent,
number_hex_start,
number_hex,
compound_equal,
};
pub fn next(self: *Self) Error!Token {
const start_index = self.index;
if (veryVerboseLexing) {
if (self.index < self.buffer.len) {
std.debug.print("{d}:'{c}'", .{ self.index, self.buffer[self.index] });
} else {
std.debug.print("eof", .{});
}
}
var result = Token{
.id = Token.Id.eof,
.start = start_index,
.end = undefined,
.char = null,
.line_number = self.line_number,
};
var state = State.start;
var string_delim: u8 = undefined;
var string_level: usize = 0;
var expected_string_level: usize = 0;
var string_escape_n: std.math.IntFittingRange(0, 999) = 0;
var string_escape_i: std.math.IntFittingRange(0, 3) = 0;
var string_escape_line_ending: u8 = undefined;
var last_line_ending_index: ?usize = null;
var number_is_float: bool = false;
var number_starting_char: u8 = undefined;
var number_exponent_signed_char: ?u8 = null;
var number_is_null_terminated: bool = false;
while (self.index < self.buffer.len) : (self.index += 1) {
const c = self.buffer[self.index];
if (veryVerboseLexing) std.debug.print(":{s}", .{@tagName(state)});
// Check for tokens that are over the size limit here as a catch-all
if (self.index - result.start >= self.max_lexical_element_size) {
return self.reportLexError(LexError.LexicalElementTooLong, result, Token.Id.eof);
}
switch (state) {
State.start => switch (c) {
'\n', '\r' => {
result.start = self.index + 1;
result.line_number = try self.incrementLineNumber(&last_line_ending_index);
},
// space, tab, vertical tab, form feed
' ', '\t', '\x0b', '\x0c' => {
// skip whitespace
result.start = self.index + 1;
},
'-' => {
// this could be the start of a comment, a long comment, or a single -
state = State.dash;
},
'a'...'z', 'A'...'Z', '_' => {
state = State.identifier;
result.id = Token.Id.name;
},
'0'...'9' => {
state = State.number;
number_starting_char = c;
if (self.check_next_bug_compat) {
number_is_null_terminated = false;
}
},
'"', '\'' => {
state = State.string_literal;
string_delim = c;
result.id = Token.Id.string;
},
'.' => {
// this could be the start of .., ..., or a single .
state = State.dot;
},
'>', '<', '~', '=' => {
state = State.compound_equal;
},
'[' => {
state = State.long_string_start;
expected_string_level = 0;
},
else => {
result.id = Token.Id.single_char;
self.index += 1;
break;
},
},
State.identifier => switch (c) {
'a'...'z', 'A'...'Z', '_', '0'...'9' => {},
else => {
const name = self.buffer[result.start..self.index];
if (Token.Keyword.idFromName(name)) |id| {
result.id = id;
}
break;
},
},
State.string_literal => switch (c) {
'\\' => {
state = State.string_literal_backslash;
string_escape_i = 0;
string_escape_n = 0;
},
'"', '\'' => {
if (c == string_delim) {
self.index += 1;
break;
}
},
'\n', '\r' => {
return self.reportLexError(LexError.UnfinishedString, result, Token.Id.string);
},
else => {},
},
State.string_literal_backslash => switch (c) {
'0'...'9' => {
// Validate that any \ddd escape sequences can actually fit
// in a byte
string_escape_n = 10 * string_escape_n + (c - '0');
string_escape_i += 1;
if (string_escape_i == 3) {
if (string_escape_n > std.math.maxInt(u8)) {
return self.reportLexErrorInc(LexError.EscapeSequenceTooLarge, result, Token.Id.string);
}
state = State.string_literal;
}
},
'\r', '\n' => {
if (string_escape_i > 0) {
return self.reportLexError(LexError.UnfinishedString, result, Token.Id.string);
}
state = State.string_literal_backslash_line_endings;
string_escape_line_ending = c;
result.line_number = try self.incrementLineNumber(&last_line_ending_index);
},
else => {
// if the escape sequence had any digits, then
// we need to backtrack so as not to escape the current
// character (since the digits are the things being escaped)
if (string_escape_i > 0) {
self.index -= 1;
}
state = State.string_literal;
},
},
State.string_literal_backslash_line_endings => switch (c) {
'\r', '\n' => {
// can only escape \r\n or \n\r pairs, not \r\r or \n\n
if (c == string_escape_line_ending) {
return self.reportLexError(LexError.UnfinishedString, result, Token.Id.string);
} else {
state = State.string_literal;
}
result.line_number = try self.incrementLineNumber(&last_line_ending_index);
},
else => {
// backtrack so that we don't escape the current char
self.index -= 1;
state = State.string_literal;
},
},
State.dash => switch (c) {
'-' => {
state = State.comment_start;
},
else => {
result.id = Token.Id.single_char;
break;
},
},
State.comment_start => switch (c) {
'[' => {
state = State.long_comment_start;
expected_string_level = 0;
},
'\r', '\n' => {
// comment immediately ends
result.start = self.index + 1;
state = State.start;
result.line_number = try self.incrementLineNumber(&last_line_ending_index);
},
else => {
state = State.short_comment;
},
},
State.long_string_start,
State.long_comment_start,
=> switch (c) {
'=' => {
expected_string_level += 1;
},
'[' => {
state = if (state == State.long_comment_start) State.long_comment else State.long_string;
},
else => {
if (state == State.long_comment_start) {
if (c == '\n' or c == '\r') {
// not a long comment, but the short comment ends immediately
result.start = self.index + 1;
state = State.start;
result.line_number = try self.incrementLineNumber(&last_line_ending_index);
} else {
state = State.short_comment;
}
} else {
// Lua makes the pattern [=X where X is anything but [ or = an explicit
// 'invalid long string delimiter' error instead of discarding
// its long-string-ness and parsing the tokens as normal
//
// - This is only true of long strings: long comments handle --[==X just fine
// since it falls back to -- (short comment)
// - The end of long strings is unaffected: [=[str]=X does not give this error
// (instead the string will just not be finished)
// - Long strings with no sep chars is unaffected: [X does not give this error
// (instead it will an give unexpected symbol error while parsing)
if (expected_string_level > 0) {
return self.reportLexError(LexError.InvalidLongStringDelimiter, result, Token.Id.string);
} else {
result.id = Token.Id.single_char;
break;
}
}
},
},
State.long_string,
State.long_comment,
=> switch (c) {
']' => {
state = if (state == State.long_comment) State.long_comment_possible_end else State.long_string_possible_end;
string_level = 0;
},
else => {
result.line_number = try self.maybeIncrementLineNumber(&last_line_ending_index);
},
},
State.long_string_possible_end,
State.long_comment_possible_end,
=> switch (c) {
']' => {
if (string_level == expected_string_level) {
if (state == State.long_comment_possible_end) {
result.start = self.index + 1;
state = State.start;
} else {
self.index += 1;
result.id = Token.Id.string;
break;
}
} else {
// don't change state, since this char could be the start of a new end sequence
// but reset the level back to 0
string_level = 0;
}
},
'=' => {
string_level += 1;
},
else => {
result.line_number = try self.maybeIncrementLineNumber(&last_line_ending_index);
state = if (state == State.long_comment_possible_end) State.long_comment else State.long_string;
},
},
State.short_comment => switch (c) {
'\n', '\r' => {
result.start = self.index + 1;
state = State.start;
result.line_number = try self.incrementLineNumber(&last_line_ending_index);
},
else => {},
},
State.dot => switch (c) {
'.' => {
state = State.concat;
},
'0'...'9' => {
state = State.number;
number_starting_char = '.';
number_is_float = true;
if (self.check_next_bug_compat) {
number_is_null_terminated = false;
}
},
else => {
if (self.check_next_bug_compat and c == '\x00') {
state = State.concat;
} else {
result.id = Token.Id.single_char;
break;
}
},
},
State.concat => switch (c) {
'.' => {
result.id = Token.Id.ellipsis;
// include this .
self.index += 1;
break;
},
else => {
if (self.check_next_bug_compat and c == '\x00') {
result.id = Token.Id.ellipsis;
// include this .
self.index += 1;
break;
} else {
result.id = Token.Id.concat;
break;
}
},
},
State.number => switch (c) {
'0'...'9' => {},
'.' => {
// multiple decimal points not allowed
if (number_is_float) {
return self.reportLexErrorInc(LexError.MalformedNumber, result, Token.Id.number);
}
number_is_float = true;
},
'x', 'X' => {
// only 0x is allowed
if (number_starting_char != '0') {
return self.reportLexErrorInc(LexError.MalformedNumber, result, Token.Id.number);
}
state = State.number_hex_start;
},
'e', 'E' => {
state = State.number_exponent_start;
number_exponent_signed_char = null;
},
// 'a'...'z' minus e and x
'a'...'d', 'A'...'D', 'f'...'w', 'F'...'W', 'y'...'z', 'Y'...'Z' => {
return self.reportLexErrorInc(LexError.MalformedNumber, result, Token.Id.number);
},
'_' => return self.reportLexErrorInc(LexError.MalformedNumber, result, Token.Id.number),
else => {
if (self.check_next_bug_compat and c == '\x00') {
state = State.number_exponent_start;
number_exponent_signed_char = null;
number_is_null_terminated = true;
} else {
result.id = Token.Id.number;
break;
}
},
},
State.number_hex_start, State.number_hex => switch (c) {
'0'...'9', 'a'...'f', 'A'...'F' => {
state = State.number_hex;
},
'g'...'z', 'G'...'Z' => {
return self.reportLexErrorInc(LexError.MalformedNumber, result, Token.Id.number);
},
'_' => return self.reportLexErrorInc(LexError.MalformedNumber, result, Token.Id.number),
else => {
result.id = Token.Id.number;
break;
},
},
State.number_exponent_start => {
const should_consume_anything = self.check_next_bug_compat and number_is_null_terminated;
if (should_consume_anything) {
switch (c) {
'\x00', '-', '+', '0'...'9', 'a'...'z', 'A'...'Z', '_' => {
state = State.number_exponent;
},
else => {
result.id = Token.Id.number;
break;
},
}
} else {
switch (c) {
'0'...'9' => state = State.number_exponent,
'-', '+' => {
if (number_exponent_signed_char) |_| {
// this is an error because e.g. "1e--" would lex as "1e-" and "-"
// and "1e-" is always invalid
return self.reportLexErrorInc(LexError.MalformedNumber, result, Token.Id.number);
}
number_exponent_signed_char = c;
},
else => {
// if we get here, then the token up to this point has to be
// either 1e, 1e-, 1e+ which *must* be followed by a digit, and
// we already know c is not a digit
return self.reportLexErrorInc(LexError.MalformedNumber, result, Token.Id.number);
},
}
}
},
State.number_exponent => {
const should_consume_anything = self.check_next_bug_compat and number_is_null_terminated;
if (should_consume_anything) {
switch (c) {
'0'...'9', 'a'...'z', 'A'...'Z', '_' => {},
else => {
result.id = Token.Id.number;
break;
},
}
} else {
switch (c) {
'0'...'9' => {},
'a'...'z', 'A'...'Z', '_' => return self.reportLexError(LexError.MalformedNumber, result, Token.Id.number),
else => {
result.id = Token.Id.number;
break;
},
}
}
},
State.compound_equal => switch (c) {
'=' => {
switch (self.buffer[self.index - 1]) {
'>' => result.id = Token.Id.ge,
'<' => result.id = Token.Id.le,
'~' => result.id = Token.Id.ne,
'=' => result.id = Token.Id.eq,
else => unreachable,
}
self.index += 1;
break;
},
else => {
result.id = Token.Id.single_char;
break;
},
},
}
} else {
// this will always be true due to the while loop condition as the
// else block is not evaluated after a break; in the while loop, but
// rather only when the loop condition fails
std.debug.assert(self.index == self.buffer.len);
switch (state) {
State.start => {},
State.identifier => {
const name = self.buffer[result.start..self.index];
if (Token.Keyword.idFromName(name)) |id| {
result.id = id;
}
},
State.dot,
State.dash,
State.compound_equal,
=> {
result.id = Token.Id.single_char;
},
State.concat => {
result.id = Token.Id.concat;
},
State.number_exponent,
State.number_hex,
State.number,
=> {
result.id = Token.Id.number;
},
State.comment_start,
State.short_comment,
State.long_comment_start,
=> {
result.start = self.index;
},
State.long_string_start => {
if (expected_string_level > 0) {
return self.reportLexError(LexError.InvalidLongStringDelimiter, result, Token.Id.string);
} else {
result.id = Token.Id.single_char;
}
},
// .eof is reported as the error context to conform with how PUC Lua reports unfinished <x> errors
// when the end of the buffer is reached before the string is closed. Not totally sure why .string
// isn't used for the unfinished string errors in this case, though--the error message seems nicer
// that way
// TODO revisit?
State.long_comment_possible_end,
State.long_comment,
=> return self.reportLexError(LexError.UnfinishedLongComment, result, Token.Id.eof),
State.long_string_possible_end,
State.long_string,
=> return self.reportLexError(LexError.UnfinishedLongString, result, Token.Id.eof),
State.string_literal,
State.string_literal_backslash,
State.string_literal_backslash_line_endings,
=> return self.reportLexError(LexError.UnfinishedString, result, Token.Id.eof),
State.number_hex_start,
State.number_exponent_start,
=> {
if (self.check_next_bug_compat and number_is_null_terminated) {
result.id = Token.Id.number;
} else {
return self.reportLexError(LexError.MalformedNumber, result, Token.Id.number);
}
},
}
}
if (veryVerboseLexing) {
if (self.index < self.buffer.len) {
std.debug.print(":{d}:'{c}'=\"{s}\"\n", .{ self.index, self.buffer[self.index], self.buffer[result.start..self.index] });
} else {
std.debug.print(":eof=\"{s}\"\n", .{self.buffer[result.start..self.index]});
}
}
if (result.id == Token.Id.single_char) {
result.char = self.buffer[result.start];
}
result.end = self.index;
return result;
}
pub fn lookahead(self: *Self) Error!Token {
var lookaheadLexer = Lexer{
.buffer = self.buffer,
.index = self.index,
.line_number = self.line_number,
.chunk_name = self.chunk_name,
.check_next_bug_compat = self.check_next_bug_compat,
.long_str_nesting_compat = self.long_str_nesting_compat,
};
return lookaheadLexer.next();
}
fn reportLexError(self: *Self, err: LexError, unfinished_token: Token, id: Token.Id) Error {
self.error_context = .{
.token = .{
.id = id,
.start = unfinished_token.start,
.end = self.index,
.line_number = self.line_number,
.char = null, // TODO double check that this is always true (single char token can't cause a lex error, right?)
},
.err = err,
};
return err;
}
fn reportLexErrorInc(self: *Self, err: LexError, unfinished_token: Token, id: Token.Id) Error {
self.index += 1;
return self.reportLexError(err, unfinished_token, id);
}
pub fn renderErrorAlloc(self: *Self, allocator: Allocator) ![]const u8 {
if (self.error_context) |*ctx| {
return ctx.renderAlloc(allocator, self);
} else {
return error.NoError;
}
}
/// Like incrementLineNumber but checks that the current char is a line ending first
fn maybeIncrementLineNumber(self: *Self, last_line_ending_index: *?usize) !usize {
const c = self.buffer[self.index];
if (c == '\r' or c == '\n') {
return try self.incrementLineNumber(last_line_ending_index);
}
return self.line_number;
}
/// Increments line_number appropriately (handling line ending pairs)
/// and returns the new line number.
/// note: mutates last_line_ending_index.*
fn incrementLineNumber(self: *Self, last_line_ending_index: *?usize) !usize {
if (self.currentIndexFormsLineEndingPair(last_line_ending_index.*)) {
last_line_ending_index.* = null;
} else {
self.line_number += 1;
last_line_ending_index.* = self.index;
}
if (self.line_number >= self.max_lines) {
// TODO using a dummy token here is pretty janky
return self.reportLexError(LexError.ChunkHasTooManyLines, .{
.id = undefined,
.start = self.index,
.end = self.index,
.line_number = self.line_number,
.char = null,
}, Token.Id.eof);
}
return self.line_number;
}
/// \r\n and \n\r pairs are treated as a single line ending (but not \r\r \n\n)
/// expects self.index and last_line_ending_index (if non-null) to contain line endings
fn currentIndexFormsLineEndingPair(self: *Self, last_line_ending_index: ?usize) bool {
if (last_line_ending_index == null) return false;
// must immediately precede the current index
if (last_line_ending_index.? != self.index - 1) return false;
const cur_line_ending = self.buffer[self.index];
const last_line_ending = self.buffer[last_line_ending_index.?];
// sanity check
std.debug.assert(cur_line_ending == '\r' or cur_line_ending == '\n');
std.debug.assert(last_line_ending == '\r' or last_line_ending == '\n');
// can't be \n\n or \r\r
if (last_line_ending == cur_line_ending) return false;
return true;
}
};
test "hello \"world\"" {
try testLex("local hello = \"wor\\\"ld\"", &[_]Token.Id{
Token.Id.keyword_local,
Token.Id.name,
Token.Id.single_char,
Token.Id.string,
});
}
test "hello 'world'" {
try testLex("local hello = 'wor\\'ld'", &[_]Token.Id{
Token.Id.keyword_local,
Token.Id.name,
Token.Id.single_char,
Token.Id.string,
});
}
test "strings" {
// none of these escaped chars have any meaning, but Lua allows
// any character to be escaped so this should lex just fine
try testLex("'\\e\\s\\c\\ any char'", &[_]Token.Id{Token.Id.string});
try testLex("'\\1'", &[_]Token.Id{Token.Id.string});
try testLex("'\\12'", &[_]Token.Id{Token.Id.string});
try testLex("'\\123'", &[_]Token.Id{Token.Id.string});
try testLex("'\\1234'", &[_]Token.Id{Token.Id.string});
// carriage returns and newlines can be escaped with \
try testLex("'\\\n\\\r'", &[_]Token.Id{Token.Id.string});
try testLex("\".\\\x0d\\\\\\\".\\\x0d\xa5[\\ArA\"", &[_]Token.Id{Token.Id.string});
// a pair of CR/LF can be escaped with a single \ (either CRLF or LFCR)
try testLex("'\\\r\n'", &[_]Token.Id{Token.Id.string});
try testLex("'\\\n\r'", &[_]Token.Id{Token.Id.string});
}
test "long strings" {
try testLex("[[]]", &[_]Token.Id{Token.Id.string});
try testLex("[===[\nhello\nworld\n]===]", &[_]Token.Id{Token.Id.string});
try testLex("[]", &[_]Token.Id{ Token.Id.single_char, Token.Id.single_char });
// TODO: this depends on LUA_COMPAT_LSTR
try testLex("[[ [[ ]]", &[_]Token.Id{Token.Id.string});
// this is always allowed
try testLex("[=[ [[ ]] ]=]", &[_]Token.Id{Token.Id.string});
// unfinished end directly into real end
try testLex("[==[]=]==]", &[_]Token.Id{Token.Id.string});
}
test "comments and dashes" {
try testLex("-", &[_]Token.Id{Token.Id.single_char});
try testLex("a-b", &[_]Token.Id{ Token.Id.name, Token.Id.single_char, Token.Id.name });
try testLex("--", &[_]Token.Id{});
try testLex("--local hello = 'wor\\'ld'", &[_]Token.Id{});
try testLex("--[this is a short comment\nreturn", &[_]Token.Id{Token.Id.keyword_return});
try testLex("--\rreturn", &[_]Token.Id{Token.Id.keyword_return});
try testLex("--[[local hello = 'wor\\'ld']]", &[_]Token.Id{});
try testLex("--[==[\nlocal\nhello\n=\n'world'\n]==]", &[_]Token.Id{});
try testLex("--[==", &[_]Token.Id{});
try testLex("--[\n]]", &[_]Token.Id{ Token.Id.single_char, Token.Id.single_char });
// unfinished end directly into real end
try testLex("--[===[]=]===]", &[_]Token.Id{});
}
test "whitespace" {
// form feed
try testLex("_\x0c_W_", &[_]Token.Id{ Token.Id.name, Token.Id.name });
// vertical tab
try testLex("_\x0b_W_", &[_]Token.Id{ Token.Id.name, Token.Id.name });
}
test "dots, concat, ellipsis" {