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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
|
//! Expects to be run after the C preprocessor and after `removeComments`.
//! This means that the lexer assumes that:
//! - Splices ('\' at the end of a line) have been handled/collapsed.
//! - Preprocessor directives and macros have been expanded (any remaining should be skipped with the exception of `#pragma code_page`).
//! - All comments have been removed.
const std = @import("std");
const ErrorDetails = @import("errors.zig").ErrorDetails;
const columnWidth = @import("literals.zig").columnWidth;
const code_pages = @import("code_pages.zig");
const SupportedCodePage = code_pages.SupportedCodePage;
const SourceMappings = @import("source_mapping.zig").SourceMappings;
const isNonAsciiDigit = @import("utils.zig").isNonAsciiDigit;
const dumpTokensDuringTests = false;
pub const default_max_string_literal_codepoints = 4097;
pub const Token = struct {
id: Id,
start: usize,
end: usize,
line_number: usize,
pub const Id = enum {
literal,
number,
quoted_ascii_string,
quoted_wide_string,
operator,
begin,
end,
comma,
open_paren,
close_paren,
/// This Id is only used for errors, the Lexer will never return one
/// of these from a `next` call.
preprocessor_command,
invalid,
eof,
pub fn nameForErrorDisplay(self: Id) []const u8 {
return switch (self) {
.literal => "<literal>",
.number => "<number>",
.quoted_ascii_string => "<quoted ascii string>",
.quoted_wide_string => "<quoted wide string>",
.operator => "<operator>",
.begin => "<'{' or BEGIN>",
.end => "<'}' or END>",
.comma => ",",
.open_paren => "(",
.close_paren => ")",
.preprocessor_command => "<preprocessor command>",
.invalid => unreachable,
.eof => "<eof>",
};
}
};
pub fn slice(self: Token, buffer: []const u8) []const u8 {
return buffer[self.start..self.end];
}
/// Returns 0-based column
pub fn calculateColumn(token: Token, source: []const u8, tab_columns: usize, maybe_line_start: ?usize) usize {
const line_start = maybe_line_start orelse token.getLineStartForColumnCalc(source);
var i: usize = line_start;
var column: usize = 0;
while (i < token.start) : (i += 1) {
column += columnWidth(column, source[i], tab_columns);
}
return column;
}
// TODO: More testing is needed to determine if this can be merged with getLineStartForErrorDisplay
// (the TODO in currentIndexFormsLineEndingPair should be taken into account as well)
pub fn getLineStartForColumnCalc(token: Token, source: []const u8) usize {
const line_start = line_start: {
if (token.start != 0) {
// start checking at the byte before the token
var index = token.start - 1;
while (true) {
if (source[index] == '\n') break :line_start @min(source.len - 1, index + 1);
if (index != 0) index -= 1 else break;
}
}
break :line_start 0;
};
return line_start;
}
pub fn getLineStartForErrorDisplay(token: Token, source: []const u8) usize {
const line_start = line_start: {
if (token.start != 0) {
// start checking at the byte before the token
var index = token.start - 1;
while (true) {
if (source[index] == '\r' or source[index] == '\n') break :line_start @min(source.len - 1, index + 1);
if (index != 0) index -= 1 else break;
}
}
break :line_start 0;
};
return line_start;
}
pub fn getLineForErrorDisplay(token: Token, source: []const u8, maybe_line_start: ?usize) []const u8 {
const line_start = maybe_line_start orelse token.getLineStartForErrorDisplay(source);
var line_end = line_start;
while (line_end < source.len and source[line_end] != '\r' and source[line_end] != '\n') : (line_end += 1) {}
return source[line_start..line_end];
}
pub fn isStringLiteral(token: Token) bool {
return token.id == .quoted_ascii_string or token.id == .quoted_wide_string;
}
};
pub const LineHandler = struct {
line_number: usize = 1,
buffer: []const u8,
last_line_ending_index: ?usize = null,
/// Like incrementLineNumber but checks that the current char is a line ending first.
/// Returns the new line number if it was incremented, null otherwise.
pub fn maybeIncrementLineNumber(self: *LineHandler, cur_index: usize) ?usize {
const c = self.buffer[cur_index];
if (c == '\r' or c == '\n') {
return self.incrementLineNumber(cur_index);
}
return null;
}
/// Increments line_number appropriately (handling line ending pairs)
/// and returns the new line number if it was incremented, or null otherwise.
pub fn incrementLineNumber(self: *LineHandler, cur_index: usize) ?usize {
if (self.currentIndexFormsLineEndingPair(cur_index)) {
self.last_line_ending_index = null;
return null;
} else {
self.line_number += 1;
self.last_line_ending_index = cur_index;
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
///
/// TODO: This is not really how the Win32 RC compiler handles line endings. Instead, it
/// seems to drop all carriage returns during preprocessing and then replace all
/// remaining line endings with well-formed CRLF pairs (e.g. `<CR>a<CR>b<LF>c` becomes `ab<CR><LF>c`).
/// Handling this the same as the Win32 RC compiler would need control over the preprocessor,
/// since Clang converts unpaired <CR> into unpaired <LF>.
pub fn currentIndexFormsLineEndingPair(self: *const LineHandler, cur_index: usize) bool {
if (self.last_line_ending_index == null) return false;
// must immediately precede the current index, we know cur_index must
// be >= 1 since last_line_ending_index is non-null (so if the subtraction
// overflows it is a bug at the callsite of this function).
if (self.last_line_ending_index.? != cur_index - 1) return false;
const cur_line_ending = self.buffer[cur_index];
const last_line_ending = self.buffer[self.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;
}
};
pub const LexError = error{
UnfinishedStringLiteral,
StringLiteralTooLong,
InvalidNumberWithExponent,
InvalidDigitCharacterInNumberLiteral,
IllegalByte,
IllegalByteOutsideStringLiterals,
IllegalCodepointOutsideStringLiterals,
IllegalByteOrderMark,
IllegalPrivateUseCharacter,
FoundCStyleEscapedQuote,
CodePagePragmaMissingLeftParen,
CodePagePragmaMissingRightParen,
/// Can be caught and ignored
CodePagePragmaInvalidCodePage,
CodePagePragmaNotInteger,
CodePagePragmaOverflow,
CodePagePragmaUnsupportedCodePage,
/// Can be caught and ignored
CodePagePragmaInIncludedFile,
};
pub const Lexer = struct {
const Self = @This();
buffer: []const u8,
index: usize,
line_handler: LineHandler,
at_start_of_line: bool = true,
error_context_token: ?Token = null,
current_code_page: SupportedCodePage,
default_code_page: SupportedCodePage,
source_mappings: ?*SourceMappings,
max_string_literal_codepoints: u15,
/// Needed to determine whether or not the output code page should
/// be set in the parser.
seen_pragma_code_pages: u2 = 0,
last_pragma_code_page_token: ?Token = null,
pub const Error = LexError;
pub const LexerOptions = struct {
default_code_page: SupportedCodePage = .windows1252,
source_mappings: ?*SourceMappings = null,
max_string_literal_codepoints: u15 = default_max_string_literal_codepoints,
};
pub fn init(buffer: []const u8, options: LexerOptions) Self {
return Self{
.buffer = buffer,
.index = 0,
.current_code_page = options.default_code_page,
.default_code_page = options.default_code_page,
.source_mappings = options.source_mappings,
.max_string_literal_codepoints = options.max_string_literal_codepoints,
.line_handler = .{ .buffer = buffer },
};
}
pub fn dump(self: *Self, token: *const Token) void {
std.debug.print("{s}:{d}: {f}\n", .{
@tagName(token.id), token.line_number, std.ascii.hexEscape(token.slice(self.buffer), .lower),
});
}
pub const LexMethod = enum {
whitespace_delimiter_only,
normal,
normal_expect_operator,
};
pub fn next(self: *Self, comptime method: LexMethod) LexError!Token {
switch (method) {
.whitespace_delimiter_only => return self.nextWhitespaceDelimeterOnly(),
.normal => return self.nextNormal(),
.normal_expect_operator => return self.nextNormalWithContext(.expect_operator),
}
}
const StateWhitespaceDelimiterOnly = enum {
start,
literal,
preprocessor,
semicolon,
};
pub fn nextWhitespaceDelimeterOnly(self: *Self) LexError!Token {
const start_index = self.index;
var result = Token{
.id = .eof,
.start = start_index,
.end = undefined,
.line_number = self.line_handler.line_number,
};
var state = StateWhitespaceDelimiterOnly.start;
while (self.current_code_page.codepointAt(self.index, self.buffer)) |codepoint| : (self.index += codepoint.byte_len) {
const c = codepoint.value;
try self.checkForIllegalCodepoint(codepoint, false);
switch (state) {
.start => switch (c) {
'\r', '\n' => {
result.start = self.index + 1;
result.line_number = self.incrementLineNumber();
},
' ', '\t', '\x05'...'\x08', '\x0B'...'\x0C', '\x0E'...'\x1F' => {
result.start = self.index + 1;
},
// NBSP only counts as whitespace at the start of a line (but
// can be intermixed with other whitespace). Who knows why.
// TODO: This should either be removed, or it should also include
// the codepoints listed in disjoint_code_page.zig
'\xA0' => if (self.at_start_of_line) {
result.start = self.index + codepoint.byte_len;
} else {
state = .literal;
self.at_start_of_line = false;
},
'#' => {
if (self.at_start_of_line) {
state = .preprocessor;
} else {
state = .literal;
}
self.at_start_of_line = false;
},
';' => {
state = .semicolon;
self.at_start_of_line = false;
},
else => {
state = .literal;
self.at_start_of_line = false;
},
},
.literal => switch (c) {
'\r', '\n', ' ', '\t', '\x05'...'\x08', '\x0B'...'\x0C', '\x0E'...'\x1F' => {
result.id = .literal;
break;
},
else => {},
},
.preprocessor => switch (c) {
'\r', '\n' => {
try self.evaluatePreprocessorCommand(result.start, self.index);
result.start = self.index + 1;
state = .start;
result.line_number = self.incrementLineNumber();
},
else => {},
},
.semicolon => switch (c) {
'\r', '\n' => {
result.start = self.index + 1;
state = .start;
result.line_number = self.incrementLineNumber();
},
else => {},
},
}
} else { // got EOF
switch (state) {
.start => {},
.semicolon => {
// Skip past everything up to the EOF
result.start = self.index;
},
.literal => {
result.id = .literal;
},
.preprocessor => {
try self.evaluatePreprocessorCommand(result.start, self.index);
result.start = self.index;
},
}
}
result.end = self.index;
// EOF tokens must have their start index match the end index
std.debug.assert(result.id != .eof or result.start == result.end);
return result;
}
const StateNormal = enum {
start,
literal_or_quoted_wide_string,
quoted_ascii_string,
quoted_wide_string,
quoted_ascii_string_escape,
quoted_wide_string_escape,
quoted_ascii_string_maybe_end,
quoted_wide_string_maybe_end,
literal,
number_literal,
preprocessor,
semicolon,
// end
e,
en,
// begin
b,
be,
beg,
begi,
};
/// TODO: A not-terrible name
pub fn nextNormal(self: *Self) LexError!Token {
return self.nextNormalWithContext(.any);
}
pub fn nextNormalWithContext(self: *Self, context: enum { expect_operator, any }) LexError!Token {
const start_index = self.index;
var result = Token{
.id = .eof,
.start = start_index,
.end = undefined,
.line_number = self.line_handler.line_number,
};
var state = StateNormal.start;
// Note: The Windows RC compiler uses a non-standard method of computing
// length for its 'string literal too long' errors; it isn't easily
// explained or intuitive (it's sort-of pre-parsed byte length but with
// a few of exceptions/edge cases).
//
// It also behaves strangely with non-ASCII codepoints, e.g. even though the default
// limit is 4097, you can only have 4094 € codepoints (1 UTF-16 code unit each),
// and 2048 𐐷 codepoints (2 UTF-16 code units each).
//
// TODO: Understand this more, bring it more in line with how the Win32 limits work.
// Alternatively, do something that makes more sense but may be more permissive.
var string_literal_length: usize = 0;
// Keeping track of the string literal column prevents pathological edge cases when
// there are tons of tab stop characters within a string literal.
var string_literal_column: usize = 0;
var string_literal_collapsing_whitespace: bool = false;
var still_could_have_exponent: bool = true;
var exponent_index: ?usize = null;
while (self.current_code_page.codepointAt(self.index, self.buffer)) |codepoint| : (self.index += codepoint.byte_len) {
const c = codepoint.value;
const in_string_literal = switch (state) {
.quoted_ascii_string,
.quoted_wide_string,
.quoted_ascii_string_escape,
.quoted_wide_string_escape,
.quoted_ascii_string_maybe_end,
.quoted_wide_string_maybe_end,
=>
// If the current line is not the same line as the start of the string literal,
// then we want to treat the current codepoint as 'not in a string literal'
// for the purposes of detecting illegal codepoints. This means that we will
// error on illegal-outside-string-literal characters that are outside string
// literals from the perspective of a C preprocessor, but that may be
// inside string literals from the perspective of the RC lexer. For example,
// "hello
// @"
// will be treated as a single string literal by the RC lexer but the Win32
// preprocessor will consider this an unclosed string literal followed by
// the character @ and ", and will therefore error since the Win32 RC preprocessor
// errors on the @ character outside string literals.
//
// By doing this here, we can effectively emulate the Win32 RC preprocessor behavior
// at lex-time, and avoid the need for a separate step that checks for this edge-case
// specifically.
result.line_number == self.line_handler.line_number,
else => false,
};
try self.checkForIllegalCodepoint(codepoint, in_string_literal);
switch (state) {
.start => switch (c) {
'\r', '\n' => {
result.start = self.index + 1;
result.line_number = self.incrementLineNumber();
},
' ', '\t', '\x05'...'\x08', '\x0B'...'\x0C', '\x0E'...'\x1F' => {
result.start = self.index + 1;
},
// NBSP only counts as whitespace at the start of a line (but
// can be intermixed with other whitespace). Who knows why.
'\xA0' => if (self.at_start_of_line) {
result.start = self.index + codepoint.byte_len;
} else {
state = .literal;
self.at_start_of_line = false;
},
'L', 'l' => {
state = .literal_or_quoted_wide_string;
self.at_start_of_line = false;
},
'E', 'e' => {
state = .e;
self.at_start_of_line = false;
},
'B', 'b' => {
state = .b;
self.at_start_of_line = false;
},
'"' => {
state = .quoted_ascii_string;
self.at_start_of_line = false;
string_literal_collapsing_whitespace = false;
string_literal_length = 0;
var dummy_token = Token{
.start = self.index,
.end = self.index,
.line_number = self.line_handler.line_number,
.id = .invalid,
};
string_literal_column = dummy_token.calculateColumn(self.buffer, 8, null);
},
'+', '&', '|' => {
self.index += 1;
result.id = .operator;
self.at_start_of_line = false;
break;
},
'-' => {
if (context == .expect_operator) {
self.index += 1;
result.id = .operator;
self.at_start_of_line = false;
break;
} else {
state = .number_literal;
still_could_have_exponent = true;
exponent_index = null;
self.at_start_of_line = false;
}
},
'0'...'9', '~' => {
state = .number_literal;
still_could_have_exponent = true;
exponent_index = null;
self.at_start_of_line = false;
},
'#' => {
if (self.at_start_of_line) {
state = .preprocessor;
} else {
state = .literal;
}
self.at_start_of_line = false;
},
';' => {
state = .semicolon;
self.at_start_of_line = false;
},
'{', '}' => {
self.index += 1;
result.id = if (c == '{') .begin else .end;
self.at_start_of_line = false;
break;
},
'(', ')' => {
self.index += 1;
result.id = if (c == '(') .open_paren else .close_paren;
self.at_start_of_line = false;
break;
},
',' => {
self.index += 1;
result.id = .comma;
self.at_start_of_line = false;
break;
},
else => {
if (isNonAsciiDigit(c)) {
self.error_context_token = .{
.id = .number,
.start = result.start,
.end = self.index + 1,
.line_number = self.line_handler.line_number,
};
return error.InvalidDigitCharacterInNumberLiteral;
}
state = .literal;
self.at_start_of_line = false;
},
},
.preprocessor => switch (c) {
'\r', '\n' => {
try self.evaluatePreprocessorCommand(result.start, self.index);
result.start = self.index + 1;
state = .start;
result.line_number = self.incrementLineNumber();
},
else => {},
},
// Semi-colon acts as a line-terminator--everything is skipped until
// the next line.
.semicolon => switch (c) {
'\r', '\n' => {
result.start = self.index + 1;
state = .start;
result.line_number = self.incrementLineNumber();
},
else => {},
},
.number_literal => switch (c) {
// zig fmt: off
' ', '\t', '\x05'...'\x08', '\x0B'...'\x0C', '\x0E'...'\x1F',
'\r', '\n', '"', ',', '{', '}', '+', '-', '|', '&', '~', '(', ')',
'\'', ';', '=',
=> {
// zig fmt: on
result.id = .number;
break;
},
'0'...'9' => {
if (exponent_index) |exp_i| {
if (self.index - 1 == exp_i) {
// Note: This being an error is a quirk of the preprocessor used by
// the Win32 RC compiler.
self.error_context_token = .{
.id = .number,
.start = result.start,
.end = self.index + 1,
.line_number = self.line_handler.line_number,
};
return error.InvalidNumberWithExponent;
}
}
},
'e', 'E' => {
if (still_could_have_exponent) {
exponent_index = self.index;
still_could_have_exponent = false;
}
},
else => {
if (isNonAsciiDigit(c)) {
self.error_context_token = .{
.id = .number,
.start = result.start,
.end = self.index + 1,
.line_number = self.line_handler.line_number,
};
return error.InvalidDigitCharacterInNumberLiteral;
}
still_could_have_exponent = false;
},
},
.literal_or_quoted_wide_string => switch (c) {
// zig fmt: off
' ', '\t', '\x05'...'\x08', '\x0B'...'\x0C', '\x0E'...'\x1F',
'\r', '\n', ',', '{', '}', '+', '-', '|', '&', '~', '(', ')',
'\'', ';', '=',
// zig fmt: on
=> {
result.id = .literal;
break;
},
'"' => {
state = .quoted_wide_string;
string_literal_collapsing_whitespace = false;
string_literal_length = 0;
var dummy_token = Token{
.start = self.index,
.end = self.index,
.line_number = self.line_handler.line_number,
.id = .invalid,
};
string_literal_column = dummy_token.calculateColumn(self.buffer, 8, null);
},
else => {
state = .literal;
},
},
.literal => switch (c) {
// zig fmt: off
' ', '\t', '\x05'...'\x08', '\x0B'...'\x0C', '\x0E'...'\x1F',
'\r', '\n', '"', ',', '{', '}', '+', '-', '|', '&', '~', '(', ')',
'\'', ';', '=',
=> {
// zig fmt: on
result.id = .literal;
break;
},
else => {},
},
.e => switch (c) {
'N', 'n' => {
state = .en;
},
else => {
state = .literal;
self.index -= 1;
},
},
.en => switch (c) {
'D', 'd' => {
result.id = .end;
self.index += 1;
break;
},
else => {
state = .literal;
self.index -= 1;
},
},
.b => switch (c) {
'E', 'e' => {
state = .be;
},
else => {
state = .literal;
self.index -= 1;
},
},
.be => switch (c) {
'G', 'g' => {
state = .beg;
},
else => {
state = .literal;
self.index -= 1;
},
},
.beg => switch (c) {
'I', 'i' => {
state = .begi;
},
else => {
state = .literal;
self.index -= 1;
},
},
.begi => switch (c) {
'N', 'n' => {
result.id = .begin;
self.index += 1;
break;
},
else => {
state = .literal;
self.index -= 1;
},
},
.quoted_ascii_string, .quoted_wide_string => switch (c) {
'"' => {
string_literal_column += 1;
state = if (state == .quoted_ascii_string) .quoted_ascii_string_maybe_end else .quoted_wide_string_maybe_end;
},
'\\' => {
string_literal_length += 1;
string_literal_column += 1;
state = if (state == .quoted_ascii_string) .quoted_ascii_string_escape else .quoted_wide_string_escape;
},
'\r' => {
string_literal_column = 0;
// \r doesn't count towards string literal length
// Increment line number but don't affect the result token's line number
_ = self.incrementLineNumber();
},
'\n' => {
string_literal_column = 0;
// first \n expands to <space><\n>
if (!string_literal_collapsing_whitespace) {
string_literal_length += 2;
string_literal_collapsing_whitespace = true;
}
// the rest are collapsed into the <space><\n>
// Increment line number but don't affect the result token's line number
_ = self.incrementLineNumber();
},
// only \t, space, Vertical Tab, and Form Feed count as whitespace when collapsing
'\t', ' ', '\x0b', '\x0c' => {
if (!string_literal_collapsing_whitespace) {
// Literal tab characters are counted as the number of space characters
// needed to reach the next 8-column tab stop.
const width = columnWidth(string_literal_column, @intCast(c), 8);
string_literal_length += width;
string_literal_column += width;
}
},
else => {
string_literal_collapsing_whitespace = false;
string_literal_length += 1;
string_literal_column += 1;
},
},
.quoted_ascii_string_escape, .quoted_wide_string_escape => switch (c) {
'"' => {
self.error_context_token = .{
.id = .invalid,
.start = self.index - 1,
.end = self.index + 1,
.line_number = self.line_handler.line_number,
};
return error.FoundCStyleEscapedQuote;
},
else => {
string_literal_length += 1;
string_literal_column += 1;
state = if (state == .quoted_ascii_string_escape) .quoted_ascii_string else .quoted_wide_string;
},
},
.quoted_ascii_string_maybe_end, .quoted_wide_string_maybe_end => switch (c) {
'"' => {
state = if (state == .quoted_ascii_string_maybe_end) .quoted_ascii_string else .quoted_wide_string;
// Escaped quotes count as 1 char for string literal length checks.
// Since we did not increment on the first " (because it could have been
// the end of the quoted string), we increment here
string_literal_length += 1;
string_literal_column += 1;
},
else => {
result.id = if (state == .quoted_ascii_string_maybe_end) .quoted_ascii_string else .quoted_wide_string;
break;
},
},
}
} else { // got EOF
switch (state) {
.start => {},
.semicolon => {
// Skip past everything up to the EOF
result.start = self.index;
},
.literal_or_quoted_wide_string, .literal, .e, .en, .b, .be, .beg, .begi => {
result.id = .literal;
},
.preprocessor => {
try self.evaluatePreprocessorCommand(result.start, self.index);
result.start = self.index;
},
.number_literal => {
result.id = .number;
},
.quoted_ascii_string_maybe_end, .quoted_wide_string_maybe_end => {
result.id = if (state == .quoted_ascii_string_maybe_end) .quoted_ascii_string else .quoted_wide_string;
},
.quoted_ascii_string,
.quoted_wide_string,
.quoted_ascii_string_escape,
.quoted_wide_string_escape,
=> {
self.error_context_token = .{
.id = .eof,
.start = self.index,
.end = self.index,
.line_number = self.line_handler.line_number,
};
return LexError.UnfinishedStringLiteral;
},
}
}
result.end = self.index;
if (result.id == .quoted_ascii_string or result.id == .quoted_wide_string) {
if (string_literal_length > self.max_string_literal_codepoints) {
self.error_context_token = result;
return LexError.StringLiteralTooLong;
}
}
// EOF tokens must have their start index match the end index
std.debug.assert(result.id != .eof or result.start == result.end);
return result;
}
/// Increments line_number appropriately (handling line ending pairs)
/// and returns the new line number.
fn incrementLineNumber(self: *Self) usize {
_ = self.line_handler.incrementLineNumber(self.index);
self.at_start_of_line = true;
return self.line_handler.line_number;
}
fn checkForIllegalCodepoint(self: *Self, codepoint: code_pages.Codepoint, in_string_literal: bool) LexError!void {
const err = switch (codepoint.value) {
// 0x00 = NUL
// 0x1A = Substitute (treated as EOF)
// NOTE: 0x1A gets treated as EOF by the clang preprocessor so after a .rc file
// is run through the clang preprocessor it will no longer have 0x1A characters in it.
// 0x7F = DEL (treated as a context-specific terminator by the Windows RC compiler)
0x00, 0x1A, 0x7F => error.IllegalByte,
// 0x01...0x03 result in strange 'macro definition too big' errors when used outside of string literals
// 0x04 is valid but behaves strangely (sort of acts as a 'skip the next character' instruction)
0x01...0x04 => if (!in_string_literal) error.IllegalByteOutsideStringLiterals else return,
// @ and ` both result in error RC2018: unknown character '0x60' (and subsequently
// fatal error RC1116: RC terminating after preprocessor errors) if they are ever used
// outside of string literals. Not exactly sure why this would be the case, though.
// TODO: Make sure there aren't any exceptions
'@', '`' => if (!in_string_literal) error.IllegalByteOutsideStringLiterals else return,
// The Byte Order Mark is mostly skipped over by the Windows RC compiler, but
// there are edge cases where it leads to cryptic 'compiler limit : macro definition too big'
// errors (e.g. a BOM within a number literal). By making this illegal we avoid having to
// deal with a lot of edge cases and remove the potential footgun of the bytes of a BOM
// being 'missing' when included in a string literal (the Windows RC compiler acts as
// if the codepoint was never part of the string literal).
'\u{FEFF}' => error.IllegalByteOrderMark,
// Similar deal with this private use codepoint, it gets skipped/ignored by the
// RC compiler (but without the cryptic errors). Silently dropping bytes still seems like
// enough of a footgun with no real use-cases that it's still worth erroring instead of
// emulating the RC compiler's behavior, though.
'\u{E000}' => error.IllegalPrivateUseCharacter,
// These codepoints lead to strange errors when used outside of string literals,
// and miscompilations when used within string literals. We avoid the miscompilation
// within string literals and emit a warning, but outside of string literals it makes
// more sense to just disallow these codepoints.
0x900, 0xA00, 0xA0D, 0x2000, 0xD00, 0xFFFE, 0xFFFF => if (!in_string_literal) error.IllegalCodepointOutsideStringLiterals else return,
else => return,
};
self.error_context_token = .{
.id = .invalid,
.start = self.index,
.end = self.index + codepoint.byte_len,
.line_number = self.line_handler.line_number,
};
return err;
}
fn evaluatePreprocessorCommand(self: *Self, start: usize, end: usize) !void {
const token = Token{
.id = .preprocessor_command,
.start = start,
.end = end,
.line_number = self.line_handler.line_number,
};
errdefer self.error_context_token = token;
const full_command = self.buffer[start..end];
const code_page = (parsePragmaCodePage(full_command) catch |err| switch (err) {
error.NotPragma, error.NotCodePagePragma => return,
else => |e| return e,
}) orelse self.default_code_page;
// https://learn.microsoft.com/en-us/windows/win32/menurc/pragma-directives
// > This pragma is not supported in an included resource file (.rc)
//
// Even though the Win32 behavior is to just ignore such directives silently,
// this is an error in the lexer to allow for emitting warnings/errors when
// such directives are found if that's wanted. The intention is for the lexer
// to still be able to work correctly after this error is returned.
if (self.source_mappings) |source_mappings| {
if (!source_mappings.isRootFile(token.line_number)) {
return error.CodePagePragmaInIncludedFile;
}
}
self.seen_pragma_code_pages +|= 1;
self.last_pragma_code_page_token = token;
self.current_code_page = code_page;
}
pub fn getErrorDetails(self: Self, lex_err: LexError) ErrorDetails {
const err = switch (lex_err) {
error.UnfinishedStringLiteral => ErrorDetails.Error.unfinished_string_literal,
error.StringLiteralTooLong => return .{
.err = .string_literal_too_long,
.code_page = self.current_code_page,
.token = self.error_context_token.?,
.extra = .{ .number = self.max_string_literal_codepoints },
},
error.InvalidNumberWithExponent => ErrorDetails.Error.invalid_number_with_exponent,
error.InvalidDigitCharacterInNumberLiteral => ErrorDetails.Error.invalid_digit_character_in_number_literal,
error.IllegalByte => ErrorDetails.Error.illegal_byte,
error.IllegalByteOutsideStringLiterals => ErrorDetails.Error.illegal_byte_outside_string_literals,
error.IllegalCodepointOutsideStringLiterals => ErrorDetails.Error.illegal_codepoint_outside_string_literals,
error.IllegalByteOrderMark => ErrorDetails.Error.illegal_byte_order_mark,
error.IllegalPrivateUseCharacter => ErrorDetails.Error.illegal_private_use_character,
error.FoundCStyleEscapedQuote => ErrorDetails.Error.found_c_style_escaped_quote,
error.CodePagePragmaMissingLeftParen => ErrorDetails.Error.code_page_pragma_missing_left_paren,
error.CodePagePragmaMissingRightParen => ErrorDetails.Error.code_page_pragma_missing_right_paren,
error.CodePagePragmaInvalidCodePage => ErrorDetails.Error.code_page_pragma_invalid_code_page,
error.CodePagePragmaNotInteger => ErrorDetails.Error.code_page_pragma_not_integer,
error.CodePagePragmaOverflow => ErrorDetails.Error.code_page_pragma_overflow,
error.CodePagePragmaUnsupportedCodePage => ErrorDetails.Error.code_page_pragma_unsupported_code_page,
error.CodePagePragmaInIncludedFile => ErrorDetails.Error.code_page_pragma_in_included_file,
};
return .{
.err = err,
.code_page = self.current_code_page,
.token = self.error_context_token.?,
};
}
};
fn parseCodePageNum(str: []const u8) !u32 {
var x: u32 = 0;
for (str) |c| {
const digit = try std.fmt.charToDigit(c, 10);
if (x != 0) x = try std.math.mul(u32, x, 10);
x = try std.math.add(u32, x, digit);
}
return x;
}
/// Returns `null` when the code_page is set to DEFAULT
pub fn parsePragmaCodePage(full_command: []const u8) !?SupportedCodePage {
var command = full_command;
// Anything besides exactly this is ignored by the Windows RC implementation
const expected_directive = "#pragma";
if (!std.mem.startsWith(u8, command, expected_directive)) return error.NotPragma;
command = command[expected_directive.len..];
if (command.len == 0 or !std.ascii.isWhitespace(command[0])) return error.NotCodePagePragma;
while (command.len > 0 and std.ascii.isWhitespace(command[0])) {
command = command[1..];
}
// Note: CoDe_PaGeZ is also treated as "code_page" by the Windows RC implementation,
// and it will error with 'Missing left parenthesis in code_page #pragma'
const expected_extension = "code_page";
if (!std.ascii.startsWithIgnoreCase(command, expected_extension)) return error.NotCodePagePragma;
command = command[expected_extension.len..];
while (command.len > 0 and std.ascii.isWhitespace(command[0])) {
command = command[1..];
}
if (command.len == 0 or command[0] != '(') {
return error.CodePagePragmaMissingLeftParen;
}
command = command[1..];
while (command.len > 0 and std.ascii.isWhitespace(command[0])) {
command = command[1..];
}
var num_str: []u8 = command[0..0];
while (command.len > 0 and (command[0] != ')' and !std.ascii.isWhitespace(command[0]))) {
command = command[1..];
num_str.len += 1;
}
if (num_str.len == 0) {
return error.CodePagePragmaNotInteger;
}
while (command.len > 0 and std.ascii.isWhitespace(command[0])) {
command = command[1..];
}
if (command.len == 0 or command[0] != ')') {
return error.CodePagePragmaMissingRightParen;
}
const code_page: ?SupportedCodePage = code_page: {
if (std.ascii.eqlIgnoreCase("DEFAULT", num_str)) {
break :code_page null;
}
// The Win32 compiler behaves fairly strangely around maxInt(u32):
// - If the overflowed u32 wraps and becomes a known code page ID, then
// it will error/warn with "Codepage not valid: ignored" (depending on /w)
// - If the overflowed u32 wraps and does not become a known code page ID,
// then it will error with 'constant too big' and 'Codepage not integer'
//
// Instead of that, we just have a separate error specifically for overflow.
const num = parseCodePageNum(num_str) catch |err| switch (err) {
error.InvalidCharacter => return error.CodePagePragmaNotInteger,
error.Overflow => return error.CodePagePragmaOverflow,
};
// Anything that starts with 0 but does not resolve to 0 is treated as invalid, e.g. 01252
if (num_str[0] == '0' and num != 0) {
return error.CodePagePragmaInvalidCodePage;
}
// Anything that resolves to 0 is treated as 'not an integer' by the Win32 implementation.
else if (num == 0) {
return error.CodePagePragmaNotInteger;
}
// Anything above u16 max is not going to be found since our CodePage enum is backed by a u16.
if (num > std.math.maxInt(u16)) {
return error.CodePagePragmaInvalidCodePage;
}
break :code_page code_pages.getByIdentifierEnsureSupported(@intCast(num)) catch |err| switch (err) {
error.InvalidCodePage => return error.CodePagePragmaInvalidCodePage,
error.UnsupportedCodePage => return error.CodePagePragmaUnsupportedCodePage,
};
};
return code_page;
}
fn testLexNormal(source: []const u8, expected_tokens: []const Token.Id) !void {
var lexer = Lexer.init(source, .{});
if (dumpTokensDuringTests) std.debug.print("\n----------------------\n{s}\n----------------------\n", .{lexer.buffer});
for (expected_tokens) |expected_token_id| {
const token = try lexer.nextNormal();
if (dumpTokensDuringTests) lexer.dump(&token);
try std.testing.expectEqual(expected_token_id, token.id);
}
const last_token = try lexer.nextNormal();
try std.testing.expectEqual(Token.Id.eof, last_token.id);
}
fn expectLexError(expected: LexError, actual: anytype) !void {
try std.testing.expectError(expected, actual);
if (dumpTokensDuringTests) std.debug.print("{!}\n", .{actual});
}
test "normal: numbers" {
try testLexNormal("1", &.{.number});
try testLexNormal("-1", &.{.number});
try testLexNormal("- 1", &.{ .number, .number });
try testLexNormal("-a", &.{.number});
}
test "normal: string literals" {
try testLexNormal("\"\"", &.{.quoted_ascii_string});
// "" is an escaped "
try testLexNormal("\" \"\" \"", &.{.quoted_ascii_string});
}
test "superscript chars and code pages" {
const firstToken = struct {
pub fn firstToken(source: []const u8, default_code_page: SupportedCodePage, comptime lex_method: Lexer.LexMethod) LexError!Token {
var lexer = Lexer.init(source, .{ .default_code_page = default_code_page });
return lexer.next(lex_method);
}
}.firstToken;
const utf8_source = "²";
const windows1252_source = "\xB2";
const windows1252_encoded_as_windows1252 = firstToken(windows1252_source, .windows1252, .normal);
try std.testing.expectError(error.InvalidDigitCharacterInNumberLiteral, windows1252_encoded_as_windows1252);
const utf8_encoded_as_windows1252 = try firstToken(utf8_source, .windows1252, .normal);
try std.testing.expectEqual(Token{
.id = .literal,
.start = 0,
.end = 2,
.line_number = 1,
}, utf8_encoded_as_windows1252);
const utf8_encoded_as_utf8 = firstToken(utf8_source, .utf8, .normal);
try std.testing.expectError(error.InvalidDigitCharacterInNumberLiteral, utf8_encoded_as_utf8);
const windows1252_encoded_as_utf8 = try firstToken(windows1252_source, .utf8, .normal);
try std.testing.expectEqual(Token{
.id = .literal,
.start = 0,
.end = 1,
.line_number = 1,
}, windows1252_encoded_as_utf8);
}
|