aboutsummaryrefslogtreecommitdiff
path: root/src/resinator/parse.zig
blob: 68537b94f6bb80ecab17af1a4b461d3a14db24eb (plain)
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
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
const std = @import("std");
const Lexer = @import("lex.zig").Lexer;
const Token = @import("lex.zig").Token;
const Node = @import("ast.zig").Node;
const Tree = @import("ast.zig").Tree;
const CodePageLookup = @import("ast.zig").CodePageLookup;
const Resource = @import("rc.zig").Resource;
const Allocator = std.mem.Allocator;
const ErrorDetails = @import("errors.zig").ErrorDetails;
const Diagnostics = @import("errors.zig").Diagnostics;
const SourceBytes = @import("literals.zig").SourceBytes;
const Compiler = @import("compile.zig").Compiler;
const rc = @import("rc.zig");
const res = @import("res.zig");

// TODO: Make these configurable?
pub const max_nested_menu_level: u32 = 512;
pub const max_nested_version_level: u32 = 512;
pub const max_nested_expression_level: u32 = 200;

pub const Parser = struct {
    const Self = @This();

    lexer: *Lexer,
    /// values that need to be initialized per-parse
    state: Parser.State = undefined,
    options: Parser.Options,

    pub const Error = error{ParseError} || Allocator.Error;

    pub const Options = struct {
        warn_instead_of_error_on_invalid_code_page: bool = false,
    };

    pub fn init(lexer: *Lexer, options: Options) Parser {
        return Parser{
            .lexer = lexer,
            .options = options,
        };
    }

    pub const State = struct {
        token: Token,
        lookahead_lexer: Lexer,
        allocator: Allocator,
        arena: Allocator,
        diagnostics: *Diagnostics,
        input_code_page_lookup: CodePageLookup,
        output_code_page_lookup: CodePageLookup,
    };

    pub fn parse(self: *Self, allocator: Allocator, diagnostics: *Diagnostics) Error!*Tree {
        var arena = std.heap.ArenaAllocator.init(allocator);
        errdefer arena.deinit();

        self.state = Parser.State{
            .token = undefined,
            .lookahead_lexer = undefined,
            .allocator = allocator,
            .arena = arena.allocator(),
            .diagnostics = diagnostics,
            .input_code_page_lookup = CodePageLookup.init(arena.allocator(), self.lexer.default_code_page),
            .output_code_page_lookup = CodePageLookup.init(arena.allocator(), self.lexer.default_code_page),
        };

        const parsed_root = try self.parseRoot();

        const tree = try self.state.arena.create(Tree);
        tree.* = .{
            .node = parsed_root,
            .input_code_pages = self.state.input_code_page_lookup,
            .output_code_pages = self.state.output_code_page_lookup,
            .source = self.lexer.buffer,
            .arena = arena.state,
            .allocator = allocator,
        };
        return tree;
    }

    fn parseRoot(self: *Self) Error!*Node {
        var statements = std.ArrayList(*Node).init(self.state.allocator);
        defer statements.deinit();

        try self.parseStatements(&statements);
        try self.check(.eof);

        const node = try self.state.arena.create(Node.Root);
        node.* = .{
            .body = try self.state.arena.dupe(*Node, statements.items),
        };
        return &node.base;
    }

    fn parseStatements(self: *Self, statements: *std.ArrayList(*Node)) Error!void {
        while (true) {
            try self.nextToken(.whitespace_delimiter_only);
            if (self.state.token.id == .eof) break;
            // The Win32 compiler will sometimes try to recover from errors
            // and then restart parsing afterwards. We don't ever do this
            // because it almost always leads to unhelpful error messages
            // (usually it will end up with bogus things like 'file
            // not found: {')
            const statement = try self.parseStatement();
            try statements.append(statement);
        }
    }

    /// Expects the current token to be the token before possible common resource attributes.
    /// After return, the current token will be the token immediately before the end of the
    /// common resource attributes (if any). If there are no common resource attributes, the
    /// current token is unchanged.
    /// The returned slice is allocated by the parser's arena
    fn parseCommonResourceAttributes(self: *Self) ![]Token {
        var common_resource_attributes = std.ArrayListUnmanaged(Token){};
        while (true) {
            const maybe_common_resource_attribute = try self.lookaheadToken(.normal);
            if (maybe_common_resource_attribute.id == .literal and rc.CommonResourceAttributes.map.has(maybe_common_resource_attribute.slice(self.lexer.buffer))) {
                try common_resource_attributes.append(self.state.arena, maybe_common_resource_attribute);
                self.nextToken(.normal) catch unreachable;
            } else {
                break;
            }
        }
        return common_resource_attributes.toOwnedSlice(self.state.arena);
    }

    /// Expects the current token to have already been dealt with, and that the
    /// optional statements will potentially start on the next token.
    /// After return, the current token will be the token immediately before the end of the
    /// optional statements (if any). If there are no optional statements, the
    /// current token is unchanged.
    /// The returned slice is allocated by the parser's arena
    fn parseOptionalStatements(self: *Self, resource: Resource) ![]*Node {
        var optional_statements = std.ArrayListUnmanaged(*Node){};
        while (true) {
            const lookahead_token = try self.lookaheadToken(.normal);
            if (lookahead_token.id != .literal) break;
            const slice = lookahead_token.slice(self.lexer.buffer);
            const optional_statement_type = rc.OptionalStatements.map.get(slice) orelse switch (resource) {
                .dialog, .dialogex => rc.OptionalStatements.dialog_map.get(slice) orelse break,
                else => break,
            };
            self.nextToken(.normal) catch unreachable;
            switch (optional_statement_type) {
                .language => {
                    const language = try self.parseLanguageStatement();
                    try optional_statements.append(self.state.arena, language);
                },
                // Number only
                .version, .characteristics, .style, .exstyle => {
                    const identifier = self.state.token;
                    const value = try self.parseExpression(.{
                        .can_contain_not_expressions = optional_statement_type == .style or optional_statement_type == .exstyle,
                        .allowed_types = .{ .number = true },
                    });
                    const node = try self.state.arena.create(Node.SimpleStatement);
                    node.* = .{
                        .identifier = identifier,
                        .value = value,
                    };
                    try optional_statements.append(self.state.arena, &node.base);
                },
                // String only
                .caption => {
                    const identifier = self.state.token;
                    try self.nextToken(.normal);
                    const value = self.state.token;
                    if (!value.isStringLiteral()) {
                        return self.addErrorDetailsAndFail(ErrorDetails{
                            .err = .expected_something_else,
                            .token = value,
                            .extra = .{ .expected_types = .{
                                .string_literal = true,
                            } },
                        });
                    }
                    // TODO: Wrapping this in a Node.Literal is superfluous but necessary
                    //       to put it in a SimpleStatement
                    const value_node = try self.state.arena.create(Node.Literal);
                    value_node.* = .{
                        .token = value,
                    };
                    const node = try self.state.arena.create(Node.SimpleStatement);
                    node.* = .{
                        .identifier = identifier,
                        .value = &value_node.base,
                    };
                    try optional_statements.append(self.state.arena, &node.base);
                },
                // String or number
                .class => {
                    const identifier = self.state.token;
                    const value = try self.parseExpression(.{ .allowed_types = .{ .number = true, .string = true } });
                    const node = try self.state.arena.create(Node.SimpleStatement);
                    node.* = .{
                        .identifier = identifier,
                        .value = value,
                    };
                    try optional_statements.append(self.state.arena, &node.base);
                },
                // Special case
                .menu => {
                    const identifier = self.state.token;
                    try self.nextToken(.whitespace_delimiter_only);
                    try self.check(.literal);
                    // TODO: Wrapping this in a Node.Literal is superfluous but necessary
                    //       to put it in a SimpleStatement
                    const value_node = try self.state.arena.create(Node.Literal);
                    value_node.* = .{
                        .token = self.state.token,
                    };
                    const node = try self.state.arena.create(Node.SimpleStatement);
                    node.* = .{
                        .identifier = identifier,
                        .value = &value_node.base,
                    };
                    try optional_statements.append(self.state.arena, &node.base);
                },
                .font => {
                    const identifier = self.state.token;
                    const point_size = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

                    // The comma between point_size and typeface is both optional and
                    // there can be any number of them
                    try self.skipAnyCommas();

                    try self.nextToken(.normal);
                    const typeface = self.state.token;
                    if (!typeface.isStringLiteral()) {
                        return self.addErrorDetailsAndFail(ErrorDetails{
                            .err = .expected_something_else,
                            .token = typeface,
                            .extra = .{ .expected_types = .{
                                .string_literal = true,
                            } },
                        });
                    }

                    const ExSpecificValues = struct {
                        weight: ?*Node = null,
                        italic: ?*Node = null,
                        char_set: ?*Node = null,
                    };
                    var ex_specific = ExSpecificValues{};
                    ex_specific: {
                        var optional_param_parser = OptionalParamParser{ .parser = self };
                        switch (resource) {
                            .dialogex => {
                                {
                                    ex_specific.weight = try optional_param_parser.parse(.{});
                                    if (optional_param_parser.finished) break :ex_specific;
                                }
                                {
                                    if (!(try self.parseOptionalToken(.comma))) break :ex_specific;
                                    ex_specific.italic = try self.parseExpression(.{ .allowed_types = .{ .number = true } });
                                }
                                {
                                    ex_specific.char_set = try optional_param_parser.parse(.{});
                                    if (optional_param_parser.finished) break :ex_specific;
                                }
                            },
                            .dialog => {},
                            else => unreachable, // only DIALOG and DIALOGEX have FONT optional-statements
                        }
                    }

                    const node = try self.state.arena.create(Node.FontStatement);
                    node.* = .{
                        .identifier = identifier,
                        .point_size = point_size,
                        .typeface = typeface,
                        .weight = ex_specific.weight,
                        .italic = ex_specific.italic,
                        .char_set = ex_specific.char_set,
                    };
                    try optional_statements.append(self.state.arena, &node.base);
                },
            }
        }
        return optional_statements.toOwnedSlice(self.state.arena);
    }

    /// Expects the current token to be the first token of the statement.
    fn parseStatement(self: *Self) Error!*Node {
        const first_token = self.state.token;
        std.debug.assert(first_token.id == .literal);

        if (rc.TopLevelKeywords.map.get(first_token.slice(self.lexer.buffer))) |keyword| switch (keyword) {
            .language => {
                const language_statement = try self.parseLanguageStatement();
                return language_statement;
            },
            .version, .characteristics => {
                const identifier = self.state.token;
                const value = try self.parseExpression(.{ .allowed_types = .{ .number = true } });
                const node = try self.state.arena.create(Node.SimpleStatement);
                node.* = .{
                    .identifier = identifier,
                    .value = value,
                };
                return &node.base;
            },
            .stringtable => {
                // common resource attributes must all be contiguous and come before optional-statements
                const common_resource_attributes = try self.parseCommonResourceAttributes();
                const optional_statements = try self.parseOptionalStatements(.stringtable);

                try self.nextToken(.normal);
                const begin_token = self.state.token;
                try self.check(.begin);

                var strings = std.ArrayList(*Node).init(self.state.allocator);
                defer strings.deinit();
                while (true) {
                    const maybe_end_token = try self.lookaheadToken(.normal);
                    switch (maybe_end_token.id) {
                        .end => {
                            self.nextToken(.normal) catch unreachable;
                            break;
                        },
                        .eof => {
                            return self.addErrorDetailsAndFail(ErrorDetails{
                                .err = .unfinished_string_table_block,
                                .token = maybe_end_token,
                            });
                        },
                        else => {},
                    }
                    const id_expression = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

                    const comma_token: ?Token = if (try self.parseOptionalToken(.comma)) self.state.token else null;

                    try self.nextToken(.normal);
                    if (self.state.token.id != .quoted_ascii_string and self.state.token.id != .quoted_wide_string) {
                        return self.addErrorDetailsAndFail(ErrorDetails{
                            .err = .expected_something_else,
                            .token = self.state.token,
                            .extra = .{ .expected_types = .{ .string_literal = true } },
                        });
                    }

                    const string_node = try self.state.arena.create(Node.StringTableString);
                    string_node.* = .{
                        .id = id_expression,
                        .maybe_comma = comma_token,
                        .string = self.state.token,
                    };
                    try strings.append(&string_node.base);
                }

                if (strings.items.len == 0) {
                    return self.addErrorDetailsAndFail(ErrorDetails{
                        .err = .expected_token, // TODO: probably a more specific error message
                        .token = self.state.token,
                        .extra = .{ .expected = .number },
                    });
                }

                const end_token = self.state.token;
                try self.check(.end);

                const node = try self.state.arena.create(Node.StringTable);
                node.* = .{
                    .type = first_token,
                    .common_resource_attributes = common_resource_attributes,
                    .optional_statements = optional_statements,
                    .begin_token = begin_token,
                    .strings = try self.state.arena.dupe(*Node, strings.items),
                    .end_token = end_token,
                };
                return &node.base;
            },
        };

        // The Win32 RC compiler allows for a 'dangling' literal at the end of a file
        // (as long as it's not a valid top-level keyword), and there is actually an
        // .rc file with a such a dangling literal in the Windows-classic-samples set
        // of projects. So, we have special compatibility for this particular case.
        const maybe_eof = try self.lookaheadToken(.whitespace_delimiter_only);
        if (maybe_eof.id == .eof) {
            // TODO: emit warning
            var context = try self.state.arena.alloc(Token, 2);
            context[0] = first_token;
            context[1] = maybe_eof;
            const invalid_node = try self.state.arena.create(Node.Invalid);
            invalid_node.* = .{
                .context = context,
            };
            return &invalid_node.base;
        }

        const id_token = first_token;
        const id_code_page = self.lexer.current_code_page;
        try self.nextToken(.whitespace_delimiter_only);
        const resource = try self.checkResource();
        const type_token = self.state.token;

        if (resource == .string_num) {
            try self.addErrorDetails(.{
                .err = .string_resource_as_numeric_type,
                .token = type_token,
            });
            return self.addErrorDetailsAndFail(.{
                .err = .string_resource_as_numeric_type,
                .token = type_token,
                .type = .note,
                .print_source_line = false,
            });
        }

        if (resource == .font) {
            const id_bytes = SourceBytes{
                .slice = id_token.slice(self.lexer.buffer),
                .code_page = id_code_page,
            };
            const maybe_ordinal = res.NameOrOrdinal.maybeOrdinalFromString(id_bytes);
            if (maybe_ordinal == null) {
                const would_be_win32_rc_ordinal = res.NameOrOrdinal.maybeNonAsciiOrdinalFromString(id_bytes);
                if (would_be_win32_rc_ordinal) |win32_rc_ordinal| {
                    try self.addErrorDetails(ErrorDetails{
                        .err = .id_must_be_ordinal,
                        .token = id_token,
                        .extra = .{ .resource = resource },
                    });
                    return self.addErrorDetailsAndFail(ErrorDetails{
                        .err = .win32_non_ascii_ordinal,
                        .token = id_token,
                        .type = .note,
                        .print_source_line = false,
                        .extra = .{ .number = win32_rc_ordinal.ordinal },
                    });
                } else {
                    return self.addErrorDetailsAndFail(ErrorDetails{
                        .err = .id_must_be_ordinal,
                        .token = id_token,
                        .extra = .{ .resource = resource },
                    });
                }
            }
        }

        switch (resource) {
            .accelerators => {
                // common resource attributes must all be contiguous and come before optional-statements
                const common_resource_attributes = try self.parseCommonResourceAttributes();
                const optional_statements = try self.parseOptionalStatements(resource);

                try self.nextToken(.normal);
                const begin_token = self.state.token;
                try self.check(.begin);

                var accelerators = std.ArrayListUnmanaged(*Node){};

                while (true) {
                    const lookahead = try self.lookaheadToken(.normal);
                    switch (lookahead.id) {
                        .end, .eof => {
                            self.nextToken(.normal) catch unreachable;
                            break;
                        },
                        else => {},
                    }
                    const event = try self.parseExpression(.{ .allowed_types = .{ .number = true, .string = true } });

                    try self.nextToken(.normal);
                    try self.check(.comma);

                    const idvalue = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

                    var type_and_options = std.ArrayListUnmanaged(Token){};
                    while (true) {
                        if (!(try self.parseOptionalToken(.comma))) break;

                        try self.nextToken(.normal);
                        if (!rc.AcceleratorTypeAndOptions.map.has(self.tokenSlice())) {
                            return self.addErrorDetailsAndFail(.{
                                .err = .expected_something_else,
                                .token = self.state.token,
                                .extra = .{ .expected_types = .{
                                    .accelerator_type_or_option = true,
                                } },
                            });
                        }
                        try type_and_options.append(self.state.arena, self.state.token);
                    }

                    const node = try self.state.arena.create(Node.Accelerator);
                    node.* = .{
                        .event = event,
                        .idvalue = idvalue,
                        .type_and_options = try type_and_options.toOwnedSlice(self.state.arena),
                    };
                    try accelerators.append(self.state.arena, &node.base);
                }

                const end_token = self.state.token;
                try self.check(.end);

                const node = try self.state.arena.create(Node.Accelerators);
                node.* = .{
                    .id = id_token,
                    .type = type_token,
                    .common_resource_attributes = common_resource_attributes,
                    .optional_statements = optional_statements,
                    .begin_token = begin_token,
                    .accelerators = try accelerators.toOwnedSlice(self.state.arena),
                    .end_token = end_token,
                };
                return &node.base;
            },
            .dialog, .dialogex => {
                // common resource attributes must all be contiguous and come before optional-statements
                const common_resource_attributes = try self.parseCommonResourceAttributes();

                const x = try self.parseExpression(.{ .allowed_types = .{ .number = true } });
                _ = try self.parseOptionalToken(.comma);

                const y = try self.parseExpression(.{ .allowed_types = .{ .number = true } });
                _ = try self.parseOptionalToken(.comma);

                const width = try self.parseExpression(.{ .allowed_types = .{ .number = true } });
                _ = try self.parseOptionalToken(.comma);

                const height = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

                var optional_param_parser = OptionalParamParser{ .parser = self };
                const help_id: ?*Node = try optional_param_parser.parse(.{});

                const optional_statements = try self.parseOptionalStatements(resource);

                try self.nextToken(.normal);
                const begin_token = self.state.token;
                try self.check(.begin);

                var controls = std.ArrayListUnmanaged(*Node){};
                defer controls.deinit(self.state.allocator);
                while (try self.parseControlStatement(resource)) |control_node| {
                    // The number of controls must fit in a u16 in order for it to
                    // be able to be written into the relevant field in the .res data.
                    if (controls.items.len >= std.math.maxInt(u16)) {
                        try self.addErrorDetails(.{
                            .err = .too_many_dialog_controls,
                            .token = id_token,
                            .extra = .{ .resource = resource },
                        });
                        return self.addErrorDetailsAndFail(.{
                            .err = .too_many_dialog_controls,
                            .type = .note,
                            .token = control_node.getFirstToken(),
                            .token_span_end = control_node.getLastToken(),
                            .extra = .{ .resource = resource },
                        });
                    }

                    try controls.append(self.state.allocator, control_node);
                }

                try self.nextToken(.normal);
                const end_token = self.state.token;
                try self.check(.end);

                const node = try self.state.arena.create(Node.Dialog);
                node.* = .{
                    .id = id_token,
                    .type = type_token,
                    .common_resource_attributes = common_resource_attributes,
                    .x = x,
                    .y = y,
                    .width = width,
                    .height = height,
                    .help_id = help_id,
                    .optional_statements = optional_statements,
                    .begin_token = begin_token,
                    .controls = try self.state.arena.dupe(*Node, controls.items),
                    .end_token = end_token,
                };
                return &node.base;
            },
            .toolbar => {
                // common resource attributes must all be contiguous and come before optional-statements
                const common_resource_attributes = try self.parseCommonResourceAttributes();

                const button_width = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

                try self.nextToken(.normal);
                try self.check(.comma);

                const button_height = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

                try self.nextToken(.normal);
                const begin_token = self.state.token;
                try self.check(.begin);

                var buttons = std.ArrayListUnmanaged(*Node){};
                while (try self.parseToolbarButtonStatement()) |button_node| {
                    try buttons.append(self.state.arena, button_node);
                }

                try self.nextToken(.normal);
                const end_token = self.state.token;
                try self.check(.end);

                const node = try self.state.arena.create(Node.Toolbar);
                node.* = .{
                    .id = id_token,
                    .type = type_token,
                    .common_resource_attributes = common_resource_attributes,
                    .button_width = button_width,
                    .button_height = button_height,
                    .begin_token = begin_token,
                    .buttons = try buttons.toOwnedSlice(self.state.arena),
                    .end_token = end_token,
                };
                return &node.base;
            },
            .menu, .menuex => {
                // common resource attributes must all be contiguous and come before optional-statements
                const common_resource_attributes = try self.parseCommonResourceAttributes();
                // help id is optional but must come between common resource attributes and optional-statements
                var help_id: ?*Node = null;
                // Note: No comma is allowed before or after help_id of MENUEX and help_id is not
                //       a possible field of MENU.
                if (resource == .menuex and try self.lookaheadCouldBeNumberExpression(.not_disallowed)) {
                    help_id = try self.parseExpression(.{
                        .is_known_to_be_number_expression = true,
                    });
                }
                const optional_statements = try self.parseOptionalStatements(.stringtable);

                try self.nextToken(.normal);
                const begin_token = self.state.token;
                try self.check(.begin);

                var items = std.ArrayListUnmanaged(*Node){};
                defer items.deinit(self.state.allocator);
                while (try self.parseMenuItemStatement(resource, id_token, 1)) |item_node| {
                    try items.append(self.state.allocator, item_node);
                }

                try self.nextToken(.normal);
                const end_token = self.state.token;
                try self.check(.end);

                if (items.items.len == 0) {
                    return self.addErrorDetailsAndFail(.{
                        .err = .empty_menu_not_allowed,
                        .token = type_token,
                    });
                }

                const node = try self.state.arena.create(Node.Menu);
                node.* = .{
                    .id = id_token,
                    .type = type_token,
                    .common_resource_attributes = common_resource_attributes,
                    .optional_statements = optional_statements,
                    .help_id = help_id,
                    .begin_token = begin_token,
                    .items = try self.state.arena.dupe(*Node, items.items),
                    .end_token = end_token,
                };
                return &node.base;
            },
            .versioninfo => {
                // common resource attributes must all be contiguous and come before optional-statements
                const common_resource_attributes = try self.parseCommonResourceAttributes();

                var fixed_info = std.ArrayListUnmanaged(*Node){};
                while (try self.parseVersionStatement()) |version_statement| {
                    try fixed_info.append(self.state.arena, version_statement);
                }

                try self.nextToken(.normal);
                const begin_token = self.state.token;
                try self.check(.begin);

                var block_statements = std.ArrayListUnmanaged(*Node){};
                while (try self.parseVersionBlockOrValue(id_token, 1)) |block_node| {
                    try block_statements.append(self.state.arena, block_node);
                }

                try self.nextToken(.normal);
                const end_token = self.state.token;
                try self.check(.end);

                const node = try self.state.arena.create(Node.VersionInfo);
                node.* = .{
                    .id = id_token,
                    .versioninfo = type_token,
                    .common_resource_attributes = common_resource_attributes,
                    .fixed_info = try fixed_info.toOwnedSlice(self.state.arena),
                    .begin_token = begin_token,
                    .block_statements = try block_statements.toOwnedSlice(self.state.arena),
                    .end_token = end_token,
                };
                return &node.base;
            },
            .dlginclude => {
                const common_resource_attributes = try self.parseCommonResourceAttributes();

                const filename_expression = try self.parseExpression(.{
                    .allowed_types = .{ .string = true },
                });

                const node = try self.state.arena.create(Node.ResourceExternal);
                node.* = .{
                    .id = id_token,
                    .type = type_token,
                    .common_resource_attributes = common_resource_attributes,
                    .filename = filename_expression,
                };
                return &node.base;
            },
            .stringtable => {
                return self.addErrorDetailsAndFail(.{
                    .err = .name_or_id_not_allowed,
                    .token = id_token,
                    .extra = .{ .resource = resource },
                });
            },
            // Just try everything as a 'generic' resource (raw data or external file)
            // TODO: More fine-grained switch cases as necessary
            else => {
                const common_resource_attributes = try self.parseCommonResourceAttributes();

                const maybe_begin = try self.lookaheadToken(.normal);
                if (maybe_begin.id == .begin) {
                    self.nextToken(.normal) catch unreachable;

                    if (!resource.canUseRawData()) {
                        try self.addErrorDetails(ErrorDetails{
                            .err = .resource_type_cant_use_raw_data,
                            .token = maybe_begin,
                            .extra = .{ .resource = resource },
                        });
                        return self.addErrorDetailsAndFail(ErrorDetails{
                            .err = .resource_type_cant_use_raw_data,
                            .type = .note,
                            .print_source_line = false,
                            .token = maybe_begin,
                        });
                    }

                    const raw_data = try self.parseRawDataBlock();
                    const end_token = self.state.token;

                    const node = try self.state.arena.create(Node.ResourceRawData);
                    node.* = .{
                        .id = id_token,
                        .type = type_token,
                        .common_resource_attributes = common_resource_attributes,
                        .begin_token = maybe_begin,
                        .raw_data = raw_data,
                        .end_token = end_token,
                    };
                    return &node.base;
                }

                const filename_expression = try self.parseExpression(.{
                    // Don't tell the user that numbers are accepted since we error on
                    // number expressions and regular number literals are treated as unquoted
                    // literals rather than numbers, so from the users perspective
                    // numbers aren't really allowed.
                    .expected_types_override = .{
                        .literal = true,
                        .string_literal = true,
                    },
                });

                const node = try self.state.arena.create(Node.ResourceExternal);
                node.* = .{
                    .id = id_token,
                    .type = type_token,
                    .common_resource_attributes = common_resource_attributes,
                    .filename = filename_expression,
                };
                return &node.base;
            },
        }
    }

    /// Expects the current token to be a begin token.
    /// After return, the current token will be the end token.
    fn parseRawDataBlock(self: *Self) Error![]*Node {
        var raw_data = std.ArrayList(*Node).init(self.state.allocator);
        defer raw_data.deinit();
        while (true) {
            const maybe_end_token = try self.lookaheadToken(.normal);
            switch (maybe_end_token.id) {
                .comma => {
                    // comma as the first token in a raw data block is an error
                    if (raw_data.items.len == 0) {
                        return self.addErrorDetailsAndFail(ErrorDetails{
                            .err = .expected_something_else,
                            .token = maybe_end_token,
                            .extra = .{ .expected_types = .{
                                .number = true,
                                .number_expression = true,
                                .string_literal = true,
                            } },
                        });
                    }
                    // otherwise just skip over commas
                    self.nextToken(.normal) catch unreachable;
                    continue;
                },
                .end => {
                    self.nextToken(.normal) catch unreachable;
                    break;
                },
                .eof => {
                    return self.addErrorDetailsAndFail(ErrorDetails{
                        .err = .unfinished_raw_data_block,
                        .token = maybe_end_token,
                    });
                },
                else => {},
            }
            const expression = try self.parseExpression(.{ .allowed_types = .{ .number = true, .string = true } });
            try raw_data.append(expression);

            if (expression.isNumberExpression()) {
                const maybe_close_paren = try self.lookaheadToken(.normal);
                if (maybe_close_paren.id == .close_paren) {
                    // <number expression>) is an error
                    return self.addErrorDetailsAndFail(ErrorDetails{
                        .err = .expected_token,
                        .token = maybe_close_paren,
                        .extra = .{ .expected = .operator },
                    });
                }
            }
        }
        return try self.state.arena.dupe(*Node, raw_data.items);
    }

    /// Expects the current token to be handled, and that the control statement will
    /// begin on the next token.
    /// After return, the current token will be the token immediately before the end of the
    /// control statement (or unchanged if the function returns null).
    fn parseControlStatement(self: *Self, resource: Resource) Error!?*Node {
        const control_token = try self.lookaheadToken(.normal);
        const control = rc.Control.map.get(control_token.slice(self.lexer.buffer)) orelse return null;
        self.nextToken(.normal) catch unreachable;

        try self.skipAnyCommas();

        var text: ?Token = null;
        if (control.hasTextParam()) {
            try self.nextToken(.normal);
            switch (self.state.token.id) {
                .quoted_ascii_string, .quoted_wide_string, .number => {
                    text = self.state.token;
                },
                else => {
                    return self.addErrorDetailsAndFail(ErrorDetails{
                        .err = .expected_something_else,
                        .token = self.state.token,
                        .extra = .{ .expected_types = .{
                            .number = true,
                            .string_literal = true,
                        } },
                    });
                },
            }
            try self.skipAnyCommas();
        }

        const id = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

        try self.skipAnyCommas();

        var class: ?*Node = null;
        var style: ?*Node = null;
        if (control == .control) {
            class = try self.parseExpression(.{});
            if (class.?.id == .literal) {
                const class_literal = @fieldParentPtr(Node.Literal, "base", class.?);
                const is_invalid_control_class = class_literal.token.id == .literal and !rc.ControlClass.map.has(class_literal.token.slice(self.lexer.buffer));
                if (is_invalid_control_class) {
                    return self.addErrorDetailsAndFail(.{
                        .err = .expected_something_else,
                        .token = self.state.token,
                        .extra = .{ .expected_types = .{
                            .control_class = true,
                        } },
                    });
                }
            }
            try self.skipAnyCommas();
            style = try self.parseExpression(.{
                .can_contain_not_expressions = true,
                .allowed_types = .{ .number = true },
            });
            // If there is no comma after the style paramter, the Win32 RC compiler
            // could misinterpret the statement and end up skipping over at least one token
            // that should have been interepeted as the next parameter (x). For example:
            //   CONTROL "text", 1, BUTTON, 15 30, 1, 2, 3, 4
            // the `15` is the style parameter, but in the Win32 implementation the `30`
            // is completely ignored (i.e. the `1, 2, 3, 4` are `x`, `y`, `w`, `h`).
            // If a comma is added after the `15`, then `30` gets interpreted (correctly)
            // as the `x` value.
            //
            // Instead of emulating this behavior, we just warn about the potential for
            // weird behavior in the Win32 implementation whenever there isn't a comma after
            // the style parameter.
            const lookahead_token = try self.lookaheadToken(.normal);
            if (lookahead_token.id != .comma and lookahead_token.id != .eof) {
                try self.addErrorDetails(.{
                    .err = .rc_could_miscompile_control_params,
                    .type = .warning,
                    .token = lookahead_token,
                });
                try self.addErrorDetails(.{
                    .err = .rc_could_miscompile_control_params,
                    .type = .note,
                    .token = style.?.getFirstToken(),
                    .token_span_end = style.?.getLastToken(),
                });
            }
            try self.skipAnyCommas();
        }

        const x = try self.parseExpression(.{ .allowed_types = .{ .number = true } });
        _ = try self.parseOptionalToken(.comma);
        const y = try self.parseExpression(.{ .allowed_types = .{ .number = true } });
        _ = try self.parseOptionalToken(.comma);
        const width = try self.parseExpression(.{ .allowed_types = .{ .number = true } });
        _ = try self.parseOptionalToken(.comma);
        const height = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

        var optional_param_parser = OptionalParamParser{ .parser = self };
        if (control != .control) {
            style = try optional_param_parser.parse(.{ .not_expression_allowed = true });
        }

        const exstyle: ?*Node = try optional_param_parser.parse(.{ .not_expression_allowed = true });
        const help_id: ?*Node = switch (resource) {
            .dialogex => try optional_param_parser.parse(.{}),
            else => null,
        };

        var extra_data: []*Node = &[_]*Node{};
        var extra_data_begin: ?Token = null;
        var extra_data_end: ?Token = null;
        // extra data is DIALOGEX-only
        if (resource == .dialogex and try self.parseOptionalToken(.begin)) {
            extra_data_begin = self.state.token;
            extra_data = try self.parseRawDataBlock();
            extra_data_end = self.state.token;
        }

        const node = try self.state.arena.create(Node.ControlStatement);
        node.* = .{
            .type = control_token,
            .text = text,
            .class = class,
            .id = id,
            .x = x,
            .y = y,
            .width = width,
            .height = height,
            .style = style,
            .exstyle = exstyle,
            .help_id = help_id,
            .extra_data_begin = extra_data_begin,
            .extra_data = extra_data,
            .extra_data_end = extra_data_end,
        };
        return &node.base;
    }

    fn parseToolbarButtonStatement(self: *Self) Error!?*Node {
        const keyword_token = try self.lookaheadToken(.normal);
        const button_type = rc.ToolbarButton.map.get(keyword_token.slice(self.lexer.buffer)) orelse return null;
        self.nextToken(.normal) catch unreachable;

        switch (button_type) {
            .separator => {
                const node = try self.state.arena.create(Node.Literal);
                node.* = .{
                    .token = keyword_token,
                };
                return &node.base;
            },
            .button => {
                const button_id = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

                const node = try self.state.arena.create(Node.SimpleStatement);
                node.* = .{
                    .identifier = keyword_token,
                    .value = button_id,
                };
                return &node.base;
            },
        }
    }

    /// Expects the current token to be handled, and that the menuitem/popup statement will
    /// begin on the next token.
    /// After return, the current token will be the token immediately before the end of the
    /// menuitem statement (or unchanged if the function returns null).
    fn parseMenuItemStatement(self: *Self, resource: Resource, top_level_menu_id_token: Token, nesting_level: u32) Error!?*Node {
        const menuitem_token = try self.lookaheadToken(.normal);
        const menuitem = rc.MenuItem.map.get(menuitem_token.slice(self.lexer.buffer)) orelse return null;
        self.nextToken(.normal) catch unreachable;

        if (nesting_level > max_nested_menu_level) {
            try self.addErrorDetails(.{
                .err = .nested_resource_level_exceeds_max,
                .token = top_level_menu_id_token,
                .extra = .{ .resource = resource },
            });
            return self.addErrorDetailsAndFail(.{
                .err = .nested_resource_level_exceeds_max,
                .type = .note,
                .token = menuitem_token,
                .extra = .{ .resource = resource },
            });
        }

        switch (resource) {
            .menu => switch (menuitem) {
                .menuitem => {
                    try self.nextToken(.normal);
                    if (rc.MenuItem.isSeparator(self.state.token.slice(self.lexer.buffer))) {
                        const separator_token = self.state.token;
                        // There can be any number of trailing commas after SEPARATOR
                        try self.skipAnyCommas();
                        const node = try self.state.arena.create(Node.MenuItemSeparator);
                        node.* = .{
                            .menuitem = menuitem_token,
                            .separator = separator_token,
                        };
                        return &node.base;
                    } else {
                        const text = self.state.token;
                        if (!text.isStringLiteral()) {
                            return self.addErrorDetailsAndFail(ErrorDetails{
                                .err = .expected_something_else,
                                .token = text,
                                .extra = .{ .expected_types = .{
                                    .string_literal = true,
                                } },
                            });
                        }
                        try self.skipAnyCommas();

                        const result = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

                        _ = try self.parseOptionalToken(.comma);

                        var options = std.ArrayListUnmanaged(Token){};
                        while (true) {
                            const option_token = try self.lookaheadToken(.normal);
                            if (!rc.MenuItem.Option.map.has(option_token.slice(self.lexer.buffer))) {
                                break;
                            }
                            self.nextToken(.normal) catch unreachable;
                            try options.append(self.state.arena, option_token);
                            try self.skipAnyCommas();
                        }

                        const node = try self.state.arena.create(Node.MenuItem);
                        node.* = .{
                            .menuitem = menuitem_token,
                            .text = text,
                            .result = result,
                            .option_list = try options.toOwnedSlice(self.state.arena),
                        };
                        return &node.base;
                    }
                },
                .popup => {
                    try self.nextToken(.normal);
                    const text = self.state.token;
                    if (!text.isStringLiteral()) {
                        return self.addErrorDetailsAndFail(ErrorDetails{
                            .err = .expected_something_else,
                            .token = text,
                            .extra = .{ .expected_types = .{
                                .string_literal = true,
                            } },
                        });
                    }
                    try self.skipAnyCommas();

                    var options = std.ArrayListUnmanaged(Token){};
                    while (true) {
                        const option_token = try self.lookaheadToken(.normal);
                        if (!rc.MenuItem.Option.map.has(option_token.slice(self.lexer.buffer))) {
                            break;
                        }
                        self.nextToken(.normal) catch unreachable;
                        try options.append(self.state.arena, option_token);
                        try self.skipAnyCommas();
                    }

                    try self.nextToken(.normal);
                    const begin_token = self.state.token;
                    try self.check(.begin);

                    var items = std.ArrayListUnmanaged(*Node){};
                    while (try self.parseMenuItemStatement(resource, top_level_menu_id_token, nesting_level + 1)) |item_node| {
                        try items.append(self.state.arena, item_node);
                    }

                    try self.nextToken(.normal);
                    const end_token = self.state.token;
                    try self.check(.end);

                    if (items.items.len == 0) {
                        return self.addErrorDetailsAndFail(.{
                            .err = .empty_menu_not_allowed,
                            .token = menuitem_token,
                        });
                    }

                    const node = try self.state.arena.create(Node.Popup);
                    node.* = .{
                        .popup = menuitem_token,
                        .text = text,
                        .option_list = try options.toOwnedSlice(self.state.arena),
                        .begin_token = begin_token,
                        .items = try items.toOwnedSlice(self.state.arena),
                        .end_token = end_token,
                    };
                    return &node.base;
                },
            },
            .menuex => {
                try self.nextToken(.normal);
                const text = self.state.token;
                if (!text.isStringLiteral()) {
                    return self.addErrorDetailsAndFail(ErrorDetails{
                        .err = .expected_something_else,
                        .token = text,
                        .extra = .{ .expected_types = .{
                            .string_literal = true,
                        } },
                    });
                }

                var param_parser = OptionalParamParser{ .parser = self };
                const id = try param_parser.parse(.{});
                const item_type = try param_parser.parse(.{});
                const state = try param_parser.parse(.{});

                if (menuitem == .menuitem) {
                    // trailing comma is allowed, skip it
                    _ = try self.parseOptionalToken(.comma);

                    const node = try self.state.arena.create(Node.MenuItemEx);
                    node.* = .{
                        .menuitem = menuitem_token,
                        .text = text,
                        .id = id,
                        .type = item_type,
                        .state = state,
                    };
                    return &node.base;
                }

                const help_id = try param_parser.parse(.{});

                // trailing comma is allowed, skip it
                _ = try self.parseOptionalToken(.comma);

                try self.nextToken(.normal);
                const begin_token = self.state.token;
                try self.check(.begin);

                var items = std.ArrayListUnmanaged(*Node){};
                while (try self.parseMenuItemStatement(resource, top_level_menu_id_token, nesting_level + 1)) |item_node| {
                    try items.append(self.state.arena, item_node);
                }

                try self.nextToken(.normal);
                const end_token = self.state.token;
                try self.check(.end);

                if (items.items.len == 0) {
                    return self.addErrorDetailsAndFail(.{
                        .err = .empty_menu_not_allowed,
                        .token = menuitem_token,
                    });
                }

                const node = try self.state.arena.create(Node.PopupEx);
                node.* = .{
                    .popup = menuitem_token,
                    .text = text,
                    .id = id,
                    .type = item_type,
                    .state = state,
                    .help_id = help_id,
                    .begin_token = begin_token,
                    .items = try items.toOwnedSlice(self.state.arena),
                    .end_token = end_token,
                };
                return &node.base;
            },
            else => unreachable,
        }
        @compileError("unreachable");
    }

    pub const OptionalParamParser = struct {
        finished: bool = false,
        parser: *Self,

        pub const Options = struct {
            not_expression_allowed: bool = false,
        };

        pub fn parse(self: *OptionalParamParser, options: OptionalParamParser.Options) Error!?*Node {
            if (self.finished) return null;
            if (!(try self.parser.parseOptionalToken(.comma))) {
                self.finished = true;
                return null;
            }
            // If the next lookahead token could be part of a number expression,
            // then parse it. Otherwise, treat it as an 'empty' expression and
            // continue parsing, since 'empty' values are allowed.
            if (try self.parser.lookaheadCouldBeNumberExpression(switch (options.not_expression_allowed) {
                true => .not_allowed,
                false => .not_disallowed,
            })) {
                const node = try self.parser.parseExpression(.{
                    .allowed_types = .{ .number = true },
                    .can_contain_not_expressions = options.not_expression_allowed,
                });
                return node;
            }
            return null;
        }
    };

    /// Expects the current token to be handled, and that the version statement will
    /// begin on the next token.
    /// After return, the current token will be the token immediately before the end of the
    /// version statement (or unchanged if the function returns null).
    fn parseVersionStatement(self: *Self) Error!?*Node {
        const type_token = try self.lookaheadToken(.normal);
        const statement_type = rc.VersionInfo.map.get(type_token.slice(self.lexer.buffer)) orelse return null;
        self.nextToken(.normal) catch unreachable;
        switch (statement_type) {
            .file_version, .product_version => {
                var parts = std.BoundedArray(*Node, 4){};

                while (parts.len < 4) {
                    const value = try self.parseExpression(.{ .allowed_types = .{ .number = true } });
                    parts.addOneAssumeCapacity().* = value;

                    if (parts.len == 4 or !(try self.parseOptionalToken(.comma))) {
                        break;
                    }
                }

                const node = try self.state.arena.create(Node.VersionStatement);
                node.* = .{
                    .type = type_token,
                    .parts = try self.state.arena.dupe(*Node, parts.slice()),
                };
                return &node.base;
            },
            else => {
                const value = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

                const node = try self.state.arena.create(Node.SimpleStatement);
                node.* = .{
                    .identifier = type_token,
                    .value = value,
                };
                return &node.base;
            },
        }
    }

    /// Expects the current token to be handled, and that the version BLOCK/VALUE will
    /// begin on the next token.
    /// After return, the current token will be the token immediately before the end of the
    /// version BLOCK/VALUE (or unchanged if the function returns null).
    fn parseVersionBlockOrValue(self: *Self, top_level_version_id_token: Token, nesting_level: u32) Error!?*Node {
        const keyword_token = try self.lookaheadToken(.normal);
        const keyword = rc.VersionBlock.map.get(keyword_token.slice(self.lexer.buffer)) orelse return null;
        self.nextToken(.normal) catch unreachable;

        if (nesting_level > max_nested_version_level) {
            try self.addErrorDetails(.{
                .err = .nested_resource_level_exceeds_max,
                .token = top_level_version_id_token,
                .extra = .{ .resource = .versioninfo },
            });
            return self.addErrorDetailsAndFail(.{
                .err = .nested_resource_level_exceeds_max,
                .type = .note,
                .token = keyword_token,
                .extra = .{ .resource = .versioninfo },
            });
        }

        try self.nextToken(.normal);
        const key = self.state.token;
        if (!key.isStringLiteral()) {
            return self.addErrorDetailsAndFail(.{
                .err = .expected_something_else,
                .token = key,
                .extra = .{ .expected_types = .{
                    .string_literal = true,
                } },
            });
        }
        // Need to keep track of this to detect a potential miscompilation when
        // the comma is omitted and the first value is a quoted string.
        const had_comma_before_first_value = try self.parseOptionalToken(.comma);
        try self.skipAnyCommas();

        const values = try self.parseBlockValuesList(had_comma_before_first_value);

        switch (keyword) {
            .block => {
                try self.nextToken(.normal);
                const begin_token = self.state.token;
                try self.check(.begin);

                var children = std.ArrayListUnmanaged(*Node){};
                while (try self.parseVersionBlockOrValue(top_level_version_id_token, nesting_level + 1)) |value_node| {
                    try children.append(self.state.arena, value_node);
                }

                try self.nextToken(.normal);
                const end_token = self.state.token;
                try self.check(.end);

                const node = try self.state.arena.create(Node.Block);
                node.* = .{
                    .identifier = keyword_token,
                    .key = key,
                    .values = values,
                    .begin_token = begin_token,
                    .children = try children.toOwnedSlice(self.state.arena),
                    .end_token = end_token,
                };
                return &node.base;
            },
            .value => {
                const node = try self.state.arena.create(Node.BlockValue);
                node.* = .{
                    .identifier = keyword_token,
                    .key = key,
                    .values = values,
                };
                return &node.base;
            },
        }
    }

    fn parseBlockValuesList(self: *Self, had_comma_before_first_value: bool) Error![]*Node {
        var values = std.ArrayListUnmanaged(*Node){};
        var seen_number: bool = false;
        var first_string_value: ?*Node = null;
        while (true) {
            const lookahead_token = try self.lookaheadToken(.normal);
            switch (lookahead_token.id) {
                .operator,
                .number,
                .open_paren,
                .quoted_ascii_string,
                .quoted_wide_string,
                => {},
                else => break,
            }
            const value = try self.parseExpression(.{});

            if (value.isNumberExpression()) {
                seen_number = true;
            } else if (first_string_value == null) {
                std.debug.assert(value.isStringLiteral());
                first_string_value = value;
            }

            const has_trailing_comma = try self.parseOptionalToken(.comma);
            try self.skipAnyCommas();

            const value_value = try self.state.arena.create(Node.BlockValueValue);
            value_value.* = .{
                .expression = value,
                .trailing_comma = has_trailing_comma,
            };
            try values.append(self.state.arena, &value_value.base);
        }
        if (seen_number and first_string_value != null) {
            // The Win32 RC compiler does some strange stuff with the data size:
            // Strings are counted as UTF-16 code units including the null-terminator
            // Numbers are counted as their byte lengths
            // So, when both strings and numbers are within a single value,
            // it incorrectly sets the value's type as binary, but then gives the
            // data length as a mixture of bytes and UTF-16 code units. This means that
            // when the length is read, it will be treated as byte length and will
            // not read the full value. We don't reproduce this behavior, so we warn
            // of the miscompilation here.
            try self.addErrorDetails(.{
                .err = .rc_would_miscompile_version_value_byte_count,
                .type = .warning,
                .token = first_string_value.?.getFirstToken(),
                .token_span_start = values.items[0].getFirstToken(),
                .token_span_end = values.items[values.items.len - 1].getLastToken(),
            });
            try self.addErrorDetails(.{
                .err = .rc_would_miscompile_version_value_byte_count,
                .type = .note,
                .token = first_string_value.?.getFirstToken(),
                .token_span_start = values.items[0].getFirstToken(),
                .token_span_end = values.items[values.items.len - 1].getLastToken(),
                .print_source_line = false,
            });
        }
        if (!had_comma_before_first_value and values.items.len > 0 and values.items[0].cast(.block_value_value).?.expression.isStringLiteral()) {
            const token = values.items[0].cast(.block_value_value).?.expression.cast(.literal).?.token;
            try self.addErrorDetails(.{
                .err = .rc_would_miscompile_version_value_padding,
                .type = .warning,
                .token = token,
            });
            try self.addErrorDetails(.{
                .err = .rc_would_miscompile_version_value_padding,
                .type = .note,
                .token = token,
                .print_source_line = false,
            });
        }
        return values.toOwnedSlice(self.state.arena);
    }

    fn numberExpressionContainsAnyLSuffixes(expression_node: *Node, source: []const u8, code_page_lookup: *const CodePageLookup) bool {
        // TODO: This could probably be done without evaluating the whole expression
        return Compiler.evaluateNumberExpression(expression_node, source, code_page_lookup).is_long;
    }

    /// Expects the current token to be a literal token that contains the string LANGUAGE
    fn parseLanguageStatement(self: *Self) Error!*Node {
        const language_token = self.state.token;

        const primary_language = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

        try self.nextToken(.normal);
        try self.check(.comma);

        const sublanguage = try self.parseExpression(.{ .allowed_types = .{ .number = true } });

        // The Win32 RC compiler errors if either parameter contains any number with an L
        // suffix. Instead of that, we want to warn and then let the values get truncated.
        // The warning is done here to allow the compiler logic to not have to deal with this.
        if (numberExpressionContainsAnyLSuffixes(primary_language, self.lexer.buffer, &self.state.input_code_page_lookup)) {
            try self.addErrorDetails(.{
                .err = .rc_would_error_u16_with_l_suffix,
                .type = .warning,
                .token = primary_language.getFirstToken(),
                .token_span_end = primary_language.getLastToken(),
                .extra = .{ .statement_with_u16_param = .language },
            });
            try self.addErrorDetails(.{
                .err = .rc_would_error_u16_with_l_suffix,
                .print_source_line = false,
                .type = .note,
                .token = primary_language.getFirstToken(),
                .token_span_end = primary_language.getLastToken(),
                .extra = .{ .statement_with_u16_param = .language },
            });
        }
        if (numberExpressionContainsAnyLSuffixes(sublanguage, self.lexer.buffer, &self.state.input_code_page_lookup)) {
            try self.addErrorDetails(.{
                .err = .rc_would_error_u16_with_l_suffix,
                .type = .warning,
                .token = sublanguage.getFirstToken(),
                .token_span_end = sublanguage.getLastToken(),
                .extra = .{ .statement_with_u16_param = .language },
            });
            try self.addErrorDetails(.{
                .err = .rc_would_error_u16_with_l_suffix,
                .print_source_line = false,
                .type = .note,
                .token = sublanguage.getFirstToken(),
                .token_span_end = sublanguage.getLastToken(),
                .extra = .{ .statement_with_u16_param = .language },
            });
        }

        const node = try self.state.arena.create(Node.LanguageStatement);
        node.* = .{
            .language_token = language_token,
            .primary_language_id = primary_language,
            .sublanguage_id = sublanguage,
        };
        return &node.base;
    }

    pub const ParseExpressionOptions = struct {
        is_known_to_be_number_expression: bool = false,
        can_contain_not_expressions: bool = false,
        nesting_context: NestingContext = .{},
        allowed_types: AllowedTypes = .{ .literal = true, .number = true, .string = true },
        expected_types_override: ?ErrorDetails.ExpectedTypes = null,

        pub const AllowedTypes = struct {
            literal: bool = false,
            number: bool = false,
            string: bool = false,
        };

        pub const NestingContext = struct {
            first_token: ?Token = null,
            last_token: ?Token = null,
            level: u32 = 0,

            /// Returns a new NestingContext with values modified appropriately for an increased nesting level
            fn incremented(ctx: NestingContext, first_token: Token, most_recent_token: Token) NestingContext {
                return .{
                    .first_token = ctx.first_token orelse first_token,
                    .last_token = most_recent_token,
                    .level = ctx.level + 1,
                };
            }
        };

        pub fn toErrorDetails(options: ParseExpressionOptions, token: Token) ErrorDetails {
            // TODO: expected_types_override interaction with is_known_to_be_number_expression?
            const expected_types = options.expected_types_override orelse ErrorDetails.ExpectedTypes{
                .number = options.allowed_types.number,
                .number_expression = options.allowed_types.number,
                .string_literal = options.allowed_types.string and !options.is_known_to_be_number_expression,
                .literal = options.allowed_types.literal and !options.is_known_to_be_number_expression,
            };
            return ErrorDetails{
                .err = .expected_something_else,
                .token = token,
                .extra = .{ .expected_types = expected_types },
            };
        }
    };

    /// Returns true if the next lookahead token is a number or could be the start of a number expression.
    /// Only useful when looking for empty expressions in optional fields.
    fn lookaheadCouldBeNumberExpression(self: *Self, not_allowed: enum { not_allowed, not_disallowed }) Error!bool {
        var lookahead_token = try self.lookaheadToken(.normal);
        switch (lookahead_token.id) {
            .literal => if (not_allowed == .not_allowed) {
                return std.ascii.eqlIgnoreCase("NOT", lookahead_token.slice(self.lexer.buffer));
            } else return false,
            .number => return true,
            .open_paren => return true,
            .operator => {
                // + can be a unary operator, see parseExpression's handling of unary +
                const operator_char = lookahead_token.slice(self.lexer.buffer)[0];
                return operator_char == '+';
            },
            else => return false,
        }
    }

    fn parsePrimary(self: *Self, options: ParseExpressionOptions) Error!*Node {
        try self.nextToken(.normal);
        const first_token = self.state.token;
        var is_close_paren_expression = false;
        var is_unary_plus_expression = false;
        switch (self.state.token.id) {
            .quoted_ascii_string, .quoted_wide_string => {
                if (!options.allowed_types.string) return self.addErrorDetailsAndFail(options.toErrorDetails(self.state.token));
                const node = try self.state.arena.create(Node.Literal);
                node.* = .{ .token = self.state.token };
                return &node.base;
            },
            .literal => {
                if (options.can_contain_not_expressions and std.ascii.eqlIgnoreCase("NOT", self.state.token.slice(self.lexer.buffer))) {
                    const not_token = self.state.token;
                    try self.nextToken(.normal);
                    try self.check(.number);
                    if (!options.allowed_types.number) return self.addErrorDetailsAndFail(options.toErrorDetails(self.state.token));
                    const node = try self.state.arena.create(Node.NotExpression);
                    node.* = .{
                        .not_token = not_token,
                        .number_token = self.state.token,
                    };
                    return &node.base;
                }
                if (!options.allowed_types.literal) return self.addErrorDetailsAndFail(options.toErrorDetails(self.state.token));
                const node = try self.state.arena.create(Node.Literal);
                node.* = .{ .token = self.state.token };
                return &node.base;
            },
            .number => {
                if (!options.allowed_types.number) return self.addErrorDetailsAndFail(options.toErrorDetails(self.state.token));
                const node = try self.state.arena.create(Node.Literal);
                node.* = .{ .token = self.state.token };
                return &node.base;
            },
            .open_paren => {
                const open_paren_token = self.state.token;

                const expression = try self.parseExpression(.{
                    .is_known_to_be_number_expression = true,
                    .can_contain_not_expressions = options.can_contain_not_expressions,
                    .nesting_context = options.nesting_context.incremented(first_token, open_paren_token),
                    .allowed_types = .{ .number = true },
                });

                try self.nextToken(.normal);
                // TODO: Add context to error about where the open paren is
                try self.check(.close_paren);

                if (!options.allowed_types.number) return self.addErrorDetailsAndFail(options.toErrorDetails(open_paren_token));
                const node = try self.state.arena.create(Node.GroupedExpression);
                node.* = .{
                    .open_token = open_paren_token,
                    .expression = expression,
                    .close_token = self.state.token,
                };
                return &node.base;
            },
            .close_paren => {
                // Note: In the Win32 implementation, a single close paren
                // counts as a valid "expression", but only when its the first and
                // only token in the expression. Such an expression is then treated
                // as a 'skip this expression' instruction. For example:
                //   1 RCDATA { 1, ), ), ), 2 }
                // will be evaluated as if it were `1 RCDATA { 1, 2 }` and only
                // 0x0001 and 0x0002 will be written to the .res data.
                //
                // This behavior is not emulated because it almost certainly has
                // no valid use cases and only introduces edge cases that are
                // not worth the effort to track down and deal with. Instead,
                // we error but also add a note about the Win32 RC behavior if
                // this edge case is detected.
                if (!options.is_known_to_be_number_expression) {
                    is_close_paren_expression = true;
                }
            },
            .operator => {
                // In the Win32 implementation, something akin to a unary +
                // is allowed but it doesn't behave exactly like a unary +.
                // Instead of emulating the Win32 behavior, we instead error
                // and add a note about unary plus not being allowed.
                //
                // This is done because unary + only works in some places,
                // and there's no real use-case for it since it's so limited
                // in how it can be used (e.g. +1 is accepted but (+1) will error)
                //
                // Even understanding when unary plus is allowed is difficult, so
                // we don't do any fancy detection of when the Win32 RC compiler would
                // allow a unary + and instead just output the note in all cases.
                //
                // Some examples of allowed expressions by the Win32 compiler:
                //  +1
                //  0|+5
                //  +1+2
                //  +~-5
                //  +(1)
                //
                // Some examples of disallowed expressions by the Win32 compiler:
                //  (+1)
                //  ++5
                //
                // TODO: Potentially re-evaluate and support the unary plus in a bug-for-bug
                //       compatible way.
                const operator_char = self.state.token.slice(self.lexer.buffer)[0];
                if (operator_char == '+') {
                    is_unary_plus_expression = true;
                }
            },
            else => {},
        }

        try self.addErrorDetails(options.toErrorDetails(self.state.token));
        if (is_close_paren_expression) {
            try self.addErrorDetails(ErrorDetails{
                .err = .close_paren_expression,
                .type = .note,
                .token = self.state.token,
                .print_source_line = false,
            });
        }
        if (is_unary_plus_expression) {
            try self.addErrorDetails(ErrorDetails{
                .err = .unary_plus_expression,
                .type = .note,
                .token = self.state.token,
                .print_source_line = false,
            });
        }
        return error.ParseError;
    }

    /// Expects the current token to have already been dealt with, and that the
    /// expression will start on the next token.
    /// After return, the current token will have been dealt with.
    fn parseExpression(self: *Self, options: ParseExpressionOptions) Error!*Node {
        if (options.nesting_context.level > max_nested_expression_level) {
            try self.addErrorDetails(.{
                .err = .nested_expression_level_exceeds_max,
                .token = options.nesting_context.first_token.?,
            });
            return self.addErrorDetailsAndFail(.{
                .err = .nested_expression_level_exceeds_max,
                .type = .note,
                .token = options.nesting_context.last_token.?,
            });
        }
        var expr: *Node = try self.parsePrimary(options);
        const first_token = expr.getFirstToken();

        // Non-number expressions can't have operators, so we can just return
        if (!expr.isNumberExpression()) return expr;

        while (try self.parseOptionalTokenAdvanced(.operator, .normal_expect_operator)) {
            const operator = self.state.token;
            const rhs_node = try self.parsePrimary(.{
                .is_known_to_be_number_expression = true,
                .can_contain_not_expressions = options.can_contain_not_expressions,
                .nesting_context = options.nesting_context.incremented(first_token, operator),
                .allowed_types = options.allowed_types,
            });

            if (!rhs_node.isNumberExpression()) {
                return self.addErrorDetailsAndFail(ErrorDetails{
                    .err = .expected_something_else,
                    .token = rhs_node.getFirstToken(),
                    .token_span_end = rhs_node.getLastToken(),
                    .extra = .{ .expected_types = .{
                        .number = true,
                        .number_expression = true,
                    } },
                });
            }

            const node = try self.state.arena.create(Node.BinaryExpression);
            node.* = .{
                .left = expr,
                .operator = operator,
                .right = rhs_node,
            };
            expr = &node.base;
        }

        return expr;
    }

    /// Skips any amount of commas (including zero)
    /// In other words, it will skip the regex `,*`
    /// Assumes the token(s) should be parsed with `.normal` as the method.
    fn skipAnyCommas(self: *Self) !void {
        while (try self.parseOptionalToken(.comma)) {}
    }

    /// Advances the current token only if the token's id matches the specified `id`.
    /// Assumes the token should be parsed with `.normal` as the method.
    /// Returns true if the token matched, false otherwise.
    fn parseOptionalToken(self: *Self, id: Token.Id) Error!bool {
        return self.parseOptionalTokenAdvanced(id, .normal);
    }

    /// Advances the current token only if the token's id matches the specified `id`.
    /// Returns true if the token matched, false otherwise.
    fn parseOptionalTokenAdvanced(self: *Self, id: Token.Id, comptime method: Lexer.LexMethod) Error!bool {
        const maybe_token = try self.lookaheadToken(method);
        if (maybe_token.id != id) return false;
        self.nextToken(method) catch unreachable;
        return true;
    }

    fn addErrorDetails(self: *Self, details: ErrorDetails) Allocator.Error!void {
        try self.state.diagnostics.append(details);
    }

    fn addErrorDetailsAndFail(self: *Self, details: ErrorDetails) Error {
        try self.addErrorDetails(details);
        return error.ParseError;
    }

    fn nextToken(self: *Self, comptime method: Lexer.LexMethod) Error!void {
        self.state.token = token: while (true) {
            const token = self.lexer.next(method) catch |err| switch (err) {
                error.CodePagePragmaInIncludedFile => {
                    // The Win32 RC compiler silently ignores such `#pragma code_point` directives,
                    // but we want to both ignore them *and* emit a warning
                    try self.addErrorDetails(.{
                        .err = .code_page_pragma_in_included_file,
                        .type = .warning,
                        .token = self.lexer.error_context_token.?,
                    });
                    continue;
                },
                error.CodePagePragmaInvalidCodePage => {
                    var details = self.lexer.getErrorDetails(err);
                    if (!self.options.warn_instead_of_error_on_invalid_code_page) {
                        return self.addErrorDetailsAndFail(details);
                    }
                    details.type = .warning;
                    try self.addErrorDetails(details);
                    continue;
                },
                error.InvalidDigitCharacterInNumberLiteral => {
                    const details = self.lexer.getErrorDetails(err);
                    try self.addErrorDetails(details);
                    return self.addErrorDetailsAndFail(.{
                        .err = details.err,
                        .type = .note,
                        .token = details.token,
                        .print_source_line = false,
                    });
                },
                else => return self.addErrorDetailsAndFail(self.lexer.getErrorDetails(err)),
            };
            break :token token;
        };
        // After every token, set the input code page for its line
        try self.state.input_code_page_lookup.setForToken(self.state.token, self.lexer.current_code_page);
        // But only set the output code page to the current code page if we are past the first code_page pragma in the file.
        // Otherwise, we want to fill the lookup using the default code page so that lookups still work for lines that
        // don't have an explicit output code page set.
        const output_code_page = if (self.lexer.seen_pragma_code_pages > 1) self.lexer.current_code_page else self.state.output_code_page_lookup.default_code_page;
        try self.state.output_code_page_lookup.setForToken(self.state.token, output_code_page);
    }

    fn lookaheadToken(self: *Self, comptime method: Lexer.LexMethod) Error!Token {
        self.state.lookahead_lexer = self.lexer.*;
        return token: while (true) {
            break :token self.state.lookahead_lexer.next(method) catch |err| switch (err) {
                // Ignore this error and get the next valid token, we'll deal with this
                // properly when getting the token for real
                error.CodePagePragmaInIncludedFile => continue,
                else => return self.addErrorDetailsAndFail(self.state.lookahead_lexer.getErrorDetails(err)),
            };
        };
    }

    fn tokenSlice(self: *Self) []const u8 {
        return self.state.token.slice(self.lexer.buffer);
    }

    /// Check that the current token is something that can be used as an ID
    fn checkId(self: *Self) !void {
        switch (self.state.token.id) {
            .literal => {},
            else => {
                return self.addErrorDetailsAndFail(ErrorDetails{
                    .err = .expected_token,
                    .token = self.state.token,
                    .extra = .{ .expected = .literal },
                });
            },
        }
    }

    fn check(self: *Self, expected_token_id: Token.Id) !void {
        if (self.state.token.id != expected_token_id) {
            return self.addErrorDetailsAndFail(ErrorDetails{
                .err = .expected_token,
                .token = self.state.token,
                .extra = .{ .expected = expected_token_id },
            });
        }
    }

    fn checkResource(self: *Self) !Resource {
        switch (self.state.token.id) {
            .literal => return Resource.fromString(.{
                .slice = self.state.token.slice(self.lexer.buffer),
                .code_page = self.lexer.current_code_page,
            }),
            else => {
                return self.addErrorDetailsAndFail(ErrorDetails{
                    .err = .expected_token,
                    .token = self.state.token,
                    .extra = .{ .expected = .literal },
                });
            },
        }
    }
};