clarity-pattern-parser 11.0.2 → 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*");
@@ -2856,7 +2859,7 @@
2856
2859
  }
2857
2860
  }
2858
2861
 
2859
- let indexId = 0;
2862
+ let indexId$1 = 0;
2860
2863
  class ExpressionPattern {
2861
2864
  get id() {
2862
2865
  return this._id;
@@ -2895,7 +2898,7 @@
2895
2898
  if (patterns.length === 0) {
2896
2899
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2897
2900
  }
2898
- this._id = `expression-${indexId++}`;
2901
+ this._id = `expression-${indexId$1++}`;
2899
2902
  this._type = "expression";
2900
2903
  this._name = name;
2901
2904
  this._parent = null;
@@ -3232,6 +3235,88 @@
3232
3235
  }
3233
3236
  }
3234
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
+
3235
3320
  let anonymousIndexId = 0;
3236
3321
  const patternNodes = {
3237
3322
  "literal": true,
@@ -3413,7 +3498,15 @@
3413
3498
  _buildOptions(name, node) {
3414
3499
  const patternNodes = node.children.filter(n => n.name !== "default-divider" && n.name !== "greedy-divider");
3415
3500
  const isGreedy = node.find(n => n.name === "greedy-divider") != null;
3416
- 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
+ });
3417
3510
  const hasRecursivePattern = patterns.some(p => this._isRecursive(name, p));
3418
3511
  if (hasRecursivePattern && !isGreedy) {
3419
3512
  try {
@@ -3432,10 +3525,15 @@
3432
3525
  return this._isRecursivePattern(name, pattern);
3433
3526
  }
3434
3527
  _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;
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);
3439
3537
  }
3440
3538
  _buildPattern(node) {
3441
3539
  const type = node.name;