clarity-pattern-parser 11.0.1 → 11.0.3

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*");
@@ -2635,7 +2638,6 @@ class Context {
2635
2638
  return Object.assign({}, this._patterns);
2636
2639
  }
2637
2640
  constructor(name, pattern, context = []) {
2638
- this.shouldCompactAst = false;
2639
2641
  this._id = `context-${contextId++}`;
2640
2642
  this._type = "context";
2641
2643
  this._name = name;
@@ -2659,7 +2661,6 @@ class Context {
2659
2661
  clone(name = this._name) {
2660
2662
  const clone = new Context(name, this._pattern, Object.values(this._patterns));
2661
2663
  clone._id = this._id;
2662
- clone.shouldCompactAst = this.shouldCompactAst;
2663
2664
  return clone;
2664
2665
  }
2665
2666
  getTokens() {
@@ -2807,7 +2808,7 @@ class PrecedenceTree {
2807
2808
  _compileAtomNode() {
2808
2809
  let node = this._atomNode;
2809
2810
  if (this._prefixNode != null && this._atomNode != null) {
2810
- node = this._prefixNode;
2811
+ node = this._prefixNode.findRoot();
2811
2812
  this._prefixPlaceholder.replaceWith(this._atomNode);
2812
2813
  }
2813
2814
  if (this._postfixNode != null && node != null) {
@@ -2856,7 +2857,7 @@ class PrecedenceTree {
2856
2857
  }
2857
2858
  }
2858
2859
 
2859
- let indexId = 0;
2860
+ let indexId$1 = 0;
2860
2861
  class ExpressionPattern {
2861
2862
  get id() {
2862
2863
  return this._id;
@@ -2895,7 +2896,7 @@ class ExpressionPattern {
2895
2896
  if (patterns.length === 0) {
2896
2897
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2897
2898
  }
2898
- this._id = `expression-${indexId++}`;
2899
+ this._id = `expression-${indexId$1++}`;
2899
2900
  this._type = "expression";
2900
2901
  this._name = name;
2901
2902
  this._parent = null;
@@ -3167,8 +3168,8 @@ class ExpressionPattern {
3167
3168
  break;
3168
3169
  }
3169
3170
  else {
3170
- cursor.resolveError();
3171
3171
  cursor.moveTo(onIndex);
3172
+ cursor.resolveError();
3172
3173
  }
3173
3174
  }
3174
3175
  if (!foundMatch) {
@@ -3232,6 +3233,88 @@ class ExpressionPattern {
3232
3233
  }
3233
3234
  }
3234
3235
 
3236
+ let indexId = 0;
3237
+ class RightAssociatedPattern {
3238
+ get id() {
3239
+ return this._id;
3240
+ }
3241
+ get type() {
3242
+ return this._type;
3243
+ }
3244
+ get name() {
3245
+ return this._name;
3246
+ }
3247
+ get parent() {
3248
+ return this._parent;
3249
+ }
3250
+ set parent(pattern) {
3251
+ this._parent = pattern;
3252
+ }
3253
+ get children() {
3254
+ return this._children;
3255
+ }
3256
+ get startedOnIndex() {
3257
+ return this._children[0].startedOnIndex;
3258
+ }
3259
+ constructor(pattern) {
3260
+ this._id = `right-associated-${indexId++}`;
3261
+ this._type = "right-associated";
3262
+ this._name = "";
3263
+ this._parent = null;
3264
+ this._children = [pattern.clone()];
3265
+ }
3266
+ parse(cursor) {
3267
+ return this.children[0].parse(cursor);
3268
+ }
3269
+ exec(text, record) {
3270
+ return this.children[0].exec(text, record);
3271
+ }
3272
+ test(text, record) {
3273
+ return this.children[0].test(text, record);
3274
+ }
3275
+ clone(_name) {
3276
+ const clone = new RightAssociatedPattern(this.children[0]);
3277
+ clone._id = this._id;
3278
+ return clone;
3279
+ }
3280
+ getTokens() {
3281
+ return this.children[0].getTokens();
3282
+ }
3283
+ getTokensAfter(_childReference) {
3284
+ if (this._parent == null) {
3285
+ return [];
3286
+ }
3287
+ return this._parent.getTokensAfter(this);
3288
+ }
3289
+ getNextTokens() {
3290
+ if (this._parent == null) {
3291
+ return [];
3292
+ }
3293
+ return this._parent.getTokensAfter(this);
3294
+ }
3295
+ getPatterns() {
3296
+ return this.children[0].getPatterns();
3297
+ }
3298
+ getPatternsAfter(_childReference) {
3299
+ if (this._parent == null) {
3300
+ return [];
3301
+ }
3302
+ return this._parent.getPatternsAfter(this);
3303
+ }
3304
+ getNextPatterns() {
3305
+ if (this._parent == null) {
3306
+ return [];
3307
+ }
3308
+ return this._parent.getPatternsAfter(this);
3309
+ }
3310
+ find(predicate) {
3311
+ return this.children[0].find(predicate);
3312
+ }
3313
+ isEqual(pattern) {
3314
+ return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
3315
+ }
3316
+ }
3317
+
3235
3318
  let anonymousIndexId = 0;
3236
3319
  const patternNodes = {
3237
3320
  "literal": true,
@@ -3413,7 +3496,15 @@ class Grammar {
3413
3496
  _buildOptions(name, node) {
3414
3497
  const patternNodes = node.children.filter(n => n.name !== "default-divider" && n.name !== "greedy-divider");
3415
3498
  const isGreedy = node.find(n => n.name === "greedy-divider") != null;
3416
- const patterns = patternNodes.map(n => this._buildPattern(n));
3499
+ const patterns = patternNodes.map(n => {
3500
+ const rightAssociated = n.find(n => n.name === "right-associated");
3501
+ if (rightAssociated != null) {
3502
+ return new RightAssociatedPattern(this._buildPattern(n.children[0]));
3503
+ }
3504
+ else {
3505
+ return this._buildPattern(n.children[0]);
3506
+ }
3507
+ });
3417
3508
  const hasRecursivePattern = patterns.some(p => this._isRecursive(name, p));
3418
3509
  if (hasRecursivePattern && !isGreedy) {
3419
3510
  try {
@@ -3432,10 +3523,15 @@ class Grammar {
3432
3523
  return this._isRecursivePattern(name, pattern);
3433
3524
  }
3434
3525
  _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;
3526
+ if (pattern.children.length === 0) {
3527
+ return false;
3528
+ }
3529
+ const firstChild = pattern.children[0];
3530
+ const lastChild = pattern.children[pattern.children.length - 1];
3531
+ const isLongEnough = pattern.children.length >= 2;
3532
+ return pattern.type === "sequence" && isLongEnough &&
3533
+ (firstChild.type === "reference" && firstChild.name === name) ||
3534
+ (lastChild.type === "reference" && lastChild.name === name);
3439
3535
  }
3440
3536
  _buildPattern(node) {
3441
3537
  const type = node.name;