@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
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { testProp } from '@fast-check/ava';
|
|
2
|
+
import { arbitraryZigRoot } from './arbitraryZig.js';
|
|
3
|
+
import { zigSourceFileUnparser } from './zigUnparser.js';
|
|
4
|
+
import { zigSourceFileParser } from './zigParser.js';
|
|
5
|
+
import { runParser } from './parser.js';
|
|
6
|
+
import { runUnparser } from './unparser.js';
|
|
7
|
+
import { stringParserInputCompanion } from './parserInputCompanion.js';
|
|
8
|
+
import { stringUnparserOutputCompanion } from './unparserOutputCompanion.js';
|
|
9
|
+
|
|
10
|
+
const seed = process.env.SEED ? Number(process.env.SEED) : undefined;
|
|
11
|
+
|
|
12
|
+
async function collectString(asyncIterable: AsyncIterable<string>): Promise<string> {
|
|
13
|
+
let result = '';
|
|
14
|
+
for await (const chunk of asyncIterable) {
|
|
15
|
+
result += chunk;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
testProp(
|
|
22
|
+
'zigUnparser roundtrip',
|
|
23
|
+
[arbitraryZigRoot],
|
|
24
|
+
async (t, root) => {
|
|
25
|
+
const source = await collectString(runUnparser(
|
|
26
|
+
zigSourceFileUnparser, root, stringUnparserOutputCompanion));
|
|
27
|
+
|
|
28
|
+
const reparsed = await runParser(
|
|
29
|
+
zigSourceFileParser, source, stringParserInputCompanion);
|
|
30
|
+
|
|
31
|
+
t.deepEqual(reparsed, root);
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
verbose: true,
|
|
35
|
+
seed,
|
|
36
|
+
},
|
|
37
|
+
);
|
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
import { escapeZigString, escapeZigChar } from './stringEscapes.js';
|
|
2
|
+
import { type Unparser } from './unparser.js';
|
|
3
|
+
import {
|
|
4
|
+
type ZigRoot,
|
|
5
|
+
type ZigContainerMember,
|
|
6
|
+
type ZigFnDecl,
|
|
7
|
+
type ZigVarDecl,
|
|
8
|
+
type ZigTestDecl,
|
|
9
|
+
type ZigUsingnamespaceDecl,
|
|
10
|
+
type ZigContainerField,
|
|
11
|
+
type ZigFnParam,
|
|
12
|
+
type ZigExpression,
|
|
13
|
+
type ZigTypeExpression,
|
|
14
|
+
type ZigStatement,
|
|
15
|
+
type ZigBlockExpr,
|
|
16
|
+
type ZigSwitchProng,
|
|
17
|
+
type ZigStructInitField,
|
|
18
|
+
type ZigBinaryOp,
|
|
19
|
+
} from './zig.js';
|
|
20
|
+
|
|
21
|
+
function unparseExpression(node: ZigExpression): string {
|
|
22
|
+
switch (node.type) {
|
|
23
|
+
case 'Identifier':
|
|
24
|
+
return node.name;
|
|
25
|
+
|
|
26
|
+
case 'IntegerLiteral':
|
|
27
|
+
return node.value;
|
|
28
|
+
|
|
29
|
+
case 'FloatLiteral':
|
|
30
|
+
return node.value;
|
|
31
|
+
|
|
32
|
+
case 'StringLiteral':
|
|
33
|
+
return '"' + escapeZigString(node.value) + '"';
|
|
34
|
+
|
|
35
|
+
case 'MultilineStringLiteral':
|
|
36
|
+
return node.value;
|
|
37
|
+
|
|
38
|
+
case 'CharLiteral':
|
|
39
|
+
return "'" + escapeZigChar(node.value) + "'";
|
|
40
|
+
|
|
41
|
+
case 'EnumLiteral':
|
|
42
|
+
return '.' + node.name;
|
|
43
|
+
|
|
44
|
+
case 'BoolLiteral':
|
|
45
|
+
return node.value ? 'true' : 'false';
|
|
46
|
+
|
|
47
|
+
case 'NullLiteral':
|
|
48
|
+
return 'null';
|
|
49
|
+
|
|
50
|
+
case 'UndefinedLiteral':
|
|
51
|
+
return 'undefined';
|
|
52
|
+
|
|
53
|
+
case 'BinaryExpr':
|
|
54
|
+
return unparseExpression(node.left) + ' ' + unparseBinaryOp(node.operator) + ' ' + unparseExpression(node.right);
|
|
55
|
+
|
|
56
|
+
case 'UnaryExpr':
|
|
57
|
+
if (node.operator === '&') {
|
|
58
|
+
return '&' + unparseExpression(node.operand);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return node.operator + unparseExpression(node.operand);
|
|
62
|
+
|
|
63
|
+
case 'FieldAccessExpr':
|
|
64
|
+
return unparseExpression(node.operand) + '.' + node.member;
|
|
65
|
+
|
|
66
|
+
case 'IndexExpr':
|
|
67
|
+
return unparseExpression(node.operand) + '[' + unparseExpression(node.index) + ']';
|
|
68
|
+
|
|
69
|
+
case 'SliceExpr': {
|
|
70
|
+
let result = unparseExpression(node.operand) + '[' + unparseExpression(node.start) + '..';
|
|
71
|
+
if (node.end) {
|
|
72
|
+
result += unparseExpression(node.end);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
result += ']';
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
case 'CallExpr':
|
|
80
|
+
return unparseExpression(node.callee) + '(' + node.args.map(a => unparseExpression(a)).join(', ') + ')';
|
|
81
|
+
|
|
82
|
+
case 'BuiltinCallExpr':
|
|
83
|
+
return '@' + node.name + '(' + node.args.map(a => unparseExpression(a)).join(', ') + ')';
|
|
84
|
+
|
|
85
|
+
case 'IfExpr': {
|
|
86
|
+
let result = 'if (' + unparseExpression(node.condition) + ')';
|
|
87
|
+
if (node.capture) {
|
|
88
|
+
result += ' |' + node.capture + '|';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
result += ' ' + unparseExpression(node.body as ZigExpression);
|
|
92
|
+
if (node.elseBody) {
|
|
93
|
+
result += ' else';
|
|
94
|
+
if (node.elseCapture) {
|
|
95
|
+
result += ' |' + node.elseCapture + '|';
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
result += ' ' + unparseExpression(node.elseBody as ZigExpression);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
case 'SwitchExpr': {
|
|
105
|
+
let result = 'switch (' + unparseExpression(node.operand) + ') {';
|
|
106
|
+
for (let i = 0; i < node.prongs.length; i++) {
|
|
107
|
+
if (i > 0) {
|
|
108
|
+
result += ',';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
result += ' ' + unparseSwitchProng(node.prongs[i]!);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (node.prongs.length > 0) {
|
|
115
|
+
result += ',';
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
result += ' }';
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
case 'StructInitExpr': {
|
|
123
|
+
let result = '';
|
|
124
|
+
if (node.operand) {
|
|
125
|
+
result += unparseExpression(node.operand);
|
|
126
|
+
} else {
|
|
127
|
+
result += '.';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
result += '{ ';
|
|
131
|
+
result += node.fields.map(f => unparseStructInitField(f)).join(', ');
|
|
132
|
+
if (node.fields.length > 0) {
|
|
133
|
+
result += ', ';
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
result += '}';
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
case 'ArrayInitExpr': {
|
|
141
|
+
let result = '';
|
|
142
|
+
if (node.operand) {
|
|
143
|
+
result += unparseExpression(node.operand);
|
|
144
|
+
} else {
|
|
145
|
+
result += '.';
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
result += '{ ';
|
|
149
|
+
result += node.elements.map(e => unparseExpression(e)).join(', ');
|
|
150
|
+
if (node.elements.length > 0) {
|
|
151
|
+
result += ', ';
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
result += '}';
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
case 'TryExpr':
|
|
159
|
+
return 'try ' + unparseExpression(node.operand);
|
|
160
|
+
|
|
161
|
+
case 'ComptimeExpr':
|
|
162
|
+
return 'comptime ' + unparseExpression(node.operand);
|
|
163
|
+
|
|
164
|
+
case 'BlockExpr':
|
|
165
|
+
return unparseBlockExpr(node);
|
|
166
|
+
|
|
167
|
+
case 'GroupedExpr':
|
|
168
|
+
return '(' + unparseExpression(node.inner) + ')';
|
|
169
|
+
|
|
170
|
+
case 'ErrorSetExpr':
|
|
171
|
+
return 'error{ ' + node.names.join(', ') + ' }';
|
|
172
|
+
|
|
173
|
+
case 'ErrorUnionType':
|
|
174
|
+
return unparseTypeExpression(node.error) + '!' + unparseTypeExpression(node.payload);
|
|
175
|
+
|
|
176
|
+
case 'PointerType':
|
|
177
|
+
return unparsePointerType(node);
|
|
178
|
+
|
|
179
|
+
case 'OptionalType':
|
|
180
|
+
return '?' + unparseTypeExpression(node.child);
|
|
181
|
+
|
|
182
|
+
case 'FnProtoType':
|
|
183
|
+
return 'fn(' + node.params.map(p => unparseFnParam(p)).join(', ') + ') ' + unparseTypeExpression(node.returnType);
|
|
184
|
+
|
|
185
|
+
case 'StructExpr': {
|
|
186
|
+
let result = 'struct { ';
|
|
187
|
+
for (const member of node.members) {
|
|
188
|
+
result += unparseContainerMember(member) + ' ';
|
|
189
|
+
}
|
|
190
|
+
result += '}';
|
|
191
|
+
return result;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
case 'ArrayType': {
|
|
195
|
+
let result = '[' + unparseExpression(node.length);
|
|
196
|
+
if (node.sentinel) {
|
|
197
|
+
result += ':' + unparseExpression(node.sentinel);
|
|
198
|
+
}
|
|
199
|
+
result += ']';
|
|
200
|
+
result += unparseTypeExpression(node.child);
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
default:
|
|
205
|
+
throw new Error(`Unknown expression type: ${(node as { type: string }).type}`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function unparsePointerType(node: { type: 'PointerType'; size: string; isConst: boolean; sentinel?: ZigExpression; child: ZigTypeExpression }): string {
|
|
210
|
+
let result = '';
|
|
211
|
+
switch (node.size) {
|
|
212
|
+
case 'one':
|
|
213
|
+
result += '*';
|
|
214
|
+
break;
|
|
215
|
+
case 'many':
|
|
216
|
+
result += '[*]';
|
|
217
|
+
break;
|
|
218
|
+
case 'slice':
|
|
219
|
+
result += '[]';
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (node.isConst) {
|
|
224
|
+
result += 'const ';
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
result += unparseTypeExpression(node.child);
|
|
228
|
+
return result;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function unparseTypeExpression(node: ZigTypeExpression): string {
|
|
232
|
+
switch (node.type) {
|
|
233
|
+
case 'IdentifierType':
|
|
234
|
+
return node.name;
|
|
235
|
+
|
|
236
|
+
case 'PointerType':
|
|
237
|
+
return unparsePointerType(node);
|
|
238
|
+
|
|
239
|
+
case 'ArrayType': {
|
|
240
|
+
let result = '[' + unparseExpression(node.length);
|
|
241
|
+
if (node.sentinel) {
|
|
242
|
+
result += ':' + unparseExpression(node.sentinel);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
result += ']';
|
|
246
|
+
result += unparseTypeExpression(node.child);
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
case 'OptionalType':
|
|
251
|
+
return '?' + unparseTypeExpression(node.child);
|
|
252
|
+
|
|
253
|
+
case 'ErrorUnionType':
|
|
254
|
+
return unparseTypeExpression(node.error) + '!' + unparseTypeExpression(node.payload);
|
|
255
|
+
|
|
256
|
+
case 'DotType':
|
|
257
|
+
return unparseTypeExpression(node.operand) + '.' + node.member;
|
|
258
|
+
|
|
259
|
+
case 'BuiltinType':
|
|
260
|
+
return '@' + node.name;
|
|
261
|
+
|
|
262
|
+
case 'FnProtoType':
|
|
263
|
+
return 'fn(' + node.params.map(p => unparseFnParam(p)).join(', ') + ') ' + unparseTypeExpression(node.returnType);
|
|
264
|
+
|
|
265
|
+
default:
|
|
266
|
+
return unparseExpression(node);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function unparseBinaryOp(op: ZigBinaryOp): string {
|
|
271
|
+
switch (op) {
|
|
272
|
+
case 'and':
|
|
273
|
+
return 'and';
|
|
274
|
+
case 'or':
|
|
275
|
+
return 'or';
|
|
276
|
+
case 'orelse':
|
|
277
|
+
return 'orelse';
|
|
278
|
+
case 'catch':
|
|
279
|
+
return 'catch';
|
|
280
|
+
default:
|
|
281
|
+
return op;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function unparseSwitchProng(prong: ZigSwitchProng): string {
|
|
286
|
+
let result = '';
|
|
287
|
+
if (prong.isElse) {
|
|
288
|
+
result += 'else';
|
|
289
|
+
} else {
|
|
290
|
+
result += prong.cases.map(c => unparseExpression(c)).join(', ');
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
result += ' =>';
|
|
294
|
+
if (prong.capture) {
|
|
295
|
+
result += ' |' + prong.capture + '|';
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
result += ' ' + unparseExpression(prong.body);
|
|
299
|
+
return result;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function unparseStructInitField(field: ZigStructInitField): string {
|
|
303
|
+
return '.' + field.name + ' = ' + unparseExpression(field.value);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function unparseBlockExpr(node: ZigBlockExpr): string {
|
|
307
|
+
let result = '';
|
|
308
|
+
if (node.label) {
|
|
309
|
+
result += node.label + ': ';
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
result += '{ ';
|
|
313
|
+
for (const stmt of node.statements) {
|
|
314
|
+
result += unparseStatement(stmt) + ' ';
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
result += '}';
|
|
318
|
+
return result;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function unparseStatement(node: ZigStatement): string {
|
|
322
|
+
switch (node.type) {
|
|
323
|
+
case 'AssignStmt':
|
|
324
|
+
return unparseExpression(node.target) + ' ' + node.operator + ' ' + unparseExpression(node.value) + ';';
|
|
325
|
+
|
|
326
|
+
case 'VarDecl':
|
|
327
|
+
return unparseVarDeclCommon(node);
|
|
328
|
+
|
|
329
|
+
case 'IfExpr': {
|
|
330
|
+
let result = 'if (' + unparseExpression(node.condition) + ')';
|
|
331
|
+
if (node.capture) {
|
|
332
|
+
result += ' |' + node.capture + '|';
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
result += ' ' + unparseStatement(node.body);
|
|
336
|
+
if (node.elseBody) {
|
|
337
|
+
result += ' else';
|
|
338
|
+
if (node.elseCapture) {
|
|
339
|
+
result += ' |' + node.elseCapture + '|';
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
result += ' ' + unparseStatement(node.elseBody);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return result;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
case 'WhileStmt': {
|
|
349
|
+
let result = '';
|
|
350
|
+
if (node.label) {
|
|
351
|
+
result += node.label + ': ';
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (node.isInline) {
|
|
355
|
+
result += 'inline ';
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
result += 'while (' + unparseExpression(node.condition) + ')';
|
|
359
|
+
if (node.capture) {
|
|
360
|
+
result += ' |' + node.capture + '|';
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if (node.continuation) {
|
|
364
|
+
result += ' : (' + unparseExpression(node.continuation) + ')';
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
result += ' ' + unparseStatement(node.body);
|
|
368
|
+
if (node.elseBody) {
|
|
369
|
+
result += ' else ' + unparseStatement(node.elseBody);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
return result;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
case 'ForStmt': {
|
|
376
|
+
let result = '';
|
|
377
|
+
if (node.label) {
|
|
378
|
+
result += node.label + ': ';
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (node.isInline) {
|
|
382
|
+
result += 'inline ';
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
result += 'for (' + node.inputs.map(i => unparseExpression(i)).join(', ') + ') |' + node.captures.join(', ') + '| ' + unparseStatement(node.body);
|
|
386
|
+
if (node.elseBody) {
|
|
387
|
+
result += ' else ' + unparseStatement(node.elseBody);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
return result;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
case 'ReturnStmt':
|
|
394
|
+
if (node.value) {
|
|
395
|
+
return 'return ' + unparseExpression(node.value) + ';';
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return 'return;';
|
|
399
|
+
|
|
400
|
+
case 'BreakStmt': {
|
|
401
|
+
let result = 'break';
|
|
402
|
+
if (node.label) {
|
|
403
|
+
result += ' :' + node.label;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (node.value) {
|
|
407
|
+
result += ' ' + unparseExpression(node.value);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
result += ';';
|
|
411
|
+
return result;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
case 'ContinueStmt': {
|
|
415
|
+
let result = 'continue';
|
|
416
|
+
if (node.label) {
|
|
417
|
+
result += ' :' + node.label;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
result += ';';
|
|
421
|
+
return result;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
case 'DeferStmt': {
|
|
425
|
+
let result = node.isErrdefer ? 'errdefer' : 'defer';
|
|
426
|
+
if (node.capture) {
|
|
427
|
+
result += ' |' + node.capture + '|';
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
result += ' ' + unparseStatement(node.body);
|
|
431
|
+
return result;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
case 'BlockExpr':
|
|
435
|
+
return unparseBlockExpr(node);
|
|
436
|
+
|
|
437
|
+
default:
|
|
438
|
+
return unparseExpression(node as ZigExpression) + ';';
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function unparseVarDeclCommon(node: {
|
|
443
|
+
isPub: boolean;
|
|
444
|
+
isExtern: boolean;
|
|
445
|
+
isComptime: boolean;
|
|
446
|
+
isThreadlocal: boolean;
|
|
447
|
+
isConst: boolean;
|
|
448
|
+
name: string;
|
|
449
|
+
typeExpr?: ZigTypeExpression;
|
|
450
|
+
alignExpr?: ZigExpression;
|
|
451
|
+
initExpr?: ZigExpression;
|
|
452
|
+
}): string {
|
|
453
|
+
let result = '';
|
|
454
|
+
if (node.isPub) {
|
|
455
|
+
result += 'pub ';
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (node.isExtern) {
|
|
459
|
+
result += 'extern ';
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
if (node.isComptime) {
|
|
463
|
+
result += 'comptime ';
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (node.isThreadlocal) {
|
|
467
|
+
result += 'threadlocal ';
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
result += node.isConst ? 'const' : 'var';
|
|
471
|
+
result += ' ' + node.name;
|
|
472
|
+
if (node.typeExpr) {
|
|
473
|
+
result += ': ' + unparseTypeExpression(node.typeExpr);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (node.alignExpr) {
|
|
477
|
+
result += ' align(' + unparseExpression(node.alignExpr) + ')';
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (node.initExpr) {
|
|
481
|
+
result += ' = ' + unparseExpression(node.initExpr);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
result += ';';
|
|
485
|
+
return result;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function unparseFnParam(param: ZigFnParam): string {
|
|
489
|
+
let result = '';
|
|
490
|
+
if (param.isComptime) {
|
|
491
|
+
result += 'comptime ';
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (param.isNoalias) {
|
|
495
|
+
result += 'noalias ';
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (param.name) {
|
|
499
|
+
result += param.name + ': ';
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (param.typeExpr) {
|
|
503
|
+
result += unparseTypeExpression(param.typeExpr);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
return result;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function unparseContainerMember(node: ZigContainerMember): string {
|
|
510
|
+
switch (node.type) {
|
|
511
|
+
case 'FnDecl':
|
|
512
|
+
return unparseFnDecl(node);
|
|
513
|
+
|
|
514
|
+
case 'VarDecl':
|
|
515
|
+
return unparseVarDeclCommon(node);
|
|
516
|
+
|
|
517
|
+
case 'TestDecl':
|
|
518
|
+
return unparseTestDecl(node);
|
|
519
|
+
|
|
520
|
+
case 'UsingnamespaceDecl':
|
|
521
|
+
return unparseUsingnamespaceDecl(node);
|
|
522
|
+
|
|
523
|
+
case 'ContainerField':
|
|
524
|
+
return unparseContainerField(node);
|
|
525
|
+
|
|
526
|
+
default:
|
|
527
|
+
throw new Error(`Unknown container member type: ${(node as { type: string }).type}`);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function unparseFnDecl(node: ZigFnDecl): string {
|
|
532
|
+
let result = '';
|
|
533
|
+
if (node.isPub) {
|
|
534
|
+
result += 'pub ';
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if (node.isExtern) {
|
|
538
|
+
result += 'extern ';
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
if (node.isExport) {
|
|
542
|
+
result += 'export ';
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
if (node.isInline) {
|
|
546
|
+
result += 'inline ';
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
if (node.isComptime) {
|
|
550
|
+
result += 'comptime ';
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
result += 'fn ' + node.name + '(' + node.params.map(p => unparseFnParam(p)).join(', ') + ') ';
|
|
554
|
+
result += unparseTypeExpression(node.returnType);
|
|
555
|
+
if (node.body) {
|
|
556
|
+
result += ' ' + unparseBlockExpr(node.body);
|
|
557
|
+
} else {
|
|
558
|
+
result += ';';
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
return result;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function unparseTestDecl(node: ZigTestDecl): string {
|
|
565
|
+
let result = 'test';
|
|
566
|
+
if (node.name) {
|
|
567
|
+
result += ' "' + escapeZigString(node.name) + '"';
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
result += ' ' + unparseBlockExpr(node.body);
|
|
571
|
+
return result;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function unparseUsingnamespaceDecl(node: ZigUsingnamespaceDecl): string {
|
|
575
|
+
let result = '';
|
|
576
|
+
if (node.isPub) {
|
|
577
|
+
result += 'pub ';
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
result += 'usingnamespace ' + unparseExpression(node.expression) + ';';
|
|
581
|
+
return result;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function unparseContainerField(node: ZigContainerField): string {
|
|
585
|
+
let result = node.name;
|
|
586
|
+
if (node.typeExpr) {
|
|
587
|
+
result += ': ' + unparseTypeExpression(node.typeExpr);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
if (node.alignExpr) {
|
|
591
|
+
result += ' align(' + unparseExpression(node.alignExpr) + ')';
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
if (node.defaultValue) {
|
|
595
|
+
result += ' = ' + unparseExpression(node.defaultValue);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
result += ',';
|
|
599
|
+
return result;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
export const zigSourceFileUnparser: Unparser<unknown, string> = async function * (input) {
|
|
603
|
+
const root = input as ZigRoot;
|
|
604
|
+
let result = '';
|
|
605
|
+
|
|
606
|
+
for (let i = 0; i < root.members.length; i++) {
|
|
607
|
+
if (i > 0) {
|
|
608
|
+
result += '\n';
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
result += unparseContainerMember(root.members[i]!);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
yield result;
|
|
615
|
+
};
|