@graffiticode/basis 1.4.0 → 1.5.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/compiler.js +34 -29
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@graffiticode/basis",
3
3
  "type": "module",
4
- "version": "1.4.0",
4
+ "version": "1.5.1",
5
5
  "description": "The basis library for creating Graffiticode languages",
6
6
  "main": "index.js",
7
7
  "scripts": {
package/src/compiler.js CHANGED
@@ -25,20 +25,25 @@ class Visitor {
25
25
  this.root = code.root;
26
26
  }
27
27
  visit(nid, options, resume) {
28
- assert(nid);
29
- let node;
30
- if (typeof nid === "object") {
31
- node = nid;
32
- } else {
33
- node = this.nodePool[nid];
34
- }
35
- assert(node && node.tag && node.elts, "2000: Visitor.visit() tag=" + node.tag + " elts= " + JSON.stringify(node.elts));
36
- assert(this[node.tag], "2000: Visitor function not defined for: " + node.tag);
37
- assert(typeof resume === "function", message(1003));
38
- if (!options.SYNC && ASYNC) {
39
- setTimeout(() => this[node.tag](node, options, resume), 0);
40
- } else {
41
- this[node.tag](node, options, resume);
28
+ try {
29
+ assert(nid);
30
+ let node;
31
+ if (typeof nid === "object") {
32
+ node = nid;
33
+ } else {
34
+ node = this.nodePool[nid];
35
+ }
36
+ assert(node && node.tag && node.elts, "2000: Visitor.visit() tag=" + node.tag + " elts= " + JSON.stringify(node.elts));
37
+ assert(this[node.tag], "2000: Visitor function not defined for: " + node.tag);
38
+ assert(typeof resume === "function", message(1003));
39
+ if (!options.SYNC && ASYNC) {
40
+ // This is used to keep from blowing the call stack.
41
+ setTimeout(() => this[node.tag](node, options, resume), 0);
42
+ } else {
43
+ this[node.tag](node, options, resume);
44
+ }
45
+ } catch (x) {
46
+ resume(error(x.stack));
42
47
  }
43
48
  }
44
49
  node(nid) {
@@ -139,7 +144,7 @@ export class Checker extends Visitor {
139
144
  }
140
145
  JSON(node, options, resume) {
141
146
  this.visit(node.elts[0], options, (e0, v0) => {
142
- assert(v0.tag === "STR");
147
+ assert(v0.tag === "STR", JSON.stringify(v0, null, 2));
143
148
  const err = [];
144
149
  const val = node;
145
150
  resume(err, val);
@@ -536,17 +541,21 @@ export class Transformer extends Visitor {
536
541
  }
537
542
  RECORD(node, options, resume) {
538
543
  let err = [];
539
- let val = {};
540
544
  let len = 0;
541
545
  if (node.elts.length === 0) {
542
546
  resume(err, val);
543
547
  } else {
548
+ const ndx = [];
544
549
  for (let elt of node.elts.reverse()) {
545
550
  // For historical reasons, the bindings are reversed in the AST.
546
551
  this.visit(elt, options, (e0, v0) => {
547
552
  err = err.concat(e0);
548
- val[v0.key] = v0.val;
553
+ ndx[elt] = v0;
549
554
  if (++len === node.elts.length) {
555
+ // This is a little trickery to restore the original order of the,
556
+ // given that they may have been reordered do to the node being
557
+ // visited asynchronously.
558
+ const val = index.reduce((acc, v0) => ({...acc, [v0.key]: v0.val}), {});
550
559
  resume(err, val);
551
560
  }
552
561
  });
@@ -608,19 +617,15 @@ export class Transformer extends Visitor {
608
617
  resume(err, val);
609
618
  }
610
619
  DATA(node, options, resume) {
611
- if (options.data && Object.keys(options.data).length !== 0) {
612
- // Got external data, so use it.
613
- const err = [];
614
- const val = options.data;
615
- resume(err, val);
616
- } else {
617
- // Otherwise, use the default data.
618
- this.visit(node.elts[0], options, (e0, v0) => {
619
- const err = e0;
620
- const val = v0;
621
- resume(err, val);
620
+ this.visit(node.elts[0], options, (e0, v0) => {
621
+ const data = options.data || {};
622
+ const err = e0;
623
+ const val = v0;
624
+ resume(err, {
625
+ ...val,
626
+ ...data, // External data overrides internal data.
622
627
  });
623
- }
628
+ });
624
629
  }
625
630
  PAREN(node, options, resume) {
626
631
  this.visit(node.elts[0], options, (e0, v0) => {