clarity-pattern-parser 10.2.3 → 10.2.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.
@@ -2738,6 +2738,7 @@
2738
2738
  this._binaryPatterns = [];
2739
2739
  this._recursivePatterns = [];
2740
2740
  this._recursiveNames = [];
2741
+ this._endsInRecursion = [];
2741
2742
  this._binaryNames = [];
2742
2743
  this._binaryAssociation = [];
2743
2744
  this._precedenceMap = {};
@@ -2771,6 +2772,7 @@
2771
2772
  tail.parent = this;
2772
2773
  this._recursivePatterns.push(tail);
2773
2774
  this._recursiveNames.push(name);
2775
+ this._endsInRecursion.push(this._endsWithRecursion(pattern));
2774
2776
  finalPatterns.push(tail);
2775
2777
  }
2776
2778
  else {
@@ -2826,6 +2828,16 @@
2826
2828
  }
2827
2829
  return new Sequence(`${pattern.name}-tail`, pattern.children.slice(1));
2828
2830
  }
2831
+ _endsWithRecursion(pattern) {
2832
+ if (pattern.type === "right-associated") {
2833
+ pattern = pattern.children[0];
2834
+ }
2835
+ const lastChild = pattern.children[pattern.children.length - 1];
2836
+ return pattern.type === "sequence" &&
2837
+ pattern.children.length > 1 &&
2838
+ lastChild.type === "reference" &&
2839
+ lastChild.name === this.name;
2840
+ }
2829
2841
  parse(cursor) {
2830
2842
  // This is a cache to help with speed
2831
2843
  this._firstIndex = cursor.index;
@@ -2850,6 +2862,7 @@
2850
2862
  let lastBinaryNode = null;
2851
2863
  let onIndex = cursor.index;
2852
2864
  outer: while (true) {
2865
+ cursor.resolveError();
2853
2866
  onIndex = cursor.index;
2854
2867
  for (let i = 0; i < this._unaryPatterns.length; i++) {
2855
2868
  cursor.moveTo(onIndex);
@@ -2884,14 +2897,23 @@
2884
2897
  if (lastBinaryNode != null && lastUnaryNode != null) {
2885
2898
  lastBinaryNode.appendChild(lastUnaryNode);
2886
2899
  }
2887
- const frontExpression = lastBinaryNode == null ? lastUnaryNode : lastBinaryNode.findRoot();
2888
2900
  const name = this._recursiveNames[i];
2889
- const recursiveNode = createNode(name, [frontExpression, ...node.children]);
2890
- recursiveNode.normalize(this._firstIndex);
2891
- return recursiveNode;
2901
+ if (this._endsInRecursion[i]) {
2902
+ const frontExpression = lastBinaryNode == null ? lastUnaryNode : lastBinaryNode.findRoot();
2903
+ const recursiveNode = createNode(name, [frontExpression, ...node.children]);
2904
+ recursiveNode.normalize(this._firstIndex);
2905
+ return recursiveNode;
2906
+ }
2907
+ else {
2908
+ const recursiveNode = createNode(name, [lastUnaryNode, ...node.children]);
2909
+ recursiveNode.normalize(this._firstIndex);
2910
+ lastUnaryNode = recursiveNode;
2911
+ break;
2912
+ }
2892
2913
  }
2893
2914
  cursor.moveTo(onIndex);
2894
2915
  }
2916
+ cursor.resolveError();
2895
2917
  onIndex = cursor.index;
2896
2918
  for (let i = 0; i < this._binaryPatterns.length; i++) {
2897
2919
  cursor.moveTo(onIndex);
@@ -2915,7 +2937,7 @@
2915
2937
  }
2916
2938
  else if (lastBinaryNode != null && lastUnaryNode != null && delimiterNode != null) {
2917
2939
  const precedence = this._precedenceMap[name];
2918
- const lastPrecendece = lastBinaryNode == null ? 0 : this._precedenceMap[lastBinaryNode.name];
2940
+ const lastPrecendece = lastBinaryNode == null ? 0 : this._precedenceMap[lastBinaryNode.name] || -1;
2919
2941
  const association = this._binaryAssociation[i];
2920
2942
  if (precedence === lastPrecendece && association === Association.right) {
2921
2943
  const node = createNode(name, [lastUnaryNode, delimiterNode]);