@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/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 { tokenize } from "./lexer.js";
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
- type: "SpriteDecl",
160
- name,
161
- body,
162
- loc: startTok ?? tok,
163
- // Tokens are the brace's position, needed to insert declarations into
164
- // the body when the brace is not on the declaration line.
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
- partial,
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 { type: "StageDecl", name: "Stage", body, loc: tok, open };
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
- name,
352
- params,
353
- returns,
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
- elseBody = this.parseBlockBody();
436
+ ({
437
+ body: elseBody,
438
+ open: elseOpen,
439
+ close: elseClose,
440
+ } = this.parseBlockBody(true));
421
441
  }
422
442
  }
423
- return { type: "If", cond, then, else: elseBody, loc: tok };
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 elseBody = this.parseBlockBody();
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
- type: "BlockCall",
503
- name: name.name,
504
- segments: name.segments,
505
- args,
506
- body,
507
- else: elseBody,
508
- loc: tok,
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
- return parseTokens(tokenize(source, file), source, file);
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
  /**
@@ -2,5 +2,5 @@
2
2
  "name": "@penguinmod/packager",
3
3
  "version": "1.9.2",
4
4
  "commit": "53f868b85ec21d2edab9ce00fd2d709da93da6d6",
5
- "builtAt": "2026-11-25T03:05:53.327Z"
5
+ "builtAt": "2026-11-26T19:16:44.275Z"
6
6
  }
@@ -3,5 +3,5 @@
3
3
  "version": "0.2.0",
4
4
  "commit": "3822f284c8cda2c3a0a5c6dd928221dd9ff2cee1",
5
5
  "inputHash": "bb8f99fcce9f4ecdc563f11df5a0510dcc94c25d256c6931c7f1f113e0a1998b",
6
- "builtAt": "2026-11-25T03:06:40.888Z"
6
+ "builtAt": "2026-11-26T19:17:49.724Z"
7
7
  }