swf-toolkit 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +125 -2
- package/README.tr.md +126 -0
- package/dist/abc/abc-file.d.ts +168 -0
- package/dist/abc/abc-file.js +656 -0
- package/dist/abc/analysis.d.ts +36 -0
- package/dist/abc/analysis.js +213 -0
- package/dist/abc/code.d.ts +59 -0
- package/dist/abc/code.js +379 -0
- package/dist/abc/constants.d.ts +80 -0
- package/dist/abc/constants.js +93 -0
- package/dist/abc/interner.d.ts +46 -0
- package/dist/abc/interner.js +168 -0
- package/dist/abc/model.d.ts +40 -0
- package/dist/abc/model.js +146 -0
- package/dist/abc/opcodes.d.ts +39 -0
- package/dist/abc/opcodes.js +196 -0
- package/dist/abc/pcode/assembler.d.ts +42 -0
- package/dist/abc/pcode/assembler.js +635 -0
- package/dist/abc/pcode/disassembler.d.ts +17 -0
- package/dist/abc/pcode/disassembler.js +147 -0
- package/dist/abc/pcode/format.d.ts +23 -0
- package/dist/abc/pcode/format.js +131 -0
- package/dist/abc/pcode/lexer.d.ts +34 -0
- package/dist/abc/pcode/lexer.js +183 -0
- package/dist/assets/assets.d.ts +26 -0
- package/dist/assets/assets.js +151 -0
- package/dist/assets/images.d.ts +31 -0
- package/dist/assets/images.js +238 -0
- package/dist/assets/png.d.ts +12 -0
- package/dist/assets/png.js +196 -0
- package/dist/assets/sounds.d.ts +37 -0
- package/dist/assets/sounds.js +236 -0
- package/dist/avm1/avm1.d.ts +55 -0
- package/dist/avm1/avm1.js +642 -0
- package/dist/avm1/project.d.ts +16 -0
- package/dist/avm1/project.js +81 -0
- package/dist/cjs/abc/abc-file.d.ts +168 -0
- package/dist/cjs/abc/abc-file.js +663 -0
- package/dist/cjs/abc/analysis.d.ts +36 -0
- package/dist/cjs/abc/analysis.js +221 -0
- package/dist/cjs/abc/code.d.ts +59 -0
- package/dist/cjs/abc/code.js +387 -0
- package/dist/cjs/abc/constants.d.ts +80 -0
- package/dist/cjs/abc/constants.js +97 -0
- package/dist/cjs/abc/interner.d.ts +46 -0
- package/dist/cjs/abc/interner.js +172 -0
- package/dist/cjs/abc/model.d.ts +40 -0
- package/dist/cjs/abc/model.js +155 -0
- package/dist/cjs/abc/opcodes.d.ts +39 -0
- package/dist/cjs/abc/opcodes.js +201 -0
- package/dist/cjs/abc/pcode/assembler.d.ts +42 -0
- package/dist/cjs/abc/pcode/assembler.js +640 -0
- package/dist/cjs/abc/pcode/disassembler.d.ts +17 -0
- package/dist/cjs/abc/pcode/disassembler.js +152 -0
- package/dist/cjs/abc/pcode/format.d.ts +23 -0
- package/dist/cjs/abc/pcode/format.js +141 -0
- package/dist/cjs/abc/pcode/lexer.d.ts +34 -0
- package/dist/cjs/abc/pcode/lexer.js +188 -0
- package/dist/cjs/assets/assets.d.ts +26 -0
- package/dist/cjs/assets/assets.js +159 -0
- package/dist/cjs/assets/images.d.ts +31 -0
- package/dist/cjs/assets/images.js +246 -0
- package/dist/cjs/assets/png.d.ts +12 -0
- package/dist/cjs/assets/png.js +202 -0
- package/dist/cjs/assets/sounds.d.ts +37 -0
- package/dist/cjs/assets/sounds.js +246 -0
- package/dist/cjs/avm1/avm1.d.ts +55 -0
- package/dist/cjs/avm1/avm1.js +649 -0
- package/dist/cjs/avm1/project.d.ts +16 -0
- package/dist/cjs/avm1/project.js +88 -0
- package/dist/cjs/compiler/ast.d.ts +285 -0
- package/dist/cjs/compiler/ast.js +3 -0
- package/dist/cjs/compiler/class-compiler.d.ts +34 -0
- package/dist/cjs/compiler/class-compiler.js +440 -0
- package/dist/cjs/compiler/codegen.d.ts +144 -0
- package/dist/cjs/compiler/codegen.js +1589 -0
- package/dist/cjs/compiler/lexer.d.ts +27 -0
- package/dist/cjs/compiler/lexer.js +254 -0
- package/dist/cjs/compiler/parser.d.ts +58 -0
- package/dist/cjs/compiler/parser.js +989 -0
- package/dist/cjs/compiler/program.d.ts +29 -0
- package/dist/cjs/compiler/program.js +36 -0
- package/dist/cjs/compiler/scope.d.ts +22 -0
- package/dist/cjs/compiler/scope.js +242 -0
- package/dist/cjs/compression/lzma.d.ts +37 -0
- package/dist/cjs/compression/lzma.js +575 -0
- package/dist/cjs/compression/swf-container.d.ts +19 -0
- package/dist/cjs/compression/swf-container.js +92 -0
- package/dist/cjs/decompiler/ast.d.ts +228 -0
- package/dist/cjs/decompiler/ast.js +455 -0
- package/dist/cjs/decompiler/cfg.d.ts +40 -0
- package/dist/cjs/decompiler/cfg.js +236 -0
- package/dist/cjs/decompiler/class-printer.d.ts +9 -0
- package/dist/cjs/decompiler/class-printer.js +173 -0
- package/dist/cjs/decompiler/method.d.ts +41 -0
- package/dist/cjs/decompiler/method.js +436 -0
- package/dist/cjs/decompiler/simulate.d.ts +55 -0
- package/dist/cjs/decompiler/simulate.js +591 -0
- package/dist/cjs/decompiler/structure.d.ts +48 -0
- package/dist/cjs/decompiler/structure.js +764 -0
- package/dist/cjs/errors.d.ts +11 -0
- package/dist/cjs/errors.js +25 -0
- package/dist/cjs/index.d.ts +41 -0
- package/dist/cjs/index.js +82 -0
- package/dist/cjs/io/reader.d.ts +48 -0
- package/dist/cjs/io/reader.js +180 -0
- package/dist/cjs/io/writer.d.ts +37 -0
- package/dist/cjs/io/writer.js +175 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/project/pcode-project.d.ts +32 -0
- package/dist/cjs/project/pcode-project.js +210 -0
- package/dist/cjs/project/source-export.d.ts +12 -0
- package/dist/cjs/project/source-export.js +51 -0
- package/dist/cjs/project/source-import.d.ts +13 -0
- package/dist/cjs/project/source-import.js +73 -0
- package/dist/cjs/swf/swf.d.ts +63 -0
- package/dist/cjs/swf/swf.js +147 -0
- package/dist/cjs/swf/tag-codes.d.ts +77 -0
- package/dist/cjs/swf/tag-codes.js +109 -0
- package/dist/cjs/swf/tags.d.ts +118 -0
- package/dist/cjs/swf/tags.js +330 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +296 -0
- package/dist/compiler/ast.d.ts +285 -0
- package/dist/compiler/ast.js +2 -0
- package/dist/compiler/class-compiler.d.ts +34 -0
- package/dist/compiler/class-compiler.js +437 -0
- package/dist/compiler/codegen.d.ts +144 -0
- package/dist/compiler/codegen.js +1585 -0
- package/dist/compiler/lexer.d.ts +27 -0
- package/dist/compiler/lexer.js +249 -0
- package/dist/compiler/parser.d.ts +58 -0
- package/dist/compiler/parser.js +984 -0
- package/dist/compiler/program.d.ts +29 -0
- package/dist/compiler/program.js +32 -0
- package/dist/compiler/scope.d.ts +22 -0
- package/dist/compiler/scope.js +238 -0
- package/dist/compression/lzma.d.ts +37 -0
- package/dist/compression/lzma.js +569 -0
- package/dist/compression/swf-container.d.ts +19 -0
- package/dist/compression/swf-container.js +87 -0
- package/dist/decompiler/ast.d.ts +228 -0
- package/dist/decompiler/ast.js +441 -0
- package/dist/decompiler/cfg.d.ts +40 -0
- package/dist/decompiler/cfg.js +231 -0
- package/dist/decompiler/class-printer.d.ts +9 -0
- package/dist/decompiler/class-printer.js +170 -0
- package/dist/decompiler/method.d.ts +41 -0
- package/dist/decompiler/method.js +427 -0
- package/dist/decompiler/simulate.d.ts +55 -0
- package/dist/decompiler/simulate.js +586 -0
- package/dist/decompiler/structure.d.ts +48 -0
- package/dist/decompiler/structure.js +760 -0
- package/dist/errors.d.ts +11 -0
- package/dist/errors.js +20 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +49 -0
- package/dist/io/reader.d.ts +48 -0
- package/dist/io/reader.js +176 -0
- package/dist/io/writer.d.ts +37 -0
- package/dist/io/writer.js +169 -0
- package/dist/project/pcode-project.d.ts +32 -0
- package/dist/project/pcode-project.js +203 -0
- package/dist/project/source-export.d.ts +12 -0
- package/dist/project/source-export.js +47 -0
- package/dist/project/source-import.d.ts +13 -0
- package/dist/project/source-import.js +70 -0
- package/dist/swf/swf.d.ts +63 -0
- package/dist/swf/swf.js +143 -0
- package/dist/swf/tag-codes.d.ts +77 -0
- package/dist/swf/tag-codes.js +105 -0
- package/dist/swf/tags.d.ts +118 -0
- package/dist/swf/tags.js +316 -0
- package/package.json +71 -4
|
@@ -0,0 +1,984 @@
|
|
|
1
|
+
import { CompileError, tokenize } from './lexer.js';
|
|
2
|
+
const BINARY_PREC = {
|
|
3
|
+
'||': 1,
|
|
4
|
+
'&&': 2,
|
|
5
|
+
'|': 3,
|
|
6
|
+
'^': 4,
|
|
7
|
+
'&': 5,
|
|
8
|
+
'==': 6,
|
|
9
|
+
'!=': 6,
|
|
10
|
+
'===': 6,
|
|
11
|
+
'!==': 6,
|
|
12
|
+
'<': 7,
|
|
13
|
+
'>': 7,
|
|
14
|
+
'<=': 7,
|
|
15
|
+
'>=': 7,
|
|
16
|
+
instanceof: 7,
|
|
17
|
+
is: 7,
|
|
18
|
+
as: 7,
|
|
19
|
+
in: 7,
|
|
20
|
+
'<<': 8,
|
|
21
|
+
'>>': 8,
|
|
22
|
+
'>>>': 8,
|
|
23
|
+
'+': 9,
|
|
24
|
+
'-': 9,
|
|
25
|
+
'*': 10,
|
|
26
|
+
'/': 10,
|
|
27
|
+
'%': 10,
|
|
28
|
+
};
|
|
29
|
+
const ASSIGN_OPS = new Set(['=', '+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '>>>=', '&=', '|=', '^=', '&&=', '||=']);
|
|
30
|
+
const MODIFIERS = new Set(['public', 'private', 'protected', 'internal', 'static', 'override', 'final', 'native', 'dynamic', 'virtual']);
|
|
31
|
+
export class Parser {
|
|
32
|
+
src;
|
|
33
|
+
source;
|
|
34
|
+
toks;
|
|
35
|
+
p = 0;
|
|
36
|
+
constructor(src, source) {
|
|
37
|
+
this.src = src;
|
|
38
|
+
this.source = source;
|
|
39
|
+
this.toks = tokenize(src, source);
|
|
40
|
+
}
|
|
41
|
+
// -------------------------------------------------------------------------
|
|
42
|
+
// Token helpers
|
|
43
|
+
// -------------------------------------------------------------------------
|
|
44
|
+
get t() {
|
|
45
|
+
return this.toks[this.p];
|
|
46
|
+
}
|
|
47
|
+
peek(o = 1) {
|
|
48
|
+
return this.toks[Math.min(this.p + o, this.toks.length - 1)];
|
|
49
|
+
}
|
|
50
|
+
pos(t = this.t) {
|
|
51
|
+
return { line: t.line, col: t.col };
|
|
52
|
+
}
|
|
53
|
+
error(msg, t = this.t) {
|
|
54
|
+
return new CompileError(msg, t.line, t.col, this.source);
|
|
55
|
+
}
|
|
56
|
+
is(value) {
|
|
57
|
+
const t = this.t;
|
|
58
|
+
return (t.kind === 'punct' || t.kind === 'kw') && t.value === value;
|
|
59
|
+
}
|
|
60
|
+
isId(value) {
|
|
61
|
+
return this.t.kind === 'id' && (value === undefined || this.t.value === value);
|
|
62
|
+
}
|
|
63
|
+
next() {
|
|
64
|
+
const t = this.t;
|
|
65
|
+
if (t.kind !== 'eof')
|
|
66
|
+
this.p++;
|
|
67
|
+
return t;
|
|
68
|
+
}
|
|
69
|
+
eat(value) {
|
|
70
|
+
if (this.is(value)) {
|
|
71
|
+
this.p++;
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
expect(value) {
|
|
77
|
+
// Split '>>' / '>>>' when closing type parameter lists.
|
|
78
|
+
if (value === '>' && this.t.kind === 'punct' && (this.t.value === '>>' || this.t.value === '>>>' || this.t.value === '>=' || this.t.value === '>>=')) {
|
|
79
|
+
const t = this.t;
|
|
80
|
+
const rest = t.value.slice(1);
|
|
81
|
+
this.toks.splice(this.p + 1, 0, { ...t, value: rest, col: t.col + 1, start: t.start + 1, nl: false });
|
|
82
|
+
this.p++;
|
|
83
|
+
return { ...t, value: '>' };
|
|
84
|
+
}
|
|
85
|
+
if (!this.is(value))
|
|
86
|
+
throw this.error(`Expected '${value}' but found '${this.t.value || 'end of file'}'`);
|
|
87
|
+
return this.next();
|
|
88
|
+
}
|
|
89
|
+
/** Identifier (keywords allowed where `allowKeyword` is set, e.g. after '.'). */
|
|
90
|
+
ident(allowKeyword = false) {
|
|
91
|
+
const t = this.t;
|
|
92
|
+
if (t.kind === 'id' || (allowKeyword && t.kind === 'kw')) {
|
|
93
|
+
this.p++;
|
|
94
|
+
return t.value;
|
|
95
|
+
}
|
|
96
|
+
throw this.error(`Expected identifier but found '${t.value || 'end of file'}'`);
|
|
97
|
+
}
|
|
98
|
+
semicolon() {
|
|
99
|
+
if (this.eat(';'))
|
|
100
|
+
return;
|
|
101
|
+
if (this.is('}') || this.t.kind === 'eof' || this.t.nl)
|
|
102
|
+
return;
|
|
103
|
+
throw this.error(`Expected ';' but found '${this.t.value}'`);
|
|
104
|
+
}
|
|
105
|
+
// -------------------------------------------------------------------------
|
|
106
|
+
// Compilation unit
|
|
107
|
+
// -------------------------------------------------------------------------
|
|
108
|
+
parseUnit() {
|
|
109
|
+
const unit = { package: '', imports: [], useNamespaces: [], classes: [], functions: [] };
|
|
110
|
+
if (this.is('package')) {
|
|
111
|
+
this.next();
|
|
112
|
+
if (!this.is('{'))
|
|
113
|
+
unit.package = this.dottedName();
|
|
114
|
+
this.expect('{');
|
|
115
|
+
this.parseDirectives(unit, '}');
|
|
116
|
+
this.expect('}');
|
|
117
|
+
}
|
|
118
|
+
this.parseDirectives(unit, '');
|
|
119
|
+
return unit;
|
|
120
|
+
}
|
|
121
|
+
dottedName(allowStar = false) {
|
|
122
|
+
let name = this.ident(true);
|
|
123
|
+
while (this.is('.')) {
|
|
124
|
+
this.next();
|
|
125
|
+
if (allowStar && this.eat('*'))
|
|
126
|
+
return name + '.*';
|
|
127
|
+
name += '.' + this.ident(true);
|
|
128
|
+
}
|
|
129
|
+
return name;
|
|
130
|
+
}
|
|
131
|
+
parseDirectives(unit, end) {
|
|
132
|
+
while (this.t.kind !== 'eof' && !(end && this.is(end))) {
|
|
133
|
+
if (this.eat(';'))
|
|
134
|
+
continue;
|
|
135
|
+
if (this.is('import')) {
|
|
136
|
+
this.next();
|
|
137
|
+
unit.imports.push(this.dottedName(true));
|
|
138
|
+
this.semicolon();
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (this.is('use')) {
|
|
142
|
+
this.next();
|
|
143
|
+
if (!this.isId('namespace') && !this.is('namespace'))
|
|
144
|
+
throw this.error("Expected 'namespace'");
|
|
145
|
+
this.next();
|
|
146
|
+
unit.useNamespaces.push(this.dottedName());
|
|
147
|
+
this.semicolon();
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const metadata = this.parseMetadataList();
|
|
151
|
+
const mods = this.parseModifiers();
|
|
152
|
+
if (this.is('class') || this.is('interface')) {
|
|
153
|
+
unit.classes.push(this.parseClass(mods, metadata));
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (this.is('function')) {
|
|
157
|
+
const start = this.t.start;
|
|
158
|
+
const m = this.parseMethodMember(mods, metadata, '', start);
|
|
159
|
+
unit.functions.push(m);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (this.isId('namespace')) {
|
|
163
|
+
// namespace declarations at package level: skip
|
|
164
|
+
this.next();
|
|
165
|
+
this.ident();
|
|
166
|
+
if (this.eat('='))
|
|
167
|
+
this.parseAssign();
|
|
168
|
+
this.semicolon();
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
throw this.error(`Unexpected '${this.t.value}' at package level`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
parseMetadataList() {
|
|
175
|
+
const list = [];
|
|
176
|
+
while (this.is('[') && this.peek().kind === 'id') {
|
|
177
|
+
this.next();
|
|
178
|
+
const name = this.ident();
|
|
179
|
+
const items = [];
|
|
180
|
+
if (this.eat('(')) {
|
|
181
|
+
while (!this.is(')')) {
|
|
182
|
+
if ((this.t.kind === 'id' || this.t.kind === 'kw') && this.peek().value === '=') {
|
|
183
|
+
const key = this.next().value;
|
|
184
|
+
this.next();
|
|
185
|
+
items.push({ key, value: this.metaValue() });
|
|
186
|
+
}
|
|
187
|
+
else
|
|
188
|
+
items.push({ value: this.metaValue() });
|
|
189
|
+
if (!this.eat(','))
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
this.expect(')');
|
|
193
|
+
}
|
|
194
|
+
this.expect(']');
|
|
195
|
+
list.push({ name, items });
|
|
196
|
+
}
|
|
197
|
+
return list;
|
|
198
|
+
}
|
|
199
|
+
metaValue() {
|
|
200
|
+
const t = this.next();
|
|
201
|
+
return t.value;
|
|
202
|
+
}
|
|
203
|
+
parseModifiers() {
|
|
204
|
+
const mods = [];
|
|
205
|
+
for (;;) {
|
|
206
|
+
const t = this.t;
|
|
207
|
+
if ((t.kind === 'kw' || t.kind === 'id') && MODIFIERS.has(t.value)) {
|
|
208
|
+
mods.push(t.value);
|
|
209
|
+
this.next();
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
// Custom namespace modifier: `mx_internal function ...`
|
|
213
|
+
if (t.kind === 'id' && !this.peek().nl && (this.peek().value === 'function' || this.peek().value === 'var' || this.peek().value === 'const' || MODIFIERS.has(this.peek().value))) {
|
|
214
|
+
mods.push(`ns:${t.value}`);
|
|
215
|
+
this.next();
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
return mods;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
accessOf(mods) {
|
|
222
|
+
for (const m of mods)
|
|
223
|
+
if (m === 'public' || m === 'private' || m === 'protected' || m === 'internal' || m.startsWith('ns:'))
|
|
224
|
+
return m;
|
|
225
|
+
return 'internal';
|
|
226
|
+
}
|
|
227
|
+
parseType() {
|
|
228
|
+
const pos = this.pos();
|
|
229
|
+
if (this.eat('*'))
|
|
230
|
+
return { name: '*', pos };
|
|
231
|
+
if (this.is('void')) {
|
|
232
|
+
this.next();
|
|
233
|
+
return { name: 'void', pos };
|
|
234
|
+
}
|
|
235
|
+
let name = this.ident(true);
|
|
236
|
+
while (this.is('.') && (this.peek().kind === 'id' || this.peek().kind === 'kw')) {
|
|
237
|
+
this.next();
|
|
238
|
+
name += '.' + this.ident(true);
|
|
239
|
+
}
|
|
240
|
+
if (this.eat('.<')) {
|
|
241
|
+
const params = [this.parseType()];
|
|
242
|
+
while (this.eat(','))
|
|
243
|
+
params.push(this.parseType());
|
|
244
|
+
this.expect('>');
|
|
245
|
+
return { name, params, pos };
|
|
246
|
+
}
|
|
247
|
+
return { name, pos };
|
|
248
|
+
}
|
|
249
|
+
parseClass(mods, metadata) {
|
|
250
|
+
const pos = this.pos();
|
|
251
|
+
const isInterface = this.next().value === 'interface';
|
|
252
|
+
const name = this.ident();
|
|
253
|
+
const cls = {
|
|
254
|
+
name,
|
|
255
|
+
isInterface,
|
|
256
|
+
isDynamic: mods.includes('dynamic'),
|
|
257
|
+
isFinal: mods.includes('final'),
|
|
258
|
+
access: this.accessOf(mods),
|
|
259
|
+
implements: [],
|
|
260
|
+
members: [],
|
|
261
|
+
metadata,
|
|
262
|
+
pos,
|
|
263
|
+
};
|
|
264
|
+
if (this.eat('extends')) {
|
|
265
|
+
if (isInterface) {
|
|
266
|
+
cls.implements.push(this.parseType());
|
|
267
|
+
while (this.eat(','))
|
|
268
|
+
cls.implements.push(this.parseType());
|
|
269
|
+
}
|
|
270
|
+
else
|
|
271
|
+
cls.extends = this.parseType();
|
|
272
|
+
}
|
|
273
|
+
if (this.eat('implements')) {
|
|
274
|
+
cls.implements.push(this.parseType());
|
|
275
|
+
while (this.eat(','))
|
|
276
|
+
cls.implements.push(this.parseType());
|
|
277
|
+
}
|
|
278
|
+
this.expect('{');
|
|
279
|
+
while (!this.is('}')) {
|
|
280
|
+
if (this.t.kind === 'eof')
|
|
281
|
+
throw this.error('Unexpected end of file in class body');
|
|
282
|
+
cls.members.push(...this.parseMember(name));
|
|
283
|
+
}
|
|
284
|
+
this.expect('}');
|
|
285
|
+
return cls;
|
|
286
|
+
}
|
|
287
|
+
parseMember(className) {
|
|
288
|
+
if (this.eat(';'))
|
|
289
|
+
return [];
|
|
290
|
+
const start = this.t.start;
|
|
291
|
+
const metadata = this.parseMetadataList();
|
|
292
|
+
const mods = this.parseModifiers();
|
|
293
|
+
if (this.is('function'))
|
|
294
|
+
return [this.parseMethodMember(mods, metadata, className, start)];
|
|
295
|
+
if (this.is('var') || this.is('const')) {
|
|
296
|
+
const isConst = this.next().value === 'const';
|
|
297
|
+
const out = [];
|
|
298
|
+
do {
|
|
299
|
+
const pos = this.pos();
|
|
300
|
+
const name = this.ident(true);
|
|
301
|
+
const type = this.eat(':') ? this.parseType() : undefined;
|
|
302
|
+
const init = this.eat('=') ? this.parseAssign() : undefined;
|
|
303
|
+
out.push({ k: 'field', name, isStatic: mods.includes('static'), isConst, access: this.accessOf(mods), type, init, metadata, pos, span: [start, 0] });
|
|
304
|
+
} while (this.eat(','));
|
|
305
|
+
this.semicolon();
|
|
306
|
+
for (const f of out)
|
|
307
|
+
f.span[1] = this.toks[this.p - 1].end;
|
|
308
|
+
return out;
|
|
309
|
+
}
|
|
310
|
+
if (this.is('{') && mods.length === 0) {
|
|
311
|
+
const pos = this.pos();
|
|
312
|
+
const body = this.parseBlock();
|
|
313
|
+
return [{ k: 'static', body, pos, span: [start, this.toks[this.p - 1].end] }];
|
|
314
|
+
}
|
|
315
|
+
if (this.isId('namespace')) {
|
|
316
|
+
this.next();
|
|
317
|
+
this.ident();
|
|
318
|
+
if (this.eat('='))
|
|
319
|
+
this.parseAssign();
|
|
320
|
+
this.semicolon();
|
|
321
|
+
return [];
|
|
322
|
+
}
|
|
323
|
+
// Loose statements in a class body run in the static initialiser.
|
|
324
|
+
const pos = this.pos();
|
|
325
|
+
const stmt = this.parseStatement();
|
|
326
|
+
return [{ k: 'static', body: [stmt], pos, span: [start, this.toks[this.p - 1].end] }];
|
|
327
|
+
}
|
|
328
|
+
parseMethodMember(mods, metadata, className, start) {
|
|
329
|
+
const pos = this.pos();
|
|
330
|
+
this.expect('function');
|
|
331
|
+
let kind = 'method';
|
|
332
|
+
if ((this.isId('get') || this.isId('set')) && (this.peek().kind === 'id' || this.peek().kind === 'kw') && this.peek().value !== '(') {
|
|
333
|
+
kind = this.next().value;
|
|
334
|
+
}
|
|
335
|
+
const name = this.ident(true);
|
|
336
|
+
if (name === className && kind === 'method' && !mods.includes('static'))
|
|
337
|
+
kind = 'constructor';
|
|
338
|
+
const fn = this.parseFunctionRest(name, mods.includes('native'));
|
|
339
|
+
return {
|
|
340
|
+
k: 'method',
|
|
341
|
+
name,
|
|
342
|
+
kind,
|
|
343
|
+
isStatic: mods.includes('static'),
|
|
344
|
+
isOverride: mods.includes('override'),
|
|
345
|
+
isFinal: mods.includes('final'),
|
|
346
|
+
isNative: mods.includes('native'),
|
|
347
|
+
access: this.accessOf(mods),
|
|
348
|
+
fn,
|
|
349
|
+
metadata,
|
|
350
|
+
pos,
|
|
351
|
+
span: [start, this.toks[this.p - 1].end],
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
/** Parses `(params):Type { body }` (body optional for interfaces / native). */
|
|
355
|
+
parseFunctionRest(name, noBody = false) {
|
|
356
|
+
const pos = this.pos();
|
|
357
|
+
this.expect('(');
|
|
358
|
+
const params = [];
|
|
359
|
+
let rest;
|
|
360
|
+
while (!this.is(')')) {
|
|
361
|
+
if (this.eat('...')) {
|
|
362
|
+
const rname = this.ident(true);
|
|
363
|
+
rest = { name: rname, type: this.eat(':') ? this.parseType() : undefined };
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
const pname = this.ident(true);
|
|
367
|
+
const type = this.eat(':') ? this.parseType() : undefined;
|
|
368
|
+
const init = this.eat('=') ? this.parseAssign() : undefined;
|
|
369
|
+
params.push({ name: pname, type, init });
|
|
370
|
+
if (!this.eat(','))
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
this.expect(')');
|
|
374
|
+
const returnType = this.eat(':') ? this.parseType() : undefined;
|
|
375
|
+
let body;
|
|
376
|
+
if (this.is('{') && !noBody)
|
|
377
|
+
body = this.parseBlock();
|
|
378
|
+
else
|
|
379
|
+
this.semicolon();
|
|
380
|
+
return { name, params, rest, returnType, body, pos };
|
|
381
|
+
}
|
|
382
|
+
// -------------------------------------------------------------------------
|
|
383
|
+
// Statements
|
|
384
|
+
// -------------------------------------------------------------------------
|
|
385
|
+
parseBlock() {
|
|
386
|
+
this.expect('{');
|
|
387
|
+
const body = [];
|
|
388
|
+
while (!this.is('}')) {
|
|
389
|
+
if (this.t.kind === 'eof')
|
|
390
|
+
throw this.error("Expected '}'");
|
|
391
|
+
body.push(this.parseStatement());
|
|
392
|
+
}
|
|
393
|
+
this.expect('}');
|
|
394
|
+
return body;
|
|
395
|
+
}
|
|
396
|
+
parseVarDecls(noIn = false) {
|
|
397
|
+
const decls = [];
|
|
398
|
+
do {
|
|
399
|
+
const pos = this.pos();
|
|
400
|
+
const name = this.ident(true);
|
|
401
|
+
const type = this.eat(':') ? this.parseType() : undefined;
|
|
402
|
+
const init = this.eat('=') ? this.parseAssign(noIn) : undefined;
|
|
403
|
+
decls.push({ name, type, init, pos });
|
|
404
|
+
} while (this.eat(','));
|
|
405
|
+
return decls;
|
|
406
|
+
}
|
|
407
|
+
parseStatement() {
|
|
408
|
+
const pos = this.pos();
|
|
409
|
+
const t = this.t;
|
|
410
|
+
if (t.kind === 'punct') {
|
|
411
|
+
if (t.value === '{')
|
|
412
|
+
return { k: 'block', body: this.parseBlock(), pos };
|
|
413
|
+
if (t.value === ';') {
|
|
414
|
+
this.next();
|
|
415
|
+
return { k: 'empty', pos };
|
|
416
|
+
}
|
|
417
|
+
if (t.value === '[') {
|
|
418
|
+
// Metadata inside function bodies is ignored; otherwise an array expression.
|
|
419
|
+
const save = this.p;
|
|
420
|
+
if (this.peek().kind === 'id') {
|
|
421
|
+
try {
|
|
422
|
+
this.parseMetadataList();
|
|
423
|
+
if (this.is('function') || this.is('var'))
|
|
424
|
+
return this.parseStatement();
|
|
425
|
+
}
|
|
426
|
+
catch {
|
|
427
|
+
/* not metadata */
|
|
428
|
+
}
|
|
429
|
+
this.p = save;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (t.kind === 'kw') {
|
|
434
|
+
switch (t.value) {
|
|
435
|
+
case 'var':
|
|
436
|
+
case 'const': {
|
|
437
|
+
this.next();
|
|
438
|
+
const decls = this.parseVarDecls();
|
|
439
|
+
this.semicolon();
|
|
440
|
+
return { k: 'var', isConst: t.value === 'const', decls, pos };
|
|
441
|
+
}
|
|
442
|
+
case 'function': {
|
|
443
|
+
if (this.peek().kind === 'id') {
|
|
444
|
+
this.next();
|
|
445
|
+
const name = this.ident();
|
|
446
|
+
return { k: 'function', fn: this.parseFunctionRest(name), pos };
|
|
447
|
+
}
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
case 'if': {
|
|
451
|
+
this.next();
|
|
452
|
+
this.expect('(');
|
|
453
|
+
const c = this.parseExpr();
|
|
454
|
+
this.expect(')');
|
|
455
|
+
const then = this.parseStatement();
|
|
456
|
+
const els = this.eat('else') ? this.parseStatement() : undefined;
|
|
457
|
+
return { k: 'if', c, then, else: els, pos };
|
|
458
|
+
}
|
|
459
|
+
case 'while': {
|
|
460
|
+
this.next();
|
|
461
|
+
this.expect('(');
|
|
462
|
+
const c = this.parseExpr();
|
|
463
|
+
this.expect(')');
|
|
464
|
+
return { k: 'while', c, body: this.parseStatement(), pos };
|
|
465
|
+
}
|
|
466
|
+
case 'do': {
|
|
467
|
+
this.next();
|
|
468
|
+
const body = this.parseStatement();
|
|
469
|
+
this.expect('while');
|
|
470
|
+
this.expect('(');
|
|
471
|
+
const c = this.parseExpr();
|
|
472
|
+
this.expect(')');
|
|
473
|
+
this.eat(';');
|
|
474
|
+
return { k: 'dowhile', c, body, pos };
|
|
475
|
+
}
|
|
476
|
+
case 'for':
|
|
477
|
+
return this.parseFor();
|
|
478
|
+
case 'switch':
|
|
479
|
+
return this.parseSwitch();
|
|
480
|
+
case 'try':
|
|
481
|
+
return this.parseTry();
|
|
482
|
+
case 'return': {
|
|
483
|
+
this.next();
|
|
484
|
+
let e;
|
|
485
|
+
if (!this.is(';') && !this.is('}') && !this.t.nl && this.t.kind !== 'eof')
|
|
486
|
+
e = this.parseExpr();
|
|
487
|
+
this.semicolon();
|
|
488
|
+
return { k: 'return', e, pos };
|
|
489
|
+
}
|
|
490
|
+
case 'throw': {
|
|
491
|
+
this.next();
|
|
492
|
+
const e = this.parseExpr();
|
|
493
|
+
this.semicolon();
|
|
494
|
+
return { k: 'throw', e, pos };
|
|
495
|
+
}
|
|
496
|
+
case 'break':
|
|
497
|
+
case 'continue': {
|
|
498
|
+
this.next();
|
|
499
|
+
let label;
|
|
500
|
+
if (this.t.kind === 'id' && !this.t.nl)
|
|
501
|
+
label = this.next().value;
|
|
502
|
+
this.semicolon();
|
|
503
|
+
return { k: t.value, label, pos };
|
|
504
|
+
}
|
|
505
|
+
case 'with': {
|
|
506
|
+
this.next();
|
|
507
|
+
this.expect('(');
|
|
508
|
+
const obj = this.parseExpr();
|
|
509
|
+
this.expect(')');
|
|
510
|
+
return { k: 'with', obj, body: this.parseStatement(), pos };
|
|
511
|
+
}
|
|
512
|
+
case 'default': {
|
|
513
|
+
// default xml namespace = expr
|
|
514
|
+
if (this.peek().value === 'xml') {
|
|
515
|
+
this.next();
|
|
516
|
+
this.next();
|
|
517
|
+
if (!this.isId('namespace') && !this.is('namespace'))
|
|
518
|
+
throw this.error("Expected 'namespace'");
|
|
519
|
+
this.next();
|
|
520
|
+
this.expect('=');
|
|
521
|
+
const e = this.parseAssign();
|
|
522
|
+
this.semicolon();
|
|
523
|
+
return { k: 'dxns', e, pos };
|
|
524
|
+
}
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
case 'use': {
|
|
528
|
+
this.next();
|
|
529
|
+
this.next();
|
|
530
|
+
this.dottedName();
|
|
531
|
+
this.semicolon();
|
|
532
|
+
return { k: 'empty', pos };
|
|
533
|
+
}
|
|
534
|
+
case 'import': {
|
|
535
|
+
this.next();
|
|
536
|
+
this.dottedName(true);
|
|
537
|
+
this.semicolon();
|
|
538
|
+
return { k: 'empty', pos };
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (t.kind === 'id' && this.peek().value === ':' && this.peek().kind === 'punct') {
|
|
543
|
+
this.next();
|
|
544
|
+
this.next();
|
|
545
|
+
return { k: 'labeled', label: t.value, body: this.parseStatement(), pos };
|
|
546
|
+
}
|
|
547
|
+
const e = this.parseExpr();
|
|
548
|
+
this.semicolon();
|
|
549
|
+
return { k: 'expr', e, pos };
|
|
550
|
+
}
|
|
551
|
+
parseFor() {
|
|
552
|
+
const pos = this.pos();
|
|
553
|
+
this.expect('for');
|
|
554
|
+
let each = false;
|
|
555
|
+
if (this.isId('each')) {
|
|
556
|
+
this.next();
|
|
557
|
+
each = true;
|
|
558
|
+
}
|
|
559
|
+
this.expect('(');
|
|
560
|
+
let init;
|
|
561
|
+
let decl;
|
|
562
|
+
let target;
|
|
563
|
+
if (this.is('var') || this.is('const')) {
|
|
564
|
+
const kpos = this.pos();
|
|
565
|
+
const isConst = this.next().value === 'const';
|
|
566
|
+
const decls = this.parseVarDecls(true);
|
|
567
|
+
if (this.is('in') && decls.length === 1)
|
|
568
|
+
decl = decls[0];
|
|
569
|
+
else
|
|
570
|
+
init = { k: 'var', isConst, decls, pos: kpos };
|
|
571
|
+
}
|
|
572
|
+
else if (!this.is(';')) {
|
|
573
|
+
const e = this.parseExpr(true);
|
|
574
|
+
if (this.is('in'))
|
|
575
|
+
target = e;
|
|
576
|
+
else
|
|
577
|
+
init = { k: 'expr', e, pos: e.pos };
|
|
578
|
+
}
|
|
579
|
+
if (this.eat('in')) {
|
|
580
|
+
const obj = this.parseExpr();
|
|
581
|
+
this.expect(')');
|
|
582
|
+
return { k: 'forin', each, decl, target, obj, body: this.parseStatement(), pos };
|
|
583
|
+
}
|
|
584
|
+
if (each)
|
|
585
|
+
throw this.error("Expected 'in' in for each");
|
|
586
|
+
this.expect(';');
|
|
587
|
+
const c = this.is(';') ? undefined : this.parseExpr();
|
|
588
|
+
this.expect(';');
|
|
589
|
+
const update = this.is(')') ? undefined : this.parseExpr();
|
|
590
|
+
this.expect(')');
|
|
591
|
+
return { k: 'for', init, c, update, body: this.parseStatement(), pos };
|
|
592
|
+
}
|
|
593
|
+
parseSwitch() {
|
|
594
|
+
const pos = this.pos();
|
|
595
|
+
this.expect('switch');
|
|
596
|
+
this.expect('(');
|
|
597
|
+
const disc = this.parseExpr();
|
|
598
|
+
this.expect(')');
|
|
599
|
+
this.expect('{');
|
|
600
|
+
const cases = [];
|
|
601
|
+
while (!this.is('}')) {
|
|
602
|
+
let test;
|
|
603
|
+
if (this.eat('default'))
|
|
604
|
+
test = null;
|
|
605
|
+
else {
|
|
606
|
+
this.expect('case');
|
|
607
|
+
test = this.parseExpr();
|
|
608
|
+
}
|
|
609
|
+
this.expect(':');
|
|
610
|
+
const body = [];
|
|
611
|
+
while (!this.is('case') && !this.is('default') && !this.is('}'))
|
|
612
|
+
body.push(this.parseStatement());
|
|
613
|
+
cases.push({ test, body });
|
|
614
|
+
}
|
|
615
|
+
this.expect('}');
|
|
616
|
+
return { k: 'switch', disc, cases, pos };
|
|
617
|
+
}
|
|
618
|
+
parseTry() {
|
|
619
|
+
const pos = this.pos();
|
|
620
|
+
this.expect('try');
|
|
621
|
+
const body = this.parseBlock();
|
|
622
|
+
const catches = [];
|
|
623
|
+
while (this.eat('catch')) {
|
|
624
|
+
this.expect('(');
|
|
625
|
+
const name = this.ident(true);
|
|
626
|
+
const type = this.eat(':') ? this.parseType() : undefined;
|
|
627
|
+
this.expect(')');
|
|
628
|
+
catches.push({ name, type, body: this.parseBlock() });
|
|
629
|
+
}
|
|
630
|
+
const fin = this.eat('finally') ? this.parseBlock() : undefined;
|
|
631
|
+
if (!catches.length && !fin)
|
|
632
|
+
throw this.error("Expected 'catch' or 'finally'");
|
|
633
|
+
return { k: 'try', body, catches, finally: fin, pos };
|
|
634
|
+
}
|
|
635
|
+
// -------------------------------------------------------------------------
|
|
636
|
+
// Expressions
|
|
637
|
+
// -------------------------------------------------------------------------
|
|
638
|
+
parseExpr(noIn = false) {
|
|
639
|
+
const first = this.parseAssign(noIn);
|
|
640
|
+
if (!this.is(','))
|
|
641
|
+
return first;
|
|
642
|
+
const list = [first];
|
|
643
|
+
while (this.eat(','))
|
|
644
|
+
list.push(this.parseAssign(noIn));
|
|
645
|
+
return { k: 'comma', list, pos: first.pos };
|
|
646
|
+
}
|
|
647
|
+
parseAssign(noIn = false) {
|
|
648
|
+
const pos = this.pos();
|
|
649
|
+
const left = this.parseConditional(noIn);
|
|
650
|
+
if (this.t.kind === 'punct' && ASSIGN_OPS.has(this.t.value)) {
|
|
651
|
+
const op = this.next().value;
|
|
652
|
+
if (!isAssignable(left))
|
|
653
|
+
throw new CompileError('Invalid assignment target', pos.line, pos.col, this.source);
|
|
654
|
+
const v = this.parseAssign(noIn);
|
|
655
|
+
return { k: 'assign', op: op === '=' ? '' : op.slice(0, -1), target: left, v, pos };
|
|
656
|
+
}
|
|
657
|
+
return left;
|
|
658
|
+
}
|
|
659
|
+
parseConditional(noIn) {
|
|
660
|
+
const c = this.parseBinary(0, noIn);
|
|
661
|
+
if (!this.eat('?'))
|
|
662
|
+
return c;
|
|
663
|
+
const t = this.parseAssign();
|
|
664
|
+
this.expect(':');
|
|
665
|
+
const f = this.parseAssign(noIn);
|
|
666
|
+
return { k: 'cond', c, t, f, pos: c.pos };
|
|
667
|
+
}
|
|
668
|
+
binaryOp(noIn) {
|
|
669
|
+
const t = this.t;
|
|
670
|
+
if (t.kind !== 'punct' && t.kind !== 'kw')
|
|
671
|
+
return undefined;
|
|
672
|
+
if (noIn && t.value === 'in')
|
|
673
|
+
return undefined;
|
|
674
|
+
return BINARY_PREC[t.value] !== undefined ? t.value : undefined;
|
|
675
|
+
}
|
|
676
|
+
parseBinary(minPrec, noIn) {
|
|
677
|
+
let left = this.parseUnary();
|
|
678
|
+
for (;;) {
|
|
679
|
+
const op = this.binaryOp(noIn);
|
|
680
|
+
if (!op)
|
|
681
|
+
return left;
|
|
682
|
+
const prec = BINARY_PREC[op];
|
|
683
|
+
if (prec <= minPrec)
|
|
684
|
+
return left;
|
|
685
|
+
this.next();
|
|
686
|
+
if (op === 'as' || op === 'is') {
|
|
687
|
+
// Right side is a type expression (may be '*' or a Vector type).
|
|
688
|
+
if (this.is('*') || this.is('void')) {
|
|
689
|
+
const tpos = this.pos();
|
|
690
|
+
this.next();
|
|
691
|
+
left = { k: 'binary', op, a: left, b: { k: 'id', name: '*', pos: tpos }, pos: left.pos };
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
const right = this.parseBinary(prec, noIn);
|
|
696
|
+
left = { k: 'binary', op, a: left, b: right, pos: left.pos };
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
parseUnary() {
|
|
700
|
+
const pos = this.pos();
|
|
701
|
+
const t = this.t;
|
|
702
|
+
if (t.kind === 'punct' && (t.value === '!' || t.value === '~' || t.value === '-' || t.value === '+')) {
|
|
703
|
+
this.next();
|
|
704
|
+
const e = this.parseUnary();
|
|
705
|
+
if (t.value === '-' && e.k === 'num')
|
|
706
|
+
return { k: 'num', v: -e.v, pos };
|
|
707
|
+
return { k: 'unary', op: t.value, e, pos };
|
|
708
|
+
}
|
|
709
|
+
if (t.kind === 'punct' && (t.value === '++' || t.value === '--')) {
|
|
710
|
+
this.next();
|
|
711
|
+
const e = this.parseUnary();
|
|
712
|
+
return { k: 'update', op: t.value, prefix: true, e, pos };
|
|
713
|
+
}
|
|
714
|
+
if (t.kind === 'kw' && (t.value === 'typeof' || t.value === 'void' || t.value === 'delete')) {
|
|
715
|
+
this.next();
|
|
716
|
+
return { k: 'unary', op: t.value, e: this.parseUnary(), pos };
|
|
717
|
+
}
|
|
718
|
+
const e = this.parsePostfix();
|
|
719
|
+
return e;
|
|
720
|
+
}
|
|
721
|
+
parsePostfix() {
|
|
722
|
+
const e = this.parseLeftHandSide();
|
|
723
|
+
if ((this.is('++') || this.is('--')) && !this.t.nl) {
|
|
724
|
+
const op = this.next().value;
|
|
725
|
+
return { k: 'update', op, prefix: false, e, pos: e.pos };
|
|
726
|
+
}
|
|
727
|
+
return e;
|
|
728
|
+
}
|
|
729
|
+
parseArgs() {
|
|
730
|
+
this.expect('(');
|
|
731
|
+
const args = [];
|
|
732
|
+
while (!this.is(')')) {
|
|
733
|
+
args.push(this.parseAssign());
|
|
734
|
+
if (!this.eat(','))
|
|
735
|
+
break;
|
|
736
|
+
}
|
|
737
|
+
this.expect(')');
|
|
738
|
+
return args;
|
|
739
|
+
}
|
|
740
|
+
parseLeftHandSide() {
|
|
741
|
+
let e;
|
|
742
|
+
const pos = this.pos();
|
|
743
|
+
if (this.is('new')) {
|
|
744
|
+
this.next();
|
|
745
|
+
if (this.is('<')) {
|
|
746
|
+
// Vector literal: new <T>[a, b]
|
|
747
|
+
this.next();
|
|
748
|
+
const type = this.parseType();
|
|
749
|
+
this.expect('>');
|
|
750
|
+
const arr = this.parsePrimary();
|
|
751
|
+
if (arr.k !== 'array')
|
|
752
|
+
throw this.error('Expected array literal after vector type');
|
|
753
|
+
e = { k: 'vector', type, items: arr.items.map((x) => x ?? { k: 'undefined', pos }), pos };
|
|
754
|
+
}
|
|
755
|
+
else {
|
|
756
|
+
const ctor = this.parseMemberOnly();
|
|
757
|
+
const args = this.is('(') ? this.parseArgs() : [];
|
|
758
|
+
e = { k: 'new', ctor, args, pos };
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
else {
|
|
762
|
+
e = this.parsePrimary();
|
|
763
|
+
}
|
|
764
|
+
return this.parseSuffixes(e, true);
|
|
765
|
+
}
|
|
766
|
+
/** Member expression without call suffixes (for `new X.Y(args)`). */
|
|
767
|
+
parseMemberOnly() {
|
|
768
|
+
if (this.is('new'))
|
|
769
|
+
return this.parseLeftHandSide();
|
|
770
|
+
return this.parseSuffixes(this.parsePrimary(), false);
|
|
771
|
+
}
|
|
772
|
+
parseSuffixes(e, allowCalls) {
|
|
773
|
+
for (;;) {
|
|
774
|
+
const pos = e.pos;
|
|
775
|
+
if (this.is('.')) {
|
|
776
|
+
this.next();
|
|
777
|
+
if (this.eat('@')) {
|
|
778
|
+
if (this.eat('[')) {
|
|
779
|
+
const index = this.parseExpr();
|
|
780
|
+
this.expect(']');
|
|
781
|
+
e = { k: 'index', obj: e, index, attr: true, pos };
|
|
782
|
+
}
|
|
783
|
+
else
|
|
784
|
+
e = { k: 'member', obj: e, name: this.is('*') ? (this.next(), '*') : this.ident(true), attr: true, pos };
|
|
785
|
+
continue;
|
|
786
|
+
}
|
|
787
|
+
if (this.is('(')) {
|
|
788
|
+
this.next();
|
|
789
|
+
const cond = this.parseExpr();
|
|
790
|
+
this.expect(')');
|
|
791
|
+
e = { k: 'filter', obj: e, cond, pos };
|
|
792
|
+
continue;
|
|
793
|
+
}
|
|
794
|
+
const name = this.is('*') ? (this.next(), '*') : this.ident(true);
|
|
795
|
+
if (this.eat('::')) {
|
|
796
|
+
const local = this.ident(true);
|
|
797
|
+
e = { k: 'member', obj: e, name: local, ns: name, pos };
|
|
798
|
+
}
|
|
799
|
+
else
|
|
800
|
+
e = { k: 'member', obj: e, name, pos };
|
|
801
|
+
continue;
|
|
802
|
+
}
|
|
803
|
+
if (this.is('..')) {
|
|
804
|
+
this.next();
|
|
805
|
+
const attr = this.eat('@');
|
|
806
|
+
e = { k: 'descendants', obj: e, name: this.is('*') ? (this.next(), '*') : this.ident(true), attr, pos };
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
if (this.is('.<')) {
|
|
810
|
+
this.next();
|
|
811
|
+
const params = [this.parseType()];
|
|
812
|
+
while (this.eat(','))
|
|
813
|
+
params.push(this.parseType());
|
|
814
|
+
this.expect('>');
|
|
815
|
+
e = { k: 'typeapply', base: e, params, pos };
|
|
816
|
+
continue;
|
|
817
|
+
}
|
|
818
|
+
if (this.is('[')) {
|
|
819
|
+
this.next();
|
|
820
|
+
const index = this.parseExpr();
|
|
821
|
+
this.expect(']');
|
|
822
|
+
e = { k: 'index', obj: e, index, pos };
|
|
823
|
+
continue;
|
|
824
|
+
}
|
|
825
|
+
if (allowCalls && this.is('(')) {
|
|
826
|
+
e = { k: 'call', fn: e, args: this.parseArgs(), pos };
|
|
827
|
+
continue;
|
|
828
|
+
}
|
|
829
|
+
return e;
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
parsePrimary() {
|
|
833
|
+
const t = this.t;
|
|
834
|
+
const pos = this.pos();
|
|
835
|
+
switch (t.kind) {
|
|
836
|
+
case 'num':
|
|
837
|
+
this.next();
|
|
838
|
+
return { k: 'num', v: t.num, pos };
|
|
839
|
+
case 'str':
|
|
840
|
+
this.next();
|
|
841
|
+
return { k: 'str', v: t.value, pos };
|
|
842
|
+
case 'regex':
|
|
843
|
+
this.next();
|
|
844
|
+
return { k: 'regex', pattern: t.value, flags: t.flags ?? '', pos };
|
|
845
|
+
case 'id': {
|
|
846
|
+
this.next();
|
|
847
|
+
if (t.value === 'undefined')
|
|
848
|
+
return { k: 'undefined', pos };
|
|
849
|
+
if (this.is('::')) {
|
|
850
|
+
this.next();
|
|
851
|
+
return { k: 'id', name: this.ident(true), ns: t.value, pos };
|
|
852
|
+
}
|
|
853
|
+
return { k: 'id', name: t.value, pos };
|
|
854
|
+
}
|
|
855
|
+
case 'kw':
|
|
856
|
+
switch (t.value) {
|
|
857
|
+
case 'this':
|
|
858
|
+
this.next();
|
|
859
|
+
return { k: 'this', pos };
|
|
860
|
+
case 'super':
|
|
861
|
+
this.next();
|
|
862
|
+
return { k: 'super', pos };
|
|
863
|
+
case 'true':
|
|
864
|
+
case 'false':
|
|
865
|
+
this.next();
|
|
866
|
+
return { k: 'bool', v: t.value === 'true', pos };
|
|
867
|
+
case 'null':
|
|
868
|
+
this.next();
|
|
869
|
+
return { k: 'null', pos };
|
|
870
|
+
case 'function': {
|
|
871
|
+
this.next();
|
|
872
|
+
const name = this.t.kind === 'id' ? this.next().value : undefined;
|
|
873
|
+
return { k: 'function', fn: this.parseFunctionRest(name), pos };
|
|
874
|
+
}
|
|
875
|
+
case 'public':
|
|
876
|
+
case 'private':
|
|
877
|
+
case 'protected':
|
|
878
|
+
case 'internal':
|
|
879
|
+
if (this.peek().value === '::') {
|
|
880
|
+
this.next();
|
|
881
|
+
this.next();
|
|
882
|
+
return { k: 'id', name: this.ident(true), ns: t.value, pos };
|
|
883
|
+
}
|
|
884
|
+
break;
|
|
885
|
+
}
|
|
886
|
+
break;
|
|
887
|
+
case 'punct':
|
|
888
|
+
switch (t.value) {
|
|
889
|
+
case '(': {
|
|
890
|
+
this.next();
|
|
891
|
+
const e = this.parseExpr();
|
|
892
|
+
this.expect(')');
|
|
893
|
+
return e;
|
|
894
|
+
}
|
|
895
|
+
case '[': {
|
|
896
|
+
this.next();
|
|
897
|
+
const items = [];
|
|
898
|
+
while (!this.is(']')) {
|
|
899
|
+
if (this.is(',')) {
|
|
900
|
+
this.next();
|
|
901
|
+
items.push(null);
|
|
902
|
+
continue;
|
|
903
|
+
}
|
|
904
|
+
items.push(this.parseAssign());
|
|
905
|
+
if (!this.eat(','))
|
|
906
|
+
break;
|
|
907
|
+
}
|
|
908
|
+
this.expect(']');
|
|
909
|
+
return { k: 'array', items, pos };
|
|
910
|
+
}
|
|
911
|
+
case '{': {
|
|
912
|
+
this.next();
|
|
913
|
+
const props = [];
|
|
914
|
+
while (!this.is('}')) {
|
|
915
|
+
const kt = this.next();
|
|
916
|
+
let key;
|
|
917
|
+
if (kt.kind === 'str' || kt.kind === 'id' || kt.kind === 'kw')
|
|
918
|
+
key = kt.value;
|
|
919
|
+
else if (kt.kind === 'num')
|
|
920
|
+
key = kt.num;
|
|
921
|
+
else
|
|
922
|
+
throw this.error('Expected property name', kt);
|
|
923
|
+
this.expect(':');
|
|
924
|
+
props.push({ key, value: this.parseAssign() });
|
|
925
|
+
if (!this.eat(','))
|
|
926
|
+
break;
|
|
927
|
+
}
|
|
928
|
+
this.expect('}');
|
|
929
|
+
return { k: 'object', props, pos };
|
|
930
|
+
}
|
|
931
|
+
case '@': {
|
|
932
|
+
this.next();
|
|
933
|
+
const name = this.is('*') ? (this.next(), '*') : this.ident(true);
|
|
934
|
+
return { k: 'member', obj: { k: 'this', pos }, name, attr: true, pos };
|
|
935
|
+
}
|
|
936
|
+
case '<':
|
|
937
|
+
return this.parseXmlLiteral();
|
|
938
|
+
}
|
|
939
|
+
break;
|
|
940
|
+
}
|
|
941
|
+
throw this.error(`Unexpected '${t.value || 'end of file'}'`);
|
|
942
|
+
}
|
|
943
|
+
/** Very small XML literal support: the literal is kept as text and parsed at runtime with `new XML(...)`. */
|
|
944
|
+
parseXmlLiteral() {
|
|
945
|
+
const pos = this.pos();
|
|
946
|
+
const start = this.t.start;
|
|
947
|
+
// Scan the raw source for the matching close tag.
|
|
948
|
+
let depth = 0;
|
|
949
|
+
let i = start;
|
|
950
|
+
const src = this.src;
|
|
951
|
+
while (i < src.length) {
|
|
952
|
+
if (src.startsWith('<!--', i)) {
|
|
953
|
+
i = src.indexOf('-->', i) + 3;
|
|
954
|
+
continue;
|
|
955
|
+
}
|
|
956
|
+
if (src[i] === '<') {
|
|
957
|
+
const close = src.indexOf('>', i);
|
|
958
|
+
if (close < 0)
|
|
959
|
+
break;
|
|
960
|
+
const tag = src.slice(i, close + 1);
|
|
961
|
+
if (tag.startsWith('</'))
|
|
962
|
+
depth--;
|
|
963
|
+
else if (!tag.endsWith('/>') && !tag.startsWith('<?'))
|
|
964
|
+
depth++;
|
|
965
|
+
i = close + 1;
|
|
966
|
+
if (depth === 0)
|
|
967
|
+
break;
|
|
968
|
+
continue;
|
|
969
|
+
}
|
|
970
|
+
i++;
|
|
971
|
+
}
|
|
972
|
+
if (depth !== 0)
|
|
973
|
+
throw this.error('Unterminated XML literal');
|
|
974
|
+
while (this.t.kind !== 'eof' && this.t.start < i)
|
|
975
|
+
this.next();
|
|
976
|
+
return { k: 'xml', text: src.slice(start, i), pos };
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
function isAssignable(e) {
|
|
980
|
+
return e.k === 'id' || e.k === 'member' || e.k === 'index' || e.k === 'descendants';
|
|
981
|
+
}
|
|
982
|
+
export function parseAs3(src, source) {
|
|
983
|
+
return new Parser(src, source).parseUnit();
|
|
984
|
+
}
|