clarity-pattern-parser 11.0.3 → 11.0.5

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/dist/index.esm.js CHANGED
@@ -2711,9 +2711,9 @@ class PrecedenceTree {
2711
2711
  this._binaryPlaceholder = Node.createNode("placeholder", "binary-placeholder");
2712
2712
  this._atomNode = null;
2713
2713
  this._binaryNode = null;
2714
- this._orphanedAtom = null;
2715
2714
  this._precedenceMap = precedenceMap;
2716
2715
  this._associationMap = associationMap;
2716
+ this._revertBinary = () => { };
2717
2717
  }
2718
2718
  addPrefix(name, ...prefix) {
2719
2719
  const lastPrefixNode = this._prefixNode;
@@ -2748,15 +2748,22 @@ class PrecedenceTree {
2748
2748
  throw new Error("Cannot add a binary without an atom node.");
2749
2749
  }
2750
2750
  this._binaryPlaceholder.remove();
2751
- this._orphanedAtom = lastAtomNode;
2752
2751
  if (lastBinaryNode == null) {
2753
2752
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2754
2753
  this._binaryNode = node;
2754
+ this._revertBinary = () => {
2755
+ lastAtomNode.remove();
2756
+ this._binaryNode = lastAtomNode;
2757
+ };
2755
2758
  return;
2756
2759
  }
2757
2760
  if (precedence === lastPrecendece && association === Association.right) {
2758
2761
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2759
2762
  lastBinaryNode.appendChild(node);
2763
+ this._revertBinary = () => {
2764
+ node.replaceWith(lastAtomNode);
2765
+ this._binaryNode = lastBinaryNode;
2766
+ };
2760
2767
  this._binaryNode = node;
2761
2768
  }
2762
2769
  else if (precedence === lastPrecendece) {
@@ -2764,6 +2771,11 @@ class PrecedenceTree {
2764
2771
  lastBinaryNode.replaceWith(node);
2765
2772
  lastBinaryNode.appendChild(lastAtomNode);
2766
2773
  node.append(lastBinaryNode, ...delimiterNode, this._binaryPlaceholder);
2774
+ this._revertBinary = () => {
2775
+ lastBinaryNode.remove();
2776
+ node.replaceWith(lastBinaryNode);
2777
+ this._binaryNode = lastBinaryNode;
2778
+ };
2767
2779
  this._binaryNode = node;
2768
2780
  }
2769
2781
  else if (precedence > lastPrecendece) {
@@ -2781,11 +2793,21 @@ class PrecedenceTree {
2781
2793
  const node = Node.createNode("expression", name, []);
2782
2794
  root.replaceWith(node);
2783
2795
  node.append(root, ...delimiterNode, this._binaryPlaceholder);
2796
+ this._revertBinary = () => {
2797
+ root.remove();
2798
+ node.replaceWith(root);
2799
+ this._binaryNode = root;
2800
+ };
2784
2801
  this._binaryNode = node;
2785
2802
  }
2786
2803
  else {
2787
2804
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2788
2805
  lastBinaryNode.appendChild(node);
2806
+ this._revertBinary = () => {
2807
+ lastAtomNode.remove();
2808
+ node.replaceWith(lastAtomNode);
2809
+ this._binaryNode = lastBinaryNode;
2810
+ };
2789
2811
  this._binaryNode = node;
2790
2812
  }
2791
2813
  }
@@ -2826,14 +2848,13 @@ class PrecedenceTree {
2826
2848
  return this._atomNode != null;
2827
2849
  }
2828
2850
  commit() {
2829
- var _a;
2830
2851
  if (this._binaryNode == null) {
2831
2852
  return this._compileAtomNode();
2832
2853
  }
2833
2854
  const atomNode = this._compileAtomNode();
2834
2855
  if (atomNode == null) {
2835
- let root = this._binaryPlaceholder.findRoot();
2836
- (_a = this._binaryPlaceholder.parent) === null || _a === void 0 ? void 0 : _a.replaceWith(this._orphanedAtom);
2856
+ this._revertBinary();
2857
+ let root = this._binaryNode.findRoot();
2837
2858
  this.reset();
2838
2859
  return root;
2839
2860
  }
@@ -2847,7 +2868,6 @@ class PrecedenceTree {
2847
2868
  reset() {
2848
2869
  this._prefixNode = null;
2849
2870
  this._atomNode = null;
2850
- this._orphanedAtom = null;
2851
2871
  this._postfixNode = null;
2852
2872
  this._binaryNode = null;
2853
2873
  }
@@ -2904,12 +2924,12 @@ class ExpressionPattern {
2904
2924
  this._postfixNames = [];
2905
2925
  this._binaryPatterns = [];
2906
2926
  this._binaryNames = [];
2907
- this.associationMap = {};
2927
+ this._associationMap = {};
2908
2928
  this._precedenceMap = {};
2909
2929
  this._originalPatterns = patterns;
2910
2930
  this._patterns = this._organizePatterns(patterns);
2911
2931
  this._shouldStopParsing = false;
2912
- this._precedenceTree = new PrecedenceTree(this._precedenceMap, this.associationMap);
2932
+ this._precedenceTree = new PrecedenceTree(this._precedenceMap, this._associationMap);
2913
2933
  if (this._atomPatterns.length === 0) {
2914
2934
  throw new Error("Need at least one terminating pattern with an 'expression' pattern.");
2915
2935
  }
@@ -2947,10 +2967,10 @@ class ExpressionPattern {
2947
2967
  this._binaryPatterns.push(clone);
2948
2968
  this._binaryNames.push(name);
2949
2969
  if (pattern.type === "right-associated") {
2950
- this.associationMap[name] = Association.right;
2970
+ this._associationMap[name] = Association.right;
2951
2971
  }
2952
2972
  else {
2953
- this.associationMap[name] = Association.left;
2973
+ this._associationMap[name] = Association.left;
2954
2974
  }
2955
2975
  finalPatterns.push(clone);
2956
2976
  }
@@ -3195,7 +3215,24 @@ class ExpressionPattern {
3195
3215
  getTokens() {
3196
3216
  return this.atomPatterns.map(p => p.getTokens()).flat();
3197
3217
  }
3198
- getTokensAfter(_childReference) {
3218
+ getTokensAfter(childReference) {
3219
+ if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
3220
+ const atomTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3221
+ const prefixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
3222
+ return [...prefixTokens, ...atomTokens];
3223
+ }
3224
+ if (this._atomPatterns.includes(childReference)) {
3225
+ const postfixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
3226
+ if (postfixTokens.length === 0) {
3227
+ return this._binaryPatterns.map(p => p.getTokens()).flat();
3228
+ }
3229
+ return postfixTokens;
3230
+ }
3231
+ if (this._postfixPatterns.includes(childReference)) {
3232
+ const postfixTokens = this.postfixPatterns.map(p => p.getTokens()).flat();
3233
+ const binaryTokens = this._binaryPatterns.map(p => p.getTokens()).flat();
3234
+ return [...postfixTokens, ...binaryTokens];
3235
+ }
3199
3236
  return [];
3200
3237
  }
3201
3238
  getNextTokens() {
@@ -3207,7 +3244,24 @@ class ExpressionPattern {
3207
3244
  getPatterns() {
3208
3245
  return this.atomPatterns.map(p => p.getPatterns()).flat();
3209
3246
  }
3210
- getPatternsAfter(_childReference) {
3247
+ getPatternsAfter(childReference) {
3248
+ if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
3249
+ const atomPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3250
+ const prefixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();
3251
+ return [...prefixPatterns, ...atomPatterns];
3252
+ }
3253
+ if (this._atomPatterns.includes(childReference)) {
3254
+ const postfixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();
3255
+ if (postfixPatterns.length === 0) {
3256
+ return this._binaryPatterns.map(p => p.getPatterns()).flat();
3257
+ }
3258
+ return postfixPatterns;
3259
+ }
3260
+ if (this._postfixPatterns.includes(childReference)) {
3261
+ const postfixPaterns = this.postfixPatterns.map(p => p.getPatterns()).flat();
3262
+ const binaryPatterns = this._binaryPatterns.map(p => p.getPatterns()).flat();
3263
+ return [...postfixPaterns, ...binaryPatterns];
3264
+ }
3211
3265
  return [];
3212
3266
  }
3213
3267
  getNextPatterns() {