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