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.
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*");
@@ -2854,7 +2857,7 @@ class PrecedenceTree {
2854
2857
  }
2855
2858
  }
2856
2859
 
2857
- let indexId = 0;
2860
+ let indexId$1 = 0;
2858
2861
  class ExpressionPattern {
2859
2862
  get id() {
2860
2863
  return this._id;
@@ -2893,7 +2896,7 @@ class ExpressionPattern {
2893
2896
  if (patterns.length === 0) {
2894
2897
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2895
2898
  }
2896
- this._id = `expression-${indexId++}`;
2899
+ this._id = `expression-${indexId$1++}`;
2897
2900
  this._type = "expression";
2898
2901
  this._name = name;
2899
2902
  this._parent = null;
@@ -3230,6 +3233,88 @@ class ExpressionPattern {
3230
3233
  }
3231
3234
  }
3232
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
+
3233
3318
  let anonymousIndexId = 0;
3234
3319
  const patternNodes = {
3235
3320
  "literal": true,
@@ -3411,7 +3496,15 @@ class Grammar {
3411
3496
  _buildOptions(name, node) {
3412
3497
  const patternNodes = node.children.filter(n => n.name !== "default-divider" && n.name !== "greedy-divider");
3413
3498
  const isGreedy = node.find(n => n.name === "greedy-divider") != null;
3414
- 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
+ });
3415
3508
  const hasRecursivePattern = patterns.some(p => this._isRecursive(name, p));
3416
3509
  if (hasRecursivePattern && !isGreedy) {
3417
3510
  try {
@@ -3430,10 +3523,15 @@ class Grammar {
3430
3523
  return this._isRecursivePattern(name, pattern);
3431
3524
  }
3432
3525
  _isRecursivePattern(name, pattern) {
3433
- return pattern.type === "sequence" &&
3434
- pattern.children[0].type === "reference" &&
3435
- pattern.children[0].name === name &&
3436
- 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);
3437
3535
  }
3438
3536
  _buildPattern(node) {
3439
3537
  const type = node.name;