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.
package/dist/index.esm.js CHANGED
@@ -2732,6 +2732,7 @@ class ExpressionPattern {
2732
2732
  this._binaryPatterns = [];
2733
2733
  this._recursivePatterns = [];
2734
2734
  this._recursiveNames = [];
2735
+ this._endsInRecursion = [];
2735
2736
  this._binaryNames = [];
2736
2737
  this._binaryAssociation = [];
2737
2738
  this._precedenceMap = {};
@@ -2765,6 +2766,7 @@ class ExpressionPattern {
2765
2766
  tail.parent = this;
2766
2767
  this._recursivePatterns.push(tail);
2767
2768
  this._recursiveNames.push(name);
2769
+ this._endsInRecursion.push(this._endsWithRecursion(pattern));
2768
2770
  finalPatterns.push(tail);
2769
2771
  }
2770
2772
  else {
@@ -2820,6 +2822,16 @@ class ExpressionPattern {
2820
2822
  }
2821
2823
  return new Sequence(`${pattern.name}-tail`, pattern.children.slice(1));
2822
2824
  }
2825
+ _endsWithRecursion(pattern) {
2826
+ if (pattern.type === "right-associated") {
2827
+ pattern = pattern.children[0];
2828
+ }
2829
+ const lastChild = pattern.children[pattern.children.length - 1];
2830
+ return pattern.type === "sequence" &&
2831
+ pattern.children.length > 1 &&
2832
+ lastChild.type === "reference" &&
2833
+ lastChild.name === this.name;
2834
+ }
2823
2835
  parse(cursor) {
2824
2836
  // This is a cache to help with speed
2825
2837
  this._firstIndex = cursor.index;
@@ -2844,6 +2856,7 @@ class ExpressionPattern {
2844
2856
  let lastBinaryNode = null;
2845
2857
  let onIndex = cursor.index;
2846
2858
  outer: while (true) {
2859
+ cursor.resolveError();
2847
2860
  onIndex = cursor.index;
2848
2861
  for (let i = 0; i < this._unaryPatterns.length; i++) {
2849
2862
  cursor.moveTo(onIndex);
@@ -2878,14 +2891,23 @@ class ExpressionPattern {
2878
2891
  if (lastBinaryNode != null && lastUnaryNode != null) {
2879
2892
  lastBinaryNode.appendChild(lastUnaryNode);
2880
2893
  }
2881
- const frontExpression = lastBinaryNode == null ? lastUnaryNode : lastBinaryNode.findRoot();
2882
2894
  const name = this._recursiveNames[i];
2883
- const recursiveNode = createNode(name, [frontExpression, ...node.children]);
2884
- recursiveNode.normalize(this._firstIndex);
2885
- return recursiveNode;
2895
+ if (this._endsInRecursion[i]) {
2896
+ const frontExpression = lastBinaryNode == null ? lastUnaryNode : lastBinaryNode.findRoot();
2897
+ const recursiveNode = createNode(name, [frontExpression, ...node.children]);
2898
+ recursiveNode.normalize(this._firstIndex);
2899
+ return recursiveNode;
2900
+ }
2901
+ else {
2902
+ const recursiveNode = createNode(name, [lastUnaryNode, ...node.children]);
2903
+ recursiveNode.normalize(this._firstIndex);
2904
+ lastUnaryNode = recursiveNode;
2905
+ break;
2906
+ }
2886
2907
  }
2887
2908
  cursor.moveTo(onIndex);
2888
2909
  }
2910
+ cursor.resolveError();
2889
2911
  onIndex = cursor.index;
2890
2912
  for (let i = 0; i < this._binaryPatterns.length; i++) {
2891
2913
  cursor.moveTo(onIndex);
@@ -2909,7 +2931,7 @@ class ExpressionPattern {
2909
2931
  }
2910
2932
  else if (lastBinaryNode != null && lastUnaryNode != null && delimiterNode != null) {
2911
2933
  const precedence = this._precedenceMap[name];
2912
- const lastPrecendece = lastBinaryNode == null ? 0 : this._precedenceMap[lastBinaryNode.name];
2934
+ const lastPrecendece = lastBinaryNode == null ? 0 : this._precedenceMap[lastBinaryNode.name] || -1;
2913
2935
  const association = this._binaryAssociation[i];
2914
2936
  if (precedence === lastPrecendece && association === Association.right) {
2915
2937
  const node = createNode(name, [lastUnaryNode, delimiterNode]);