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.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*");
@@ -2850,7 +2853,7 @@ class PrecedenceTree {
2850
2853
  }
2851
2854
  }
2852
2855
 
2853
- let indexId = 0;
2856
+ let indexId$1 = 0;
2854
2857
  class ExpressionPattern {
2855
2858
  get id() {
2856
2859
  return this._id;
@@ -2889,7 +2892,7 @@ class ExpressionPattern {
2889
2892
  if (patterns.length === 0) {
2890
2893
  throw new Error("Need at least one pattern with an 'expression' pattern.");
2891
2894
  }
2892
- this._id = `expression-${indexId++}`;
2895
+ this._id = `expression-${indexId$1++}`;
2893
2896
  this._type = "expression";
2894
2897
  this._name = name;
2895
2898
  this._parent = null;
@@ -3226,6 +3229,88 @@ class ExpressionPattern {
3226
3229
  }
3227
3230
  }
3228
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
+
3229
3314
  let anonymousIndexId = 0;
3230
3315
  const patternNodes = {
3231
3316
  "literal": true,
@@ -3407,7 +3492,15 @@ class Grammar {
3407
3492
  _buildOptions(name, node) {
3408
3493
  const patternNodes = node.children.filter(n => n.name !== "default-divider" && n.name !== "greedy-divider");
3409
3494
  const isGreedy = node.find(n => n.name === "greedy-divider") != null;
3410
- 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
+ });
3411
3504
  const hasRecursivePattern = patterns.some(p => this._isRecursive(name, p));
3412
3505
  if (hasRecursivePattern && !isGreedy) {
3413
3506
  try {
@@ -3426,10 +3519,15 @@ class Grammar {
3426
3519
  return this._isRecursivePattern(name, pattern);
3427
3520
  }
3428
3521
  _isRecursivePattern(name, pattern) {
3429
- return pattern.type === "sequence" &&
3430
- pattern.children[0].type === "reference" &&
3431
- pattern.children[0].name === name &&
3432
- 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);
3433
3531
  }
3434
3532
  _buildPattern(node) {
3435
3533
  const type = node.name;