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.
@@ -2717,9 +2717,9 @@
2717
2717
  this._binaryPlaceholder = Node.createNode("placeholder", "binary-placeholder");
2718
2718
  this._atomNode = null;
2719
2719
  this._binaryNode = null;
2720
- this._orphanedAtom = null;
2721
2720
  this._precedenceMap = precedenceMap;
2722
2721
  this._associationMap = associationMap;
2722
+ this._revertBinary = () => { };
2723
2723
  }
2724
2724
  addPrefix(name, ...prefix) {
2725
2725
  const lastPrefixNode = this._prefixNode;
@@ -2754,15 +2754,22 @@
2754
2754
  throw new Error("Cannot add a binary without an atom node.");
2755
2755
  }
2756
2756
  this._binaryPlaceholder.remove();
2757
- this._orphanedAtom = lastAtomNode;
2758
2757
  if (lastBinaryNode == null) {
2759
2758
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2760
2759
  this._binaryNode = node;
2760
+ this._revertBinary = () => {
2761
+ lastAtomNode.remove();
2762
+ this._binaryNode = lastAtomNode;
2763
+ };
2761
2764
  return;
2762
2765
  }
2763
2766
  if (precedence === lastPrecendece && association === Association.right) {
2764
2767
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2765
2768
  lastBinaryNode.appendChild(node);
2769
+ this._revertBinary = () => {
2770
+ node.replaceWith(lastAtomNode);
2771
+ this._binaryNode = lastBinaryNode;
2772
+ };
2766
2773
  this._binaryNode = node;
2767
2774
  }
2768
2775
  else if (precedence === lastPrecendece) {
@@ -2770,6 +2777,11 @@
2770
2777
  lastBinaryNode.replaceWith(node);
2771
2778
  lastBinaryNode.appendChild(lastAtomNode);
2772
2779
  node.append(lastBinaryNode, ...delimiterNode, this._binaryPlaceholder);
2780
+ this._revertBinary = () => {
2781
+ lastBinaryNode.remove();
2782
+ node.replaceWith(lastBinaryNode);
2783
+ this._binaryNode = lastBinaryNode;
2784
+ };
2773
2785
  this._binaryNode = node;
2774
2786
  }
2775
2787
  else if (precedence > lastPrecendece) {
@@ -2787,11 +2799,21 @@
2787
2799
  const node = Node.createNode("expression", name, []);
2788
2800
  root.replaceWith(node);
2789
2801
  node.append(root, ...delimiterNode, this._binaryPlaceholder);
2802
+ this._revertBinary = () => {
2803
+ root.remove();
2804
+ node.replaceWith(root);
2805
+ this._binaryNode = root;
2806
+ };
2790
2807
  this._binaryNode = node;
2791
2808
  }
2792
2809
  else {
2793
2810
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2794
2811
  lastBinaryNode.appendChild(node);
2812
+ this._revertBinary = () => {
2813
+ lastAtomNode.remove();
2814
+ node.replaceWith(lastAtomNode);
2815
+ this._binaryNode = lastBinaryNode;
2816
+ };
2795
2817
  this._binaryNode = node;
2796
2818
  }
2797
2819
  }
@@ -2832,14 +2854,13 @@
2832
2854
  return this._atomNode != null;
2833
2855
  }
