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.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*");
@@ -2631,7 +2634,6 @@ class Context {
2631
2634
  return Object.assign({}, this._patterns);
2632
2635
  }
2633
2636
  constructor(name, pattern, context = []) {
2634
- this.shouldCompactAst = false;
2635
2637
  this._id = `context-${contextId++}`;
2636
2638
  this._type = "context";
2637
2639
  this._name = name;
@@ -2655,7 +2657,6 @@ class Context {
2655
2657
  clone(name = this._name) {
2656
2658
  const clone = new Context(name, this._pattern, Object.values(this._patterns));
2657
2659
  clone._id = this._id;
2658
- clone.shouldCompactAst = this.shouldCompactAst;
2659
2660
  return clone;
2660
2661
  }
2661
2662
  getTokens() {
@@ -2803,7 +2804,7 @@ class PrecedenceTree {
2803
2804
  _compileAtomNode() {
2804
2805
  let node = this._atomNode;
2805
2806
  if (this._prefixNode != null && this._atomNode != null) {
2806
- node = this._prefixNode;
2807
+ node = this._prefixNode.findRoot();
2807
2808
  this._prefixPlaceholder.replaceWith(this._atomNode);
2808
2809
  }
2809
2810
  if (this._postfixNode != null && node != null) {
@@ -2852,7 +2853,7 @@ class PrecedenceTree {
2852
2853
  }
2853
2854
  }
2854
2855
 
2855
- let indexId = 0;
2856
+ let indexId$1 = 0;
2856
2857
  class ExpressionPattern {
2857
2858
  get id() {
2858
2859
  return this._id;
@@ -2891,7 +2892,7 @@ class ExpressionPattern {
2891
2892
  if (patterns.length === 0) {
2892
2893
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2893
2894
  }
2894
- this._id = `expression-${indexId++}`;
2895
+ this._id = `expression-${indexId$1++}`;
2895
2896
  this._type = "expression";
2896
2897
  this._name = name;
2897
2898
  this._parent = null;
@@ -3163,8 +3164,8 @@ class ExpressionPattern {
3163
3164
  break;
3164
3165
  }
3165
3166
  else {
3166
- cursor.resolveError();
3167
3167
  cursor.moveTo(onIndex);
3168
+ cursor.resolveError();
3168
3169
  }
3169
3170
  }
3170
3171
  if (!foundMatch) {
@@ -3228,6 +3229,88 @@ class ExpressionPattern {
3228
3229
  }
3229
3230
  }
3230
3231
 
3232
+ let indexId = 0;
3233
+ class RightAssociatedPattern {
3234
+ get id() {
3235
+ return this._id;
3236
+ }
3237
+ get type() {
3238
+ return this._type;
3239
+ }
3240
+ get name() {
3241
+ return this._name;
3242
+ }
3243
+ get parent() {
3244
+ return this._parent;
3245
+ }
3246
+ set parent(pattern) {
3247
+ this._parent = pattern;
3248
+ }
3249
+ get children() {
3250
+ return this._children;
3251
+ }
3252
+ get startedOnIndex() {
3253
+ return this._children[0].startedOnIndex;
3254
+ }
3255
+ constructor(pattern) {
3256
+ this._id = `right-associated-${indexId++}`;
3257
+ this._type = "right-associated";
3258
+ this._name = "";
3259
+ this._parent = null;
3260
+ this._children = [pattern.clone()];
3261
+ }
3262
+ parse(cursor) {
3263
+ return this.children[0].parse(cursor);
3264
+ }
3265
+ exec(text, record) {
3266
+ return this.children[0].exec(text, record);
3267
+ }
3268
+ test(text, record) {
3269
+ return this.children[0].test(text, record);
3270
+ }
3271
+ clone(_name) {
3272
+ const clone = new RightAssociatedPattern(this.children[0]);
3273
+ clone._id = this._id;
3274
+ return clone;
3275
+ }
3276
+ getTokens() {
3277
+ return this.children[0].getTokens();
3278
+ }
3279
+ getTokensAfter(_childReference) {
3280
+ if (this._parent == null) {
3281
+ return [];
3282
+ }
3283
+ return this._parent.getTokensAfter(this);
3284
+ }
3285
+ getNextTokens() {
3286
+ if (this._parent == null) {
3287
+ return [];
3288
+ }
3289
+ return this._parent.getTokensAfter(this);
3290
+ }
3291
+ getPatterns() {
3292
+ return this.children[0].getPatterns();
3293
+ }
3294
+ getPatternsAfter(_childReference) {
3295
+ if (this._parent == null) {
3296
+ return [];
3297
+ }
3298
+ return this._parent.getPatternsAfter(this);
3299
+ }
3300
+ getNextPatterns() {
3301
+ if (this._parent == null) {
3302
+ return [];
3303
+ }
3304
+ return this._parent.getPatternsAfter(this);
3305
+ }
3306
+ find(predicate) {
3307
+ return this.children[0].find(predicate);
3308
+ }
3309
+ isEqual(pattern) {
3310
+ return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
3311
+ }
3312
+ }
3313
+
3231
3314
  let anonymousIndexId = 0;
3232
3315
  const patternNodes = {
3233
3316
  "literal": true,
@@ -3409,7 +3492,15 @@ class Grammar {
3409
3492
  _buildOptions(name, node) {
3410
3493
  const patternNodes = node.children.filter(n => n.name !== "default-divider" && n.name !== "greedy-divider");
3411
3494
  const isGreedy = node.find(n => n.name === "greedy-divider") != null;
3412
- const patterns = patternNodes.map(n => this._buildPattern(n));
3495
+ const patterns = patternNodes.map(n => {
3496
+ const rightAssociated = n.find(n => n.name === "right-associated");
3497
+ if (rightAssociated != null) {
3498
+ return new RightAssociatedPattern(this._buildPattern(n.children[0]));
3499
+ }
3500
+ else {
3501
+ return this._buildPattern(n.children[0]);
3502
+ }
3503
+ });
3413
3504
  const hasRecursivePattern = patterns.some(p => this._isRecursive(name, p));
3414
3505
  if (hasRecursivePattern && !isGreedy) {
3415
3506
  try {
@@ -3428,10 +3519,15 @@ class Grammar {
3428
3519
  return this._isRecursivePattern(name, pattern);
3429
3520
  }
3430
3521
  _isRecursivePattern(name, pattern) {
3431
- return pattern.type === "sequence" &&
3432
- pattern.children[0].type === "reference" &&
3433
- pattern.children[0].name === name &&
3434
- pattern.children.length > 2;
3522
+ if (pattern.children.length === 0) {
3523
+ return false;
3524
+ }
3525
+ const firstChild = pattern.children[0];
3526
+ const lastChild = pattern.children[pattern.children.length - 1];
3527
+ const isLongEnough = pattern.children.length >= 2;
3528
+ return pattern.type === "sequence" && isLongEnough &&
3529
+ (firstChild.type === "reference" && firstChild.name === name) ||
3530
+ (lastChild.type === "reference" && lastChild.name === name);
3435
3531
  }
3436
3532
  _buildPattern(node) {
3437
3533
  const type = node.name;