clarity-pattern-parser 11.0.2 → 11.0.4

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
@@ -2138,7 +2138,10 @@ const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: div
2138
2138
 
2139
2139
  const patternName = name$1.clone("pattern-name");
2140
2140
  patternName.setTokens(["[PATTERN_NAME]"]);
2141
- const patterns$1 = new Options("options-patterns", [patternName, anonymousPattern]);
2141
+ const patterns$1 = new Sequence("patterns", [
2142
+ new Options("options-patterns", [patternName, anonymousPattern]),
2143
+ new Optional("optional-right-associated", new Literal("right-associated", " right"))
2144
+ ]);
2142
2145
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2143
2146
  defaultDivider.setTokens(["|"]);
2144
2147
  const greedyDivider = new Regex("greedy-divider", "\\s*[<][|][>]\\s*");
@@ -2708,9 +2711,9 @@ class PrecedenceTree {
2708
2711
  this._binaryPlaceholder = Node.createNode("placeholder", "binary-placeholder");
2709
2712
  this._atomNode = null;
2710
2713
  this._binaryNode = null;
2711
- this._orphanedAtom = null;
2712
2714
  this._precedenceMap = precedenceMap;
2713
2715
  this._associationMap = associationMap;
2716
+ this._revertBinary = () => { };
2714
2717
  }
