clarity-pattern-parser 11.2.0 → 11.2.1

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
@@ -554,7 +554,7 @@ function testPattern(pattern, text, record = false) {
554
554
  return !result.cursor.hasError;
555
555
  }
556
556
 
557
- let idIndex$9 = 0;
557
+ let idIndex$a = 0;
558
558
  class Literal {
559
559
  get id() {
560
560
  return this._id;
@@ -584,7 +584,7 @@ class Literal {
584
584
  if (value.length === 0) {
585
585
  throw new Error("Value Cannot be empty.");
586
586
  }
587
- this._id = `literal-${idIndex$9++}`;
587
+ this._id = `literal-${idIndex$a++}`;
588
588
  this._type = "literal";
589
589
  this._name = name;
590
590
  this._token = value;
@@ -675,7 +675,7 @@ class Literal {
675
675
  }
676
676
  }
677
677
 
678
- let idIndex$8 = 0;
678
+ let idIndex$9 = 0;
679
679
  class Regex {
680
680
  get id() {
681
681
  return this._id;
@@ -707,7 +707,7 @@ class Regex {
707
707
  this._firstIndex = 0;
708
708
  this._substring = "";
709
709
  this._tokens = [];
710
- this._id = `regex-${idIndex$8++}`;
710
+ this._id = `regex-${idIndex$9++}`;
711
711
  this._type = "regex";
712
712
  this._name = name;
713
713
  this._parent = null;
@@ -827,7 +827,7 @@ function findPattern(pattern, predicate) {
827
827
  }
828
828
  }
829
829
 
830
- let idIndex$7 = 0;
830
+ let idIndex$8 = 0;
831
831
  class Reference {
832
832
  get id() {
833
833
  return this._id;
@@ -851,7 +851,7 @@ class Reference {
851
851
  return this._firstIndex;
852
852
  }
853
853
  constructor(name, referencePatternName) {
854
- this._id = `reference-${idIndex$7++}`;
854
+ this._id = `reference-${idIndex$8++}`;
855
855
  this._type = "reference";
856
856
  this._name = name;
857
857
  this._referencePatternName = referencePatternName || name;
@@ -1028,7 +1028,7 @@ function isRecursivePattern(pattern) {
1028
1028
  return false;
1029
1029
  }
1030
1030
 
1031
- let idIndex$6 = 0;
1031
+ let idIndex$7 = 0;
1032
1032
  class Options {
1033
1033
  get id() {
1034
1034
  return this._id;
@@ -1057,7 +1057,7 @@ class Options {
1057
1057
  }
1058
1058
  const children = clonePatterns(options);
1059
1059
  this._assignChildrenToParent(children);
1060
- this._id = `options-${idIndex$6++}`;
1060
+ this._id = `options-${idIndex$7++}`;
1061
1061
  this._type = "options";
1062
1062
  this._name = name;
1063
1063
  this._parent = null;
@@ -1162,7 +1162,7 @@ class Options {
1162
1162
  }
1163
1163
  }
1164
1164
 
1165
- let idIndex$5 = 0;
1165
+ let idIndex$6 = 0;
1166
1166
  class FiniteRepeat {
1167
1167
  get id() {
1168
1168
  return this._id;
@@ -1192,7 +1192,7 @@ class FiniteRepeat {
1192
1192
  return this._firstIndex;
1193
1193
  }
1194
1194
  constructor(name, pattern, options = {}) {
1195
- this._id = `finite-repeat-${idIndex$5++}`;
1195
+ this._id = `finite-repeat-${idIndex$6++}`;
1196
1196
  this._type = "finite-repeat";
1197
1197
  this._name = name;
1198
1198
  this._parent = null;
@@ -1334,7 +1334,7 @@ class FiniteRepeat {
1334
1334
  }
1335
1335
  }
1336
1336
 
1337
- let idIndex$4 = 0;
1337
+ let idIndex$5 = 0;
1338
1338
  class InfiniteRepeat {
1339
1339
  get id() {
1340
1340
  return this._id;
@@ -1371,7 +1371,7 @@ class InfiniteRepeat {
1371
1371
  children = [pattern.clone()];
1372
1372
  }
1373
1373
  this._assignChildrenToParent(children);
1374
- this._id = `infinite-repeat-${idIndex$4++}`;
1374
+ this._id = `infinite-repeat-${idIndex$5++}`;
1375
1375
  this._type = "infinite-repeat";
1376
1376
  this._name = name;
1377
1377
  this._min = min;
@@ -1589,7 +1589,7 @@ class InfiniteRepeat {
1589
1589
  }
1590
1590
  }
1591
1591
 
1592
- let idIndex$3 = 0;
1592
+ let idIndex$4 = 0;
1593
1593
  class Repeat {
1594
1594
  get id() {
1595
1595
  return this._id;
@@ -1625,7 +1625,7 @@ class Repeat {
1625
1625
  return this._options;
1626
1626
  }
1627
1627
  constructor(name, pattern, options = {}) {
1628
- this._id = `repeat-${idIndex$3++}`;
1628
+ this._id = `repeat-${idIndex$4++}`;
1629
1629
  this._pattern = pattern;
1630
1630
  this._parent = null;
1631
1631
  this._options = Object.assign(Object.assign({}, options), { min: options.min == null ? 1 : options.min, max: options.max == null ? Infinity : options.max });
@@ -1704,7 +1704,7 @@ function filterOutNull(nodes) {
1704
1704
  return filteredNodes;
1705
1705
  }
1706
1706
 
1707
- let idIndex$2 = 0;
1707
+ let idIndex$3 = 0;
1708
1708
  class Sequence {
1709
1709
  get id() {
1710
1710
  return this._id;
@@ -1733,7 +1733,7 @@ class Sequence {
1733
1733
  }
1734
1734
  const children = clonePatterns(sequence);
1735
1735
  this._assignChildrenToParent(children);
1736
- this._id = `sequence-${idIndex$2++}`;
1736
+ this._id = `sequence-${idIndex$3++}`;
1737
1737
  this._type = "sequence";
1738
1738
  this._name = name;
1739
1739
  this._parent = null;
@@ -1969,12 +1969,13 @@ const anonymousLiterals = new Options("anonymous-literals", [
1969
1969
  new Reference("repeat-literal"),
1970
1970
  ]);
1971
1971
  const anonymousWrappedLiterals = new Options("anonymous-wrapped-literals", [
1972
+ new Reference("take-until-literal"),
1972
1973
  new Reference("options-literal"),
1973
1974
  new Reference("sequence-literal"),
1974
1975
  new Reference("complex-anonymous-pattern")
1975
1976
  ]);
1976
1977
 
1977
- let idIndex$1 = 0;
1978
+ let idIndex$2 = 0;
1978
1979
  class Optional {
1979
1980
  get id() {
1980
1981
  return this._id;
@@ -1998,7 +1999,7 @@ class Optional {
1998
1999
  return this._children[0].startedOnIndex;
1999
2000
  }
2000
2001
  constructor(name, pattern) {
2001
- this._id = `optional-${idIndex$1++}`;
2002
+ this._id = `optional-${idIndex$2++}`;
2002
2003
  this._type = "optional";
2003
2004
  this._name = name;
2004
2005
  this._parent = null;
@@ -2070,12 +2071,12 @@ class Optional {
2070
2071
 
2071
2072
  const inlinePatternOpenParen = new Literal("anonymous-pattern-open-paren", "(");
2072
2073
  const inlinePatternCloseParen = new Literal("anonymous-pattern-close-paren", ")");
2073
- const optionalLineSpaces$3 = new Optional("optional-line-spaces", lineSpaces$1);
2074
+ const optionalLineSpaces$4 = new Optional("optional-line-spaces", lineSpaces$1);
2074
2075
  const complexAnonymousPattern = new Sequence("complex-anonymous-pattern", [
2075
2076
  inlinePatternOpenParen,
2076
- optionalLineSpaces$3,
2077
+ optionalLineSpaces$4,
2077
2078
  anonymousWrappedLiterals,
2078
- optionalLineSpaces$3,
2079
+ optionalLineSpaces$4,
2079
2080
  inlinePatternCloseParen,
2080
2081
  ]);
2081
2082
  const anonymousPattern = new Options("anonymous-pattern", [
@@ -2162,6 +2163,20 @@ greedyDivider.setTokens(["<|>"]);
2162
2163
  const divider = new Options("options-divider", [defaultDivider, greedyDivider]);
2163
2164
  const optionsLiteral = new Repeat("options-literal", patterns$1, { divider, min: 2, trimDivider: true });
2164
2165
 
2166
+ const anyChar = new Literal("any-char", "?");
2167
+ const upTo = new Literal("up-to", "->");
2168
+ const wall = new Literal("wall", "|");
2169
+ const optionalLineSpaces$3 = new Optional("optional-line-spaces", lineSpaces$1);
2170
+ const takeUntilLiteral = new Sequence("take-until-literal", [
2171
+ anyChar,
2172
+ optionalLineSpaces$3,
2173
+ upTo,
2174
+ optionalLineSpaces$3,
2175
+ wall,
2176
+ optionalLineSpaces$3,
2177
+ new Reference("pattern")
2178
+ ]);
2179
+
2165
2180
  const aliasLiteral = name$1.clone("alias-literal");
2166
2181
  aliasLiteral.setTokens(["[ALIAS_LITERAL]"]);
2167
2182
  const optionalIsOptional = new Optional("optional-flag", new Literal("is-optional", "?"));
@@ -2170,6 +2185,7 @@ const pattern = new Options("pattern", [
2170
2185
  literal$1,
2171
2186
  regexLiteral,
2172
2187
  repeatLiteral,
2188
+ takeUntilLiteral,
2173
2189
  aliasLiteral,
2174
2190
  optionsLiteral,
2175
2191
  sequenceLiteral,
@@ -2363,7 +2379,7 @@ const grammar = new Sequence("grammar", [
2363
2379
  optionalSpaces
2364
2380
  ]);
2365
2381
 
2366
- let idIndex = 0;
2382
+ let idIndex$1 = 0;
2367
2383
  class Not {
2368
2384
  get id() {
2369
2385
  return this._id;
@@ -2387,7 +2403,7 @@ class Not {
2387
2403
  return this.children[0].startedOnIndex;
2388
2404
  }
2389
2405
  constructor(name, pattern) {
2390
- this._id = `not-${idIndex++}`;
2406
+ this._id = `not-${idIndex$1++}`;
2391
2407
  this._type = "not";
2392
2408
  this._name = name;
2393
2409
  this._parent = null;
@@ -3156,6 +3172,126 @@ class Expression {
3156
3172
  }
3157
3173
  }
3158
3174
 
3175
+ let idIndex = 0;
3176
+ class TakeUntil {
3177
+ get id() {
3178
+ return this._id;
3179
+ }
3180
+ get type() {
3181
+ return this._type;
3182
+ }
3183
+ get name() {
3184
+ return this._name;
3185
+ }
3186
+ get children() {
3187
+ return this._children;
3188
+ }
3189
+ get parent() {
3190
+ return this._parent;
3191
+ }
3192
+ set parent(pattern) {
3193
+ this._parent = pattern;
3194
+ }
3195
+ get startedOnIndex() {
3196
+ return this._startedOnIndex;
3197
+ }
3198
+ constructor(name, terminatingPattern) {
3199
+ this._id = String(idIndex++);
3200
+ this._type = "take-until";
3201
+ this._name = name;
3202
+ this._parent = null;
3203
+ this._terminatingPattern = terminatingPattern;
3204
+ this._children = [this._terminatingPattern];
3205
+ this._tokens = [];
3206
+ this._startedOnIndex = 0;
3207
+ }
3208
+ parse(cursor) {
3209
+ let cursorIndex = cursor.index;
3210
+ let foundMatch = false;
3211
+ this._startedOnIndex = cursor.index;
3212
+ let terminatingResult = this._terminatingPattern.parse(cursor);
3213
+ if (terminatingResult == null) {
3214
+ foundMatch = true;
3215
+ cursor.moveTo(cursorIndex);
3216
+ cursorIndex += 1;
3217
+ cursor.hasNext() && cursor.next();
3218
+ cursor.resolveError();
3219
+ }
3220
+ while (true) {
3221
+ terminatingResult = this._terminatingPattern.parse(cursor);
3222
+ if (terminatingResult == null) {
3223
+ cursor.moveTo(cursorIndex);
3224
+ cursorIndex += 1;
3225
+ if (cursor.hasNext()) {
3226
+ cursor.next();
3227
+ }
3228
+ else {
3229
+ break;
3230
+ }
3231
+ cursor.resolveError();
3232
+ }
3233
+ else {
3234
+ break;
3235
+ }
3236
+ }
3237
+ if (foundMatch) {
3238
+ cursor.moveTo(cursorIndex - 1);
3239
+ const value = cursor.getChars(this.startedOnIndex, cursorIndex - 1);
3240
+ return Node.createValueNode(this._type, this._name, value);
3241
+ }
3242
+ else {
3243
+ cursor.resolveError();
3244
+ cursor.moveTo(this.startedOnIndex);
3245
+ cursor.recordErrorAt(this._startedOnIndex, this._startedOnIndex, this);
3246
+ return null;
3247
+ }
3248
+ }
3249
+ exec(text, record) {
3250
+ return execPattern(this, text, record);
3251
+ }
3252
+ test(text, record) {
3253
+ return testPattern(this, text, record);
3254
+ }
3255
+ clone(name = this.name) {
3256
+ const clone = new TakeUntil(name, this._terminatingPattern);
3257
+ clone._id = this._id;
3258
+ return clone;
3259
+ }
3260
+ getTokens() {
3261
+ return this._tokens;
3262
+ }
3263
+ getTokensAfter(_childReference) {
3264
+ return [];
3265
+ }
3266
+ getNextTokens() {
3267
+ if (this.parent == null) {
3268
+ return [];
3269
+ }
3270
+ return this.parent.getTokensAfter(this);
3271
+ }
3272
+ getPatterns() {
3273
+ return [this];
3274
+ }
3275
+ getPatternsAfter(_childReference) {
3276
+ return [];
3277
+ }
3278
+ getNextPatterns() {
3279
+ if (this.parent == null) {
3280
+ return [];
3281
+ }
3282
+ return this.parent.getPatternsAfter(this);
3283
+ }
3284
+ find(_predicate) {
3285
+ return null;
3286
+ }
3287
+ setTokens(tokens) {
3288
+ this._tokens = tokens;
3289
+ }
3290
+ isEqual(pattern) {
3291
+ return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
3292
+ }
3293
+ }
3294
+
3159
3295
  let indexId = 0;
3160
3296
  class RightAssociated {
3161
3297
  get id() {
@@ -3286,6 +3422,7 @@ const patternNodes = {
3286
3422
  "sequence-literal": true,
3287
3423
  "repeat-literal": true,
3288
3424
  "alias-literal": true,
3425
+ "take-until-literal": true,
3289
3426
  "configurable-anonymous-pattern": true
3290
3427
  };
3291
3428
  class ParseContext {
@@ -3396,6 +3533,10 @@ class Grammar {
3396
3533
  this._saveAlias(n);
3397
3534
  break;
3398
3535
  }
3536
+ case "take-until-literal": {
3537
+ this._saveTakeUntil(n);
3538
+ break;
3539
+ }
3399
3540
  case "configurable-anonymous-pattern": {
3400
3541
  this._saveConfigurableAnonymous(n);
3401
3542
  break;
@@ -3515,6 +3656,9 @@ class Grammar {
3515
3656
  case "sequence-literal": {
3516
3657
  return this._buildSequence(name, node);
3517
3658
  }
3659
+ case "take-until-literal": {
3660
+ return this._buildTakeUntil(name, node);
3661
+ }
3518
3662
  case "complex-anonymous-pattern": {
3519
3663
  return this._buildComplexAnonymousPattern(node);
3520
3664
  }
@@ -3598,6 +3742,19 @@ class Grammar {
3598
3742
  }
3599
3743
  return isOptional ? new Optional(name, new Repeat(`inner-optional-${name}`, pattern, options)) : new Repeat(name, pattern, options);
3600
3744
  }
3745
+ _saveTakeUntil(statementNode) {
3746
+ const nameNode = statementNode.find(n => n.name === "name");
3747
+ const name = nameNode.value;
3748
+ const takeUntilNode = statementNode.find(n => n.name === "take-until-literal");
3749
+ const takeUntil = this._buildTakeUntil(name, takeUntilNode);
3750
+ this._applyDecorators(statementNode, takeUntil);
3751
+ this._parseContext.patternsByName.set(name, takeUntil);
3752
+ }
3753
+ _buildTakeUntil(name, takeUntilNode) {
3754
+ const patternNode = takeUntilNode.children[takeUntilNode.children.length - 1];
3755
+ const untilPattern = this._buildPattern(patternNode);
3756
+ return new TakeUntil(name, untilPattern);
3757
+ }
3601
3758
  _saveConfigurableAnonymous(node) {
3602
3759
  const nameNode = node.find(n => n.name === "name");
3603
3760
  const name = nameNode.value;