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.js CHANGED
@@ -2142,7 +2142,10 @@ const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: div
2142
2142
 
2143
2143
  const patternName = name$1.clone("pattern-name");
2144
2144
  patternName.setTokens(["[PATTERN_NAME]"]);
2145
- const patterns$1 = new Options("options-patterns", [patternName, anonymousPattern]);
2145
+ const patterns$1 = new Sequence("patterns", [
2146
+ new Options("options-patterns", [patternName, anonymousPattern]),
2147
+ new Optional("optional-right-associated", new Literal("right-associated", " right"))
2148
+ ]);
2146
2149
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2147
2150
  defaultDivider.setTokens(["|"]);
2148
2151
  const greedyDivider = new Regex("greedy-divider", "\\s*[<][|][>]\\s*");
@@ -2712,9 +2715,9 @@ class PrecedenceTree {
2712
2715
  this._binaryPlaceholder = Node.createNode("placeholder", "binary-placeholder");
2713
2716
  this._atomNode = null;
2714
2717
  this._binaryNode = null;
2715
- this._orphanedAtom = null;
2716
2718
  this._precedenceMap = precedenceMap;
2717
2719
  this._associationMap = associationMap;
2720
+ this._revertBinary = () => { };
2718
2721
  }
2719
2722
  addPrefix(name, ...prefix) {
2720
2723
  const lastPrefixNode = this._prefixNode;
@@ -2749,15 +2752,22 @@ class PrecedenceTree {
2749
2752
  throw new Error("Cannot add a binary without an atom node.");
2750
2753
  }
2751
2754
  this._binaryPlaceholder.remove();
2752
- this._orphanedAtom = lastAtomNode;
2753
2755
  if (lastBinaryNode == null) {
2754
2756
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2755
2757
  this._binaryNode = node;
2758
+ this._revertBinary = () => {
2759
+ lastAtomNode.remove();
2760
+ this._binaryNode = lastAtomNode;
2761
+ };
2756
2762
  return;
2757
2763
  }
2758
2764
  if (precedence === lastPrecendece && association === Association.right) {
2759
2765
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2760
2766
  lastBinaryNode.appendChild(node);
2767
+ this._revertBinary = () => {
2768
+ node.replaceWith(lastAtomNode);
2769
+ this._binaryNode = lastBinaryNode;
2770
+ };
2761
2771
  this._binaryNode = node;
2762
2772
  }
2763
2773
  else if (precedence === lastPrecendece) {
@@ -2765,6 +2775,11 @@ class PrecedenceTree {
2765
2775
  lastBinaryNode.replaceWith(node);
2766
2776
  lastBinaryNode.appendChild(lastAtomNode);
2767
2777
  node.append(lastBinaryNode, ...delimiterNode, this._binaryPlaceholder);
2778
+ this._revertBinary = () => {
2779
+ lastBinaryNode.remove();
2780
+ node.replaceWith(lastBinaryNode);
2781
+ this._binaryNode = lastBinaryNode;
2782
+ };
2768
2783
  this._binaryNode = node;
2769
2784
  }
2770
2785
  else if (precedence > lastPrecendece) {
@@ -2782,11 +2797,21 @@ class PrecedenceTree {
2782
2797
  const node = Node.createNode("expression", name, []);
2783
2798
  root.replaceWith(node);
2784
2799
  node.append(root, ...delimiterNode, this._binaryPlaceholder);
2800
+ this._revertBinary = () => {
2801
+ root.remove();
2802
+ node.replaceWith(root);
2803
+ this._binaryNode = root;
2804
+ };
2785
2805
  this._binaryNode = node;
2786
2806
  }
2787
2807
  else {
2788
2808
  const node = Node.createNode("expression", name, [lastAtomNode, ...delimiterNode, this._binaryPlaceholder]);
2789
2809
  lastBinaryNode.appendChild(node);
2810
+ this._revertBinary = () => {
2811
+ lastAtomNode.remove();
2812
+ node.replaceWith(lastAtomNode);
2813
+ this._binaryNode = lastBinaryNode;
2814
+ };
2790
2815
  this._binaryNode = node;
2791
2816
  }
2792
2817
  }
@@ -2827,14 +2852,13 @@ class PrecedenceTree {
2827
2852
  return this._atomNode != null;
2828
2853
  }
2829
2854
  commit() {
2830
- var _a;
2831
2855
  if (this._binaryNode == null) {
2832
2856
  return this._compileAtomNode();
2833
2857
  }
2834
2858
  const atomNode = this._compileAtomNode();
2835
2859
  if (atomNode == null) {
2836
- let root = this._binaryPlaceholder.findRoot();
2837
- (_a = this._binaryPlaceholder.parent) === null || _a === void 0 ? void 0 : _a.replaceWith(this._orphanedAtom);
2860
+ this._revertBinary();
2861
+ let root = this._binaryNode.findRoot();
2838
2862
  this.reset();
2839
2863
  return root;
2840
2864
  }
@@ -2848,13 +2872,12 @@ class PrecedenceTree {
2848
2872
  reset() {
2849
2873
  this._prefixNode = null;
2850
2874
  this._atomNode = null;
2851
- this._orphanedAtom = null;
2852
2875
  this._postfixNode = null;
2853
2876
  this._binaryNode = null;
2854
2877
  }
2855
2878
  }
2856
2879
 
2857
- let indexId = 0;
2880
+ let indexId$1 = 0;
2858
2881
  class ExpressionPattern {
2859
2882
  get id() {
2860
2883
  return this._id;
@@ -2893,7 +2916,7 @@ class ExpressionPattern {
2893
2916
  if (patterns.length === 0) {
2894
2917
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2895
2918
  }
2896
- this._id = `expression-${indexId++}`;
2919
+ this._id = `expression-${indexId$1++}`;
2897
2920
  this._type = "expression";
2898
2921
  this._name = name;
2899
2922
  this._parent = null;
@@ -3230,6 +3253,88 @@ class ExpressionPattern {
3230
3253
  }
3231
3254
  }
3232
3255
 
3256
+ let indexId = 0;
3257
+ class RightAssociatedPattern {
3258
+ get id() {
3259
+ return this._id;
3260
+ }
3261
+ get type() {
3262
+ return this._type;
3263
+ }
3264
+ get name() {
3265
+ return this._name;
3266
+ }
3267
+ get parent() {
3268
+ return this._parent;
3269
+ }
3270
+ set parent(pattern) {
3271
+ this._parent = pattern;
3272
+ }
3273
+ get children() {
3274
+ return this._children;
3275
+ }
3276
+ get startedOnIndex() {
3277
+ return this._children[0].startedOnIndex;
3278
+ }
3279
+ constructor(pattern) {
3280
+ this._id = `right-associated-${indexId++}`;
3281
+ this._type = "right-associated";
3282
+ this._name = "";
3283
+ this._parent = null;
3284
+ this._children = [pattern.clone()];
3285
+ }
3286
+ parse(cursor) {
3287
+ return this.children[0].parse(cursor);
3288
+ }
3289
+ exec(text, record) {
3290
+ return this.children[0].exec(text, record);
3291
+ }
3292
+ test(text, record) {
3293
+ return this.children[0].test(text, record);
3294
+ }
3295
+ clone(_name) {
3296
+ const clone = new RightAssociatedPattern(this.children[0]);
3297
+ clone._id = this._id;
3298
+ return clone;
3299
+ }
3300
+ getTokens() {
3301
+ return this.children[0].getTokens();
3302
+ }
3303
+ getTokensAfter(_childReference) {
3304
+ if (this._parent == null) {
3305
+ return [];
3306
+ }
3307
+ return this._parent.getTokensAfter(this);
3308
+ }
3309
+ getNextTokens() {
3310
+ if (this._parent == null) {
3311
+ return [];
3312
+ }
3313
+ return this._parent.getTokensAfter(this);
3314
+ }
3315
+ getPatterns() {
3316
+ return this.children[0].getPatterns();
3317
+ }
3318
+ getPatternsAfter(_childReference) {
3319
+ if (this._parent == null) {
3320
+ return [];
3321
+ }
3322
+ return this._parent.getPatternsAfter(this);
3323
+ }
3324
+ getNextPatterns() {
3325
+ if (this._parent == null) {
3326
+ return [];
3327
+ }
3328
+ return this._parent.getPatternsAfter(this);
3329
+ }
3330
+ find(predicate) {
3331
+ return this.children[0].find(predicate);
3332
+ }
3333
+ isEqual(pattern) {
3334
+ return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
3335
+ }
3336
+ }
3337
+
3233
3338
  let anonymousIndexId = 0;
3234
3339
  const patternNodes = {
3235
3340
  "literal": true,
@@ -3411,7 +3516,15 @@ class Grammar {
3411
3516
  _buildOptions(name, node) {
3412
3517
  const patternNodes = node.children.filter(n => n.name !== "default-divider" && n.name !== "greedy-divider");
3413
3518
  const isGreedy = node.find(n => n.name === "greedy-divider") != null;
3414
- const patterns = patternNodes.map(n => this._buildPattern(n));
3519
+ const patterns = patternNodes.map(n => {
3520
+ const rightAssociated = n.find(n => n.name === "right-associated");
3521
+ if (rightAssociated != null) {
3522
+ return new RightAssociatedPattern(this._buildPattern(n.children[0]));
3523
+ }
3524
+ else {
3525
+ return this._buildPattern(n.children[0]);
3526
+ }
3527
+ });
3415
3528
  const hasRecursivePattern = patterns.some(p => this._isRecursive(name, p));
3416
3529
  if (hasRecursivePattern && !isGreedy) {
3417
3530
  try {
@@ -3430,10 +3543,15 @@ class Grammar {
3430
3543
  return this._isRecursivePattern(name, pattern);
3431
3544
  }
3432
3545
  _isRecursivePattern(name, pattern) {
3433
- return pattern.type === "sequence" &&
3434
- pattern.children[0].type === "reference" &&
3435
- pattern.children[0].name === name &&
3436
- pattern.children.length > 2;
3546
+ if (pattern.children.length === 0) {
3547
+ return false;
3548
+ }
3549
+ const firstChild = pattern.children[0];
3550
+ const lastChild = pattern.children[pattern.children.length - 1];
3551
+ const isLongEnough = pattern.children.length >= 2;
3552
+ return pattern.type === "sequence" && isLongEnough &&
3553
+ (firstChild.type === "reference" && firstChild.name === name) ||
3554
+ (lastChild.type === "reference" && lastChild.name === name);
3437
3555
  }
3438
3556
  _buildPattern(node) {
3439
3557
  const type = node.name;