@yannosay/sinth 4.0.0 → 5.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/CHANGELOG.md +83 -0
- package/DOCS.md +1359 -0
- package/README.md +83 -74
- package/bin/sinth.js +2 -1
- package/dist/cli/args.d.ts +11 -0
- package/dist/cli/args.js +74 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +437 -0
- package/dist/cli/templates.d.ts +10 -0
- package/dist/cli/templates.js +193 -0
- package/dist/cli/ui.d.ts +28 -0
- package/dist/cli/ui.js +61 -0
- package/dist/cli/update-check.d.ts +2 -0
- package/dist/cli/update-check.js +124 -0
- package/dist/compiler/ast.d.ts +315 -0
- package/dist/compiler/ast.js +4 -0
- package/dist/compiler/codegen.d.ts +143 -0
- package/dist/compiler/codegen.js +1326 -0
- package/dist/compiler/diagnostics.d.ts +36 -0
- package/dist/compiler/diagnostics.js +142 -0
- package/dist/compiler/elements.d.ts +14 -0
- package/dist/compiler/elements.js +78 -0
- package/dist/compiler/globals.d.ts +2 -0
- package/dist/compiler/globals.js +27 -0
- package/dist/compiler/html.d.ts +37 -0
- package/dist/compiler/html.js +152 -0
- package/dist/compiler/lexer.d.ts +42 -0
- package/dist/compiler/lexer.js +404 -0
- package/dist/compiler/parser.d.ts +95 -0
- package/dist/compiler/parser.js +1374 -0
- package/dist/compiler/prerender.d.ts +72 -0
- package/dist/compiler/prerender.js +910 -0
- package/dist/compiler/script-analysis.d.ts +6 -0
- package/dist/compiler/script-analysis.js +106 -0
- package/dist/compiler/style.d.ts +39 -0
- package/dist/compiler/style.js +463 -0
- package/dist/deploy/cloudflare.d.ts +9 -0
- package/dist/deploy/cloudflare.js +111 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +23 -0
- package/dist/project/build.d.ts +31 -0
- package/dist/project/build.js +425 -0
- package/dist/project/config.d.ts +33 -0
- package/dist/project/config.js +142 -0
- package/dist/project/project.d.ts +74 -0
- package/dist/project/project.js +535 -0
- package/dist/runtime/index.d.ts +68 -0
- package/dist/runtime/index.js +912 -0
- package/dist/server/client.d.ts +1 -0
- package/dist/server/client.js +112 -0
- package/dist/server/dev.d.ts +8 -0
- package/dist/server/dev.js +244 -0
- package/dist/server/preview.d.ts +6 -0
- package/dist/server/preview.js +106 -0
- package/dist/server/static.d.ts +10 -0
- package/dist/server/static.js +136 -0
- package/lib/ui/index.sinth +436 -0
- package/package.json +33 -15
- package/schema/sinth.config.schema.json +54 -0
- package/dist/browser/compile.js +0 -62
- package/dist/cli.js +0 -509
- package/dist/core/builtins.js +0 -117
- package/dist/core/cli-compiler.js +0 -683
- package/dist/core/compiler.js +0 -1428
- package/dist/core/expr.js +0 -125
- package/dist/core/head-builder.js +0 -130
- package/dist/core/lexer.js +0 -414
- package/dist/core/parser.js +0 -1665
- package/dist/core/runtime/custom-element.js +0 -81
- package/dist/core/runtime/functions.js +0 -99
- package/dist/core/runtime/helpers.js +0 -347
- package/dist/core/runtime/render.js +0 -136
- package/dist/core/style-processor.js +0 -317
- package/dist/core/types.js +0 -93
- package/dist/resolver.js +0 -175
- package/dist/server.js +0 -174
- package/dist/update-check.js +0 -109
- package/dist/utils.js +0 -80
|
@@ -0,0 +1,1374 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Parser = exports.META_KEYS = void 0;
|
|
4
|
+
exports.isMetaKey = isMetaKey;
|
|
5
|
+
exports.isStatementExpr = isStatementExpr;
|
|
6
|
+
exports.classifyImport = classifyImport;
|
|
7
|
+
exports.parseSinth = parseSinth;
|
|
8
|
+
const ast_1 = require("./ast");
|
|
9
|
+
const diagnostics_1 = require("./diagnostics");
|
|
10
|
+
const lexer_1 = require("./lexer");
|
|
11
|
+
exports.META_KEYS = new Set([
|
|
12
|
+
"title", "descr", "description", "fav", "favicon", "lang", "charset", "viewport", "author", "keywords", "robots",
|
|
13
|
+
"themeColor", "colorScheme", "canonical", "generator", "manifest", "appleTouchIcon", "base", "bodyClass", "htmlClass", "paths",
|
|
14
|
+
]);
|
|
15
|
+
function isMetaKey(key) {
|
|
16
|
+
return exports.META_KEYS.has(key) || /^(og|twitter)[A-Z]/.test(key);
|
|
17
|
+
}
|
|
18
|
+
const CAPITALIZED_JS_CALLABLES = new Set([
|
|
19
|
+
"String", "Number", "Boolean", "BigInt", "Symbol", "Array", "Object", "Date", "Error", "RegExp", "Promise",
|
|
20
|
+
]);
|
|
21
|
+
const CAPITALIZED_JS_VALUES = new Set(["Infinity", "NaN"]);
|
|
22
|
+
const RESERVED = new Set([
|
|
23
|
+
"break", "case", "catch", "class", "const", "continue", "debugger", "default", "delete", "do", "else", "enum", "export",
|
|
24
|
+
"extends", "false", "finally", "for", "function", "if", "import", "in", "instanceof", "new", "null", "return", "super",
|
|
25
|
+
"switch", "this", "throw", "true", "try", "typeof", "var", "void", "while", "with", "yield", "let", "static", "await",
|
|
26
|
+
"implements", "interface", "package", "private", "protected", "public", "and", "or", "not", "arguments", "eval",
|
|
27
|
+
]);
|
|
28
|
+
const ASSIGN_OPS = new Set(["=", "+=", "-=", "*=", "/=", "%=", "**=", "??=", "&&=", "||="]);
|
|
29
|
+
class Parser {
|
|
30
|
+
source;
|
|
31
|
+
file;
|
|
32
|
+
pos = 0;
|
|
33
|
+
tokens;
|
|
34
|
+
constructor(source, file) {
|
|
35
|
+
this.source = source;
|
|
36
|
+
this.file = file;
|
|
37
|
+
(0, diagnostics_1.registerSource)(file, source);
|
|
38
|
+
this.tokens = (0, lexer_1.tokenize)(source, file);
|
|
39
|
+
}
|
|
40
|
+
static fromTokens(tokens, file) {
|
|
41
|
+
const p = Object.create(Parser.prototype);
|
|
42
|
+
Object.assign(p, { pos: 0, tokens, file, source: "" });
|
|
43
|
+
return p;
|
|
44
|
+
}
|
|
45
|
+
peek(offset = 0) {
|
|
46
|
+
return this.tokens[Math.min(this.pos + offset, this.tokens.length - 1)];
|
|
47
|
+
}
|
|
48
|
+
get tok() {
|
|
49
|
+
return this.peek();
|
|
50
|
+
}
|
|
51
|
+
next() {
|
|
52
|
+
const t = this.tokens[this.pos];
|
|
53
|
+
if (this.pos < this.tokens.length - 1)
|
|
54
|
+
this.pos++;
|
|
55
|
+
return t;
|
|
56
|
+
}
|
|
57
|
+
isPunct(value, offset = 0) {
|
|
58
|
+
const t = this.peek(offset);
|
|
59
|
+
return t.kind === "punct" && t.value === value;
|
|
60
|
+
}
|
|
61
|
+
isIdent(value, offset = 0) {
|
|
62
|
+
const t = this.peek(offset);
|
|
63
|
+
return t.kind === "ident" && (value === undefined || t.value === value);
|
|
64
|
+
}
|
|
65
|
+
eat(value) {
|
|
66
|
+
if (this.isPunct(value)) {
|
|
67
|
+
this.next();
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
eatIdent(value) {
|
|
73
|
+
if (this.isIdent(value)) {
|
|
74
|
+
this.next();
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
describe(t) {
|
|
80
|
+
if (t.kind === "eof")
|
|
81
|
+
return "end of file";
|
|
82
|
+
if (t.kind === "string")
|
|
83
|
+
return `string "${t.value}"`;
|
|
84
|
+
if (t.kind === "raw")
|
|
85
|
+
return "block";
|
|
86
|
+
return `'${t.value}'`;
|
|
87
|
+
}
|
|
88
|
+
fail(message, t = this.tok, hint, code = "E0100") {
|
|
89
|
+
throw new diagnostics_1.SinthError(code, message, { ...t.loc, length: Math.max(1, t.end - t.start) }, hint);
|
|
90
|
+
}
|
|
91
|
+
expect(value, context) {
|
|
92
|
+
if (!this.isPunct(value)) {
|
|
93
|
+
this.fail(`Expected '${value}'${context ? " " + context : ""}, found ${this.describe(this.tok)}`);
|
|
94
|
+
}
|
|
95
|
+
return this.next();
|
|
96
|
+
}
|
|
97
|
+
expectIdent(context) {
|
|
98
|
+
if (this.tok.kind !== "ident")
|
|
99
|
+
this.fail(`Expected ${context}, found ${this.describe(this.tok)}`);
|
|
100
|
+
return this.next();
|
|
101
|
+
}
|
|
102
|
+
sameLine() {
|
|
103
|
+
return !this.tok.newlineBefore;
|
|
104
|
+
}
|
|
105
|
+
adjacent() {
|
|
106
|
+
const prev = this.tokens[this.pos - 1];
|
|
107
|
+
return !!prev && prev.end === this.tok.start;
|
|
108
|
+
}
|
|
109
|
+
parseFile() {
|
|
110
|
+
const file = {
|
|
111
|
+
path: this.file, source: this.source, isPage: false, imports: [], meta: [], externs: [], vars: [], functions: [],
|
|
112
|
+
components: [], styles: [], scripts: [], lifecycle: [], watchers: [], body: [],
|
|
113
|
+
};
|
|
114
|
+
if (this.isIdent("page")) {
|
|
115
|
+
this.next();
|
|
116
|
+
file.isPage = true;
|
|
117
|
+
}
|
|
118
|
+
const declared = new Set();
|
|
119
|
+
while (this.tok.kind !== "eof") {
|
|
120
|
+
const t = this.tok;
|
|
121
|
+
if (t.kind === "ident") {
|
|
122
|
+
switch (t.value) {
|
|
123
|
+
case "import":
|
|
124
|
+
file.imports.push(this.parseImport());
|
|
125
|
+
continue;
|
|
126
|
+
case "extern":
|
|
127
|
+
this.next();
|
|
128
|
+
do {
|
|
129
|
+
const n = this.expectIdent("a name after 'extern'");
|
|
130
|
+
file.externs.push({ name: n.value, loc: n.loc });
|
|
131
|
+
} while (this.eat(","));
|
|
132
|
+
continue;
|
|
133
|
+
case "private":
|
|
134
|
+
case "export":
|
|
135
|
+
if (this.isIdent("component", 1) || this.isIdent("custom-element", 1)) {
|
|
136
|
+
const exported = t.value === "export";
|
|
137
|
+
this.next();
|
|
138
|
+
const comp = this.isIdent("component") ? this.parseComponent() : this.parseLegacyCustomElement();
|
|
139
|
+
comp.exported = exported;
|
|
140
|
+
file.components.push(comp);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
break;
|
|
144
|
+
case "component":
|
|
145
|
+
file.components.push(this.parseComponent());
|
|
146
|
+
continue;
|
|
147
|
+
case "custom-element":
|
|
148
|
+
case "custom":
|
|
149
|
+
if (this.peek(1).kind === "ident") {
|
|
150
|
+
file.components.push(this.parseLegacyCustomElement());
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
default:
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
if (this.tryParseOwnerItem(file, declared))
|
|
158
|
+
continue;
|
|
159
|
+
if (t.value !== "var" && this.isPunct("=", 1) && !declared.has(t.value) && isMetaKey(t.value)) {
|
|
160
|
+
this.next();
|
|
161
|
+
this.next();
|
|
162
|
+
file.meta.push({ key: t.value, value: this.parseExpr(), loc: t.loc });
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
file.body.push(...this.parseNode(file));
|
|
167
|
+
}
|
|
168
|
+
return file;
|
|
169
|
+
}
|
|
170
|
+
tryParseOwnerItem(owner, declared) {
|
|
171
|
+
const t = this.tok;
|
|
172
|
+
if (t.kind !== "ident")
|
|
173
|
+
return false;
|
|
174
|
+
if (t.value === "var" || t.value === "const" || t.value === "computed" || (t.value === "persist" && this.isIdent("var", 1))) {
|
|
175
|
+
const decl = this.parseVarDecl();
|
|
176
|
+
owner.vars.push(decl);
|
|
177
|
+
declared?.add(decl.name);
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
if (t.value === "function" || (t.value === "async" && this.isIdent("function", 1))) {
|
|
181
|
+
owner.functions.push(this.parseFunction());
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
if (t.value === "style" && this.peekPastModifiersIsRaw()) {
|
|
185
|
+
owner.styles.push(this.parseStyle());
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
if (t.value === "script" && this.peekPastModifiersIsRaw()) {
|
|
189
|
+
owner.scripts.push(this.parseScript());
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
if ((t.value === "onMount" || t.value === "onDestroy") && this.isPunct("{", 1)) {
|
|
193
|
+
this.next();
|
|
194
|
+
owner.lifecycle.push({ kind: t.value === "onMount" ? "mount" : "destroy", body: this.parseStmtBlock(), loc: t.loc });
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
if (t.value === "watch" && !this.peek(1).newlineBefore && (this.peek(1).kind !== "punct" || this.isPunct("(", 1))) {
|
|
198
|
+
return this.parseWatch(owner);
|
|
199
|
+
}
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
parseWatch(owner) {
|
|
203
|
+
const t = this.next();
|
|
204
|
+
const expr = this.parseExpr();
|
|
205
|
+
owner.watchers.push({ expr, body: this.parseStmtBlock(), loc: t.loc });
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
peekPastModifiersIsRaw() {
|
|
209
|
+
for (let i = 1; i < 32; i++) {
|
|
210
|
+
const t = this.peek(i);
|
|
211
|
+
if (t.kind === "raw")
|
|
212
|
+
return true;
|
|
213
|
+
if (t.kind === "eof" || (t.kind === "punct" && t.value !== "="))
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
parseImport() {
|
|
219
|
+
const start = this.next();
|
|
220
|
+
const loc = start.loc;
|
|
221
|
+
const decl = { kind: "module", source: "", named: [], attrs: {}, sideEffect: false, loc };
|
|
222
|
+
let forced;
|
|
223
|
+
if (this.isIdent("css") || this.isIdent("style")) {
|
|
224
|
+
this.next();
|
|
225
|
+
forced = "style";
|
|
226
|
+
}
|
|
227
|
+
else if (this.isIdent("js") || this.isIdent("script")) {
|
|
228
|
+
this.next();
|
|
229
|
+
forced = "script";
|
|
230
|
+
}
|
|
231
|
+
else if (this.isIdent("font")) {
|
|
232
|
+
this.next();
|
|
233
|
+
forced = "font";
|
|
234
|
+
}
|
|
235
|
+
if (this.tok.kind === "string") {
|
|
236
|
+
decl.source = this.next().value;
|
|
237
|
+
decl.sideEffect = true;
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
if (this.eat("*")) {
|
|
241
|
+
if (!this.eatIdent("as"))
|
|
242
|
+
this.fail("Expected 'as' after '*' in import");
|
|
243
|
+
decl.namespace = this.expectIdent("a namespace name").value;
|
|
244
|
+
}
|
|
245
|
+
else if (this.isPunct("{")) {
|
|
246
|
+
decl.named = this.parseImportSpecifiers();
|
|
247
|
+
}
|
|
248
|
+
else if (this.tok.kind === "ident" && !this.isIdent("from")) {
|
|
249
|
+
const name = this.next().value;
|
|
250
|
+
if (this.isIdent("as")) {
|
|
251
|
+
this.next();
|
|
252
|
+
decl.defaultName = this.expectIdent("an alias").value;
|
|
253
|
+
decl.named.push({ name, alias: decl.defaultName, loc });
|
|
254
|
+
decl.defaultName = undefined;
|
|
255
|
+
if (!this.isIdent("from")) {
|
|
256
|
+
decl.source = name;
|
|
257
|
+
decl.kind = "library";
|
|
258
|
+
return decl;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
decl.defaultName = name;
|
|
263
|
+
}
|
|
264
|
+
if (this.eat(",")) {
|
|
265
|
+
if (this.eat("*")) {
|
|
266
|
+
this.eatIdent("as");
|
|
267
|
+
decl.namespace = this.expectIdent("a namespace name").value;
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
decl.named.push(...this.parseImportSpecifiers());
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (!this.isIdent("from")) {
|
|
274
|
+
decl.source = name;
|
|
275
|
+
decl.defaultName = undefined;
|
|
276
|
+
decl.kind = "library";
|
|
277
|
+
decl.sideEffect = true;
|
|
278
|
+
return decl;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (!this.eatIdent("from"))
|
|
282
|
+
this.fail(`Expected 'from' in import, found ${this.describe(this.tok)}`);
|
|
283
|
+
if (this.peek().kind !== "string")
|
|
284
|
+
this.fail("Expected a module path string after 'from'");
|
|
285
|
+
decl.source = this.next().value;
|
|
286
|
+
}
|
|
287
|
+
let alias;
|
|
288
|
+
if (this.isIdent("as") && decl.sideEffect) {
|
|
289
|
+
this.next();
|
|
290
|
+
alias = this.expectIdent("an alias").value;
|
|
291
|
+
decl.sideEffect = false;
|
|
292
|
+
}
|
|
293
|
+
if (this.isPunct("{") && this.sameLine())
|
|
294
|
+
decl.attrs = this.parseImportAttrs();
|
|
295
|
+
decl.kind = forced ?? classifyImport(decl.source);
|
|
296
|
+
if (alias) {
|
|
297
|
+
if (decl.kind === "sinth")
|
|
298
|
+
decl.defaultName = alias;
|
|
299
|
+
else
|
|
300
|
+
decl.namespace = alias;
|
|
301
|
+
}
|
|
302
|
+
return decl;
|
|
303
|
+
}
|
|
304
|
+
parseImportSpecifiers() {
|
|
305
|
+
const out = [];
|
|
306
|
+
this.expect("{");
|
|
307
|
+
while (!this.isPunct("}")) {
|
|
308
|
+
const t = this.expectIdent("an imported name");
|
|
309
|
+
let alias = t.value;
|
|
310
|
+
if (this.eatIdent("as"))
|
|
311
|
+
alias = this.expectIdent("an alias").value;
|
|
312
|
+
out.push({ name: t.value, alias, loc: t.loc });
|
|
313
|
+
if (!this.eat(","))
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
this.expect("}", "to close the import list");
|
|
317
|
+
return out;
|
|
318
|
+
}
|
|
319
|
+
parseImportAttrs() {
|
|
320
|
+
const attrs = {};
|
|
321
|
+
this.expect("{");
|
|
322
|
+
while (!this.isPunct("}")) {
|
|
323
|
+
const key = this.expectIdent("an attribute name").value;
|
|
324
|
+
if (this.eat(":")) {
|
|
325
|
+
const v = this.next();
|
|
326
|
+
attrs[key] = v.kind === "string" ? v.value : v.value === "true" ? true : v.value === "false" ? false : v.value;
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
attrs[key] = true;
|
|
330
|
+
}
|
|
331
|
+
this.eat(",");
|
|
332
|
+
}
|
|
333
|
+
this.expect("}");
|
|
334
|
+
return attrs;
|
|
335
|
+
}
|
|
336
|
+
parseTypeRef() {
|
|
337
|
+
const t = this.expectIdent("a type");
|
|
338
|
+
if (!ast_1.TYPE_NAMES.has(t.value)) {
|
|
339
|
+
this.fail(`Unknown type '${t.value}'`, t, "Valid types: str, num, bool, obj, ui, any, void (add [] for arrays, ? for nullable)", "E0201");
|
|
340
|
+
}
|
|
341
|
+
const ref = { base: t.value, array: 0, nullable: false };
|
|
342
|
+
while (this.isPunct("[") && this.isPunct("]", 1) && this.adjacent()) {
|
|
343
|
+
this.next();
|
|
344
|
+
this.next();
|
|
345
|
+
ref.array++;
|
|
346
|
+
}
|
|
347
|
+
if (this.isPunct("?") && this.adjacent()) {
|
|
348
|
+
this.next();
|
|
349
|
+
ref.nullable = true;
|
|
350
|
+
}
|
|
351
|
+
return ref;
|
|
352
|
+
}
|
|
353
|
+
looksLikeType(offset = 0) {
|
|
354
|
+
const t = this.peek(offset);
|
|
355
|
+
return t.kind === "ident" && ast_1.TYPE_NAMES.has(t.value);
|
|
356
|
+
}
|
|
357
|
+
parseVarDecl() {
|
|
358
|
+
const start = this.tok;
|
|
359
|
+
let persist = false;
|
|
360
|
+
if (this.isIdent("persist")) {
|
|
361
|
+
this.next();
|
|
362
|
+
persist = true;
|
|
363
|
+
}
|
|
364
|
+
const kindTok = this.next();
|
|
365
|
+
const kind = kindTok.value === "const" ? "const" : kindTok.value === "computed" ? "computed" : "var";
|
|
366
|
+
let type;
|
|
367
|
+
if (this.looksLikeType() && (this.peek(1).kind === "ident" || (this.isPunct("[", 1) && this.isPunct("]", 2)) || this.isPunct("?", 1))) {
|
|
368
|
+
type = this.parseTypeRef();
|
|
369
|
+
}
|
|
370
|
+
const nameTok = this.expectIdent("a variable name");
|
|
371
|
+
this.checkName(nameTok);
|
|
372
|
+
if (!type && this.isPunct(":")) {
|
|
373
|
+
this.next();
|
|
374
|
+
type = this.parseTypeRef();
|
|
375
|
+
}
|
|
376
|
+
let init;
|
|
377
|
+
if (this.eat("="))
|
|
378
|
+
init = this.parseExpr();
|
|
379
|
+
if (kind !== "var" && !init)
|
|
380
|
+
this.fail(`'${kind}' declarations need an initial value`, nameTok);
|
|
381
|
+
this.eat(";");
|
|
382
|
+
return { kind, name: nameTok.value, type, init, persist, loc: start.loc };
|
|
383
|
+
}
|
|
384
|
+
checkName(t) {
|
|
385
|
+
if (RESERVED.has(t.value))
|
|
386
|
+
this.fail(`'${t.value}' is a reserved word and cannot be used as a name`, t, undefined, "E0102");
|
|
387
|
+
if (t.value.startsWith("$$"))
|
|
388
|
+
this.fail(`Names starting with '$$' are reserved for the Sinth runtime`, t, undefined, "E0102");
|
|
389
|
+
}
|
|
390
|
+
parseParams() {
|
|
391
|
+
const params = [];
|
|
392
|
+
if (!this.eat("("))
|
|
393
|
+
return params;
|
|
394
|
+
while (!this.isPunct(")")) {
|
|
395
|
+
const start = this.tok;
|
|
396
|
+
const rest = this.eat("...");
|
|
397
|
+
const bindable = this.isIdent("bind") && this.peek(1).kind === "ident" && !this.isPunct(":", 1);
|
|
398
|
+
if (bindable)
|
|
399
|
+
this.next();
|
|
400
|
+
let type;
|
|
401
|
+
if (this.looksLikeType() && (this.peek(1).kind === "ident" || (this.isPunct("[", 1) && this.isPunct("]", 2)) || this.isPunct("?", 1))) {
|
|
402
|
+
type = this.parseTypeRef();
|
|
403
|
+
}
|
|
404
|
+
const nameTok = this.expectIdent("a parameter name");
|
|
405
|
+
this.checkName(nameTok);
|
|
406
|
+
let optional = false;
|
|
407
|
+
if (this.isPunct("?") && this.adjacent()) {
|
|
408
|
+
this.next();
|
|
409
|
+
optional = true;
|
|
410
|
+
}
|
|
411
|
+
if (!type && this.eat(":"))
|
|
412
|
+
type = this.parseTypeRef();
|
|
413
|
+
let defaultValue;
|
|
414
|
+
if (this.eat("="))
|
|
415
|
+
defaultValue = this.parseExpr();
|
|
416
|
+
if (type?.nullable && !defaultValue)
|
|
417
|
+
optional = true;
|
|
418
|
+
params.push({ name: nameTok.value, type, defaultValue, optional: optional || !!defaultValue, rest, bindable, loc: start.loc });
|
|
419
|
+
if (!this.eat(","))
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
this.expect(")", "to close the parameter list");
|
|
423
|
+
return params;
|
|
424
|
+
}
|
|
425
|
+
parseFunction() {
|
|
426
|
+
const start = this.tok;
|
|
427
|
+
const isAsync = this.eatIdent("async");
|
|
428
|
+
this.next();
|
|
429
|
+
const nameTok = this.expectIdent("a function name");
|
|
430
|
+
this.checkName(nameTok);
|
|
431
|
+
if (/^[A-Z]/.test(nameTok.value)) {
|
|
432
|
+
this.fail(`Function '${nameTok.value}' must start with a lowercase letter`, nameTok, "Use 'component' for reusable UI with a capitalized name");
|
|
433
|
+
}
|
|
434
|
+
const params = this.parseParams();
|
|
435
|
+
let returnType;
|
|
436
|
+
if (this.eat("->") || this.eat(":"))
|
|
437
|
+
returnType = this.parseTypeRef();
|
|
438
|
+
const fn = { name: nameTok.value, params, returnType, body: [], async: isAsync, loc: start.loc };
|
|
439
|
+
if (returnType?.base === "ui") {
|
|
440
|
+
this.expect("{", "to open the function body");
|
|
441
|
+
const owner = emptyOwner();
|
|
442
|
+
fn.view = this.parseNodesUntilClose(owner, true);
|
|
443
|
+
this.expect("}", "to close the function body");
|
|
444
|
+
fn.body = owner.vars.map(decl => ({ k: "var", decl, loc: decl.loc }));
|
|
445
|
+
}
|
|
446
|
+
else {
|
|
447
|
+
fn.body = this.parseStmtBlock();
|
|
448
|
+
}
|
|
449
|
+
return fn;
|
|
450
|
+
}
|
|
451
|
+
parseComponent() {
|
|
452
|
+
const start = this.next();
|
|
453
|
+
const nameTok = this.expectIdent("a component name");
|
|
454
|
+
if (!/^[A-Z]/.test(nameTok.value))
|
|
455
|
+
this.fail(`Component '${nameTok.value}' must start with an uppercase letter`, nameTok);
|
|
456
|
+
const params = this.parseParams();
|
|
457
|
+
let customTag;
|
|
458
|
+
if (this.eatIdent("as"))
|
|
459
|
+
customTag = this.parseTagLiteral();
|
|
460
|
+
const comp = {
|
|
461
|
+
name: nameTok.value, params, vars: [], functions: [], body: [], styles: [], scripts: [], lifecycle: [], watchers: [],
|
|
462
|
+
customTag, exported: true, file: this.file, loc: start.loc,
|
|
463
|
+
};
|
|
464
|
+
this.expect("{", `to open the body of component '${nameTok.value}'`);
|
|
465
|
+
comp.body = this.parseNodesUntilClose(comp, false);
|
|
466
|
+
this.expect("}", `to close component '${nameTok.value}'`);
|
|
467
|
+
return comp;
|
|
468
|
+
}
|
|
469
|
+
parseTagLiteral() {
|
|
470
|
+
this.expect("<", "to start a tag name like <my-element/>");
|
|
471
|
+
let tag = this.expectIdent("a tag name").value;
|
|
472
|
+
while (this.isPunct("-") && this.adjacent()) {
|
|
473
|
+
this.next();
|
|
474
|
+
tag += "-" + this.expectIdent("a tag name segment").value;
|
|
475
|
+
}
|
|
476
|
+
this.eat("/");
|
|
477
|
+
this.expect(">", "to close the tag literal");
|
|
478
|
+
if (!/^[a-z][a-z0-9]*-[a-z0-9-]+$/.test(tag))
|
|
479
|
+
this.fail(`'${tag}' is not a valid custom element name (it must be lowercase and contain a dash)`);
|
|
480
|
+
return tag;
|
|
481
|
+
}
|
|
482
|
+
parseLegacyCustomElement() {
|
|
483
|
+
const start = this.next();
|
|
484
|
+
const nameTok = this.expectIdent("an element name");
|
|
485
|
+
let customTag;
|
|
486
|
+
const params = [];
|
|
487
|
+
if (this.eat("(")) {
|
|
488
|
+
while (!this.isPunct(")")) {
|
|
489
|
+
const key = this.expectIdent("an option").value;
|
|
490
|
+
if (key === "export") {
|
|
491
|
+
this.expect(":");
|
|
492
|
+
customTag = this.parseTagLiteral();
|
|
493
|
+
}
|
|
494
|
+
else {
|
|
495
|
+
params.push({ name: key, optional: true, rest: false, loc: this.tok.loc, defaultValue: this.eat("=") ? this.parseExpr() : undefined });
|
|
496
|
+
}
|
|
497
|
+
if (!this.eat(","))
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
this.expect(")");
|
|
501
|
+
}
|
|
502
|
+
const name = /^[A-Z]/.test(nameTok.value) ? nameTok.value : nameTok.value[0].toUpperCase() + nameTok.value.slice(1);
|
|
503
|
+
const comp = {
|
|
504
|
+
name, params, vars: [], functions: [], body: [], styles: [], scripts: [], lifecycle: [], watchers: [],
|
|
505
|
+
customTag: customTag ?? `sinth-${nameTok.value.toLowerCase()}`, exported: true, file: this.file, loc: start.loc,
|
|
506
|
+
};
|
|
507
|
+
if (this.tok.kind === "raw") {
|
|
508
|
+
const raw = this.next();
|
|
509
|
+
const sub = Parser.fromTokens((0, lexer_1.tokenize)(raw.value, this.file, raw.loc), this.file);
|
|
510
|
+
comp.body = sub.parseNodesUntilClose(comp, false);
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
this.expect("{");
|
|
514
|
+
comp.body = this.parseNodesUntilClose(comp, false);
|
|
515
|
+
this.expect("}");
|
|
516
|
+
}
|
|
517
|
+
return comp;
|
|
518
|
+
}
|
|
519
|
+
parseStyle() {
|
|
520
|
+
const start = this.next();
|
|
521
|
+
let global = false;
|
|
522
|
+
let lang = "sinth";
|
|
523
|
+
while (this.tok.kind !== "raw") {
|
|
524
|
+
if (this.eatIdent("global"))
|
|
525
|
+
global = true;
|
|
526
|
+
else if (this.eatIdent("scoped"))
|
|
527
|
+
global = false;
|
|
528
|
+
else if (this.isIdent("lang")) {
|
|
529
|
+
this.next();
|
|
530
|
+
this.expect("=");
|
|
531
|
+
const v = this.next();
|
|
532
|
+
if (v.value !== "css" && v.value !== "scss" && v.value !== "sinth")
|
|
533
|
+
this.fail(`Unsupported style language '${v.value}'`, v, "Use lang=\"css\", lang=\"scss\" or omit it");
|
|
534
|
+
lang = v.value;
|
|
535
|
+
}
|
|
536
|
+
else
|
|
537
|
+
this.fail(`Unexpected ${this.describe(this.tok)} in style block header`);
|
|
538
|
+
}
|
|
539
|
+
const raw = this.next();
|
|
540
|
+
return { global, lang, raw: raw.value, loc: { ...raw.loc, line: raw.loc.line, col: start.loc.col } };
|
|
541
|
+
}
|
|
542
|
+
parseScript() {
|
|
543
|
+
this.next();
|
|
544
|
+
const attrs = {};
|
|
545
|
+
while (this.tok.kind !== "raw") {
|
|
546
|
+
const key = this.expectIdent("a script attribute").value;
|
|
547
|
+
attrs[key] = this.eat("=") ? this.next().value : "true";
|
|
548
|
+
}
|
|
549
|
+
const raw = this.next();
|
|
550
|
+
return { raw: raw.value, attrs, loc: raw.loc };
|
|
551
|
+
}
|
|
552
|
+
parseNodesUntilClose(owner, allowReturn) {
|
|
553
|
+
const nodes = [];
|
|
554
|
+
while (!this.isPunct("}") && this.tok.kind !== "eof") {
|
|
555
|
+
if (this.tryParseOwnerItem(owner))
|
|
556
|
+
continue;
|
|
557
|
+
if (allowReturn && this.isIdent("return")) {
|
|
558
|
+
this.next();
|
|
559
|
+
if (this.isPunct("}"))
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
nodes.push(...this.parseNode(owner));
|
|
563
|
+
}
|
|
564
|
+
return nodes;
|
|
565
|
+
}
|
|
566
|
+
startsElement() {
|
|
567
|
+
const t = this.tok;
|
|
568
|
+
if (t.kind !== "ident" || !/^[A-Z]/.test(t.value))
|
|
569
|
+
return false;
|
|
570
|
+
if (CAPITALIZED_JS_VALUES.has(t.value))
|
|
571
|
+
return false;
|
|
572
|
+
if (this.isPunct(".", 1)) {
|
|
573
|
+
const after = this.peek(2);
|
|
574
|
+
return after.kind === "ident" && /^[A-Z]/.test(after.value);
|
|
575
|
+
}
|
|
576
|
+
if (CAPITALIZED_JS_CALLABLES.has(t.value) && this.isPunct("(", 1))
|
|
577
|
+
return false;
|
|
578
|
+
return true;
|
|
579
|
+
}
|
|
580
|
+
parseNode(owner) {
|
|
581
|
+
const t = this.tok;
|
|
582
|
+
if (t.kind === "ident") {
|
|
583
|
+
if (t.value === "if")
|
|
584
|
+
return [this.parseIfNode(owner)];
|
|
585
|
+
if (t.value === "for")
|
|
586
|
+
return [this.parseForNode(owner)];
|
|
587
|
+
if (t.value === "$slot")
|
|
588
|
+
return [this.parseSlotOutlet(owner)];
|
|
589
|
+
if (t.value === "var" || t.value === "const" || t.value === "computed") {
|
|
590
|
+
const decl = this.parseVarDecl();
|
|
591
|
+
return [{ k: "var", decl, loc: decl.loc }];
|
|
592
|
+
}
|
|
593
|
+
if (t.value === "slot" && this.isPunct("(", 1)) {
|
|
594
|
+
this.fail("'slot(...) { }' can only be used directly inside a component usage to fill a named slot");
|
|
595
|
+
}
|
|
596
|
+
if (this.startsElement())
|
|
597
|
+
return [this.parseElement(owner)];
|
|
598
|
+
}
|
|
599
|
+
if (t.kind === "raw")
|
|
600
|
+
this.fail("Unexpected block");
|
|
601
|
+
const startLoc = t.loc;
|
|
602
|
+
const expr = this.parseExpr();
|
|
603
|
+
this.eat(";");
|
|
604
|
+
if (isStatementExpr(expr))
|
|
605
|
+
return [{ k: "stmt", stmt: { k: "expr", expr, loc: startLoc }, loc: startLoc }];
|
|
606
|
+
const node = { k: "text", expr, loc: startLoc };
|
|
607
|
+
const next = this.tok;
|
|
608
|
+
const startsNode = next.kind === "ident" && (/^[A-Z]/.test(next.value) || ["if", "for", "var", "const", "computed", "$slot", "slot"].includes(next.value));
|
|
609
|
+
if (!next.newlineBefore && (next.kind === "string" || next.kind === "number" || next.kind === "template" || (next.kind === "ident" && !startsNode))) {
|
|
610
|
+
this.fail(`Unexpected ${this.describe(this.tok)} after expression`, this.tok, "Use '+' to join values, or put each child on its own line");
|
|
611
|
+
}
|
|
612
|
+
return [node];
|
|
613
|
+
}
|
|
614
|
+
parseSlotOutlet(owner) {
|
|
615
|
+
const t = this.next();
|
|
616
|
+
let name = "default";
|
|
617
|
+
let fallback = [];
|
|
618
|
+
if (this.isPunct("(") && this.adjacent()) {
|
|
619
|
+
this.next();
|
|
620
|
+
while (!this.isPunct(")")) {
|
|
621
|
+
const key = this.expectIdent("'name' or 'fallback'");
|
|
622
|
+
this.expect(":");
|
|
623
|
+
if (key.value === "name") {
|
|
624
|
+
const v = this.next();
|
|
625
|
+
if (v.kind !== "string")
|
|
626
|
+
this.fail("Slot names must be string literals", v);
|
|
627
|
+
name = v.value;
|
|
628
|
+
}
|
|
629
|
+
else if (key.value === "fallback") {
|
|
630
|
+
const e = this.parseExpr();
|
|
631
|
+
fallback = [{ k: "text", expr: e, loc: e.loc }];
|
|
632
|
+
}
|
|
633
|
+
else
|
|
634
|
+
this.fail(`Unknown slot option '${key.value}'`, key);
|
|
635
|
+
if (!this.eat(","))
|
|
636
|
+
break;
|
|
637
|
+
}
|
|
638
|
+
this.expect(")");
|
|
639
|
+
}
|
|
640
|
+
if (this.isPunct("{") && this.sameLine()) {
|
|
641
|
+
this.next();
|
|
642
|
+
fallback = this.parseNodesUntilClose(owner, false);
|
|
643
|
+
this.expect("}");
|
|
644
|
+
}
|
|
645
|
+
return { k: "slot", name, fallback, loc: t.loc };
|
|
646
|
+
}
|
|
647
|
+
parseElement(owner) {
|
|
648
|
+
const start = this.next();
|
|
649
|
+
let name = start.value;
|
|
650
|
+
while (this.isPunct(".") && this.peek(1).kind === "ident") {
|
|
651
|
+
this.next();
|
|
652
|
+
name += "." + this.next().value;
|
|
653
|
+
}
|
|
654
|
+
const el = { k: "element", name, attrs: [], children: [], slots: [], hasBody: false, loc: start.loc };
|
|
655
|
+
if (this.isPunct("(") && this.sameLine())
|
|
656
|
+
el.attrs = this.parseAttrs();
|
|
657
|
+
if (this.isPunct("{")) {
|
|
658
|
+
this.next();
|
|
659
|
+
el.hasBody = true;
|
|
660
|
+
while (!this.isPunct("}") && this.tok.kind !== "eof") {
|
|
661
|
+
if (this.isIdent("slot") && this.isPunct("(", 1)) {
|
|
662
|
+
el.slots.push(this.parseSlotFill(owner));
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
if (this.tryParseOwnerItem(owner))
|
|
666
|
+
continue;
|
|
667
|
+
el.children.push(...this.parseNode(owner));
|
|
668
|
+
}
|
|
669
|
+
this.expect("}", `to close '${name}'`);
|
|
670
|
+
}
|
|
671
|
+
return el;
|
|
672
|
+
}
|
|
673
|
+
parseSlotFill(owner) {
|
|
674
|
+
const t = this.next();
|
|
675
|
+
this.expect("(");
|
|
676
|
+
let name = "default";
|
|
677
|
+
while (!this.isPunct(")")) {
|
|
678
|
+
const key = this.expectIdent("'name'");
|
|
679
|
+
this.expect(":");
|
|
680
|
+
const v = this.next();
|
|
681
|
+
if (key.value !== "name" || v.kind !== "string")
|
|
682
|
+
this.fail("Expected slot(name: \"...\")", v);
|
|
683
|
+
name = v.value;
|
|
684
|
+
if (!this.eat(","))
|
|
685
|
+
break;
|
|
686
|
+
}
|
|
687
|
+
this.expect(")");
|
|
688
|
+
this.expect("{", "to open the slot content");
|
|
689
|
+
const children = this.parseNodesUntilClose(owner, false);
|
|
690
|
+
this.expect("}");
|
|
691
|
+
return { name, children, loc: t.loc };
|
|
692
|
+
}
|
|
693
|
+
parseAttrName() {
|
|
694
|
+
const first = this.tok;
|
|
695
|
+
if (first.kind !== "ident" && first.kind !== "string")
|
|
696
|
+
this.fail(`Expected an attribute name, found ${this.describe(first)}`);
|
|
697
|
+
this.next();
|
|
698
|
+
let full = first.value;
|
|
699
|
+
while (first.kind === "ident" && this.isPunct("-") && this.adjacent() && this.peek(1).kind === "ident" && this.peek(1).start === this.tok.end) {
|
|
700
|
+
this.next();
|
|
701
|
+
full += "-" + this.next().value;
|
|
702
|
+
}
|
|
703
|
+
return { full, loc: first.loc };
|
|
704
|
+
}
|
|
705
|
+
parseAttrs() {
|
|
706
|
+
const attrs = [];
|
|
707
|
+
this.expect("(");
|
|
708
|
+
while (!this.isPunct(")") && this.tok.kind !== "eof") {
|
|
709
|
+
if (this.isPunct("...")) {
|
|
710
|
+
const t = this.next();
|
|
711
|
+
attrs.push({ name: "...", modifiers: [], value: this.parseExpr(), loc: t.loc });
|
|
712
|
+
this.eat(",");
|
|
713
|
+
continue;
|
|
714
|
+
}
|
|
715
|
+
const nameTok = this.parseAttrName();
|
|
716
|
+
const modifiers = [];
|
|
717
|
+
while (this.isPunct(".") && this.adjacent()) {
|
|
718
|
+
this.next();
|
|
719
|
+
modifiers.push(this.expectIdent("an event modifier").value);
|
|
720
|
+
}
|
|
721
|
+
const attr = { name: nameTok.full, modifiers, value: null, loc: nameTok.loc };
|
|
722
|
+
if (this.eat(":")) {
|
|
723
|
+
if (/^on[A-Z]/.test(attr.name))
|
|
724
|
+
attr.handler = this.parseHandler();
|
|
725
|
+
else
|
|
726
|
+
attr.value = this.parseExpr();
|
|
727
|
+
}
|
|
728
|
+
attrs.push(attr);
|
|
729
|
+
this.eat(",");
|
|
730
|
+
}
|
|
731
|
+
this.expect(")", "to close the attribute list");
|
|
732
|
+
return attrs;
|
|
733
|
+
}
|
|
734
|
+
atAttrBoundary() {
|
|
735
|
+
if (this.isPunct(",") || this.isPunct(")"))
|
|
736
|
+
return true;
|
|
737
|
+
return this.tok.kind === "ident" && this.isPunct(":", 1) && !this.isIdent("else");
|
|
738
|
+
}
|
|
739
|
+
parseHandler() {
|
|
740
|
+
const stmts = [];
|
|
741
|
+
for (;;) {
|
|
742
|
+
if (this.atAttrBoundary())
|
|
743
|
+
break;
|
|
744
|
+
stmts.push(this.parseStmt());
|
|
745
|
+
if (!this.eat(";"))
|
|
746
|
+
break;
|
|
747
|
+
}
|
|
748
|
+
if (stmts.length === 0)
|
|
749
|
+
this.fail("Expected an event handler");
|
|
750
|
+
return stmts;
|
|
751
|
+
}
|
|
752
|
+
parseIfNode(owner) {
|
|
753
|
+
const start = this.next();
|
|
754
|
+
const node = { k: "if", branches: [], persist: false, loc: start.loc };
|
|
755
|
+
const firstTest = this.parseExpr();
|
|
756
|
+
this.parseIfModifiers(node);
|
|
757
|
+
node.branches.push({ test: firstTest, body: this.parseNodeBlock(owner), loc: start.loc });
|
|
758
|
+
for (;;) {
|
|
759
|
+
if (this.isIdent("elif")) {
|
|
760
|
+
const t = this.next();
|
|
761
|
+
const test = this.parseExpr();
|
|
762
|
+
this.parseIfModifiers(node);
|
|
763
|
+
node.branches.push({ test, body: this.parseNodeBlock(owner), loc: t.loc });
|
|
764
|
+
continue;
|
|
765
|
+
}
|
|
766
|
+
if (this.isIdent("else")) {
|
|
767
|
+
const t = this.next();
|
|
768
|
+
if (this.eatIdent("if")) {
|
|
769
|
+
const test = this.parseExpr();
|
|
770
|
+
this.parseIfModifiers(node);
|
|
771
|
+
node.branches.push({ test, body: this.parseNodeBlock(owner), loc: t.loc });
|
|
772
|
+
continue;
|
|
773
|
+
}
|
|
774
|
+
this.parseIfModifiers(node);
|
|
775
|
+
node.branches.push({ body: this.parseNodeBlock(owner), loc: t.loc });
|
|
776
|
+
}
|
|
777
|
+
break;
|
|
778
|
+
}
|
|
779
|
+
return node;
|
|
780
|
+
}
|
|
781
|
+
parseIfModifiers(node) {
|
|
782
|
+
for (;;) {
|
|
783
|
+
if (this.eatIdent("persist"))
|
|
784
|
+
node.persist = true;
|
|
785
|
+
else if (this.eatIdent("replace"))
|
|
786
|
+
continue;
|
|
787
|
+
else if (this.isIdent("delay") && this.isPunct("(", 1)) {
|
|
788
|
+
this.next();
|
|
789
|
+
this.next();
|
|
790
|
+
node.delay = this.parseExpr();
|
|
791
|
+
this.expect(")");
|
|
792
|
+
}
|
|
793
|
+
else if (this.isIdent("hide") && this.isPunct("(", 1)) {
|
|
794
|
+
this.next();
|
|
795
|
+
this.next();
|
|
796
|
+
const v = this.parseExpr();
|
|
797
|
+
this.expect(")");
|
|
798
|
+
if (v.k === "lit" && v.value === true)
|
|
799
|
+
node.persist = true;
|
|
800
|
+
}
|
|
801
|
+
else if (this.isIdent("transition") && this.isPunct("(", 1)) {
|
|
802
|
+
this.next();
|
|
803
|
+
this.next();
|
|
804
|
+
const v = this.next();
|
|
805
|
+
if (v.kind !== "string")
|
|
806
|
+
this.fail("transition(...) expects a string name like \"fade\"", v);
|
|
807
|
+
node.transition = v.value;
|
|
808
|
+
this.expect(")");
|
|
809
|
+
}
|
|
810
|
+
else
|
|
811
|
+
break;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
parseNodeBlock(owner) {
|
|
815
|
+
this.expect("{", "to open the block");
|
|
816
|
+
const nodes = this.parseNodesUntilClose(owner, false);
|
|
817
|
+
this.expect("}", "to close the block");
|
|
818
|
+
return nodes;
|
|
819
|
+
}
|
|
820
|
+
parseLoopHead() {
|
|
821
|
+
const paren = this.isPunct("(") && this.loopHeadIsParenthesized();
|
|
822
|
+
if (paren)
|
|
823
|
+
this.next();
|
|
824
|
+
const names = [];
|
|
825
|
+
do {
|
|
826
|
+
const n = this.expectIdent("a loop variable name");
|
|
827
|
+
this.checkName(n);
|
|
828
|
+
names.push(n.value);
|
|
829
|
+
} while (this.eat(","));
|
|
830
|
+
if (names.length > 3)
|
|
831
|
+
this.fail("A for loop can declare at most three names: item, index, key");
|
|
832
|
+
if (!this.eatIdent("in"))
|
|
833
|
+
this.fail(`Expected 'in' after loop variable${names.length > 1 ? "s" : ""}`);
|
|
834
|
+
const iter = this.parseExpr();
|
|
835
|
+
if (paren)
|
|
836
|
+
this.expect(")", "to close the loop header");
|
|
837
|
+
let keyExpr;
|
|
838
|
+
let filter;
|
|
839
|
+
for (;;) {
|
|
840
|
+
if (this.isIdent("key")) {
|
|
841
|
+
this.next();
|
|
842
|
+
keyExpr = this.parseExpr();
|
|
843
|
+
}
|
|
844
|
+
else if (this.isIdent("if")) {
|
|
845
|
+
this.next();
|
|
846
|
+
filter = this.parseExpr();
|
|
847
|
+
}
|
|
848
|
+
else
|
|
849
|
+
break;
|
|
850
|
+
}
|
|
851
|
+
return { names, iter, keyExpr, filter };
|
|
852
|
+
}
|
|
853
|
+
loopHeadIsParenthesized() {
|
|
854
|
+
let depth = 0;
|
|
855
|
+
for (let i = 0; i < 4096; i++) {
|
|
856
|
+
const t = this.peek(i);
|
|
857
|
+
if (t.kind === "eof")
|
|
858
|
+
return false;
|
|
859
|
+
if (t.kind === "punct" && (t.value === "(" || t.value === "[" || t.value === "{"))
|
|
860
|
+
depth++;
|
|
861
|
+
if (t.kind === "punct" && (t.value === ")" || t.value === "]" || t.value === "}"))
|
|
862
|
+
depth--;
|
|
863
|
+
if (depth === 1 && t.kind === "ident" && t.value === "in")
|
|
864
|
+
return true;
|
|
865
|
+
if (depth === 0)
|
|
866
|
+
return false;
|
|
867
|
+
}
|
|
868
|
+
return false;
|
|
869
|
+
}
|
|
870
|
+
parseForNode(owner) {
|
|
871
|
+
const start = this.next();
|
|
872
|
+
const head = this.parseLoopHead();
|
|
873
|
+
this.eatIdent("persist");
|
|
874
|
+
const node = { k: "for", ...head, body: this.parseNodeBlock(owner), loc: start.loc };
|
|
875
|
+
if (this.isIdent("else") && this.isPunct("{", 1)) {
|
|
876
|
+
this.next();
|
|
877
|
+
node.empty = this.parseNodeBlock(owner);
|
|
878
|
+
}
|
|
879
|
+
return node;
|
|
880
|
+
}
|
|
881
|
+
parseStmtBlock() {
|
|
882
|
+
this.expect("{", "to open the block");
|
|
883
|
+
const stmts = [];
|
|
884
|
+
while (!this.isPunct("}") && this.tok.kind !== "eof") {
|
|
885
|
+
stmts.push(this.parseStmt());
|
|
886
|
+
this.eat(";");
|
|
887
|
+
}
|
|
888
|
+
this.expect("}", "to close the block");
|
|
889
|
+
return stmts;
|
|
890
|
+
}
|
|
891
|
+
parseStmt() {
|
|
892
|
+
const t = this.tok;
|
|
893
|
+
if (t.kind === "ident") {
|
|
894
|
+
switch (t.value) {
|
|
895
|
+
case "var":
|
|
896
|
+
case "const":
|
|
897
|
+
case "computed": {
|
|
898
|
+
const decl = this.parseVarDecl();
|
|
899
|
+
return { k: "var", decl, loc: decl.loc };
|
|
900
|
+
}
|
|
901
|
+
case "if": {
|
|
902
|
+
this.next();
|
|
903
|
+
const test = this.parseExpr();
|
|
904
|
+
const then = this.isPunct("{") ? this.parseStmtBlock() : [this.parseStmt()];
|
|
905
|
+
let otherwise;
|
|
906
|
+
if (this.isIdent("elif")) {
|
|
907
|
+
otherwise = [this.parseStmt()];
|
|
908
|
+
}
|
|
909
|
+
else if (this.isIdent("else")) {
|
|
910
|
+
this.next();
|
|
911
|
+
otherwise = this.isPunct("{") ? this.parseStmtBlock() : [this.parseStmt()];
|
|
912
|
+
}
|
|
913
|
+
return { k: "if", test, then, else: otherwise, loc: t.loc };
|
|
914
|
+
}
|
|
915
|
+
case "elif": {
|
|
916
|
+
this.next();
|
|
917
|
+
const test = this.parseExpr();
|
|
918
|
+
const then = this.parseStmtBlock();
|
|
919
|
+
let otherwise;
|
|
920
|
+
if (this.isIdent("elif"))
|
|
921
|
+
otherwise = [this.parseStmt()];
|
|
922
|
+
else if (this.eatIdent("else"))
|
|
923
|
+
otherwise = this.isPunct("{") ? this.parseStmtBlock() : [this.parseStmt()];
|
|
924
|
+
return { k: "if", test, then, else: otherwise, loc: t.loc };
|
|
925
|
+
}
|
|
926
|
+
case "for": {
|
|
927
|
+
this.next();
|
|
928
|
+
const head = this.parseLoopHead();
|
|
929
|
+
return { k: "for", names: head.names, iter: head.iter, filter: head.filter, body: this.parseStmtBlock(), loc: t.loc };
|
|
930
|
+
}
|
|
931
|
+
case "while": {
|
|
932
|
+
this.next();
|
|
933
|
+
const test = this.parseExpr();
|
|
934
|
+
return { k: "while", test, body: this.parseStmtBlock(), loc: t.loc };
|
|
935
|
+
}
|
|
936
|
+
case "return": {
|
|
937
|
+
this.next();
|
|
938
|
+
if (this.isPunct("}") || this.isPunct(";") || this.tok.newlineBefore || this.tok.kind === "eof" || this.atAttrBoundary()) {
|
|
939
|
+
return { k: "return", loc: t.loc };
|
|
940
|
+
}
|
|
941
|
+
return { k: "return", value: this.parseExpr(), loc: t.loc };
|
|
942
|
+
}
|
|
943
|
+
case "break":
|
|
944
|
+
this.next();
|
|
945
|
+
return { k: "break", loc: t.loc };
|
|
946
|
+
case "continue":
|
|
947
|
+
this.next();
|
|
948
|
+
return { k: "continue", loc: t.loc };
|
|
949
|
+
default:
|
|
950
|
+
break;
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
const expr = this.parseExpr();
|
|
954
|
+
return { k: "expr", expr, loc: t.loc };
|
|
955
|
+
}
|
|
956
|
+
parseExpr() {
|
|
957
|
+
return this.parseAssign();
|
|
958
|
+
}
|
|
959
|
+
parseAssign() {
|
|
960
|
+
const arrow = this.tryParseArrow();
|
|
961
|
+
if (arrow)
|
|
962
|
+
return arrow;
|
|
963
|
+
const left = this.parseTernary();
|
|
964
|
+
const t = this.tok;
|
|
965
|
+
if (t.kind === "punct" && ASSIGN_OPS.has(t.value)) {
|
|
966
|
+
if (left.k !== "id" && left.k !== "member" && left.k !== "index")
|
|
967
|
+
this.fail("Invalid assignment target", t, undefined, "E0103");
|
|
968
|
+
this.next();
|
|
969
|
+
const value = this.parseAssign();
|
|
970
|
+
return { k: "assign", op: t.value, target: left, value, loc: left.loc };
|
|
971
|
+
}
|
|
972
|
+
return left;
|
|
973
|
+
}
|
|
974
|
+
tryParseArrow() {
|
|
975
|
+
const start = this.tok;
|
|
976
|
+
const isAsync = this.isIdent("async") && (this.isPunct("(", 1) || (this.peek(1).kind === "ident" && this.isPunct("=>", 2)));
|
|
977
|
+
const offset = isAsync ? 1 : 0;
|
|
978
|
+
if (this.peek(offset).kind === "ident" && this.isPunct("=>", offset + 1)) {
|
|
979
|
+
if (isAsync)
|
|
980
|
+
this.next();
|
|
981
|
+
const p = this.next();
|
|
982
|
+
this.next();
|
|
983
|
+
return { k: "arrow", params: [{ name: p.value, optional: false, rest: false, loc: p.loc }], body: this.parseArrowBody(), async: isAsync, loc: start.loc };
|
|
984
|
+
}
|
|
985
|
+
if (!this.isPunct("(", offset))
|
|
986
|
+
return undefined;
|
|
987
|
+
let depth = 0;
|
|
988
|
+
let i = offset;
|
|
989
|
+
for (; i < 4096; i++) {
|
|
990
|
+
const t = this.peek(i);
|
|
991
|
+
if (t.kind === "eof")
|
|
992
|
+
return undefined;
|
|
993
|
+
if (t.kind === "punct" && (t.value === "(" || t.value === "[" || t.value === "{"))
|
|
994
|
+
depth++;
|
|
995
|
+
if (t.kind === "punct" && (t.value === ")" || t.value === "]" || t.value === "}"))
|
|
996
|
+
depth--;
|
|
997
|
+
if (depth === 0)
|
|
998
|
+
break;
|
|
999
|
+
}
|
|
1000
|
+
const after = this.peek(i + 1);
|
|
1001
|
+
const isArrow = after.kind === "punct" && (after.value === "=>" || (after.value === "->" && this.peek(i + 2).kind === "ident" && ast_1.TYPE_NAMES.has(this.peek(i + 2).value)));
|
|
1002
|
+
if (!isArrow)
|
|
1003
|
+
return undefined;
|
|
1004
|
+
if (isAsync)
|
|
1005
|
+
this.next();
|
|
1006
|
+
const params = this.parseParams();
|
|
1007
|
+
if (this.eat("->")) {
|
|
1008
|
+
this.parseTypeRef();
|
|
1009
|
+
if (this.eat("=>"))
|
|
1010
|
+
return { k: "arrow", params, body: this.parseArrowBody(), async: isAsync, loc: start.loc };
|
|
1011
|
+
return { k: "arrow", params, body: this.parseStmtBlock(), async: isAsync, loc: start.loc };
|
|
1012
|
+
}
|
|
1013
|
+
this.expect("=>");
|
|
1014
|
+
return { k: "arrow", params, body: this.parseArrowBody(), async: isAsync, loc: start.loc };
|
|
1015
|
+
}
|
|
1016
|
+
parseArrowBody() {
|
|
1017
|
+
if (this.isPunct("{"))
|
|
1018
|
+
return this.parseStmtBlock();
|
|
1019
|
+
return this.parseAssign();
|
|
1020
|
+
}
|
|
1021
|
+
parseTernary() {
|
|
1022
|
+
const test = this.parseNullish();
|
|
1023
|
+
if (this.isPunct("?")) {
|
|
1024
|
+
this.next();
|
|
1025
|
+
const then = this.parseAssign();
|
|
1026
|
+
this.expect(":", "in conditional expression");
|
|
1027
|
+
const otherwise = this.parseAssign();
|
|
1028
|
+
return { k: "cond", test, then, else: otherwise, loc: test.loc };
|
|
1029
|
+
}
|
|
1030
|
+
return test;
|
|
1031
|
+
}
|
|
1032
|
+
binaryLevel(next, ops, words = {}) {
|
|
1033
|
+
let left = next();
|
|
1034
|
+
for (;;) {
|
|
1035
|
+
const t = this.tok;
|
|
1036
|
+
let op;
|
|
1037
|
+
if (t.kind === "punct")
|
|
1038
|
+
op = ops[t.value];
|
|
1039
|
+
else if (t.kind === "ident")
|
|
1040
|
+
op = words[t.value];
|
|
1041
|
+
if (!op)
|
|
1042
|
+
return left;
|
|
1043
|
+
this.next();
|
|
1044
|
+
const right = next();
|
|
1045
|
+
left = { k: "binary", op, left, right, loc: left.loc };
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
parseNullish() {
|
|
1049
|
+
return this.binaryLevel(() => this.parseOr(), { "??": "??" });
|
|
1050
|
+
}
|
|
1051
|
+
parseOr() {
|
|
1052
|
+
return this.binaryLevel(() => this.parseAnd(), { "||": "||" }, { or: "||" });
|
|
1053
|
+
}
|
|
1054
|
+
parseAnd() {
|
|
1055
|
+
return this.binaryLevel(() => this.parseEquality(), { "&&": "&&" }, { and: "&&" });
|
|
1056
|
+
}
|
|
1057
|
+
parseEquality() {
|
|
1058
|
+
return this.binaryLevel(() => this.parseRelational(), { "==": "==", "!=": "!=" });
|
|
1059
|
+
}
|
|
1060
|
+
parseRelational() {
|
|
1061
|
+
return this.binaryLevel(() => this.parseAdditive(), { "<": "<", ">": ">", "<=": "<=", ">=": ">=" });
|
|
1062
|
+
}
|
|
1063
|
+
parseAdditive() {
|
|
1064
|
+
return this.binaryLevel(() => this.parseMultiplicative(), { "+": "+", "-": "-" });
|
|
1065
|
+
}
|
|
1066
|
+
parseMultiplicative() {
|
|
1067
|
+
return this.binaryLevel(() => this.parseExponent(), { "*": "*", "/": "/", "%": "%" });
|
|
1068
|
+
}
|
|
1069
|
+
parseExponent() {
|
|
1070
|
+
const base = this.parseUnary();
|
|
1071
|
+
if (this.isPunct("**")) {
|
|
1072
|
+
this.next();
|
|
1073
|
+
const exp = this.parseExponent();
|
|
1074
|
+
return { k: "binary", op: "**", left: base, right: exp, loc: base.loc };
|
|
1075
|
+
}
|
|
1076
|
+
return base;
|
|
1077
|
+
}
|
|
1078
|
+
parseUnary() {
|
|
1079
|
+
const t = this.tok;
|
|
1080
|
+
if (t.kind === "punct" && (t.value === "!" || t.value === "-" || t.value === "+" || t.value === "~")) {
|
|
1081
|
+
this.next();
|
|
1082
|
+
return { k: "unary", op: t.value, arg: this.parseUnary(), loc: t.loc };
|
|
1083
|
+
}
|
|
1084
|
+
if (t.kind === "ident" && (t.value === "not" || t.value === "typeof" || t.value === "await")) {
|
|
1085
|
+
this.next();
|
|
1086
|
+
const op = t.value === "not" ? "!" : t.value;
|
|
1087
|
+
return { k: "unary", op, arg: this.parseUnary(), loc: t.loc };
|
|
1088
|
+
}
|
|
1089
|
+
if (this.isPunct("++")) {
|
|
1090
|
+
this.next();
|
|
1091
|
+
const target = this.parseUnary();
|
|
1092
|
+
return { k: "update", op: "++", prefix: true, target, loc: t.loc };
|
|
1093
|
+
}
|
|
1094
|
+
return this.parsePostfix();
|
|
1095
|
+
}
|
|
1096
|
+
parsePostfix() {
|
|
1097
|
+
let expr = this.parseCallMember();
|
|
1098
|
+
if (this.isPunct("++") && this.sameLine()) {
|
|
1099
|
+
this.next();
|
|
1100
|
+
expr = { k: "update", op: "++", prefix: false, target: expr, loc: expr.loc };
|
|
1101
|
+
}
|
|
1102
|
+
return expr;
|
|
1103
|
+
}
|
|
1104
|
+
parseArgs() {
|
|
1105
|
+
const args = [];
|
|
1106
|
+
this.expect("(");
|
|
1107
|
+
while (!this.isPunct(")") && this.tok.kind !== "eof") {
|
|
1108
|
+
const loc = this.tok.loc;
|
|
1109
|
+
if (this.eat("...")) {
|
|
1110
|
+
args.push({ value: this.parseExpr(), spread: true, loc });
|
|
1111
|
+
}
|
|
1112
|
+
else if (this.tok.kind === "ident" && this.isPunct(":", 1) && !this.isPunct(":", 2)) {
|
|
1113
|
+
const name = this.next().value;
|
|
1114
|
+
this.next();
|
|
1115
|
+
args.push({ name, value: this.parseExpr(), spread: false, loc });
|
|
1116
|
+
}
|
|
1117
|
+
else {
|
|
1118
|
+
args.push({ value: this.parseExpr(), spread: false, loc });
|
|
1119
|
+
}
|
|
1120
|
+
if (!this.eat(","))
|
|
1121
|
+
break;
|
|
1122
|
+
}
|
|
1123
|
+
this.expect(")", "to close the argument list");
|
|
1124
|
+
return args;
|
|
1125
|
+
}
|
|
1126
|
+
parseCallMember() {
|
|
1127
|
+
let expr;
|
|
1128
|
+
if (this.isIdent("new")) {
|
|
1129
|
+
const t = this.next();
|
|
1130
|
+
let callee = this.parsePrimary();
|
|
1131
|
+
while (this.isPunct(".")) {
|
|
1132
|
+
this.next();
|
|
1133
|
+
const prop = this.expectIdent("a property name");
|
|
1134
|
+
callee = { k: "member", object: callee, prop: prop.value, optional: false, loc: callee.loc };
|
|
1135
|
+
}
|
|
1136
|
+
const args = this.isPunct("(") ? this.parseArgs() : [];
|
|
1137
|
+
expr = { k: "new", callee, args, loc: t.loc };
|
|
1138
|
+
}
|
|
1139
|
+
else {
|
|
1140
|
+
expr = this.parsePrimary();
|
|
1141
|
+
}
|
|
1142
|
+
for (;;) {
|
|
1143
|
+
if (this.isPunct(".")) {
|
|
1144
|
+
this.next();
|
|
1145
|
+
const prop = this.tok;
|
|
1146
|
+
if (prop.kind !== "ident")
|
|
1147
|
+
this.fail(`Expected a property name after '.', found ${this.describe(prop)}`);
|
|
1148
|
+
this.next();
|
|
1149
|
+
expr = { k: "member", object: expr, prop: prop.value, optional: false, loc: expr.loc };
|
|
1150
|
+
}
|
|
1151
|
+
else if (this.isPunct("?.")) {
|
|
1152
|
+
this.next();
|
|
1153
|
+
if (this.isPunct("(")) {
|
|
1154
|
+
expr = { k: "call", callee: expr, args: this.parseArgs(), optional: true, memo: false, loc: expr.loc };
|
|
1155
|
+
}
|
|
1156
|
+
else if (this.isPunct("[")) {
|
|
1157
|
+
this.next();
|
|
1158
|
+
const index = this.parseExpr();
|
|
1159
|
+
this.expect("]");
|
|
1160
|
+
expr = { k: "index", object: expr, index, optional: true, loc: expr.loc };
|
|
1161
|
+
}
|
|
1162
|
+
else {
|
|
1163
|
+
const prop = this.expectIdent("a property name");
|
|
1164
|
+
expr = { k: "member", object: expr, prop: prop.value, optional: true, loc: expr.loc };
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
else if (this.isPunct("(") && this.sameLine()) {
|
|
1168
|
+
let memo = false;
|
|
1169
|
+
let callee = expr;
|
|
1170
|
+
if (callee.k === "id" && callee.name.startsWith("$") && callee.name.length > 1 && callee.name !== "$slot") {
|
|
1171
|
+
memo = true;
|
|
1172
|
+
callee = { ...callee, name: callee.name.slice(1) };
|
|
1173
|
+
}
|
|
1174
|
+
expr = { k: "call", callee, args: this.parseArgs(), optional: false, memo, loc: expr.loc };
|
|
1175
|
+
}
|
|
1176
|
+
else if (this.isPunct("[") && this.sameLine()) {
|
|
1177
|
+
this.next();
|
|
1178
|
+
const index = this.parseExpr();
|
|
1179
|
+
this.expect("]");
|
|
1180
|
+
expr = { k: "index", object: expr, index, optional: false, loc: expr.loc };
|
|
1181
|
+
}
|
|
1182
|
+
else if (this.tok.kind === "template" && this.tok.quote === "`" && this.adjacent()) {
|
|
1183
|
+
this.fail("Tagged templates are not supported");
|
|
1184
|
+
}
|
|
1185
|
+
else {
|
|
1186
|
+
return expr;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
parsePrimary() {
|
|
1191
|
+
const t = this.tok;
|
|
1192
|
+
switch (t.kind) {
|
|
1193
|
+
case "number":
|
|
1194
|
+
this.next();
|
|
1195
|
+
return { k: "lit", value: Number(t.value), loc: t.loc };
|
|
1196
|
+
case "string":
|
|
1197
|
+
this.next();
|
|
1198
|
+
return { k: "lit", value: t.value, loc: t.loc };
|
|
1199
|
+
case "template":
|
|
1200
|
+
this.next();
|
|
1201
|
+
return this.parseTemplate(t);
|
|
1202
|
+
case "ident":
|
|
1203
|
+
this.next();
|
|
1204
|
+
if (t.value === "true" || t.value === "false")
|
|
1205
|
+
return { k: "lit", value: t.value === "true", loc: t.loc };
|
|
1206
|
+
if (t.value === "null" || t.value === "undefined")
|
|
1207
|
+
return { k: "lit", value: null, loc: t.loc };
|
|
1208
|
+
return { k: "id", name: t.value, loc: t.loc };
|
|
1209
|
+
case "punct":
|
|
1210
|
+
if (t.value === "(") {
|
|
1211
|
+
this.next();
|
|
1212
|
+
const inner = this.parseExpr();
|
|
1213
|
+
this.expect(")", "to close the parenthesized expression");
|
|
1214
|
+
return inner;
|
|
1215
|
+
}
|
|
1216
|
+
if (t.value === "[")
|
|
1217
|
+
return this.parseArray();
|
|
1218
|
+
if (t.value === "{")
|
|
1219
|
+
return this.parseObject();
|
|
1220
|
+
break;
|
|
1221
|
+
default:
|
|
1222
|
+
break;
|
|
1223
|
+
}
|
|
1224
|
+
return this.fail(`Expected an expression, found ${this.describe(t)}`);
|
|
1225
|
+
}
|
|
1226
|
+
parseArray() {
|
|
1227
|
+
const start = this.next();
|
|
1228
|
+
const items = [];
|
|
1229
|
+
while (!this.isPunct("]") && this.tok.kind !== "eof") {
|
|
1230
|
+
const spread = this.eat("...");
|
|
1231
|
+
items.push({ value: this.parseExpr(), spread });
|
|
1232
|
+
if (!this.eat(",") && !this.tok.newlineBefore)
|
|
1233
|
+
break;
|
|
1234
|
+
}
|
|
1235
|
+
this.expect("]", "to close the array");
|
|
1236
|
+
return { k: "array", items, loc: start.loc };
|
|
1237
|
+
}
|
|
1238
|
+
parseObject() {
|
|
1239
|
+
const start = this.next();
|
|
1240
|
+
const props = [];
|
|
1241
|
+
while (!this.isPunct("}") && this.tok.kind !== "eof") {
|
|
1242
|
+
const loc = this.tok.loc;
|
|
1243
|
+
if (this.eat("...")) {
|
|
1244
|
+
props.push({ key: "", value: this.parseExpr(), spread: true, loc });
|
|
1245
|
+
}
|
|
1246
|
+
else if (this.isPunct("[")) {
|
|
1247
|
+
this.next();
|
|
1248
|
+
const computedKey = this.parseExpr();
|
|
1249
|
+
this.expect("]");
|
|
1250
|
+
this.expect(":");
|
|
1251
|
+
props.push({ key: "", computedKey, value: this.parseExpr(), spread: false, loc });
|
|
1252
|
+
}
|
|
1253
|
+
else {
|
|
1254
|
+
const k = this.tok;
|
|
1255
|
+
if (k.kind !== "ident" && k.kind !== "string" && k.kind !== "number")
|
|
1256
|
+
this.fail(`Expected an object key, found ${this.describe(k)}`);
|
|
1257
|
+
this.next();
|
|
1258
|
+
let key = k.value;
|
|
1259
|
+
while (k.kind === "ident" && this.isPunct("-") && this.adjacent() && this.peek(1).kind === "ident") {
|
|
1260
|
+
this.next();
|
|
1261
|
+
key += "-" + this.next().value;
|
|
1262
|
+
}
|
|
1263
|
+
if (this.eat(":"))
|
|
1264
|
+
props.push({ key, value: this.parseExpr(), spread: false, loc });
|
|
1265
|
+
else if (k.kind === "ident")
|
|
1266
|
+
props.push({ key, value: { k: "id", name: key, loc: k.loc }, spread: false, loc });
|
|
1267
|
+
else
|
|
1268
|
+
this.fail("Expected ':' after object key");
|
|
1269
|
+
}
|
|
1270
|
+
if (!this.eat(",") && !this.eat(";") && !this.tok.newlineBefore)
|
|
1271
|
+
break;
|
|
1272
|
+
}
|
|
1273
|
+
this.expect("}", "to close the object");
|
|
1274
|
+
return { k: "object", props, loc: start.loc };
|
|
1275
|
+
}
|
|
1276
|
+
parseTemplate(t) {
|
|
1277
|
+
const parts = [];
|
|
1278
|
+
const raw = t.value;
|
|
1279
|
+
let buf = "";
|
|
1280
|
+
let i = 0;
|
|
1281
|
+
while (i < raw.length) {
|
|
1282
|
+
const c = raw[i];
|
|
1283
|
+
if (c === "\\" && (raw[i + 1] === "{" || raw[i + 1] === "}")) {
|
|
1284
|
+
buf += raw[i + 1];
|
|
1285
|
+
i += 2;
|
|
1286
|
+
continue;
|
|
1287
|
+
}
|
|
1288
|
+
if (c === "{") {
|
|
1289
|
+
let depth = 1;
|
|
1290
|
+
let j = i + 1;
|
|
1291
|
+
let quote = "";
|
|
1292
|
+
for (; j < raw.length; j++) {
|
|
1293
|
+
const ch = raw[j];
|
|
1294
|
+
if (quote) {
|
|
1295
|
+
if (ch === "\\")
|
|
1296
|
+
j++;
|
|
1297
|
+
else if (ch === quote)
|
|
1298
|
+
quote = "";
|
|
1299
|
+
continue;
|
|
1300
|
+
}
|
|
1301
|
+
if (ch === '"' || ch === "'")
|
|
1302
|
+
quote = ch;
|
|
1303
|
+
else if (ch === "{")
|
|
1304
|
+
depth++;
|
|
1305
|
+
else if (ch === "}" && --depth === 0)
|
|
1306
|
+
break;
|
|
1307
|
+
}
|
|
1308
|
+
if (depth !== 0)
|
|
1309
|
+
this.fail("Unmatched '{' in formatted string", t, "Escape literal braces as \\{ and \\}");
|
|
1310
|
+
const inner = raw.slice(i + 1, j);
|
|
1311
|
+
if (buf)
|
|
1312
|
+
parts.push(buf);
|
|
1313
|
+
buf = "";
|
|
1314
|
+
const sub = Parser.fromTokens((0, lexer_1.tokenize)(inner, this.file, { ...t.loc, col: t.loc.col + 2 + i }), this.file);
|
|
1315
|
+
const e = sub.parseExpr();
|
|
1316
|
+
if (sub.tok.kind !== "eof")
|
|
1317
|
+
sub.fail(`Unexpected ${sub.describe(sub.tok)} inside formatted string`);
|
|
1318
|
+
parts.push(e);
|
|
1319
|
+
i = j + 1;
|
|
1320
|
+
continue;
|
|
1321
|
+
}
|
|
1322
|
+
buf += c;
|
|
1323
|
+
i++;
|
|
1324
|
+
}
|
|
1325
|
+
if (buf)
|
|
1326
|
+
parts.push(buf);
|
|
1327
|
+
if (parts.every(p => typeof p === "string"))
|
|
1328
|
+
return { k: "lit", value: parts.join(""), loc: t.loc };
|
|
1329
|
+
return { k: "tpl", parts, loc: t.loc };
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
exports.Parser = Parser;
|
|
1333
|
+
function emptyOwner() {
|
|
1334
|
+
return { vars: [], functions: [], styles: [], scripts: [], lifecycle: [], watchers: [] };
|
|
1335
|
+
}
|
|
1336
|
+
function isStatementExpr(expr) {
|
|
1337
|
+
if (expr.k === "assign" || expr.k === "update")
|
|
1338
|
+
return true;
|
|
1339
|
+
if (expr.k === "unary" && expr.op === "await")
|
|
1340
|
+
return true;
|
|
1341
|
+
if (expr.k === "call") {
|
|
1342
|
+
const root = calleeRoot(expr.callee);
|
|
1343
|
+
return root === "console" || root === "alert" || root === "localStorage" || root === "sessionStorage" || root === "history";
|
|
1344
|
+
}
|
|
1345
|
+
return false;
|
|
1346
|
+
}
|
|
1347
|
+
function calleeRoot(e) {
|
|
1348
|
+
if (e.k === "id")
|
|
1349
|
+
return e.name;
|
|
1350
|
+
if (e.k === "member")
|
|
1351
|
+
return calleeRoot(e.object);
|
|
1352
|
+
return undefined;
|
|
1353
|
+
}
|
|
1354
|
+
function classifyImport(source) {
|
|
1355
|
+
const clean = source.split(/[?#]/)[0].toLowerCase();
|
|
1356
|
+
if (/\.(css|scss|sass)$/.test(clean))
|
|
1357
|
+
return "style";
|
|
1358
|
+
if (clean.endsWith(".sinth"))
|
|
1359
|
+
return "sinth";
|
|
1360
|
+
if (clean.endsWith(".json"))
|
|
1361
|
+
return "json";
|
|
1362
|
+
if (/^https?:\/\//.test(clean) || clean.startsWith("//"))
|
|
1363
|
+
return "script";
|
|
1364
|
+
if (/\.(m?js|ts|mts|jsx|tsx)$/.test(clean))
|
|
1365
|
+
return "module";
|
|
1366
|
+
if (clean.startsWith("sinth:"))
|
|
1367
|
+
return "sinth";
|
|
1368
|
+
if (clean.startsWith(".") || clean.startsWith("/"))
|
|
1369
|
+
return "sinth";
|
|
1370
|
+
return "module";
|
|
1371
|
+
}
|
|
1372
|
+
function parseSinth(source, file) {
|
|
1373
|
+
return new Parser(source, file).parseFile();
|
|
1374
|
+
}
|