2715
2718
  addPrefix(name, ...prefix) {
2716
2719
  const lastPrefixNode = this._prefixNode;
@@ -2745,15 +2748,22 @@ class PrecedenceTree {
2745
2748
  throw new Error("Cannot add a binary without an atom node.");
2746
2749
  }
2747
2750
  this._binaryPlaceholder.remove();
2748
- this._orphanedAtom = lastAtomNode;
2749
2751
  if (lastBinaryNode == null) {
2750
2752
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2751
2753
  this._binaryNode = node;
2754
+ this._revertBinary = () => {
2755
+ lastAtomNode.remove();
2756
+ this._binaryNode = lastAtomNode;
2757
+ };
2752
2758
  return;
2753
2759
  }
2754
2760
  if (precedence === lastPrecendece && association === Association.right) {
2755
2761
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2756
2762
  lastBinaryNode.appendChild(node);
2763
+ this._revertBinary = () => {
2764
+ node.replaceWith(lastAtomNode);
2765
+ this._binaryNode = lastBinaryNode;
2766
+ };
2757
2767
  this._binaryNode = node;
2758
2768
  }
2759
2769
  else if (precedence === lastPrecendece) {
@@ -2761,6 +2771,11 @@ class PrecedenceTree {
2761
2771
  lastBinaryNode.replaceWith(node);
2762
2772
  lastBinaryNode.appendChild(lastAtomNode);
2763
2773
  node.append(lastBinaryNode, ...delimiterNode, this._binaryPlaceholder);
2774
+ this._revertBinary = () => {
2775
+ lastBinaryNode.remove();
2776
+ node.replaceWith(lastBinaryNode);
2777
+ this._binaryNode = lastBinaryNode;
2778
+ };
2764
2779
  this._binaryNode = node;
2765
2780
  }
2766
2781
  else if (precedence > lastPrecendece) {
@@ -2778,11 +2793,21 @@ class PrecedenceTree {
2778
2793
  const node = Node.createNode("expression", name, []);
2779
2794
  root.replaceWith(node);
2780
2795
  node.append(root, ...delimiterNode, this._binaryPlaceholder);
2796
+ this._revertBinary = () => {
2797
+ root.remove();
2798
+ node.replaceWith(root);
2799
+ this._binaryNode = root;
2800
+ };
2781
2801
  this._binaryNode = node;
2782
2802
  }
2783
2803
  else {
2784
2804
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2785
2805
  lastBinaryNode.appendChild(node);
2806
+ this._revertBinary = () => {
2807
+ lastAtomNode.remove();
2808
+ node.replaceWith(lastAtomNode);
2809
+ this._binaryNode = lastBinaryNode;
2810
+ };
2786
2811
  this._binaryNode = node;
2787
2812
  }
2788
2813
  }
@@ -2823,14 +2848,13 @@ class PrecedenceTree {
2823
2848
  return this._atomNode != null;
2824
2849
  }
2825
2850
  commit() {
2826
- var _a;
2827
2851
  if (this._binaryNode == null) {
2828
2852
  return this._compileAtomNode();
2829
2853
  }
2830
2854
  const atomNode = this._compileAtomNode();
2831
2855
  if (atomNode == null) {
2832
- let root = this._binaryPlaceholder.findRoot();
2833
- (_a = this._binaryPlaceholder.parent) === null || _a === void 0 ? void 0 : _a.replaceWith(this._orphanedAtom);
2856
+ this._revertBinary();
2857
+ let root = this._binaryNode.findRoot();
2834
2858
  this.reset();
2835
2859
  return root;
2836
2860
  }
@@ -2844,13 +2868,12 @@ class PrecedenceTree {
2844
2868
  reset() {
2845
2869
  this._prefixNode = null;
2846
2870
  this._atomNode = null;
2847
- this._orphanedAtom = null;
2848
2871
  this._postfixNode = null;
2849
2872
  this._binaryNode = null;
2850
2873
  }
2851
2874
  }
2852
2875
 
2853
- let indexId = 0;
2876
+ let indexId$1 = 0;
2854
2877
  class ExpressionPattern {
2855
2878
  get id() {
2856
2879
  return this._id;
@@ -2889,7 +2912,7 @@ class ExpressionPattern {
2889
2912
  if (patterns.length === 0) {
2890
2913
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2891
2914
  }
2892
- this._id = `expression-${indexId++}`;
2915
+ this._id = `expression-${indexId$1++}`;
2893
2916
  this._type = "expression";
2894
2917
  this._name = name;
2895
2918
  this._parent = null;
@@ -3226,6 +3249,88 @@ class ExpressionPattern {
3226
3249
  }
3227
3250
  }
3228
3251
 
3252
+ let indexId = 0;
3253
+ class RightAssociatedPattern {
3254
+ get id() {
3255
+ return this._id;
3256
+ }
3257
+ get type() {
3258
+ return this._type;
3259
+ }
3260
+ get name() {
3261
+ return this._name;
3262
+ }
3263
+ get parent() {
3264
+ return this._parent;
3265
+ }
3266
+ set parent(pattern) {
3267
+ this._parent = pattern;
3268
+ }
3269
+ get children() {
3270
+ return this._children;
3271
+ }
3272
+ get startedOnIndex() {
3273
+ return this._children[0].startedOnIndex;
3274
+ }
3275
+ constructor(pattern) {
3276
+ this._id = `right-associated-${indexId++}`;
3277
+ this._type = "right-associated";
3278
+ this._name = "";
3279
+ this._parent = null;
3280
+ this._children = [pattern.clone()];
3281
+ }
3282
+ parse(cursor) {
3283
+ return this.children[0].parse(cursor);
3284
+ }
3285
+ exec(text, record) {
3286
+ return this.children[0].exec(text, record);
3287
+ }
3288
+ test(text, record) {
3289
+ return this.children[0].test(text, record);
3290
+ }
3291
+ clone(_name) {
3292
+ const clone = new RightAssociatedPattern(this.children[0]);
3293
+ clone._id = this._id;
3294
+ return clone;
3295
+ }
3296
+ getTokens() {
3297
+ return this.children[0].getTokens();
3298
+ }
3299
+ getTokensAfter(_childReference) {
3300
+ if (this._parent == null) {
3301
+ return [];
3302
+ }
3303
+ return this._parent.getTokensAfter(this);
3304
+ }
3305
+ getNextTokens() {
3306
+ if (this._parent == null) {
3307
+ return [];
3308
+ }
3309
+ return this._parent.getTokensAfter(this);
3310
+ }
3311
+ getPatterns() {
3312
+ return this.children[0].getPatterns();
3313
+ }
3314
+ getPatternsAfter(_childReference) {
3315
+ if (this._parent == null) {
3316
+ return [];
3317
+ }
3318
+ return this._parent.getPatternsAfter(this);
3319
+ }
3320
+ getNextPatterns() {
3321
+ if (this._parent == null) {
3322
+ return [];
3323
+ }
3324
+ return this._parent.getPatternsAfter(this);
3325
+ }
3326
+ find(predicate) {
3327
+ return this.children[0].find(predicate);
3328
+ }
3329
+ isEqual(pattern) {
3330
+ return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
3331
+ }
3332
+ }
3333
+
3229
3334
  let anonymousIndexId = 0;
3230
3335
  const patternNodes = {
3231
3336
  "literal": true,
@@ -3407,7 +3512,15 @@ class Grammar {
3407
3512
  _buildOptions(name, node) {
3408
3513
  const patternNodes = node.children.filter(n => n.name !== "default-divider" && n.name !== "greedy-divider");
3409
3514
  const isGreedy = node.find(n => n.name === "greedy-divider") != null;
3410
- const patterns = patternNodes.map(n => this._buildPattern(n));
3515
+ const patterns = patternNodes.map(n => {
3516
+ const rightAssociated = n.find(n => n.name === "right-associated");
3517
+ if (rightAssociated != null) {
3518
+ return new RightAssociatedPattern(this._buildPattern(n.children[0]));
3519
+ }
3520
+ else {
3521
+ return this._buildPattern(n.children[0]);
3522
+ }
3523
+ });
3411
3524
  const hasRecursivePattern = patterns.some(p => this._isRecursive(name, p));
3412
3525
  if (hasRecursivePattern && !isGreedy) {
3413
3526
  try {
@@ -3426,10 +3539,15 @@ class Grammar {
3426
3539
  return this._isRecursivePattern(name, pattern);
3427
3540
  }
3428
3541
  _isRecursivePattern(name, pattern) {
3429
- return pattern.type === "sequence" &&
3430
- pattern.children[0].type === "reference" &&
3431
- pattern.children[0].name === name &&
3432
- pattern.children.length > 2;
3542
+ if (pattern.children.length === 0) {
3543
+ return false;
3544
+ }
3545
+ const firstChild = pattern.children[0];
3546
+ const lastChild = pattern.children[pattern.children.length - 1];
3547
+ const isLongEnough = pattern.children.length >= 2;
3548
+ return pattern.type === "sequence" && isLongEnough &&
3549
+ (firstChild.type === "reference" && firstChild.name === name) ||
3550
+ (lastChild.type === "reference" && lastChild.name === name);
3433
3551
  }
3434
3552
  _buildPattern(node) {
3435
3553
  const type = node.name;