@triflare/fx 2.2.0 → 2.3.0-alpha.2
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/package.json +1 -1
- package/product.yml +1 -1
- package/src/cli/commands/feature.js +90 -19
- package/src/cli/templates/index.js +2 -1
- package/src/comments.js +287 -0
- package/src/compiler.js +97 -33
- package/src/config-edit.js +341 -1
- package/src/config.js +139 -7
- package/src/feature-registry.js +85 -0
- package/src/features.js +219 -111
- package/src/layout.js +402 -0
- package/src/lint/config-file.js +26 -5
- package/src/merge.js +5 -1
- package/src/parser.js +96 -50
- package/vendor/pm-packager-dist/.fx-build.json +1 -1
- package/vendor/pm-vm-dist/.fx-build.json +1 -1
package/src/parser.js
CHANGED
|
@@ -5,8 +5,9 @@
|
|
|
5
5
|
* Triflare Fx is intentionally context-free: keywords are recognised by shape
|
|
6
6
|
* (`sprite "..."`, `when <event>`, `let ...`) rather than being reserved words.
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { tokenizeWithComments } from "./lexer.js";
|
|
9
9
|
import { ParseError } from "./errors.js";
|
|
10
|
+
import { attachComments } from "./comments.js";
|
|
10
11
|
|
|
11
12
|
const ASSIGN_OPS = new Set(["=", "+=", "-=", "*=", "/="]);
|
|
12
13
|
const PROPERTY_KEYS = new Set(["layer", "costumes", "sounds"]);
|
|
@@ -22,6 +23,15 @@ class Parser {
|
|
|
22
23
|
this.source = source;
|
|
23
24
|
this.file = file;
|
|
24
25
|
this.pos = 0;
|
|
26
|
+
// Brace tokens for every body, kept out of the AST so comments can be
|
|
27
|
+
// placed by source range without changing the node shapes.
|
|
28
|
+
this.spans = new WeakMap();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Record the braces surrounding a node's body (and else branch). */
|
|
32
|
+
span(node, bodyOpen, bodyClose, elseOpen = null, elseClose = null) {
|
|
33
|
+
this.spans.set(node, { bodyOpen, bodyClose, elseOpen, elseClose });
|
|
34
|
+
return node;
|
|
25
35
|
}
|
|
26
36
|
|
|
27
37
|
peek(offset = 0) {
|
|
@@ -136,8 +146,8 @@ class Parser {
|
|
|
136
146
|
parseTestDecl() {
|
|
137
147
|
const tok = this.expect("test");
|
|
138
148
|
const name = this.expectType("string", "a test name string").value;
|
|
139
|
-
const body = this.parseBlockBody();
|
|
140
|
-
return { type: "TestDecl", name, body, loc: tok };
|
|
149
|
+
const { body, open, close } = this.parseBlockBody(true);
|
|
150
|
+
return this.span({ type: "TestDecl", name, body, loc: tok }, open, close);
|
|
141
151
|
}
|
|
142
152
|
|
|
143
153
|
/** `expect <expression>[, "message"];` */
|
|
@@ -154,23 +164,31 @@ class Parser {
|
|
|
154
164
|
parseSprite(partial = false, startTok = null) {
|
|
155
165
|
const tok = this.expect("sprite");
|
|
156
166
|
const name = this.expectType("string", "a sprite name string").value;
|
|
157
|
-
const { body, open } = this.parseBlockBody(true);
|
|
158
|
-
return
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
167
|
+
const { body, open, close } = this.parseBlockBody(true);
|
|
168
|
+
return this.span(
|
|
169
|
+
{
|
|
170
|
+
type: "SpriteDecl",
|
|
171
|
+
name,
|
|
172
|
+
body,
|
|
173
|
+
loc: startTok ?? tok,
|
|
174
|
+
// Tokens are the brace's position, needed to insert declarations into
|
|
175
|
+
// the body when the brace is not on the declaration line.
|
|
176
|
+
open,
|
|
177
|
+
partial,
|
|
178
|
+
},
|
|
165
179
|
open,
|
|
166
|
-
|
|
167
|
-
|
|
180
|
+
close,
|
|
181
|
+
);
|
|
168
182
|
}
|
|
169
183
|
|
|
170
184
|
parseStage() {
|
|
171
185
|
const tok = this.expect("stage");
|
|
172
|
-
const { body, open } = this.parseBlockBody(true);
|
|
173
|
-
return
|
|
186
|
+
const { body, open, close } = this.parseBlockBody(true);
|
|
187
|
+
return this.span(
|
|
188
|
+
{ type: "StageDecl", name: "Stage", body, loc: tok, open },
|
|
189
|
+
open,
|
|
190
|
+
close,
|
|
191
|
+
);
|
|
174
192
|
}
|
|
175
193
|
|
|
176
194
|
/**
|
|
@@ -185,8 +203,8 @@ class Parser {
|
|
|
185
203
|
this.fail('Unterminated block; expected "}"');
|
|
186
204
|
items.push(this.parseBodyItem());
|
|
187
205
|
}
|
|
188
|
-
this.expect("}");
|
|
189
|
-
return withOpen ? { body: items, open } : items;
|
|
206
|
+
const close = this.expect("}");
|
|
207
|
+
return withOpen ? { body: items, open, close } : items;
|
|
190
208
|
}
|
|
191
209
|
|
|
192
210
|
parseBodyItem() {
|
|
@@ -345,23 +363,19 @@ class Parser {
|
|
|
345
363
|
if (!this.eat(",")) break;
|
|
346
364
|
}
|
|
347
365
|
this.expect(")");
|
|
348
|
-
const body = this.parseBlockBody();
|
|
349
|
-
return
|
|
350
|
-
type: "FunctionDecl",
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
warp,
|
|
355
|
-
body,
|
|
356
|
-
loc: tok,
|
|
357
|
-
};
|
|
366
|
+
const { body, open, close } = this.parseBlockBody(true);
|
|
367
|
+
return this.span(
|
|
368
|
+
{ type: "FunctionDecl", name, params, returns, warp, body, loc: tok },
|
|
369
|
+
open,
|
|
370
|
+
close,
|
|
371
|
+
);
|
|
358
372
|
}
|
|
359
373
|
|
|
360
374
|
parseHat() {
|
|
361
375
|
const tok = this.expect("when");
|
|
362
376
|
const event = this.parseCallOrIdentifier();
|
|
363
|
-
const body = this.parseBlockBody();
|
|
364
|
-
return { type: "HatDecl", event, body, loc: tok };
|
|
377
|
+
const { body, open, close } = this.parseBlockBody(true);
|
|
378
|
+
return this.span({ type: "HatDecl", event, body, loc: tok }, open, close);
|
|
365
379
|
}
|
|
366
380
|
|
|
367
381
|
parseProperty() {
|
|
@@ -410,17 +424,29 @@ class Parser {
|
|
|
410
424
|
} else {
|
|
411
425
|
cond = this.parseExpression();
|
|
412
426
|
}
|
|
413
|
-
const then = this.parseBlockBody();
|
|
427
|
+
const then = this.parseBlockBody(true);
|
|
414
428
|
let elseBody = null;
|
|
429
|
+
let elseOpen = null;
|
|
430
|
+
let elseClose = null;
|
|
415
431
|
if (this.atIdent("else")) {
|
|
416
432
|
this.next();
|
|
417
433
|
if (this.atIdent("if")) {
|
|
418
434
|
elseBody = [this.parseIf()];
|
|
419
435
|
} else {
|
|
420
|
-
|
|
436
|
+
({
|
|
437
|
+
body: elseBody,
|
|
438
|
+
open: elseOpen,
|
|
439
|
+
close: elseClose,
|
|
440
|
+
} = this.parseBlockBody(true));
|
|
421
441
|
}
|
|
422
442
|
}
|
|
423
|
-
return
|
|
443
|
+
return this.span(
|
|
444
|
+
{ type: "If", cond, then: then.body, else: elseBody, loc: tok },
|
|
445
|
+
then.open,
|
|
446
|
+
then.close,
|
|
447
|
+
elseOpen,
|
|
448
|
+
elseClose,
|
|
449
|
+
);
|
|
424
450
|
}
|
|
425
451
|
|
|
426
452
|
parseRepeat() {
|
|
@@ -432,8 +458,8 @@ class Parser {
|
|
|
432
458
|
} else {
|
|
433
459
|
count = this.parseExpression();
|
|
434
460
|
}
|
|
435
|
-
const body = this.parseBlockBody();
|
|
436
|
-
return { type: "Repeat", count, body, loc: tok };
|
|
461
|
+
const { body, open, close } = this.parseBlockBody(true);
|
|
462
|
+
return this.span({ type: "Repeat", count, body, loc: tok }, open, close);
|
|
437
463
|
}
|
|
438
464
|
|
|
439
465
|
parseWhile() {
|
|
@@ -445,14 +471,14 @@ class Parser {
|
|
|
445
471
|
} else {
|
|
446
472
|
cond = this.parseExpression();
|
|
447
473
|
}
|
|
448
|
-
const body = this.parseBlockBody();
|
|
449
|
-
return { type: "While", cond, body, loc: tok };
|
|
474
|
+
const { body, open, close } = this.parseBlockBody(true);
|
|
475
|
+
return this.span({ type: "While", cond, body, loc: tok }, open, close);
|
|
450
476
|
}
|
|
451
477
|
|
|
452
478
|
parseForever() {
|
|
453
479
|
const tok = this.expect("forever");
|
|
454
|
-
const body = this.parseBlockBody();
|
|
455
|
-
return { type: "Forever", body, loc: tok };
|
|
480
|
+
const { body, open, close } = this.parseBlockBody(true);
|
|
481
|
+
return this.span({ type: "Forever", body, loc: tok }, open, close);
|
|
456
482
|
}
|
|
457
483
|
|
|
458
484
|
parseReturn() {
|
|
@@ -486,27 +512,43 @@ class Parser {
|
|
|
486
512
|
}
|
|
487
513
|
|
|
488
514
|
let body = null;
|
|
515
|
+
let bodyOpen = null;
|
|
516
|
+
let bodyClose = null;
|
|
489
517
|
let elseBody = null;
|
|
518
|
+
let elseOpen = null;
|
|
519
|
+
let elseClose = null;
|
|
490
520
|
if (this.atPunct("{")) {
|
|
491
|
-
body = this.parseBlockBody();
|
|
521
|
+
({ body, open: bodyOpen, close: bodyClose } = this.parseBlockBody(true));
|
|
492
522
|
if (this.atIdent("else")) {
|
|
493
523
|
this.next();
|
|
494
524
|
if (this.atIdent("if")) elseBody = [this.parseIf()];
|
|
495
|
-
else
|
|
525
|
+
else {
|
|
526
|
+
({
|
|
527
|
+
body: elseBody,
|
|
528
|
+
open: elseOpen,
|
|
529
|
+
close: elseClose,
|
|
530
|
+
} = this.parseBlockBody(true));
|
|
531
|
+
}
|
|
496
532
|
}
|
|
497
533
|
this.eat(";");
|
|
498
534
|
} else {
|
|
499
535
|
this.expect(";");
|
|
500
536
|
}
|
|
501
|
-
return
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
537
|
+
return this.span(
|
|
538
|
+
{
|
|
539
|
+
type: "BlockCall",
|
|
540
|
+
name: name.name,
|
|
541
|
+
segments: name.segments,
|
|
542
|
+
args,
|
|
543
|
+
body,
|
|
544
|
+
else: elseBody,
|
|
545
|
+
loc: tok,
|
|
546
|
+
},
|
|
547
|
+
bodyOpen,
|
|
548
|
+
bodyClose,
|
|
549
|
+
elseOpen,
|
|
550
|
+
elseClose,
|
|
551
|
+
);
|
|
510
552
|
}
|
|
511
553
|
|
|
512
554
|
parseCallOrIdentifier() {
|
|
@@ -708,7 +750,11 @@ class Parser {
|
|
|
708
750
|
* @param {string} [file]
|
|
709
751
|
*/
|
|
710
752
|
export function parse(source, file = "<input>") {
|
|
711
|
-
|
|
753
|
+
const { tokens, comments } = tokenizeWithComments(source, file);
|
|
754
|
+
const parser = new Parser(tokens, source, file);
|
|
755
|
+
const ast = parser.parse();
|
|
756
|
+
attachComments(ast, comments, tokens, parser.spans);
|
|
757
|
+
return ast;
|
|
712
758
|
}
|
|
713
759
|
|
|
714
760
|
/**
|