@futpib/parser 1.0.3 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +24 -0
- package/.github/workflows/main.yml +1 -0
- package/build/androidPackageParser.js +30 -32
- package/build/arbitraryDalvikBytecode.d.ts +3 -3
- package/build/arbitraryDalvikBytecode.js +33 -27
- package/build/arbitraryDalvikExecutable.js +55 -17
- package/build/arbitraryJava.d.ts +31 -0
- package/build/arbitraryJava.js +532 -0
- package/build/arbitraryJavaScript.d.ts +3 -0
- package/build/arbitraryJavaScript.js +263 -0
- package/build/arbitraryJavascript.d.ts +3 -0
- package/build/arbitraryJavascript.js +263 -0
- package/build/arbitraryZig.d.ts +3 -0
- package/build/arbitraryZig.js +240 -0
- package/build/arbitraryZipStream.d.ts +1 -1
- package/build/arrayParser.js +72 -13
- package/build/backsmali.d.ts +4 -3
- package/build/backsmali.js +26 -6
- package/build/bash.d.ts +89 -0
- package/build/bash.js +1 -0
- package/build/bashParser.d.ts +6 -0
- package/build/bashParser.js +335 -0
- package/build/bashParser.test.d.ts +1 -0
- package/build/bashParser.test.js +343 -0
- package/build/bashParserEdgeCases.test.d.ts +1 -0
- package/build/bashParserEdgeCases.test.js +117 -0
- package/build/dalvikBytecodeParser/addressConversion.d.ts +110 -0
- package/build/dalvikBytecodeParser/addressConversion.js +334 -0
- package/build/dalvikBytecodeParser/formatParsers.d.ts +7 -6
- package/build/dalvikBytecodeParser/formatParsers.js +13 -14
- package/build/dalvikBytecodeParser.d.ts +60 -31
- package/build/dalvikBytecodeParser.js +92 -35
- package/build/dalvikBytecodeParser.test-d.d.ts +1 -0
- package/build/dalvikBytecodeParser.test-d.js +268 -0
- package/build/dalvikBytecodeUnparser/formatUnparsers.d.ts +9 -8
- package/build/dalvikBytecodeUnparser/formatUnparsers.js +13 -12
- package/build/dalvikBytecodeUnparser.d.ts +2 -2
- package/build/dalvikBytecodeUnparser.js +23 -23
- package/build/dalvikBytecodeUnparser.test.js +7 -7
- package/build/dalvikExecutable.d.ts +3 -3
- package/build/dalvikExecutable.test-d.d.ts +1 -0
- package/build/dalvikExecutable.test-d.js +59 -0
- package/build/dalvikExecutableParser/typedNumbers.d.ts +18 -0
- package/build/dalvikExecutableParser/typedNumbers.js +3 -0
- package/build/dalvikExecutableParser.d.ts +2 -1
- package/build/dalvikExecutableParser.js +96 -77
- package/build/dalvikExecutableParser.test.js +24 -3
- package/build/dalvikExecutableParserAgainstSmaliParser.test.js +3 -0
- package/build/dalvikExecutableUnparser/poolScanners.d.ts +2 -2
- package/build/dalvikExecutableUnparser/sectionUnparsers.d.ts +3 -3
- package/build/dalvikExecutableUnparser/sectionUnparsers.js +26 -11
- package/build/dalvikExecutableUnparser.d.ts +2 -2
- package/build/dalvikExecutableUnparser.test.js +2 -1
- package/build/disjunctionParser.d.ts +5 -3
- package/build/disjunctionParser.js +79 -17
- package/build/disjunctionParser.test-d.d.ts +1 -0
- package/build/disjunctionParser.test-d.js +72 -0
- package/build/elementSwitchParser.d.ts +4 -0
- package/build/{exactElementSwitchParser.js → elementSwitchParser.js} +3 -4
- package/build/elementSwitchParser.test-d.d.ts +1 -0
- package/build/elementSwitchParser.test-d.js +44 -0
- package/build/exactSequenceParser.d.ts +4 -2
- package/build/exactSequenceParser.test-d.d.ts +1 -0
- package/build/exactSequenceParser.test-d.js +36 -0
- package/build/fetchCid.js +2 -66
- package/build/index.d.ts +25 -2
- package/build/index.js +23 -1
- package/build/index.test.js +16 -1
- package/build/inputReader.d.ts +10 -0
- package/build/inputReader.js +36 -0
- package/build/java.d.ts +502 -0
- package/build/java.js +2 -0
- package/build/javaKeyStoreParser.js +14 -17
- package/build/javaParser.d.ts +51 -0
- package/build/javaParser.js +1538 -0
- package/build/javaParser.test.d.ts +1 -0
- package/build/javaParser.test.js +1287 -0
- package/build/javaScript.d.ts +35 -0
- package/build/javaScript.js +1 -0
- package/build/javaScriptParser.d.ts +9 -0
- package/build/javaScriptParser.js +34 -0
- package/build/javaScriptUnparser.d.ts +3 -0
- package/build/javaScriptUnparser.js +4 -0
- package/build/javaScriptUnparser.test.d.ts +1 -0
- package/build/javaScriptUnparser.test.js +24 -0
- package/build/javaUnparser.d.ts +2 -0
- package/build/javaUnparser.js +519 -0
- package/build/javaUnparser.test.d.ts +1 -0
- package/build/javaUnparser.test.js +24 -0
- package/build/javascript.d.ts +35 -0
- package/build/javascript.js +1 -0
- package/build/javascriptParser.d.ts +9 -0
- package/build/javascriptParser.js +34 -0
- package/build/javascriptUnparser.d.ts +3 -0
- package/build/javascriptUnparser.js +4 -0
- package/build/javascriptUnparser.test.d.ts +1 -0
- package/build/javascriptUnparser.test.js +24 -0
- package/build/jsonParser.js +2 -12
- package/build/lazyMessageError.d.ts +3 -0
- package/build/lookaheadParser.js +60 -3
- package/build/negativeLookaheadParser.js +70 -11
- package/build/nonEmptyArrayParser.js +72 -13
- package/build/objectParser.d.ts +12 -0
- package/build/objectParser.js +31 -0
- package/build/objectParser.test-d.d.ts +1 -0
- package/build/objectParser.test-d.js +112 -0
- package/build/objectParser.test.d.ts +1 -0
- package/build/objectParser.test.js +55 -0
- package/build/optionalParser.js +69 -10
- package/build/parser.d.ts +4 -0
- package/build/parser.js +3 -1
- package/build/parser.test.js +114 -1
- package/build/parserConsumedSequenceParser.js +66 -7
- package/build/parserContext.d.ts +6 -0
- package/build/parserContext.js +20 -11
- package/build/parserError.d.ts +119 -27
- package/build/parserError.js +16 -8
- package/build/regexpParser.d.ts +2 -0
- package/build/regexpParser.js +101 -0
- package/build/regexpParser.test.d.ts +1 -0
- package/build/regexpParser.test.js +114 -0
- package/build/regularExpression.d.ts +63 -0
- package/build/regularExpression.js +1 -0
- package/build/regularExpressionParser.d.ts +3 -0
- package/build/regularExpressionParser.js +600 -0
- package/build/regularExpressionParser.test.d.ts +1 -0
- package/build/regularExpressionParser.test.js +89 -0
- package/build/separatedArrayParser.js +73 -14
- package/build/separatedNonEmptyArrayParser.js +73 -14
- package/build/sliceBoundedParser.js +62 -5
- package/build/smaliParser.d.ts +7 -7
- package/build/smaliParser.js +185 -268
- package/build/smaliParser.test.js +58 -0
- package/build/stringEscapes.d.ts +5 -0
- package/build/stringEscapes.js +244 -0
- package/build/symbolicExpression.d.ts +29 -0
- package/build/symbolicExpression.js +1 -0
- package/build/symbolicExpressionParser.d.ts +4 -0
- package/build/symbolicExpressionParser.js +123 -0
- package/build/symbolicExpressionParser.test.d.ts +1 -0
- package/build/symbolicExpressionParser.test.js +289 -0
- package/build/terminatedArrayParser.js +113 -38
- package/build/terminatedArrayParser.test.js +4 -2
- package/build/tupleParser.d.ts +7 -15
- package/build/tupleParser.js +1 -0
- package/build/unionParser.d.ts +5 -3
- package/build/unionParser.js +7 -2
- package/build/unionParser.test-d.d.ts +1 -0
- package/build/unionParser.test-d.js +72 -0
- package/build/unionParser.test.js +10 -11
- package/build/zig.d.ts +280 -0
- package/build/zig.js +2 -0
- package/build/zigParser.d.ts +3 -0
- package/build/zigParser.js +1119 -0
- package/build/zigParser.test.d.ts +1 -0
- package/build/zigParser.test.js +1590 -0
- package/build/zigUnparser.d.ts +2 -0
- package/build/zigUnparser.js +460 -0
- package/build/zigUnparser.test.d.ts +1 -0
- package/build/zigUnparser.test.js +24 -0
- package/build/zipParser.js +19 -32
- package/build/zipUnparser.js +19 -7
- package/build/zipUnparser.test.js +1 -1
- package/node_modules-@types/s-expression/index.d.ts +5 -0
- package/package.json +25 -6
- package/src/androidPackageParser.ts +33 -60
- package/src/arbitraryDalvikBytecode.ts +39 -31
- package/src/arbitraryDalvikExecutable.ts +65 -20
- package/src/arbitraryJava.ts +804 -0
- package/src/arbitraryJavaScript.ts +410 -0
- package/src/arbitraryZig.ts +380 -0
- package/src/arrayParser.ts +1 -3
- package/src/backsmali.ts +35 -4
- package/src/bash.ts +127 -0
- package/src/bashParser.test.ts +590 -0
- package/src/bashParser.ts +498 -0
- package/src/dalvikBytecodeParser/addressConversion.ts +496 -0
- package/src/dalvikBytecodeParser/formatParsers.ts +19 -29
- package/src/dalvikBytecodeParser.test-d.ts +310 -0
- package/src/dalvikBytecodeParser.ts +194 -69
- package/src/dalvikBytecodeUnparser/formatUnparsers.ts +27 -26
- package/src/dalvikBytecodeUnparser.test.ts +7 -7
- package/src/dalvikBytecodeUnparser.ts +31 -30
- package/src/dalvikExecutable.test-d.ts +132 -0
- package/src/dalvikExecutable.ts +3 -3
- package/src/dalvikExecutableParser/typedNumbers.ts +11 -0
- package/src/dalvikExecutableParser.test.ts +37 -3
- package/src/dalvikExecutableParser.test.ts.md +163 -2
- package/src/dalvikExecutableParser.test.ts.snap +0 -0
- package/src/dalvikExecutableParser.ts +121 -139
- package/src/dalvikExecutableParserAgainstSmaliParser.test.ts +4 -0
- package/src/dalvikExecutableUnparser/poolScanners.ts +6 -6
- package/src/dalvikExecutableUnparser/sectionUnparsers.ts +38 -14
- package/src/dalvikExecutableUnparser.test.ts +3 -2
- package/src/dalvikExecutableUnparser.ts +4 -4
- package/src/disjunctionParser.test-d.ts +105 -0
- package/src/disjunctionParser.ts +18 -15
- package/src/elementSwitchParser.test-d.ts +74 -0
- package/src/elementSwitchParser.ts +51 -0
- package/src/exactSequenceParser.test-d.ts +43 -0
- package/src/exactSequenceParser.ts +13 -8
- package/src/fetchCid.ts +2 -76
- package/src/index.test.ts +22 -1
- package/src/index.ts +119 -2
- package/src/inputReader.ts +53 -0
- package/src/java.ts +708 -0
- package/src/javaKeyStoreParser.ts +18 -32
- package/src/javaParser.test.ts +1592 -0
- package/src/javaParser.ts +2640 -0
- package/src/javaScript.ts +36 -0
- package/src/javaScriptParser.ts +57 -0
- package/src/javaScriptUnparser.test.ts +37 -0
- package/src/javaScriptUnparser.ts +7 -0
- package/src/javaUnparser.test.ts +37 -0
- package/src/javaUnparser.ts +640 -0
- package/src/jsonParser.ts +6 -27
- package/src/lookaheadParser.ts +2 -6
- package/src/negativeLookaheadParser.ts +1 -3
- package/src/nonEmptyArrayParser.ts +1 -3
- package/src/objectParser.test-d.ts +152 -0
- package/src/objectParser.test.ts +71 -0
- package/src/objectParser.ts +69 -0
- package/src/optionalParser.ts +1 -3
- package/src/parser.test.ts +151 -4
- package/src/parser.ts +11 -1
- package/src/parserConsumedSequenceParser.ts +2 -4
- package/src/parserContext.ts +26 -11
- package/src/parserError.ts +17 -3
- package/src/regexpParser.test.ts +264 -0
- package/src/regexpParser.ts +126 -0
- package/src/regularExpression.ts +24 -0
- package/src/regularExpressionParser.test.ts +102 -0
- package/src/regularExpressionParser.ts +920 -0
- package/src/separatedArrayParser.ts +1 -3
- package/src/separatedNonEmptyArrayParser.ts +1 -3
- package/src/sliceBoundedParser.test.ts +2 -2
- package/src/sliceBoundedParser.ts +15 -19
- package/src/smaliParser.test.ts +64 -0
- package/src/smaliParser.test.ts.md +12 -12
- package/src/smaliParser.test.ts.snap +0 -0
- package/src/smaliParser.ts +246 -534
- package/src/stringEscapes.ts +253 -0
- package/src/symbolicExpression.ts +17 -0
- package/src/symbolicExpressionParser.test.ts +466 -0
- package/src/symbolicExpressionParser.ts +190 -0
- package/src/terminatedArrayParser.test.ts +9 -6
- package/src/terminatedArrayParser.ts +25 -29
- package/src/tupleParser.ts +21 -18
- package/src/unionParser.test-d.ts +105 -0
- package/src/unionParser.test.ts +18 -17
- package/src/unionParser.ts +28 -16
- package/src/zig.ts +411 -0
- package/src/zigParser.test.ts +1693 -0
- package/src/zigParser.ts +1745 -0
- package/src/zigUnparser.test.ts +37 -0
- package/src/zigUnparser.ts +615 -0
- package/src/zipParser.ts +20 -56
- package/src/zipUnparser.test.ts +1 -1
- package/src/zipUnparser.ts +22 -7
- package/tsconfig.json +2 -2
- package/build/exactElementSwitchParser.d.ts +0 -3
- package/src/exactElementSwitchParser.ts +0 -41
package/src/zigParser.ts
ADDED
|
@@ -0,0 +1,1745 @@
|
|
|
1
|
+
import { type Parser, setParserName } from './parser.js';
|
|
2
|
+
import { createUnionParser } from './unionParser.js';
|
|
3
|
+
import { createExactSequenceParser } from './exactSequenceParser.js';
|
|
4
|
+
import { promiseCompose } from './promiseCompose.js';
|
|
5
|
+
import { createTupleParser } from './tupleParser.js';
|
|
6
|
+
import { createDisjunctionParser } from './disjunctionParser.js';
|
|
7
|
+
import { createArrayParser } from './arrayParser.js';
|
|
8
|
+
import { createOptionalParser } from './optionalParser.js';
|
|
9
|
+
import { createRegExpParser } from './regexpParser.js';
|
|
10
|
+
import { createSeparatedNonEmptyArrayParser } from './separatedNonEmptyArrayParser.js';
|
|
11
|
+
import { createObjectParser } from './objectParser.js';
|
|
12
|
+
import { createNonEmptyArrayParser } from './nonEmptyArrayParser.js';
|
|
13
|
+
import { createParserAccessorParser } from './parserAccessorParser.js';
|
|
14
|
+
import { unescapeZigString } from './stringEscapes.js';
|
|
15
|
+
import {
|
|
16
|
+
type ZigExpression,
|
|
17
|
+
type ZigTypeExpression,
|
|
18
|
+
type ZigStatement,
|
|
19
|
+
type ZigBlockExpr,
|
|
20
|
+
type ZigFnParam,
|
|
21
|
+
type ZigFnDecl,
|
|
22
|
+
type ZigVarDecl,
|
|
23
|
+
type ZigTestDecl,
|
|
24
|
+
type ZigUsingnamespaceDecl,
|
|
25
|
+
type ZigContainerMember,
|
|
26
|
+
type ZigContainerField,
|
|
27
|
+
type ZigRoot,
|
|
28
|
+
} from './zig.js';
|
|
29
|
+
|
|
30
|
+
// Whitespace (spaces, tabs, newlines)
|
|
31
|
+
const zigWhitespaceParser: Parser<string, string> = promiseCompose(
|
|
32
|
+
createRegExpParser(/\s+/),
|
|
33
|
+
match => match[0],
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
// Line comment: // ...
|
|
37
|
+
const zigLineCommentParser: Parser<string, string> = promiseCompose(
|
|
38
|
+
createRegExpParser(/\/\/[^\n]*/),
|
|
39
|
+
match => match[0],
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
// Whitespace or comment
|
|
43
|
+
const zigWhitespaceOrCommentParser: Parser<string, string> = createUnionParser([
|
|
44
|
+
zigWhitespaceParser,
|
|
45
|
+
zigLineCommentParser,
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
// Optional whitespace/comments (skippable)
|
|
49
|
+
const zigSkippableParser: Parser<unknown, string> = createArrayParser(zigWhitespaceOrCommentParser);
|
|
50
|
+
|
|
51
|
+
setParserName(zigSkippableParser, 'zigSkippableParser');
|
|
52
|
+
|
|
53
|
+
// Mandatory whitespace (at least one whitespace or comment)
|
|
54
|
+
const zigMandatorySkipParser: Parser<unknown, string> = createNonEmptyArrayParser(zigWhitespaceOrCommentParser);
|
|
55
|
+
|
|
56
|
+
setParserName(zigMandatorySkipParser, 'zigMandatorySkipParser');
|
|
57
|
+
|
|
58
|
+
// Keywords
|
|
59
|
+
const zigKeywords = new Set([
|
|
60
|
+
'addrspace', 'align', 'allowzero', 'and', 'anyframe', 'anytype',
|
|
61
|
+
'asm', 'async', 'await', 'break', 'callconv', 'catch', 'comptime',
|
|
62
|
+
'const', 'continue', 'defer', 'else', 'enum', 'errdefer', 'error',
|
|
63
|
+
'export', 'extern', 'false', 'fn', 'for', 'if', 'inline',
|
|
64
|
+
'linksection', 'noalias', 'nosuspend', 'null', 'opaque', 'or',
|
|
65
|
+
'orelse', 'packed', 'pub', 'resume', 'return', 'struct',
|
|
66
|
+
'suspend', 'switch', 'test', 'threadlocal', 'true', 'try',
|
|
67
|
+
'undefined', 'union', 'unreachable', 'var', 'volatile', 'while',
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
// Identifier: [a-zA-Z_][a-zA-Z0-9_]* (not a keyword) or @"string"
|
|
71
|
+
const zigIdentifierParser: Parser<string, string> = createDisjunctionParser([
|
|
72
|
+
promiseCompose(
|
|
73
|
+
createRegExpParser(/@"(?:[^"\\]|\\.)*"/),
|
|
74
|
+
match => match[0],
|
|
75
|
+
),
|
|
76
|
+
promiseCompose(
|
|
77
|
+
createRegExpParser(/[a-zA-Z_][a-zA-Z0-9_]*/),
|
|
78
|
+
match => {
|
|
79
|
+
if (zigKeywords.has(match[0])) {
|
|
80
|
+
throw Object.assign(
|
|
81
|
+
new Error(`Expected identifier, got keyword "${match[0]}"`),
|
|
82
|
+
{ depth: 0, position: 0, furthestReadPosition: 0, furthestPeekedPosition: 0 },
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return match[0];
|
|
86
|
+
},
|
|
87
|
+
),
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
setParserName(zigIdentifierParser, 'zigIdentifierParser');
|
|
91
|
+
|
|
92
|
+
// Keyword parser helper
|
|
93
|
+
function zigKeyword(kw: string): Parser<string, string> {
|
|
94
|
+
return promiseCompose(
|
|
95
|
+
createRegExpParser(new RegExp(kw + '(?![a-zA-Z0-9_])')),
|
|
96
|
+
match => match[0],
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Literals
|
|
101
|
+
const zigIntegerLiteralParser: Parser<ZigExpression, string> = promiseCompose(
|
|
102
|
+
createRegExpParser(/0x[0-9a-fA-F][0-9a-fA-F_]*|0o[0-7][0-7_]*|0b[01][01_]*|[0-9][0-9_]*/),
|
|
103
|
+
match => ({
|
|
104
|
+
type: 'IntegerLiteral' as const,
|
|
105
|
+
value: match[0],
|
|
106
|
+
}),
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
setParserName(zigIntegerLiteralParser, 'zigIntegerLiteralParser');
|
|
110
|
+
|
|
111
|
+
const zigFloatLiteralParser: Parser<ZigExpression, string> = promiseCompose(
|
|
112
|
+
createRegExpParser(/[0-9][0-9_]*\.[0-9][0-9_]*(?:[eE][+-]?[0-9][0-9_]*)?|[0-9][0-9_]*[eE][+-]?[0-9][0-9_]*|0x[0-9a-fA-F][0-9a-fA-F_]*\.[0-9a-fA-F][0-9a-fA-F_]*(?:[pP][+-]?[0-9][0-9_]*)?|0x[0-9a-fA-F][0-9a-fA-F_]*[pP][+-]?[0-9][0-9_]*/),
|
|
113
|
+
match => ({
|
|
114
|
+
type: 'FloatLiteral' as const,
|
|
115
|
+
value: match[0],
|
|
116
|
+
}),
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
setParserName(zigFloatLiteralParser, 'zigFloatLiteralParser');
|
|
120
|
+
|
|
121
|
+
const zigStringLiteralParser: Parser<ZigExpression, string> = promiseCompose(
|
|
122
|
+
createRegExpParser(/"(?:[^"\\]|\\.)*"/),
|
|
123
|
+
match => ({
|
|
124
|
+
type: 'StringLiteral' as const,
|
|
125
|
+
value: unescapeZigString(match[0].slice(1, -1)),
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
setParserName(zigStringLiteralParser, 'zigStringLiteralParser');
|
|
130
|
+
|
|
131
|
+
const zigMultilineStringLiteralParser: Parser<ZigExpression, string> = promiseCompose(
|
|
132
|
+
createRegExpParser(/(?:\\\\[^\n]*\n?)+/),
|
|
133
|
+
match => ({
|
|
134
|
+
type: 'MultilineStringLiteral' as const,
|
|
135
|
+
value: match[0],
|
|
136
|
+
}),
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
setParserName(zigMultilineStringLiteralParser, 'zigMultilineStringLiteralParser');
|
|
140
|
+
|
|
141
|
+
const zigCharLiteralParser: Parser<ZigExpression, string> = promiseCompose(
|
|
142
|
+
createRegExpParser(/'(?:[^'\\]|\\.)*'/),
|
|
143
|
+
match => ({
|
|
144
|
+
type: 'CharLiteral' as const,
|
|
145
|
+
value: unescapeZigString(match[0].slice(1, -1)),
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
setParserName(zigCharLiteralParser, 'zigCharLiteralParser');
|
|
150
|
+
|
|
151
|
+
const zigEnumLiteralParser: Parser<ZigExpression, string> = promiseCompose(
|
|
152
|
+
createTupleParser([
|
|
153
|
+
createExactSequenceParser('.'),
|
|
154
|
+
zigIdentifierParser,
|
|
155
|
+
]),
|
|
156
|
+
([, name]) => ({
|
|
157
|
+
type: 'EnumLiteral' as const,
|
|
158
|
+
name,
|
|
159
|
+
}),
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
setParserName(zigEnumLiteralParser, 'zigEnumLiteralParser');
|
|
163
|
+
|
|
164
|
+
// Forward references via accessor parsers
|
|
165
|
+
const zigExpressionParser: Parser<ZigExpression, string> = createParserAccessorParser(() => zigExpressionParserImpl);
|
|
166
|
+
const zigTypeExprParser: Parser<ZigExpression, string> = createParserAccessorParser(() => zigErrorUnionExprParser);
|
|
167
|
+
const zigStatementParser: Parser<ZigStatement, string> = createParserAccessorParser(() => zigStatementParserImpl);
|
|
168
|
+
const zigBlockExprParser: Parser<ZigBlockExpr, string> = createParserAccessorParser(() => zigBlockExprParserImpl);
|
|
169
|
+
const zigPrefixExprParser: Parser<ZigExpression, string> = createParserAccessorParser(() => zigPrefixExprParserImpl);
|
|
170
|
+
const zigPrimaryExprParser: Parser<ZigExpression, string> = createParserAccessorParser(() => zigPrimaryExprParserImpl);
|
|
171
|
+
|
|
172
|
+
// Builtin call: @import("std"), @intCast(x), etc.
|
|
173
|
+
const zigBuiltinCallExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
174
|
+
createTupleParser([
|
|
175
|
+
createRegExpParser(/@[a-zA-Z_][a-zA-Z0-9_]*/),
|
|
176
|
+
zigSkippableParser,
|
|
177
|
+
createExactSequenceParser('('),
|
|
178
|
+
zigSkippableParser,
|
|
179
|
+
createOptionalParser(
|
|
180
|
+
createSeparatedNonEmptyArrayParser(
|
|
181
|
+
promiseCompose(
|
|
182
|
+
createTupleParser([
|
|
183
|
+
zigExpressionParser,
|
|
184
|
+
zigSkippableParser,
|
|
185
|
+
]),
|
|
186
|
+
([expr]) => expr,
|
|
187
|
+
),
|
|
188
|
+
promiseCompose(
|
|
189
|
+
createTupleParser([
|
|
190
|
+
createExactSequenceParser(','),
|
|
191
|
+
zigSkippableParser,
|
|
192
|
+
]),
|
|
193
|
+
() => ',',
|
|
194
|
+
),
|
|
195
|
+
),
|
|
196
|
+
),
|
|
197
|
+
zigSkippableParser,
|
|
198
|
+
createOptionalParser(createExactSequenceParser(',')),
|
|
199
|
+
zigSkippableParser,
|
|
200
|
+
createExactSequenceParser(')'),
|
|
201
|
+
]),
|
|
202
|
+
([name, , , , args]) => ({
|
|
203
|
+
type: 'BuiltinCallExpr' as const,
|
|
204
|
+
name: name[0].slice(1),
|
|
205
|
+
args: args ?? [],
|
|
206
|
+
}),
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
setParserName(zigBuiltinCallExprParser, 'zigBuiltinCallExprParser');
|
|
210
|
+
|
|
211
|
+
// Error set: error{OutOfMemory, ...}
|
|
212
|
+
const zigErrorSetExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
213
|
+
createTupleParser([
|
|
214
|
+
zigKeyword('error'),
|
|
215
|
+
zigSkippableParser,
|
|
216
|
+
createExactSequenceParser('{'),
|
|
217
|
+
zigSkippableParser,
|
|
218
|
+
createOptionalParser(
|
|
219
|
+
createSeparatedNonEmptyArrayParser(
|
|
220
|
+
promiseCompose(
|
|
221
|
+
createTupleParser([
|
|
222
|
+
zigIdentifierParser,
|
|
223
|
+
zigSkippableParser,
|
|
224
|
+
]),
|
|
225
|
+
([name]) => name,
|
|
226
|
+
),
|
|
227
|
+
promiseCompose(
|
|
228
|
+
createTupleParser([
|
|
229
|
+
createExactSequenceParser(','),
|
|
230
|
+
zigSkippableParser,
|
|
231
|
+
]),
|
|
232
|
+
() => ',',
|
|
233
|
+
),
|
|
234
|
+
),
|
|
235
|
+
),
|
|
236
|
+
createOptionalParser(createExactSequenceParser(',')),
|
|
237
|
+
zigSkippableParser,
|
|
238
|
+
createExactSequenceParser('}'),
|
|
239
|
+
]),
|
|
240
|
+
([, , , , names]) => ({
|
|
241
|
+
type: 'ErrorSetExpr' as const,
|
|
242
|
+
names: names ?? [],
|
|
243
|
+
}),
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
setParserName(zigErrorSetExprParser, 'zigErrorSetExprParser');
|
|
247
|
+
|
|
248
|
+
// Grouped expression: (expr)
|
|
249
|
+
const zigGroupedExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
250
|
+
createTupleParser([
|
|
251
|
+
createExactSequenceParser('('),
|
|
252
|
+
zigSkippableParser,
|
|
253
|
+
zigExpressionParser,
|
|
254
|
+
zigSkippableParser,
|
|
255
|
+
createExactSequenceParser(')'),
|
|
256
|
+
]),
|
|
257
|
+
([, , inner]) => ({
|
|
258
|
+
type: 'GroupedExpr' as const,
|
|
259
|
+
inner,
|
|
260
|
+
}),
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
setParserName(zigGroupedExprParser, 'zigGroupedExprParser');
|
|
264
|
+
|
|
265
|
+
// Capture: |name|
|
|
266
|
+
const zigCaptureParser: Parser<string, string> = promiseCompose(
|
|
267
|
+
createTupleParser([
|
|
268
|
+
createExactSequenceParser('|'),
|
|
269
|
+
zigSkippableParser,
|
|
270
|
+
zigIdentifierParser,
|
|
271
|
+
zigSkippableParser,
|
|
272
|
+
createExactSequenceParser('|'),
|
|
273
|
+
zigSkippableParser,
|
|
274
|
+
]),
|
|
275
|
+
([, , name]) => name,
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
setParserName(zigCaptureParser, 'zigCaptureParser');
|
|
279
|
+
|
|
280
|
+
// If expression: if (cond) |capture| body else |capture| elseBody
|
|
281
|
+
const zigIfExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
282
|
+
createTupleParser([
|
|
283
|
+
zigKeyword('if'),
|
|
284
|
+
zigSkippableParser,
|
|
285
|
+
createExactSequenceParser('('),
|
|
286
|
+
zigSkippableParser,
|
|
287
|
+
zigExpressionParser,
|
|
288
|
+
zigSkippableParser,
|
|
289
|
+
createExactSequenceParser(')'),
|
|
290
|
+
zigSkippableParser,
|
|
291
|
+
createOptionalParser(zigCaptureParser),
|
|
292
|
+
zigExpressionParser,
|
|
293
|
+
zigSkippableParser,
|
|
294
|
+
createOptionalParser(
|
|
295
|
+
promiseCompose(
|
|
296
|
+
createTupleParser([
|
|
297
|
+
zigKeyword('else'),
|
|
298
|
+
zigSkippableParser,
|
|
299
|
+
createOptionalParser(zigCaptureParser),
|
|
300
|
+
zigExpressionParser,
|
|
301
|
+
]),
|
|
302
|
+
([, , capture, body]) => ({ capture, body }),
|
|
303
|
+
),
|
|
304
|
+
),
|
|
305
|
+
]),
|
|
306
|
+
([, , , , condition, , , , capture, body, , elsePart]) => ({
|
|
307
|
+
type: 'IfExpr' as const,
|
|
308
|
+
condition,
|
|
309
|
+
...(capture ? { capture } : {}),
|
|
310
|
+
body,
|
|
311
|
+
...(elsePart?.capture ? { elseCapture: elsePart.capture } : {}),
|
|
312
|
+
...(elsePart ? { elseBody: elsePart.body } : {}),
|
|
313
|
+
}),
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
setParserName(zigIfExprParser, 'zigIfExprParser');
|
|
317
|
+
|
|
318
|
+
// Switch expression
|
|
319
|
+
const zigSwitchProngParser = promiseCompose(
|
|
320
|
+
createTupleParser([
|
|
321
|
+
createDisjunctionParser([
|
|
322
|
+
promiseCompose(
|
|
323
|
+
zigKeyword('else'),
|
|
324
|
+
(): { isElse: boolean; cases: ZigExpression[] } => ({ isElse: true, cases: [] }),
|
|
325
|
+
),
|
|
326
|
+
promiseCompose(
|
|
327
|
+
createSeparatedNonEmptyArrayParser(
|
|
328
|
+
promiseCompose(
|
|
329
|
+
createTupleParser([
|
|
330
|
+
zigExpressionParser,
|
|
331
|
+
zigSkippableParser,
|
|
332
|
+
]),
|
|
333
|
+
([expr]) => expr,
|
|
334
|
+
),
|
|
335
|
+
promiseCompose(
|
|
336
|
+
createTupleParser([
|
|
337
|
+
createExactSequenceParser(','),
|
|
338
|
+
zigSkippableParser,
|
|
339
|
+
]),
|
|
340
|
+
() => ',',
|
|
341
|
+
),
|
|
342
|
+
),
|
|
343
|
+
(cases): { isElse: boolean; cases: ZigExpression[] } => ({ isElse: false, cases }),
|
|
344
|
+
),
|
|
345
|
+
]),
|
|
346
|
+
zigSkippableParser,
|
|
347
|
+
createExactSequenceParser('=>'),
|
|
348
|
+
zigSkippableParser,
|
|
349
|
+
createOptionalParser(zigCaptureParser),
|
|
350
|
+
zigExpressionParser,
|
|
351
|
+
]),
|
|
352
|
+
([casesInfo, , , , capture, body]) => ({
|
|
353
|
+
type: 'SwitchProng' as const,
|
|
354
|
+
cases: casesInfo.cases,
|
|
355
|
+
isElse: casesInfo.isElse,
|
|
356
|
+
...(capture ? { capture } : {}),
|
|
357
|
+
body,
|
|
358
|
+
}),
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
const zigSwitchExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
362
|
+
createTupleParser([
|
|
363
|
+
zigKeyword('switch'),
|
|
364
|
+
zigSkippableParser,
|
|
365
|
+
createExactSequenceParser('('),
|
|
366
|
+
zigSkippableParser,
|
|
367
|
+
zigExpressionParser,
|
|
368
|
+
zigSkippableParser,
|
|
369
|
+
createExactSequenceParser(')'),
|
|
370
|
+
zigSkippableParser,
|
|
371
|
+
createExactSequenceParser('{'),
|
|
372
|
+
zigSkippableParser,
|
|
373
|
+
createOptionalParser(
|
|
374
|
+
createSeparatedNonEmptyArrayParser(
|
|
375
|
+
promiseCompose(
|
|
376
|
+
createTupleParser([
|
|
377
|
+
zigSwitchProngParser,
|
|
378
|
+
zigSkippableParser,
|
|
379
|
+
]),
|
|
380
|
+
([prong]) => prong,
|
|
381
|
+
),
|
|
382
|
+
promiseCompose(
|
|
383
|
+
createTupleParser([
|
|
384
|
+
createExactSequenceParser(','),
|
|
385
|
+
zigSkippableParser,
|
|
386
|
+
]),
|
|
387
|
+
() => ',',
|
|
388
|
+
),
|
|
389
|
+
),
|
|
390
|
+
),
|
|
391
|
+
createOptionalParser(createExactSequenceParser(',')),
|
|
392
|
+
zigSkippableParser,
|
|
393
|
+
createExactSequenceParser('}'),
|
|
394
|
+
]),
|
|
395
|
+
([, , , , operand, , , , , , prongs]) => ({
|
|
396
|
+
type: 'SwitchExpr' as const,
|
|
397
|
+
operand,
|
|
398
|
+
prongs: prongs ?? [],
|
|
399
|
+
}),
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
setParserName(zigSwitchExprParser, 'zigSwitchExprParser');
|
|
403
|
+
|
|
404
|
+
// Struct init: .{ .field = value, ... }
|
|
405
|
+
const zigAnonStructInitParser: Parser<ZigExpression, string> = promiseCompose(
|
|
406
|
+
createTupleParser([
|
|
407
|
+
createExactSequenceParser('.'),
|
|
408
|
+
zigSkippableParser,
|
|
409
|
+
createExactSequenceParser('{'),
|
|
410
|
+
zigSkippableParser,
|
|
411
|
+
createOptionalParser(
|
|
412
|
+
createSeparatedNonEmptyArrayParser(
|
|
413
|
+
promiseCompose(
|
|
414
|
+
createTupleParser([
|
|
415
|
+
createExactSequenceParser('.'),
|
|
416
|
+
zigIdentifierParser,
|
|
417
|
+
zigSkippableParser,
|
|
418
|
+
createExactSequenceParser('='),
|
|
419
|
+
zigSkippableParser,
|
|
420
|
+
zigExpressionParser,
|
|
421
|
+
zigSkippableParser,
|
|
422
|
+
]),
|
|
423
|
+
([, name, , , , value]) => ({ type: 'StructInitField' as const, name, value }),
|
|
424
|
+
),
|
|
425
|
+
promiseCompose(
|
|
426
|
+
createTupleParser([
|
|
427
|
+
createExactSequenceParser(','),
|
|
428
|
+
zigSkippableParser,
|
|
429
|
+
]),
|
|
430
|
+
() => ',',
|
|
431
|
+
),
|
|
432
|
+
),
|
|
433
|
+
),
|
|
434
|
+
createOptionalParser(createExactSequenceParser(',')),
|
|
435
|
+
zigSkippableParser,
|
|
436
|
+
createExactSequenceParser('}'),
|
|
437
|
+
]),
|
|
438
|
+
([, , , , fields]) => ({
|
|
439
|
+
type: 'StructInitExpr' as const,
|
|
440
|
+
fields: fields ?? [],
|
|
441
|
+
}),
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
setParserName(zigAnonStructInitParser, 'zigAnonStructInitParser');
|
|
445
|
+
|
|
446
|
+
// Anonymous array init: .{ expr1, expr2, ... }
|
|
447
|
+
const zigAnonArrayInitParser: Parser<ZigExpression, string> = promiseCompose(
|
|
448
|
+
createTupleParser([
|
|
449
|
+
createExactSequenceParser('.'),
|
|
450
|
+
zigSkippableParser,
|
|
451
|
+
createExactSequenceParser('{'),
|
|
452
|
+
zigSkippableParser,
|
|
453
|
+
createOptionalParser(
|
|
454
|
+
createSeparatedNonEmptyArrayParser(
|
|
455
|
+
promiseCompose(
|
|
456
|
+
createTupleParser([
|
|
457
|
+
zigExpressionParser,
|
|
458
|
+
zigSkippableParser,
|
|
459
|
+
]),
|
|
460
|
+
([expr]) => expr,
|
|
461
|
+
),
|
|
462
|
+
promiseCompose(
|
|
463
|
+
createTupleParser([
|
|
464
|
+
createExactSequenceParser(','),
|
|
465
|
+
zigSkippableParser,
|
|
466
|
+
]),
|
|
467
|
+
() => ',',
|
|
468
|
+
),
|
|
469
|
+
),
|
|
470
|
+
),
|
|
471
|
+
createOptionalParser(createExactSequenceParser(',')),
|
|
472
|
+
zigSkippableParser,
|
|
473
|
+
createExactSequenceParser('}'),
|
|
474
|
+
]),
|
|
475
|
+
([, , , , elements]) => ({
|
|
476
|
+
type: 'ArrayInitExpr' as const,
|
|
477
|
+
elements: elements ?? [],
|
|
478
|
+
}),
|
|
479
|
+
);
|
|
480
|
+
|
|
481
|
+
setParserName(zigAnonArrayInitParser, 'zigAnonArrayInitParser');
|
|
482
|
+
|
|
483
|
+
// Block expression: [label:] { statements... }
|
|
484
|
+
const zigBlockExprParserImpl: Parser<ZigBlockExpr, string> = promiseCompose(
|
|
485
|
+
createTupleParser([
|
|
486
|
+
createOptionalParser(
|
|
487
|
+
promiseCompose(
|
|
488
|
+
createTupleParser([
|
|
489
|
+
zigIdentifierParser,
|
|
490
|
+
zigSkippableParser,
|
|
491
|
+
createExactSequenceParser(':'),
|
|
492
|
+
zigSkippableParser,
|
|
493
|
+
]),
|
|
494
|
+
([label]) => label,
|
|
495
|
+
),
|
|
496
|
+
),
|
|
497
|
+
createExactSequenceParser('{'),
|
|
498
|
+
zigSkippableParser,
|
|
499
|
+
createArrayParser(
|
|
500
|
+
promiseCompose(
|
|
501
|
+
createTupleParser([
|
|
502
|
+
zigStatementParser,
|
|
503
|
+
zigSkippableParser,
|
|
504
|
+
]),
|
|
505
|
+
([stmt]) => stmt,
|
|
506
|
+
),
|
|
507
|
+
),
|
|
508
|
+
createExactSequenceParser('}'),
|
|
509
|
+
]),
|
|
510
|
+
([label, , , statements]) => ({
|
|
511
|
+
type: 'BlockExpr' as const,
|
|
512
|
+
...(label ? { label } : {}),
|
|
513
|
+
statements,
|
|
514
|
+
}),
|
|
515
|
+
);
|
|
516
|
+
|
|
517
|
+
setParserName(zigBlockExprParserImpl, 'zigBlockExprParserImpl');
|
|
518
|
+
|
|
519
|
+
// Try and comptime are handled as prefix operators below
|
|
520
|
+
|
|
521
|
+
// Identifier expression
|
|
522
|
+
const zigIdentifierExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
523
|
+
zigIdentifierParser,
|
|
524
|
+
name => ({
|
|
525
|
+
type: 'Identifier' as const,
|
|
526
|
+
name,
|
|
527
|
+
}),
|
|
528
|
+
);
|
|
529
|
+
|
|
530
|
+
setParserName(zigIdentifierExprParser, 'zigIdentifierExprParser');
|
|
531
|
+
|
|
532
|
+
// Forward references for items defined later
|
|
533
|
+
const zigFnParamListParserRef: Parser<ZigFnParam[], string> = createParserAccessorParser(() => zigFnParamListParser);
|
|
534
|
+
const zigContainerMemberParserRef: Parser<ZigContainerMember, string> = createParserAccessorParser(() => zigContainerMemberParser);
|
|
535
|
+
|
|
536
|
+
// Function prototype type expression: fn(params) returnType
|
|
537
|
+
const zigFnProtoTypeExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
538
|
+
createTupleParser([
|
|
539
|
+
zigKeyword('fn'),
|
|
540
|
+
zigSkippableParser,
|
|
541
|
+
zigFnParamListParserRef,
|
|
542
|
+
zigSkippableParser,
|
|
543
|
+
zigTypeExprParser,
|
|
544
|
+
]),
|
|
545
|
+
([, , params, , returnType]): ZigExpression => ({
|
|
546
|
+
type: 'FnProtoType' as const,
|
|
547
|
+
params,
|
|
548
|
+
returnType,
|
|
549
|
+
}),
|
|
550
|
+
);
|
|
551
|
+
|
|
552
|
+
setParserName(zigFnProtoTypeExprParser, 'zigFnProtoTypeExprParser');
|
|
553
|
+
|
|
554
|
+
// Container field: name: type [align(expr)] [= default],
|
|
555
|
+
const zigContainerFieldParser: Parser<ZigContainerMember, string> = promiseCompose(
|
|
556
|
+
createTupleParser([
|
|
557
|
+
zigIdentifierParser,
|
|
558
|
+
zigSkippableParser,
|
|
559
|
+
createOptionalParser(promiseCompose(
|
|
560
|
+
createTupleParser([createExactSequenceParser(':'), zigSkippableParser, zigExpressionParser, zigSkippableParser]),
|
|
561
|
+
([, , type_]) => type_,
|
|
562
|
+
)),
|
|
563
|
+
createOptionalParser(promiseCompose(
|
|
564
|
+
createTupleParser([zigKeyword('align'), zigSkippableParser, createExactSequenceParser('('), zigSkippableParser, zigExpressionParser, zigSkippableParser, createExactSequenceParser(')'), zigSkippableParser]),
|
|
565
|
+
([, , , , expr]) => expr,
|
|
566
|
+
)),
|
|
567
|
+
createOptionalParser(promiseCompose(
|
|
568
|
+
createTupleParser([createExactSequenceParser('='), zigSkippableParser, zigExpressionParser, zigSkippableParser]),
|
|
569
|
+
([, , init]) => init,
|
|
570
|
+
)),
|
|
571
|
+
createExactSequenceParser(','),
|
|
572
|
+
]),
|
|
573
|
+
([name, , typeExpr, alignExpr, defaultValue]): ZigContainerMember => ({
|
|
574
|
+
type: 'ContainerField',
|
|
575
|
+
name,
|
|
576
|
+
...(typeExpr !== undefined ? { typeExpr } : {}),
|
|
577
|
+
...(alignExpr !== undefined ? { alignExpr } : {}),
|
|
578
|
+
...(defaultValue !== undefined ? { defaultValue } : {}),
|
|
579
|
+
}),
|
|
580
|
+
);
|
|
581
|
+
|
|
582
|
+
setParserName(zigContainerFieldParser, 'zigContainerFieldParser');
|
|
583
|
+
|
|
584
|
+
// Anonymous struct expression: struct { members... }
|
|
585
|
+
const zigStructExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
586
|
+
createTupleParser([
|
|
587
|
+
zigKeyword('struct'),
|
|
588
|
+
zigSkippableParser,
|
|
589
|
+
createExactSequenceParser('{'),
|
|
590
|
+
zigSkippableParser,
|
|
591
|
+
createArrayParser(
|
|
592
|
+
promiseCompose(
|
|
593
|
+
createTupleParser([
|
|
594
|
+
createDisjunctionParser([
|
|
595
|
+
zigContainerMemberParserRef,
|
|
596
|
+
zigContainerFieldParser,
|
|
597
|
+
]),
|
|
598
|
+
zigSkippableParser,
|
|
599
|
+
]),
|
|
600
|
+
([member]) => member,
|
|
601
|
+
),
|
|
602
|
+
),
|
|
603
|
+
createExactSequenceParser('}'),
|
|
604
|
+
]),
|
|
605
|
+
([, , , , members]): ZigExpression => ({
|
|
606
|
+
type: 'StructExpr',
|
|
607
|
+
members,
|
|
608
|
+
}),
|
|
609
|
+
);
|
|
610
|
+
|
|
611
|
+
setParserName(zigStructExprParser, 'zigStructExprParser');
|
|
612
|
+
|
|
613
|
+
// Primary expression impl
|
|
614
|
+
const zigPrimaryExprParserImpl: Parser<ZigExpression, string> = createDisjunctionParser([
|
|
615
|
+
zigBuiltinCallExprParser,
|
|
616
|
+
zigStringLiteralParser,
|
|
617
|
+
zigMultilineStringLiteralParser,
|
|
618
|
+
zigCharLiteralParser,
|
|
619
|
+
zigFloatLiteralParser,
|
|
620
|
+
zigIntegerLiteralParser,
|
|
621
|
+
promiseCompose(zigKeyword('true'), (): ZigExpression => ({ type: 'BoolLiteral', value: true })),
|
|
622
|
+
promiseCompose(zigKeyword('false'), (): ZigExpression => ({ type: 'BoolLiteral', value: false })),
|
|
623
|
+
promiseCompose(zigKeyword('null'), (): ZigExpression => ({ type: 'NullLiteral' })),
|
|
624
|
+
promiseCompose(zigKeyword('undefined'), (): ZigExpression => ({ type: 'UndefinedLiteral' })),
|
|
625
|
+
promiseCompose(zigKeyword('unreachable'), (): ZigExpression => ({ type: 'Identifier', name: 'unreachable' })),
|
|
626
|
+
promiseCompose(zigKeyword('anytype'), (): ZigExpression => ({ type: 'Identifier', name: 'anytype' })),
|
|
627
|
+
zigErrorSetExprParser,
|
|
628
|
+
zigIfExprParser,
|
|
629
|
+
zigSwitchExprParser,
|
|
630
|
+
zigAnonStructInitParser,
|
|
631
|
+
zigAnonArrayInitParser,
|
|
632
|
+
zigFnProtoTypeExprParser,
|
|
633
|
+
zigStructExprParser,
|
|
634
|
+
promiseCompose(zigKeyword('type'), (): ZigExpression => ({ type: 'Identifier', name: 'type' })),
|
|
635
|
+
zigBlockExprParser,
|
|
636
|
+
zigGroupedExprParser,
|
|
637
|
+
zigEnumLiteralParser,
|
|
638
|
+
zigIdentifierExprParser,
|
|
639
|
+
]);
|
|
640
|
+
|
|
641
|
+
setParserName(zigPrimaryExprParserImpl, 'zigPrimaryExprParserImpl');
|
|
642
|
+
|
|
643
|
+
// Suffix operations
|
|
644
|
+
type SuffixOp =
|
|
645
|
+
| { kind: 'field'; member: string }
|
|
646
|
+
| { kind: 'call'; args: ZigExpression[] }
|
|
647
|
+
| { kind: 'index'; index: ZigExpression }
|
|
648
|
+
| { kind: 'slice'; start: ZigExpression; end?: ZigExpression; sentinel?: ZigExpression }
|
|
649
|
+
| { kind: 'deref' }
|
|
650
|
+
| { kind: 'unwrap' }
|
|
651
|
+
| { kind: 'structInit'; fields: { type: 'StructInitField'; name: string; value: ZigExpression }[] }
|
|
652
|
+
| { kind: 'arrayInit'; elements: ZigExpression[] };
|
|
653
|
+
|
|
654
|
+
const zigFieldAccessSuffixParser: Parser<SuffixOp, string> = promiseCompose(
|
|
655
|
+
createTupleParser([
|
|
656
|
+
createExactSequenceParser('.'),
|
|
657
|
+
zigIdentifierParser,
|
|
658
|
+
]),
|
|
659
|
+
([, member]): SuffixOp => ({ kind: 'field', member }),
|
|
660
|
+
);
|
|
661
|
+
|
|
662
|
+
const zigDerefSuffixParser: Parser<SuffixOp, string> = promiseCompose(
|
|
663
|
+
createExactSequenceParser('.*'),
|
|
664
|
+
(): SuffixOp => ({ kind: 'deref' }),
|
|
665
|
+
);
|
|
666
|
+
|
|
667
|
+
const zigUnwrapSuffixParser: Parser<SuffixOp, string> = promiseCompose(
|
|
668
|
+
createExactSequenceParser('.?'),
|
|
669
|
+
(): SuffixOp => ({ kind: 'unwrap' }),
|
|
670
|
+
);
|
|
671
|
+
|
|
672
|
+
const zigCallSuffixParser: Parser<SuffixOp, string> = promiseCompose(
|
|
673
|
+
createTupleParser([
|
|
674
|
+
createExactSequenceParser('('),
|
|
675
|
+
zigSkippableParser,
|
|
676
|
+
createOptionalParser(
|
|
677
|
+
createSeparatedNonEmptyArrayParser(
|
|
678
|
+
promiseCompose(
|
|
679
|
+
createTupleParser([
|
|
680
|
+
zigExpressionParser,
|
|
681
|
+
zigSkippableParser,
|
|
682
|
+
]),
|
|
683
|
+
([expr]) => expr,
|
|
684
|
+
),
|
|
685
|
+
promiseCompose(
|
|
686
|
+
createTupleParser([
|
|
687
|
+
createExactSequenceParser(','),
|
|
688
|
+
zigSkippableParser,
|
|
689
|
+
]),
|
|
690
|
+
() => ',',
|
|
691
|
+
),
|
|
692
|
+
),
|
|
693
|
+
),
|
|
694
|
+
createOptionalParser(createExactSequenceParser(',')),
|
|
695
|
+
zigSkippableParser,
|
|
696
|
+
createExactSequenceParser(')'),
|
|
697
|
+
]),
|
|
698
|
+
([, , args]): SuffixOp => ({ kind: 'call', args: args ?? [] }),
|
|
699
|
+
);
|
|
700
|
+
|
|
701
|
+
const zigIndexSuffixParser: Parser<SuffixOp, string> = promiseCompose(
|
|
702
|
+
createTupleParser([
|
|
703
|
+
createExactSequenceParser('['),
|
|
704
|
+
zigSkippableParser,
|
|
705
|
+
zigExpressionParser,
|
|
706
|
+
zigSkippableParser,
|
|
707
|
+
createOptionalParser(
|
|
708
|
+
promiseCompose(
|
|
709
|
+
createTupleParser([
|
|
710
|
+
createExactSequenceParser('..'),
|
|
711
|
+
zigSkippableParser,
|
|
712
|
+
createOptionalParser(
|
|
713
|
+
promiseCompose(
|
|
714
|
+
createTupleParser([
|
|
715
|
+
zigExpressionParser,
|
|
716
|
+
zigSkippableParser,
|
|
717
|
+
]),
|
|
718
|
+
([expr]) => expr,
|
|
719
|
+
),
|
|
720
|
+
),
|
|
721
|
+
]),
|
|
722
|
+
([, , end]) => end,
|
|
723
|
+
),
|
|
724
|
+
),
|
|
725
|
+
createExactSequenceParser(']'),
|
|
726
|
+
]),
|
|
727
|
+
([, , start, , sliceEnd]): SuffixOp => {
|
|
728
|
+
if (sliceEnd !== undefined) {
|
|
729
|
+
return { kind: 'slice', start, end: sliceEnd || undefined };
|
|
730
|
+
}
|
|
731
|
+
return { kind: 'index', index: start };
|
|
732
|
+
},
|
|
733
|
+
);
|
|
734
|
+
|
|
735
|
+
// Typed struct init suffix: { .field = value, ... }
|
|
736
|
+
const zigStructInitSuffixParser: Parser<SuffixOp, string> = promiseCompose(
|
|
737
|
+
createTupleParser([
|
|
738
|
+
createExactSequenceParser('{'),
|
|
739
|
+
zigSkippableParser,
|
|
740
|
+
createOptionalParser(
|
|
741
|
+
createSeparatedNonEmptyArrayParser(
|
|
742
|
+
promiseCompose(
|
|
743
|
+
createTupleParser([
|
|
744
|
+
createExactSequenceParser('.'),
|
|
745
|
+
zigIdentifierParser,
|
|
746
|
+
zigSkippableParser,
|
|
747
|
+
createExactSequenceParser('='),
|
|
748
|
+
zigSkippableParser,
|
|
749
|
+
zigExpressionParser,
|
|
750
|
+
zigSkippableParser,
|
|
751
|
+
]),
|
|
752
|
+
([, name, , , , value]) => ({ type: 'StructInitField' as const, name, value }),
|
|
753
|
+
),
|
|
754
|
+
promiseCompose(
|
|
755
|
+
createTupleParser([
|
|
756
|
+
createExactSequenceParser(','),
|
|
757
|
+
zigSkippableParser,
|
|
758
|
+
]),
|
|
759
|
+
() => ',',
|
|
760
|
+
),
|
|
761
|
+
),
|
|
762
|
+
),
|
|
763
|
+
createOptionalParser(createExactSequenceParser(',')),
|
|
764
|
+
zigSkippableParser,
|
|
765
|
+
createExactSequenceParser('}'),
|
|
766
|
+
]),
|
|
767
|
+
([, , fields]): SuffixOp => ({ kind: 'structInit', fields: fields ?? [] }),
|
|
768
|
+
);
|
|
769
|
+
|
|
770
|
+
// Typed array init suffix: { expr, expr, ... }
|
|
771
|
+
const zigArrayInitSuffixParser: Parser<SuffixOp, string> = promiseCompose(
|
|
772
|
+
createTupleParser([
|
|
773
|
+
createExactSequenceParser('{'),
|
|
774
|
+
zigSkippableParser,
|
|
775
|
+
createOptionalParser(
|
|
776
|
+
createSeparatedNonEmptyArrayParser(
|
|
777
|
+
promiseCompose(
|
|
778
|
+
createTupleParser([
|
|
779
|
+
zigExpressionParser,
|
|
780
|
+
zigSkippableParser,
|
|
781
|
+
]),
|
|
782
|
+
([expr]) => expr,
|
|
783
|
+
),
|
|
784
|
+
promiseCompose(
|
|
785
|
+
createTupleParser([
|
|
786
|
+
createExactSequenceParser(','),
|
|
787
|
+
zigSkippableParser,
|
|
788
|
+
]),
|
|
789
|
+
() => ',',
|
|
790
|
+
),
|
|
791
|
+
),
|
|
792
|
+
),
|
|
793
|
+
createOptionalParser(createExactSequenceParser(',')),
|
|
794
|
+
zigSkippableParser,
|
|
795
|
+
createExactSequenceParser('}'),
|
|
796
|
+
]),
|
|
797
|
+
([, , elements]): SuffixOp => ({ kind: 'arrayInit', elements: elements ?? [] }),
|
|
798
|
+
);
|
|
799
|
+
|
|
800
|
+
const zigSuffixOpParser: Parser<SuffixOp, string> = createDisjunctionParser([
|
|
801
|
+
zigDerefSuffixParser,
|
|
802
|
+
zigUnwrapSuffixParser,
|
|
803
|
+
zigFieldAccessSuffixParser,
|
|
804
|
+
zigCallSuffixParser,
|
|
805
|
+
zigIndexSuffixParser,
|
|
806
|
+
]);
|
|
807
|
+
|
|
808
|
+
// Postfix expression: primary with suffix operations
|
|
809
|
+
const zigPostfixExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
810
|
+
createTupleParser([
|
|
811
|
+
zigPrimaryExprParser,
|
|
812
|
+
createArrayParser(
|
|
813
|
+
promiseCompose(
|
|
814
|
+
createTupleParser([
|
|
815
|
+
zigSkippableParser,
|
|
816
|
+
zigSuffixOpParser,
|
|
817
|
+
]),
|
|
818
|
+
([, op]) => op,
|
|
819
|
+
),
|
|
820
|
+
),
|
|
821
|
+
]),
|
|
822
|
+
([primary, suffixes]): ZigExpression => {
|
|
823
|
+
let result: ZigExpression = primary;
|
|
824
|
+
for (const suffix of suffixes) {
|
|
825
|
+
switch (suffix.kind) {
|
|
826
|
+
case 'field':
|
|
827
|
+
result = { type: 'FieldAccessExpr', operand: result, member: suffix.member };
|
|
828
|
+
break;
|
|
829
|
+
case 'call':
|
|
830
|
+
result = { type: 'CallExpr', callee: result, args: suffix.args };
|
|
831
|
+
break;
|
|
832
|
+
case 'index':
|
|
833
|
+
result = { type: 'IndexExpr', operand: result, index: suffix.index };
|
|
834
|
+
break;
|
|
835
|
+
case 'slice':
|
|
836
|
+
result = { type: 'SliceExpr', operand: result, start: suffix.start, ...(suffix.end ? { end: suffix.end } : {}), ...(suffix.sentinel ? { sentinel: suffix.sentinel } : {}) };
|
|
837
|
+
break;
|
|
838
|
+
case 'deref':
|
|
839
|
+
result = { type: 'FieldAccessExpr', operand: result, member: '*' };
|
|
840
|
+
break;
|
|
841
|
+
case 'unwrap':
|
|
842
|
+
result = { type: 'FieldAccessExpr', operand: result, member: '?' };
|
|
843
|
+
break;
|
|
844
|
+
case 'structInit':
|
|
845
|
+
result = { type: 'StructInitExpr', operand: result, fields: suffix.fields };
|
|
846
|
+
break;
|
|
847
|
+
case 'arrayInit':
|
|
848
|
+
result = { type: 'ArrayInitExpr', operand: result, elements: suffix.elements };
|
|
849
|
+
break;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
return result;
|
|
853
|
+
},
|
|
854
|
+
);
|
|
855
|
+
|
|
856
|
+
setParserName(zigPostfixExprParser, 'zigPostfixExprParser');
|
|
857
|
+
|
|
858
|
+
// Pointer/array/slice type prefix: [*]T, [*c]T, []T, [N]T
|
|
859
|
+
const zigBracketTypePrefixParser: Parser<ZigExpression, string> = promiseCompose(
|
|
860
|
+
createTupleParser([
|
|
861
|
+
createExactSequenceParser('['),
|
|
862
|
+
zigSkippableParser,
|
|
863
|
+
createDisjunctionParser([
|
|
864
|
+
promiseCompose(createRegExpParser(/\*c(?![a-zA-Z0-9_])/), () => '*c' as const),
|
|
865
|
+
promiseCompose(createRegExpParser(/\*(?!=)/), () => '*' as const),
|
|
866
|
+
promiseCompose(createExactSequenceParser(''), () => '' as const),
|
|
867
|
+
]),
|
|
868
|
+
zigSkippableParser,
|
|
869
|
+
createOptionalParser(promiseCompose(
|
|
870
|
+
createTupleParser([createExactSequenceParser(':'), zigSkippableParser, zigExpressionParser, zigSkippableParser]),
|
|
871
|
+
([, , sentinel]) => sentinel,
|
|
872
|
+
)),
|
|
873
|
+
createExactSequenceParser(']'),
|
|
874
|
+
zigSkippableParser,
|
|
875
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('const'), zigMandatorySkipParser]), () => true as const)),
|
|
876
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('volatile'), zigMandatorySkipParser]), () => true as const)),
|
|
877
|
+
zigPrefixExprParser,
|
|
878
|
+
]),
|
|
879
|
+
([, , kind, , _sentinel, , , _isConst, _isVolatile, child]): ZigExpression => ({
|
|
880
|
+
type: 'PointerType',
|
|
881
|
+
size: kind === '' ? 'slice' : kind === '*' ? 'many' : 'one',
|
|
882
|
+
isConst: _isConst ?? false,
|
|
883
|
+
child,
|
|
884
|
+
}),
|
|
885
|
+
);
|
|
886
|
+
|
|
887
|
+
setParserName(zigBracketTypePrefixParser, 'zigBracketTypePrefixParser');
|
|
888
|
+
|
|
889
|
+
// Array type prefix: [N]T or [N:sentinel]T
|
|
890
|
+
const zigArrayTypePrefixParser: Parser<ZigExpression, string> = promiseCompose(
|
|
891
|
+
createTupleParser([
|
|
892
|
+
createExactSequenceParser('['),
|
|
893
|
+
zigSkippableParser,
|
|
894
|
+
zigExpressionParser,
|
|
895
|
+
zigSkippableParser,
|
|
896
|
+
createOptionalParser(promiseCompose(
|
|
897
|
+
createTupleParser([createExactSequenceParser(':'), zigSkippableParser, zigExpressionParser, zigSkippableParser]),
|
|
898
|
+
([, , sentinel]) => sentinel,
|
|
899
|
+
)),
|
|
900
|
+
createExactSequenceParser(']'),
|
|
901
|
+
zigSkippableParser,
|
|
902
|
+
zigPrefixExprParser,
|
|
903
|
+
]),
|
|
904
|
+
([, , length, , sentinel, , , child]): ZigExpression => ({
|
|
905
|
+
type: 'ArrayType' as const,
|
|
906
|
+
length,
|
|
907
|
+
...(sentinel !== undefined ? { sentinel } : {}),
|
|
908
|
+
child,
|
|
909
|
+
}),
|
|
910
|
+
);
|
|
911
|
+
|
|
912
|
+
setParserName(zigArrayTypePrefixParser, 'zigArrayTypePrefixParser');
|
|
913
|
+
|
|
914
|
+
// Optional type prefix: ?T
|
|
915
|
+
const zigOptionalTypePrefixParser: Parser<ZigExpression, string> = promiseCompose(
|
|
916
|
+
createTupleParser([
|
|
917
|
+
createExactSequenceParser('?'),
|
|
918
|
+
zigSkippableParser,
|
|
919
|
+
zigPrefixExprParser,
|
|
920
|
+
]),
|
|
921
|
+
([, , child]): ZigExpression => ({
|
|
922
|
+
type: 'OptionalType',
|
|
923
|
+
child,
|
|
924
|
+
}),
|
|
925
|
+
);
|
|
926
|
+
|
|
927
|
+
// Single pointer prefix: *T, *const T
|
|
928
|
+
const zigSinglePointerPrefixParser: Parser<ZigExpression, string> = promiseCompose(
|
|
929
|
+
createTupleParser([
|
|
930
|
+
createRegExpParser(/\*(?![*=])/),
|
|
931
|
+
zigSkippableParser,
|
|
932
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('const'), zigMandatorySkipParser]), () => true as const)),
|
|
933
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('volatile'), zigMandatorySkipParser]), () => true as const)),
|
|
934
|
+
zigPrefixExprParser,
|
|
935
|
+
]),
|
|
936
|
+
([, , isConst, _isVolatile, child]): ZigExpression => ({
|
|
937
|
+
type: 'PointerType',
|
|
938
|
+
size: 'one',
|
|
939
|
+
isConst: isConst ?? false,
|
|
940
|
+
child,
|
|
941
|
+
}),
|
|
942
|
+
);
|
|
943
|
+
|
|
944
|
+
// Prefix unary operators
|
|
945
|
+
const zigPrefixUnaryParser: Parser<ZigExpression, string> = createDisjunctionParser([
|
|
946
|
+
zigArrayTypePrefixParser,
|
|
947
|
+
zigBracketTypePrefixParser,
|
|
948
|
+
zigOptionalTypePrefixParser,
|
|
949
|
+
zigSinglePointerPrefixParser,
|
|
950
|
+
promiseCompose(
|
|
951
|
+
createTupleParser([
|
|
952
|
+
zigKeyword('try'),
|
|
953
|
+
zigMandatorySkipParser,
|
|
954
|
+
zigPrefixExprParser,
|
|
955
|
+
]),
|
|
956
|
+
([, , operand]): ZigExpression => ({ type: 'TryExpr', operand }),
|
|
957
|
+
),
|
|
958
|
+
promiseCompose(
|
|
959
|
+
createTupleParser([
|
|
960
|
+
zigKeyword('comptime'),
|
|
961
|
+
zigMandatorySkipParser,
|
|
962
|
+
zigPrefixExprParser,
|
|
963
|
+
]),
|
|
964
|
+
([, , operand]): ZigExpression => ({ type: 'ComptimeExpr', operand }),
|
|
965
|
+
),
|
|
966
|
+
promiseCompose(
|
|
967
|
+
createTupleParser([
|
|
968
|
+
createExactSequenceParser('!'),
|
|
969
|
+
zigSkippableParser,
|
|
970
|
+
zigPrefixExprParser,
|
|
971
|
+
]),
|
|
972
|
+
([, , operand]): ZigExpression => ({ type: 'UnaryExpr', operator: '!', operand }),
|
|
973
|
+
),
|
|
974
|
+
promiseCompose(
|
|
975
|
+
createTupleParser([
|
|
976
|
+
createRegExpParser(/-(?![-=>])/),
|
|
977
|
+
zigSkippableParser,
|
|
978
|
+
zigPrefixExprParser,
|
|
979
|
+
]),
|
|
980
|
+
([, , operand]): ZigExpression => ({ type: 'UnaryExpr', operator: '-', operand }),
|
|
981
|
+
),
|
|
982
|
+
promiseCompose(
|
|
983
|
+
createTupleParser([
|
|
984
|
+
createExactSequenceParser('~'),
|
|
985
|
+
zigSkippableParser,
|
|
986
|
+
zigPrefixExprParser,
|
|
987
|
+
]),
|
|
988
|
+
([, , operand]): ZigExpression => ({ type: 'UnaryExpr', operator: '~', operand }),
|
|
989
|
+
),
|
|
990
|
+
promiseCompose(
|
|
991
|
+
createTupleParser([
|
|
992
|
+
createRegExpParser(/&(?![&=])/),
|
|
993
|
+
zigSkippableParser,
|
|
994
|
+
zigPrefixExprParser,
|
|
995
|
+
]),
|
|
996
|
+
([, , operand]): ZigExpression => ({ type: 'UnaryExpr', operator: '&', operand }),
|
|
997
|
+
),
|
|
998
|
+
]);
|
|
999
|
+
|
|
1000
|
+
const zigPrefixExprParserImpl: Parser<ZigExpression, string> = createDisjunctionParser([
|
|
1001
|
+
zigPrefixUnaryParser,
|
|
1002
|
+
zigPostfixExprParser,
|
|
1003
|
+
]);
|
|
1004
|
+
|
|
1005
|
+
setParserName(zigPrefixExprParserImpl, 'zigPrefixExprParserImpl');
|
|
1006
|
+
|
|
1007
|
+
// Helper to build left-associative binary expression parser
|
|
1008
|
+
function createBinaryExprParser(
|
|
1009
|
+
operandParser: Parser<ZigExpression, string>,
|
|
1010
|
+
operatorParsers: Parser<string, string>[],
|
|
1011
|
+
): Parser<ZigExpression, string> {
|
|
1012
|
+
return promiseCompose(
|
|
1013
|
+
createTupleParser([
|
|
1014
|
+
operandParser,
|
|
1015
|
+
createArrayParser(
|
|
1016
|
+
promiseCompose(
|
|
1017
|
+
createTupleParser([
|
|
1018
|
+
zigSkippableParser,
|
|
1019
|
+
createDisjunctionParser(operatorParsers),
|
|
1020
|
+
zigSkippableParser,
|
|
1021
|
+
operandParser,
|
|
1022
|
+
]),
|
|
1023
|
+
([, op, , right]) => ({ op, right }),
|
|
1024
|
+
),
|
|
1025
|
+
),
|
|
1026
|
+
]),
|
|
1027
|
+
([left, rest]): ZigExpression => {
|
|
1028
|
+
let result: ZigExpression = left;
|
|
1029
|
+
for (const { op, right } of rest) {
|
|
1030
|
+
result = { type: 'BinaryExpr', operator: op as ZigExpression extends { type: 'BinaryExpr'; operator: infer O } ? O : never, left: result, right };
|
|
1031
|
+
}
|
|
1032
|
+
return result;
|
|
1033
|
+
},
|
|
1034
|
+
);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
// Multiplication operators
|
|
1038
|
+
const zigMultiplyExprParser = createBinaryExprParser(zigPrefixExprParser, [
|
|
1039
|
+
promiseCompose(createRegExpParser(/\*\*(?!=)/), match => match[0]),
|
|
1040
|
+
promiseCompose(createRegExpParser(/\*(?![*=])/), match => match[0]),
|
|
1041
|
+
promiseCompose(createRegExpParser(/\/(?!=)/), match => match[0]),
|
|
1042
|
+
promiseCompose(createRegExpParser(/%(?!=)/), match => match[0]),
|
|
1043
|
+
]);
|
|
1044
|
+
|
|
1045
|
+
setParserName(zigMultiplyExprParser, 'zigMultiplyExprParser');
|
|
1046
|
+
|
|
1047
|
+
// Addition operators
|
|
1048
|
+
const zigAddExprParser = createBinaryExprParser(zigMultiplyExprParser, [
|
|
1049
|
+
promiseCompose(createRegExpParser(/\+\+(?!=)/), match => match[0]),
|
|
1050
|
+
promiseCompose(createRegExpParser(/\+(?![+=])/), match => match[0]),
|
|
1051
|
+
promiseCompose(createRegExpParser(/-(?![-=])/), match => match[0]),
|
|
1052
|
+
]);
|
|
1053
|
+
|
|
1054
|
+
setParserName(zigAddExprParser, 'zigAddExprParser');
|
|
1055
|
+
|
|
1056
|
+
// Bit shift operators
|
|
1057
|
+
const zigShiftExprParser = createBinaryExprParser(zigAddExprParser, [
|
|
1058
|
+
promiseCompose(createRegExpParser(/<<(?!=)/), match => match[0]),
|
|
1059
|
+
promiseCompose(createRegExpParser(/>>(?!=)/), match => match[0]),
|
|
1060
|
+
]);
|
|
1061
|
+
|
|
1062
|
+
setParserName(zigShiftExprParser, 'zigShiftExprParser');
|
|
1063
|
+
|
|
1064
|
+
// Bitwise operators
|
|
1065
|
+
const zigBitwiseExprParser = createBinaryExprParser(zigShiftExprParser, [
|
|
1066
|
+
promiseCompose(createRegExpParser(/&(?![&=])/), match => match[0]),
|
|
1067
|
+
promiseCompose(createRegExpParser(/\|(?![|=])/), match => match[0]),
|
|
1068
|
+
promiseCompose(createRegExpParser(/\^(?!=)/), match => match[0]),
|
|
1069
|
+
]);
|
|
1070
|
+
|
|
1071
|
+
setParserName(zigBitwiseExprParser, 'zigBitwiseExprParser');
|
|
1072
|
+
|
|
1073
|
+
// Comparison operators (non-associative)
|
|
1074
|
+
const zigCompareExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
1075
|
+
createTupleParser([
|
|
1076
|
+
zigBitwiseExprParser,
|
|
1077
|
+
createOptionalParser(
|
|
1078
|
+
promiseCompose(
|
|
1079
|
+
createTupleParser([
|
|
1080
|
+
zigSkippableParser,
|
|
1081
|
+
createDisjunctionParser([
|
|
1082
|
+
promiseCompose(createExactSequenceParser('=='), () => '=='),
|
|
1083
|
+
promiseCompose(createExactSequenceParser('!='), () => '!='),
|
|
1084
|
+
promiseCompose(createRegExpParser(/<=(?!=)/), match => match[0]),
|
|
1085
|
+
promiseCompose(createRegExpParser(/>=(?!=)/), match => match[0]),
|
|
1086
|
+
promiseCompose(createRegExpParser(/<(?![<=])/), match => match[0]),
|
|
1087
|
+
promiseCompose(createRegExpParser(/>(?![>=])/), match => match[0]),
|
|
1088
|
+
]),
|
|
1089
|
+
zigSkippableParser,
|
|
1090
|
+
zigBitwiseExprParser,
|
|
1091
|
+
]),
|
|
1092
|
+
([, op, , right]) => ({ op, right }),
|
|
1093
|
+
),
|
|
1094
|
+
),
|
|
1095
|
+
]),
|
|
1096
|
+
([left, rest]): ZigExpression => {
|
|
1097
|
+
if (!rest) return left;
|
|
1098
|
+
return { type: 'BinaryExpr', operator: rest.op as ZigExpression extends { type: 'BinaryExpr'; operator: infer O } ? O : never, left, right: rest.right };
|
|
1099
|
+
},
|
|
1100
|
+
);
|
|
1101
|
+
|
|
1102
|
+
setParserName(zigCompareExprParser, 'zigCompareExprParser');
|
|
1103
|
+
|
|
1104
|
+
// orelse operator
|
|
1105
|
+
const zigOrelseExprParser = createBinaryExprParser(zigCompareExprParser, [
|
|
1106
|
+
zigKeyword('orelse'),
|
|
1107
|
+
]);
|
|
1108
|
+
|
|
1109
|
+
setParserName(zigOrelseExprParser, 'zigOrelseExprParser');
|
|
1110
|
+
|
|
1111
|
+
// catch operator
|
|
1112
|
+
const zigCatchExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
1113
|
+
createTupleParser([
|
|
1114
|
+
zigOrelseExprParser,
|
|
1115
|
+
createArrayParser(
|
|
1116
|
+
promiseCompose(
|
|
1117
|
+
createTupleParser([
|
|
1118
|
+
zigSkippableParser,
|
|
1119
|
+
zigKeyword('catch'),
|
|
1120
|
+
zigSkippableParser,
|
|
1121
|
+
createOptionalParser(zigCaptureParser),
|
|
1122
|
+
zigOrelseExprParser,
|
|
1123
|
+
]),
|
|
1124
|
+
([, , , _capture, right]) => right,
|
|
1125
|
+
),
|
|
1126
|
+
),
|
|
1127
|
+
]),
|
|
1128
|
+
([left, rest]): ZigExpression => {
|
|
1129
|
+
let result: ZigExpression = left;
|
|
1130
|
+
for (const right of rest) {
|
|
1131
|
+
result = { type: 'BinaryExpr', operator: 'catch', left: result, right };
|
|
1132
|
+
}
|
|
1133
|
+
return result;
|
|
1134
|
+
},
|
|
1135
|
+
);
|
|
1136
|
+
|
|
1137
|
+
setParserName(zigCatchExprParser, 'zigCatchExprParser');
|
|
1138
|
+
|
|
1139
|
+
// Boolean and
|
|
1140
|
+
const zigAndExprParser = createBinaryExprParser(zigCatchExprParser, [
|
|
1141
|
+
zigKeyword('and'),
|
|
1142
|
+
]);
|
|
1143
|
+
|
|
1144
|
+
setParserName(zigAndExprParser, 'zigAndExprParser');
|
|
1145
|
+
|
|
1146
|
+
// Boolean or
|
|
1147
|
+
const zigOrExprParser = createBinaryExprParser(zigAndExprParser, [
|
|
1148
|
+
zigKeyword('or'),
|
|
1149
|
+
]);
|
|
1150
|
+
|
|
1151
|
+
setParserName(zigOrExprParser, 'zigOrExprParser');
|
|
1152
|
+
|
|
1153
|
+
// Error union: Type!Type
|
|
1154
|
+
const zigErrorUnionExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
1155
|
+
createTupleParser([
|
|
1156
|
+
zigOrExprParser,
|
|
1157
|
+
createOptionalParser(
|
|
1158
|
+
promiseCompose(
|
|
1159
|
+
createTupleParser([
|
|
1160
|
+
zigSkippableParser,
|
|
1161
|
+
createRegExpParser(/!(?!=)/),
|
|
1162
|
+
zigSkippableParser,
|
|
1163
|
+
zigOrExprParser,
|
|
1164
|
+
]),
|
|
1165
|
+
([, , , right]) => right,
|
|
1166
|
+
),
|
|
1167
|
+
),
|
|
1168
|
+
]),
|
|
1169
|
+
([left, right]): ZigExpression => {
|
|
1170
|
+
if (!right) return left;
|
|
1171
|
+
return { type: 'ErrorUnionType', error: left, payload: right };
|
|
1172
|
+
},
|
|
1173
|
+
);
|
|
1174
|
+
|
|
1175
|
+
setParserName(zigErrorUnionExprParser, 'zigErrorUnionExprParser');
|
|
1176
|
+
|
|
1177
|
+
// Curly suffix expression: TypeExpr { .field = val, ... } or TypeExpr { val1, val2, ... }
|
|
1178
|
+
// In Zig grammar, CurlySuffixExpr = TypeExpr ("{" init "}")?
|
|
1179
|
+
type CurlySuffix =
|
|
1180
|
+
| { kind: 'structInit'; fields: { type: 'StructInitField'; name: string; value: ZigExpression }[] }
|
|
1181
|
+
| { kind: 'arrayInit'; elements: ZigExpression[] };
|
|
1182
|
+
|
|
1183
|
+
const zigCurlySuffixExprParser: Parser<ZigExpression, string> = promiseCompose(
|
|
1184
|
+
createTupleParser([
|
|
1185
|
+
zigErrorUnionExprParser,
|
|
1186
|
+
createOptionalParser(
|
|
1187
|
+
promiseCompose(
|
|
1188
|
+
createTupleParser([
|
|
1189
|
+
zigSkippableParser,
|
|
1190
|
+
createDisjunctionParser([
|
|
1191
|
+
zigStructInitSuffixParser,
|
|
1192
|
+
zigArrayInitSuffixParser,
|
|
1193
|
+
]),
|
|
1194
|
+
]),
|
|
1195
|
+
([, suffix]): CurlySuffix => suffix as CurlySuffix,
|
|
1196
|
+
),
|
|
1197
|
+
),
|
|
1198
|
+
]),
|
|
1199
|
+
([expr, suffix]): ZigExpression => {
|
|
1200
|
+
if (!suffix) return expr;
|
|
1201
|
+
if (suffix.kind === 'structInit') {
|
|
1202
|
+
return { type: 'StructInitExpr', operand: expr, fields: suffix.fields };
|
|
1203
|
+
}
|
|
1204
|
+
return { type: 'ArrayInitExpr', operand: expr, elements: suffix.elements };
|
|
1205
|
+
},
|
|
1206
|
+
);
|
|
1207
|
+
|
|
1208
|
+
// Expression impl
|
|
1209
|
+
const zigExpressionParserImpl: Parser<ZigExpression, string> = zigCurlySuffixExprParser;
|
|
1210
|
+
|
|
1211
|
+
setParserName(zigExpressionParserImpl, 'zigExpressionParserImpl');
|
|
1212
|
+
|
|
1213
|
+
// Statements
|
|
1214
|
+
|
|
1215
|
+
// Variable declaration statement: [pub] [extern] [comptime] [threadlocal] const/var name [: type] [align(expr)] [= init];
|
|
1216
|
+
const zigVarDeclStmtParser: Parser<ZigStatement, string> = promiseCompose(
|
|
1217
|
+
createTupleParser([
|
|
1218
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('pub'), zigMandatorySkipParser]), () => true as const)),
|
|
1219
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('extern'), zigMandatorySkipParser]), () => true as const)),
|
|
1220
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('comptime'), zigMandatorySkipParser]), () => true as const)),
|
|
1221
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('threadlocal'), zigMandatorySkipParser]), () => true as const)),
|
|
1222
|
+
createDisjunctionParser([
|
|
1223
|
+
promiseCompose(zigKeyword('const'), () => true as const),
|
|
1224
|
+
promiseCompose(zigKeyword('var'), () => false as const),
|
|
1225
|
+
]),
|
|
1226
|
+
zigMandatorySkipParser,
|
|
1227
|
+
zigIdentifierParser,
|
|
1228
|
+
zigSkippableParser,
|
|
1229
|
+
createOptionalParser(promiseCompose(
|
|
1230
|
+
createTupleParser([createExactSequenceParser(':'), zigSkippableParser, zigExpressionParser, zigSkippableParser]),
|
|
1231
|
+
([, , type_]) => type_,
|
|
1232
|
+
)),
|
|
1233
|
+
createOptionalParser(promiseCompose(
|
|
1234
|
+
createTupleParser([zigKeyword('align'), zigSkippableParser, createExactSequenceParser('('), zigSkippableParser, zigExpressionParser, zigSkippableParser, createExactSequenceParser(')'), zigSkippableParser]),
|
|
1235
|
+
([, , , , expr]) => expr,
|
|
1236
|
+
)),
|
|
1237
|
+
createOptionalParser(promiseCompose(
|
|
1238
|
+
createTupleParser([createExactSequenceParser('='), zigSkippableParser, zigExpressionParser, zigSkippableParser]),
|
|
1239
|
+
([, , init]) => init,
|
|
1240
|
+
)),
|
|
1241
|
+
createExactSequenceParser(';'),
|
|
1242
|
+
]),
|
|
1243
|
+
([isPub, isExtern, isComptime, isThreadlocal, isConst, , name, , typeExpr, alignExpr, initExpr]): ZigStatement => ({
|
|
1244
|
+
type: 'VarDecl' as const,
|
|
1245
|
+
isConst,
|
|
1246
|
+
isPub: isPub ?? false,
|
|
1247
|
+
isExtern: isExtern ?? false,
|
|
1248
|
+
isComptime: isComptime ?? false,
|
|
1249
|
+
isThreadlocal: isThreadlocal ?? false,
|
|
1250
|
+
name,
|
|
1251
|
+
...(typeExpr !== undefined ? { typeExpr } : {}),
|
|
1252
|
+
...(alignExpr !== undefined ? { alignExpr } : {}),
|
|
1253
|
+
...(initExpr !== undefined ? { initExpr } : {}),
|
|
1254
|
+
}),
|
|
1255
|
+
);
|
|
1256
|
+
|
|
1257
|
+
setParserName(zigVarDeclStmtParser, 'zigVarDeclStmtParser');
|
|
1258
|
+
|
|
1259
|
+
// Return statement: return [expr];
|
|
1260
|
+
const zigReturnStmtParser: Parser<ZigStatement, string> = promiseCompose(
|
|
1261
|
+
createTupleParser([
|
|
1262
|
+
zigKeyword('return'),
|
|
1263
|
+
createOptionalParser(promiseCompose(
|
|
1264
|
+
createTupleParser([zigMandatorySkipParser, zigExpressionParser]),
|
|
1265
|
+
([, expr]) => expr,
|
|
1266
|
+
)),
|
|
1267
|
+
zigSkippableParser,
|
|
1268
|
+
createExactSequenceParser(';'),
|
|
1269
|
+
]),
|
|
1270
|
+
([, value]): ZigStatement => ({
|
|
1271
|
+
type: 'ReturnStmt',
|
|
1272
|
+
...(value !== undefined ? { value } : {}),
|
|
1273
|
+
}),
|
|
1274
|
+
);
|
|
1275
|
+
|
|
1276
|
+
setParserName(zigReturnStmtParser, 'zigReturnStmtParser');
|
|
1277
|
+
|
|
1278
|
+
// Break statement: break [:label] [value];
|
|
1279
|
+
const zigBreakStmtParser: Parser<ZigStatement, string> = promiseCompose(
|
|
1280
|
+
createTupleParser([
|
|
1281
|
+
zigKeyword('break'),
|
|
1282
|
+
zigSkippableParser,
|
|
1283
|
+
createOptionalParser(promiseCompose(
|
|
1284
|
+
createTupleParser([createExactSequenceParser(':'), zigIdentifierParser, zigSkippableParser]),
|
|
1285
|
+
([, label]) => label,
|
|
1286
|
+
)),
|
|
1287
|
+
createOptionalParser(promiseCompose(
|
|
1288
|
+
createTupleParser([zigExpressionParser, zigSkippableParser]),
|
|
1289
|
+
([expr]) => expr,
|
|
1290
|
+
)),
|
|
1291
|
+
createExactSequenceParser(';'),
|
|
1292
|
+
]),
|
|
1293
|
+
([, , label, value]): ZigStatement => ({
|
|
1294
|
+
type: 'BreakStmt',
|
|
1295
|
+
...(label ? { label } : {}),
|
|
1296
|
+
...(value !== undefined ? { value } : {}),
|
|
1297
|
+
}),
|
|
1298
|
+
);
|
|
1299
|
+
|
|
1300
|
+
setParserName(zigBreakStmtParser, 'zigBreakStmtParser');
|
|
1301
|
+
|
|
1302
|
+
// Continue statement: continue [:label];
|
|
1303
|
+
const zigContinueStmtParser: Parser<ZigStatement, string> = promiseCompose(
|
|
1304
|
+
createTupleParser([
|
|
1305
|
+
zigKeyword('continue'),
|
|
1306
|
+
zigSkippableParser,
|
|
1307
|
+
createOptionalParser(promiseCompose(
|
|
1308
|
+
createTupleParser([createExactSequenceParser(':'), zigIdentifierParser, zigSkippableParser]),
|
|
1309
|
+
([, label]) => label,
|
|
1310
|
+
)),
|
|
1311
|
+
createExactSequenceParser(';'),
|
|
1312
|
+
]),
|
|
1313
|
+
([, , label]): ZigStatement => ({
|
|
1314
|
+
type: 'ContinueStmt',
|
|
1315
|
+
...(label ? { label } : {}),
|
|
1316
|
+
}),
|
|
1317
|
+
);
|
|
1318
|
+
|
|
1319
|
+
setParserName(zigContinueStmtParser, 'zigContinueStmtParser');
|
|
1320
|
+
|
|
1321
|
+
// Defer/errdefer statement
|
|
1322
|
+
const zigDeferStmtParser: Parser<ZigStatement, string> = promiseCompose(
|
|
1323
|
+
createTupleParser([
|
|
1324
|
+
createDisjunctionParser([
|
|
1325
|
+
promiseCompose(zigKeyword('errdefer'), () => true as const),
|
|
1326
|
+
promiseCompose(zigKeyword('defer'), () => false as const),
|
|
1327
|
+
]),
|
|
1328
|
+
zigMandatorySkipParser,
|
|
1329
|
+
createOptionalParser(zigCaptureParser),
|
|
1330
|
+
zigStatementParser,
|
|
1331
|
+
]),
|
|
1332
|
+
([isErrdefer, , capture, body]): ZigStatement => ({
|
|
1333
|
+
type: 'DeferStmt',
|
|
1334
|
+
isErrdefer,
|
|
1335
|
+
...(capture ? { capture } : {}),
|
|
1336
|
+
body,
|
|
1337
|
+
}),
|
|
1338
|
+
);
|
|
1339
|
+
|
|
1340
|
+
setParserName(zigDeferStmtParser, 'zigDeferStmtParser');
|
|
1341
|
+
|
|
1342
|
+
// If statement (produces IfExpr with statement bodies)
|
|
1343
|
+
const zigIfStmtParser: Parser<ZigStatement, string> = promiseCompose(
|
|
1344
|
+
createTupleParser([
|
|
1345
|
+
zigKeyword('if'),
|
|
1346
|
+
zigSkippableParser,
|
|
1347
|
+
createExactSequenceParser('('),
|
|
1348
|
+
zigSkippableParser,
|
|
1349
|
+
zigExpressionParser,
|
|
1350
|
+
zigSkippableParser,
|
|
1351
|
+
createExactSequenceParser(')'),
|
|
1352
|
+
zigSkippableParser,
|
|
1353
|
+
createOptionalParser(zigCaptureParser),
|
|
1354
|
+
zigStatementParser,
|
|
1355
|
+
zigSkippableParser,
|
|
1356
|
+
createOptionalParser(promiseCompose(
|
|
1357
|
+
createTupleParser([zigKeyword('else'), zigSkippableParser, createOptionalParser(zigCaptureParser), zigStatementParser]),
|
|
1358
|
+
([, , capture, body]) => ({ capture, body }),
|
|
1359
|
+
)),
|
|
1360
|
+
]),
|
|
1361
|
+
([, , , , condition, , , , capture, body, , elsePart]): ZigStatement => ({
|
|
1362
|
+
type: 'IfExpr' as const,
|
|
1363
|
+
condition,
|
|
1364
|
+
...(capture ? { capture } : {}),
|
|
1365
|
+
body,
|
|
1366
|
+
...(elsePart?.capture ? { elseCapture: elsePart.capture } : {}),
|
|
1367
|
+
...(elsePart ? { elseBody: elsePart.body } : {}),
|
|
1368
|
+
}),
|
|
1369
|
+
);
|
|
1370
|
+
|
|
1371
|
+
setParserName(zigIfStmtParser, 'zigIfStmtParser');
|
|
1372
|
+
|
|
1373
|
+
// While statement
|
|
1374
|
+
const zigWhileStmtParser: Parser<ZigStatement, string> = promiseCompose(
|
|
1375
|
+
createTupleParser([
|
|
1376
|
+
createOptionalParser(promiseCompose(
|
|
1377
|
+
createTupleParser([zigIdentifierParser, zigSkippableParser, createExactSequenceParser(':'), zigSkippableParser]),
|
|
1378
|
+
([label]) => label,
|
|
1379
|
+
)),
|
|
1380
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('inline'), zigMandatorySkipParser]), () => true as const)),
|
|
1381
|
+
zigKeyword('while'),
|
|
1382
|
+
zigSkippableParser,
|
|
1383
|
+
createExactSequenceParser('('),
|
|
1384
|
+
zigSkippableParser,
|
|
1385
|
+
zigExpressionParser,
|
|
1386
|
+
zigSkippableParser,
|
|
1387
|
+
createExactSequenceParser(')'),
|
|
1388
|
+
zigSkippableParser,
|
|
1389
|
+
createOptionalParser(zigCaptureParser),
|
|
1390
|
+
createOptionalParser(promiseCompose(
|
|
1391
|
+
createTupleParser([createExactSequenceParser(':'), zigSkippableParser, createExactSequenceParser('('), zigSkippableParser, zigExpressionParser, zigSkippableParser, createExactSequenceParser(')'), zigSkippableParser]),
|
|
1392
|
+
([, , , , expr]) => expr,
|
|
1393
|
+
)),
|
|
1394
|
+
zigStatementParser,
|
|
1395
|
+
zigSkippableParser,
|
|
1396
|
+
createOptionalParser(promiseCompose(
|
|
1397
|
+
createTupleParser([zigKeyword('else'), zigSkippableParser, zigStatementParser]),
|
|
1398
|
+
([, , body]) => body,
|
|
1399
|
+
)),
|
|
1400
|
+
]),
|
|
1401
|
+
([label, isInline, , , , , condition, , , , capture, continuation, body, , elseBody]): ZigStatement => ({
|
|
1402
|
+
type: 'WhileStmt',
|
|
1403
|
+
condition,
|
|
1404
|
+
...(capture ? { capture } : {}),
|
|
1405
|
+
...(continuation !== undefined ? { continuation } : {}),
|
|
1406
|
+
body,
|
|
1407
|
+
...(elseBody !== undefined ? { elseBody } : {}),
|
|
1408
|
+
...(label ? { label } : {}),
|
|
1409
|
+
isInline: isInline ?? false,
|
|
1410
|
+
}),
|
|
1411
|
+
);
|
|
1412
|
+
|
|
1413
|
+
setParserName(zigWhileStmtParser, 'zigWhileStmtParser');
|
|
1414
|
+
|
|
1415
|
+
// For statement
|
|
1416
|
+
const zigForStmtParser: Parser<ZigStatement, string> = promiseCompose(
|
|
1417
|
+
createTupleParser([
|
|
1418
|
+
createOptionalParser(promiseCompose(
|
|
1419
|
+
createTupleParser([zigIdentifierParser, zigSkippableParser, createExactSequenceParser(':'), zigSkippableParser]),
|
|
1420
|
+
([label]) => label,
|
|
1421
|
+
)),
|
|
1422
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('inline'), zigMandatorySkipParser]), () => true as const)),
|
|
1423
|
+
zigKeyword('for'),
|
|
1424
|
+
zigSkippableParser,
|
|
1425
|
+
createExactSequenceParser('('),
|
|
1426
|
+
zigSkippableParser,
|
|
1427
|
+
createSeparatedNonEmptyArrayParser(
|
|
1428
|
+
promiseCompose(createTupleParser([zigExpressionParser, zigSkippableParser]), ([expr]) => expr),
|
|
1429
|
+
promiseCompose(createTupleParser([createExactSequenceParser(','), zigSkippableParser]), () => ','),
|
|
1430
|
+
),
|
|
1431
|
+
createExactSequenceParser(')'),
|
|
1432
|
+
zigSkippableParser,
|
|
1433
|
+
createExactSequenceParser('|'),
|
|
1434
|
+
zigSkippableParser,
|
|
1435
|
+
createSeparatedNonEmptyArrayParser(
|
|
1436
|
+
promiseCompose(
|
|
1437
|
+
createTupleParser([
|
|
1438
|
+
createOptionalParser(createExactSequenceParser('*')),
|
|
1439
|
+
zigSkippableParser,
|
|
1440
|
+
zigIdentifierParser,
|
|
1441
|
+
zigSkippableParser,
|
|
1442
|
+
]),
|
|
1443
|
+
([star, , name]) => (star ? '*' + name : name),
|
|
1444
|
+
),
|
|
1445
|
+
promiseCompose(createTupleParser([createExactSequenceParser(','), zigSkippableParser]), () => ','),
|
|
1446
|
+
),
|
|
1447
|
+
createExactSequenceParser('|'),
|
|
1448
|
+
zigSkippableParser,
|
|
1449
|
+
zigStatementParser,
|
|
1450
|
+
zigSkippableParser,
|
|
1451
|
+
createOptionalParser(promiseCompose(
|
|
1452
|
+
createTupleParser([zigKeyword('else'), zigSkippableParser, zigStatementParser]),
|
|
1453
|
+
([, , body]) => body,
|
|
1454
|
+
)),
|
|
1455
|
+
]),
|
|
1456
|
+
([label, isInline, , , , , inputs, , , , , captures, , , body, , elseBody]): ZigStatement => ({
|
|
1457
|
+
type: 'ForStmt',
|
|
1458
|
+
inputs,
|
|
1459
|
+
captures,
|
|
1460
|
+
body,
|
|
1461
|
+
...(elseBody !== undefined ? { elseBody } : {}),
|
|
1462
|
+
...(label ? { label } : {}),
|
|
1463
|
+
isInline: isInline ?? false,
|
|
1464
|
+
}),
|
|
1465
|
+
);
|
|
1466
|
+
|
|
1467
|
+
setParserName(zigForStmtParser, 'zigForStmtParser');
|
|
1468
|
+
|
|
1469
|
+
// Block statement (produces BlockExpr)
|
|
1470
|
+
const zigBlockStmtParser: Parser<ZigStatement, string> = promiseCompose(
|
|
1471
|
+
createTupleParser([
|
|
1472
|
+
createOptionalParser(promiseCompose(
|
|
1473
|
+
createTupleParser([zigIdentifierParser, zigSkippableParser, createExactSequenceParser(':'), zigSkippableParser]),
|
|
1474
|
+
([label]) => label,
|
|
1475
|
+
)),
|
|
1476
|
+
createExactSequenceParser('{'),
|
|
1477
|
+
zigSkippableParser,
|
|
1478
|
+
createArrayParser(promiseCompose(
|
|
1479
|
+
createTupleParser([zigStatementParser, zigSkippableParser]),
|
|
1480
|
+
([stmt]) => stmt,
|
|
1481
|
+
)),
|
|
1482
|
+
createExactSequenceParser('}'),
|
|
1483
|
+
]),
|
|
1484
|
+
([label, , , statements]): ZigStatement => ({
|
|
1485
|
+
type: 'BlockExpr' as const,
|
|
1486
|
+
...(label ? { label } : {}),
|
|
1487
|
+
statements,
|
|
1488
|
+
}),
|
|
1489
|
+
);
|
|
1490
|
+
|
|
1491
|
+
setParserName(zigBlockStmtParser, 'zigBlockStmtParser');
|
|
1492
|
+
|
|
1493
|
+
// Assignment statement: expr op= expr;
|
|
1494
|
+
const zigAssignStmtParser: Parser<ZigStatement, string> = promiseCompose(
|
|
1495
|
+
createTupleParser([
|
|
1496
|
+
zigExpressionParser,
|
|
1497
|
+
zigSkippableParser,
|
|
1498
|
+
createDisjunctionParser([
|
|
1499
|
+
promiseCompose(createExactSequenceParser('<<='), () => '<<=' as const),
|
|
1500
|
+
promiseCompose(createExactSequenceParser('>>='), () => '>>=' as const),
|
|
1501
|
+
promiseCompose(createExactSequenceParser('+='), () => '+=' as const),
|
|
1502
|
+
promiseCompose(createExactSequenceParser('-='), () => '-=' as const),
|
|
1503
|
+
promiseCompose(createExactSequenceParser('*='), () => '*=' as const),
|
|
1504
|
+
promiseCompose(createExactSequenceParser('/='), () => '/=' as const),
|
|
1505
|
+
promiseCompose(createExactSequenceParser('%='), () => '%=' as const),
|
|
1506
|
+
promiseCompose(createExactSequenceParser('&='), () => '&=' as const),
|
|
1507
|
+
promiseCompose(createExactSequenceParser('|='), () => '|=' as const),
|
|
1508
|
+
promiseCompose(createExactSequenceParser('^='), () => '^=' as const),
|
|
1509
|
+
promiseCompose(createExactSequenceParser('='), () => '=' as const),
|
|
1510
|
+
]),
|
|
1511
|
+
zigSkippableParser,
|
|
1512
|
+
zigExpressionParser,
|
|
1513
|
+
zigSkippableParser,
|
|
1514
|
+
createExactSequenceParser(';'),
|
|
1515
|
+
]),
|
|
1516
|
+
([target, , operator, , value]): ZigStatement => ({
|
|
1517
|
+
type: 'AssignStmt',
|
|
1518
|
+
target,
|
|
1519
|
+
operator,
|
|
1520
|
+
value,
|
|
1521
|
+
}),
|
|
1522
|
+
);
|
|
1523
|
+
|
|
1524
|
+
setParserName(zigAssignStmtParser, 'zigAssignStmtParser');
|
|
1525
|
+
|
|
1526
|
+
// Expression statement: expr; (returns the expression directly without wrapping)
|
|
1527
|
+
const zigExprStmtParser: Parser<ZigStatement, string> = promiseCompose(
|
|
1528
|
+
createTupleParser([
|
|
1529
|
+
zigExpressionParser,
|
|
1530
|
+
zigSkippableParser,
|
|
1531
|
+
createExactSequenceParser(';'),
|
|
1532
|
+
]),
|
|
1533
|
+
([expression]): ZigStatement => expression,
|
|
1534
|
+
);
|
|
1535
|
+
|
|
1536
|
+
setParserName(zigExprStmtParser, 'zigExprStmtParser');
|
|
1537
|
+
|
|
1538
|
+
// Statement impl
|
|
1539
|
+
const zigStatementParserImpl: Parser<ZigStatement, string> = createDisjunctionParser([
|
|
1540
|
+
zigVarDeclStmtParser,
|
|
1541
|
+
zigReturnStmtParser,
|
|
1542
|
+
zigBreakStmtParser,
|
|
1543
|
+
zigContinueStmtParser,
|
|
1544
|
+
zigDeferStmtParser,
|
|
1545
|
+
zigIfStmtParser,
|
|
1546
|
+
zigWhileStmtParser,
|
|
1547
|
+
zigForStmtParser,
|
|
1548
|
+
zigBlockStmtParser,
|
|
1549
|
+
zigAssignStmtParser,
|
|
1550
|
+
zigExprStmtParser,
|
|
1551
|
+
]);
|
|
1552
|
+
|
|
1553
|
+
setParserName(zigStatementParserImpl, 'zigStatementParserImpl');
|
|
1554
|
+
|
|
1555
|
+
// Top-level declarations
|
|
1556
|
+
|
|
1557
|
+
// Function parameter
|
|
1558
|
+
const zigFnParamParser: Parser<ZigFnParam, string> = promiseCompose(
|
|
1559
|
+
createTupleParser([
|
|
1560
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('comptime'), zigMandatorySkipParser]), () => true as const)),
|
|
1561
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('noalias'), zigMandatorySkipParser]), () => true as const)),
|
|
1562
|
+
createOptionalParser(promiseCompose(
|
|
1563
|
+
createTupleParser([zigIdentifierParser, zigSkippableParser, createExactSequenceParser(':'), zigSkippableParser]),
|
|
1564
|
+
([name]) => name,
|
|
1565
|
+
)),
|
|
1566
|
+
zigExpressionParser,
|
|
1567
|
+
]),
|
|
1568
|
+
([isComptime, isNoalias, name, typeExpr]): ZigFnParam => ({
|
|
1569
|
+
type: 'FnParam',
|
|
1570
|
+
...(name ? { name } : {}),
|
|
1571
|
+
isComptime: isComptime ?? false,
|
|
1572
|
+
isNoalias: isNoalias ?? false,
|
|
1573
|
+
typeExpr,
|
|
1574
|
+
}),
|
|
1575
|
+
);
|
|
1576
|
+
|
|
1577
|
+
setParserName(zigFnParamParser, 'zigFnParamParser');
|
|
1578
|
+
|
|
1579
|
+
// Function parameter list
|
|
1580
|
+
const zigFnParamListParser: Parser<ZigFnParam[], string> = promiseCompose(
|
|
1581
|
+
createTupleParser([
|
|
1582
|
+
createExactSequenceParser('('),
|
|
1583
|
+
zigSkippableParser,
|
|
1584
|
+
createOptionalParser(
|
|
1585
|
+
createSeparatedNonEmptyArrayParser(
|
|
1586
|
+
promiseCompose(createTupleParser([zigFnParamParser, zigSkippableParser]), ([param]) => param),
|
|
1587
|
+
promiseCompose(createTupleParser([createExactSequenceParser(','), zigSkippableParser]), () => ','),
|
|
1588
|
+
),
|
|
1589
|
+
),
|
|
1590
|
+
createOptionalParser(createExactSequenceParser(',')),
|
|
1591
|
+
zigSkippableParser,
|
|
1592
|
+
createExactSequenceParser(')'),
|
|
1593
|
+
]),
|
|
1594
|
+
([, , params]) => params ?? [],
|
|
1595
|
+
);
|
|
1596
|
+
|
|
1597
|
+
setParserName(zigFnParamListParser, 'zigFnParamListParser');
|
|
1598
|
+
|
|
1599
|
+
// Function declaration
|
|
1600
|
+
const zigFnDeclParser: Parser<ZigFnDecl, string> = promiseCompose(
|
|
1601
|
+
createTupleParser([
|
|
1602
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('pub'), zigMandatorySkipParser]), () => true as const)),
|
|
1603
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('extern'), zigMandatorySkipParser]), () => true as const)),
|
|
1604
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('export'), zigMandatorySkipParser]), () => true as const)),
|
|
1605
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('inline'), zigMandatorySkipParser]), () => true as const)),
|
|
1606
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('comptime'), zigMandatorySkipParser]), () => true as const)),
|
|
1607
|
+
zigKeyword('fn'),
|
|
1608
|
+
zigMandatorySkipParser,
|
|
1609
|
+
zigIdentifierParser,
|
|
1610
|
+
zigSkippableParser,
|
|
1611
|
+
zigFnParamListParser,
|
|
1612
|
+
zigSkippableParser,
|
|
1613
|
+
zigTypeExprParser,
|
|
1614
|
+
zigSkippableParser,
|
|
1615
|
+
createDisjunctionParser([
|
|
1616
|
+
zigBlockExprParser,
|
|
1617
|
+
promiseCompose(createExactSequenceParser(';'), (): undefined => undefined),
|
|
1618
|
+
]),
|
|
1619
|
+
]),
|
|
1620
|
+
([isPub, isExtern, isExport, isInline, isComptime, , , name, , params, , returnType, , body]): ZigFnDecl => ({
|
|
1621
|
+
type: 'FnDecl',
|
|
1622
|
+
isPub: isPub ?? false,
|
|
1623
|
+
isExtern: isExtern ?? false,
|
|
1624
|
+
isExport: isExport ?? false,
|
|
1625
|
+
isInline: isInline ?? false,
|
|
1626
|
+
isComptime: isComptime ?? false,
|
|
1627
|
+
name,
|
|
1628
|
+
params,
|
|
1629
|
+
returnType,
|
|
1630
|
+
...(body !== undefined ? { body } : {}),
|
|
1631
|
+
}),
|
|
1632
|
+
);
|
|
1633
|
+
|
|
1634
|
+
setParserName(zigFnDeclParser, 'zigFnDeclParser');
|
|
1635
|
+
|
|
1636
|
+
// Top-level variable declaration
|
|
1637
|
+
const zigVarDeclParser: Parser<ZigVarDecl, string> = promiseCompose(
|
|
1638
|
+
createTupleParser([
|
|
1639
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('pub'), zigMandatorySkipParser]), () => true as const)),
|
|
1640
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('extern'), zigMandatorySkipParser]), () => true as const)),
|
|
1641
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('comptime'), zigMandatorySkipParser]), () => true as const)),
|
|
1642
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('threadlocal'), zigMandatorySkipParser]), () => true as const)),
|
|
1643
|
+
createDisjunctionParser([
|
|
1644
|
+
promiseCompose(zigKeyword('const'), () => true as const),
|
|
1645
|
+
promiseCompose(zigKeyword('var'), () => false as const),
|
|
1646
|
+
]),
|
|
1647
|
+
zigMandatorySkipParser,
|
|
1648
|
+
zigIdentifierParser,
|
|
1649
|
+
zigSkippableParser,
|
|
1650
|
+
createOptionalParser(promiseCompose(
|
|
1651
|
+
createTupleParser([createExactSequenceParser(':'), zigSkippableParser, zigExpressionParser, zigSkippableParser]),
|
|
1652
|
+
([, , type_]) => type_,
|
|
1653
|
+
)),
|
|
1654
|
+
createOptionalParser(promiseCompose(
|
|
1655
|
+
createTupleParser([zigKeyword('align'), zigSkippableParser, createExactSequenceParser('('), zigSkippableParser, zigExpressionParser, zigSkippableParser, createExactSequenceParser(')'), zigSkippableParser]),
|
|
1656
|
+
([, , , , expr]) => expr,
|
|
1657
|
+
)),
|
|
1658
|
+
createOptionalParser(promiseCompose(
|
|
1659
|
+
createTupleParser([createExactSequenceParser('='), zigSkippableParser, zigExpressionParser, zigSkippableParser]),
|
|
1660
|
+
([, , init]) => init,
|
|
1661
|
+
)),
|
|
1662
|
+
createExactSequenceParser(';'),
|
|
1663
|
+
]),
|
|
1664
|
+
([isPub, isExtern, isComptime, isThreadlocal, isConst, , name, , typeExpr, alignExpr, initExpr]): ZigVarDecl => ({
|
|
1665
|
+
type: 'VarDecl',
|
|
1666
|
+
isConst,
|
|
1667
|
+
isPub: isPub ?? false,
|
|
1668
|
+
isExtern: isExtern ?? false,
|
|
1669
|
+
isComptime: isComptime ?? false,
|
|
1670
|
+
isThreadlocal: isThreadlocal ?? false,
|
|
1671
|
+
name,
|
|
1672
|
+
...(typeExpr !== undefined ? { typeExpr } : {}),
|
|
1673
|
+
...(alignExpr !== undefined ? { alignExpr } : {}),
|
|
1674
|
+
...(initExpr !== undefined ? { initExpr } : {}),
|
|
1675
|
+
}),
|
|
1676
|
+
);
|
|
1677
|
+
|
|
1678
|
+
setParserName(zigVarDeclParser, 'zigVarDeclParser');
|
|
1679
|
+
|
|
1680
|
+
// Test declaration: test ["name"] { ... }
|
|
1681
|
+
const zigTestDeclParser: Parser<ZigTestDecl, string> = promiseCompose(
|
|
1682
|
+
createTupleParser([
|
|
1683
|
+
zigKeyword('test'),
|
|
1684
|
+
zigSkippableParser,
|
|
1685
|
+
createOptionalParser(promiseCompose(
|
|
1686
|
+
createTupleParser([zigStringLiteralParser, zigSkippableParser]),
|
|
1687
|
+
([str]) => (str as { type: 'StringLiteral'; value: string }).value,
|
|
1688
|
+
)),
|
|
1689
|
+
zigBlockExprParser,
|
|
1690
|
+
]),
|
|
1691
|
+
([, , name, body]): ZigTestDecl => ({
|
|
1692
|
+
type: 'TestDecl',
|
|
1693
|
+
...(name ? { name } : {}),
|
|
1694
|
+
body,
|
|
1695
|
+
}),
|
|
1696
|
+
);
|
|
1697
|
+
|
|
1698
|
+
setParserName(zigTestDeclParser, 'zigTestDeclParser');
|
|
1699
|
+
|
|
1700
|
+
// Usingnamespace declaration: [pub] usingnamespace expr;
|
|
1701
|
+
const zigUsingnamespaceDeclParser: Parser<ZigUsingnamespaceDecl, string> = promiseCompose(
|
|
1702
|
+
createTupleParser([
|
|
1703
|
+
createOptionalParser(promiseCompose(createTupleParser([zigKeyword('pub'), zigMandatorySkipParser]), () => true as const)),
|
|
1704
|
+
zigKeyword('usingnamespace'),
|
|
1705
|
+
zigMandatorySkipParser,
|
|
1706
|
+
zigExpressionParser,
|
|
1707
|
+
zigSkippableParser,
|
|
1708
|
+
createExactSequenceParser(';'),
|
|
1709
|
+
]),
|
|
1710
|
+
([isPub, , , expression]): ZigUsingnamespaceDecl => ({
|
|
1711
|
+
type: 'UsingnamespaceDecl',
|
|
1712
|
+
isPub: isPub ?? false,
|
|
1713
|
+
expression,
|
|
1714
|
+
}),
|
|
1715
|
+
);
|
|
1716
|
+
|
|
1717
|
+
setParserName(zigUsingnamespaceDeclParser, 'zigUsingnamespaceDeclParser');
|
|
1718
|
+
|
|
1719
|
+
// Container member (top-level declaration)
|
|
1720
|
+
const zigContainerMemberParser: Parser<ZigContainerMember, string> = createDisjunctionParser([
|
|
1721
|
+
zigFnDeclParser,
|
|
1722
|
+
zigVarDeclParser,
|
|
1723
|
+
zigTestDeclParser,
|
|
1724
|
+
zigUsingnamespaceDeclParser,
|
|
1725
|
+
]);
|
|
1726
|
+
|
|
1727
|
+
setParserName(zigContainerMemberParser, 'zigContainerMemberParser');
|
|
1728
|
+
|
|
1729
|
+
// Root: source file
|
|
1730
|
+
export const zigSourceFileParser: Parser<ZigRoot, string> = createObjectParser({
|
|
1731
|
+
_ws1: zigSkippableParser,
|
|
1732
|
+
type: 'Root' as const,
|
|
1733
|
+
members: createArrayParser(
|
|
1734
|
+
promiseCompose(
|
|
1735
|
+
createTupleParser([
|
|
1736
|
+
zigContainerMemberParser,
|
|
1737
|
+
zigSkippableParser,
|
|
1738
|
+
]),
|
|
1739
|
+
([member]) => member,
|
|
1740
|
+
),
|
|
1741
|
+
),
|
|
1742
|
+
_ws2: zigSkippableParser,
|
|
1743
|
+
});
|
|
1744
|
+
|
|
1745
|
+
setParserName(zigSourceFileParser, 'zigSourceFileParser');
|