2834
2856
  commit() {
2835
- var _a;
2836
2857
  if (this._binaryNode == null) {
2837
2858
  return this._compileAtomNode();
2838
2859
  }
2839
2860
  const atomNode = this._compileAtomNode();
2840
2861
  if (atomNode == null) {
2841
- let root = this._binaryPlaceholder.findRoot();
2842
- (_a = this._binaryPlaceholder.parent) === null || _a === void 0 ? void 0 : _a.replaceWith(this._orphanedAtom);
2862
+ this._revertBinary();
2863
+ let root = this._binaryNode.findRoot();
2843
2864
  this.reset();
2844
2865
  return root;
2845
2866
  }
@@ -2853,7 +2874,6 @@
2853
2874
  reset() {
2854
2875
  this._prefixNode = null;
2855
2876
  this._atomNode = null;
2856
- this._orphanedAtom = null;
2857
2877
  this._postfixNode = null;
2858
2878
  this._binaryNode = null;
2859
2879
  }
@@ -2910,12 +2930,12 @@
2910
2930
  this._postfixNames = [];
2911
2931
  this._binaryPatterns = [];
2912
2932
  this._binaryNames = [];
2913
- this.associationMap = {};
2933
+ this._associationMap = {};
2914
2934
  this._precedenceMap = {};
2915
2935
  this._originalPatterns = patterns;
2916
2936
  this._patterns = this._organizePatterns(patterns);
2917
2937
  this._shouldStopParsing = false;
2918
- this._precedenceTree = new PrecedenceTree(this._precedenceMap, this.associationMap);
2938
+ this._precedenceTree = new PrecedenceTree(this._precedenceMap, this._associationMap);
2919
2939
  if (this._atomPatterns.length === 0) {
2920
2940
  throw new Error("Need at least one terminating pattern with an 'expression' pattern.");
2921
2941
  }
@@ -2953,10 +2973,10 @@
2953
2973
  this._binaryPatterns.push(clone);
2954
2974
  this._binaryNames.push(name);
2955
2975
  if (pattern.type === "right-associated") {
2956
- this.associationMap[name] = Association.right;
2976
+ this._associationMap[name] = Association.right;
2957
2977
  }
2958
2978
  else {
2959
- this.associationMap[name] = Association.left;
2979
+ this._associationMap[name] = Association.left;
2960
2980
  }
2961
2981
  finalPatterns.push(clone);
2962
2982
  }
@@ -3201,7 +3221,24 @@
3201
3221
  getTokens() {
3202
3222
  return this.atomPatterns.map(p => p.getTokens()).flat();
3203
3223
  }
3204
- getTokensAfter(_childReference) {
3224
+ getTokensAfter(childReference) {
3225
+ if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
3226
+ const atomTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3227
+ const prefixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
3228
+ return [...prefixTokens, ...atomTokens];
3229
+ }
3230
+ if (this._atomPatterns.includes(childReference)) {
3231
+ const postfixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
3232
+ if (postfixTokens.length === 0) {
3233
+ return this._binaryPatterns.map(p => p.getTokens()).flat();
3234
+ }
3235
+ return postfixTokens;
3236
+ }
3237
+ if (this._postfixPatterns.includes(childReference)) {
3238
+ const postfixTokens = this.postfixPatterns.map(p => p.getTokens()).flat();
3239
+ const binaryTokens = this._binaryPatterns.map(p => p.getTokens()).flat();
3240
+ return [...postfixTokens, ...binaryTokens];
3241
+ }
3205
3242
  return [];
3206
3243
  }
3207
3244
  getNextTokens() {
@@ -3213,7 +3250,24 @@
3213
3250
  getPatterns() {
3214
3251
  return this.atomPatterns.map(p => p.getPatterns()).flat();
3215
3252
  }
3216
- getPatternsAfter(_childReference) {
3253
+ getPatternsAfter(childReference) {
3254
+ if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
3255
+ const atomPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3256
+ const prefixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();
3257
+ return [...prefixPatterns, ...atomPatterns];
3258
+ }
3259
+ if (this._atomPatterns.includes(childReference)) {
3260
+ const postfixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();
3261
+ if (postfixPatterns.length === 0) {
3262
+ return this._binaryPatterns.map(p => p.getPatterns()).flat();
3263
+ }
3264
+ return postfixPatterns;
3265
+ }
3266
+ if (this._postfixPatterns.includes(childReference)) {
3267
+ const postfixPaterns = this.postfixPatterns.map(p => p.getPatterns()).flat();
3268
+ const binaryPatterns = this._binaryPatterns.map(p => p.getPatterns()).flat();
3269
+ return [...postfixPaterns, ...binaryPatterns];
3270
+ }
3217
3271
  return [];
3218
3272
  }
3219
3273
  getNextPatterns() {