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