clarity-pattern-parser 10.0.2 → 10.0.4

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.
@@ -52,6 +52,7 @@ export declare class Node {
52
52
  toString(): string;
53
53
  toCycleFreeObject(): CycleFreeNode;
54
54
  toJson(space?: number): string;
55
+ isEqual(node: Node): boolean;
55
56
  static createValueNode(name: string, value: string): Node;
56
57
  static createNode(name: string, children: Node[]): Node;
57
58
  }
@@ -225,6 +225,9 @@
225
225
  toJson(space) {
226
226
  return JSON.stringify(this.toCycleFreeObject(), null, space);
227
227
  }
228
+ isEqual(node) {
229
+ return node.toJson(0) === this.toJson(0);
230
+ }
228
231
  static createValueNode(name, value) {
229
232
  return new Node("custom-value-node", name, 0, 0, [], value);
230
233
  }
@@ -375,6 +378,13 @@
375
378
  }
376
379
  }
377
380
 
381
+ class CyclicalParseError extends Error {
382
+ constructor(patternId, patternName) {
383
+ super("Cyclical Parse Error");
384
+ this.patternId = patternId;
385
+ this.patternName = patternName;
386
+ }
387
+ }
378
388
  class Cursor {
379
389
  get text() {
380
390
  return this._text;
@@ -480,14 +490,13 @@
480
490
  this._history.stopRecording();
481
491
  }
482
492
  startParseWith(pattern) {
483
- const patternName = pattern.name;
484
493
  const trace = {
485
494
  pattern,
486
495
  cursorIndex: this.index
487
496
  };
488
- const hasCycle = this._stackTrace.find(t => t.pattern.id === pattern.id && this.index === t.cursorIndex);
497
+ const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
489
498
  if (hasCycle) {
490
- throw new Error(`Cyclical Pattern: ${this._stackTrace.map(t => `${t.pattern.name}#${t.pattern.id}{${t.cursorIndex}}`).join(" -> ")} -> ${patternName}#${pattern.id}{${this.index}}.`);
499
+ throw new CyclicalParseError(pattern.id, pattern.name);
491
500
  }
492
501
  this._history.pushStackTrace(trace);
493
502
  this._stackTrace.push(trace);
@@ -995,7 +1004,19 @@
995
1004
  const results = [];
996
1005
  for (const pattern of this._children) {
997
1006
  cursor.moveTo(this._firstIndex);
998
- const result = pattern.parse(cursor);
1007
+ let result = null;
1008
+ try {
1009
+ result = pattern.parse(cursor);
1010
+ }
1011
+ catch (error) {
1012
+ if (error.patternId === this._id) {
1013
+ continue;
1014
+ }
1015
+ else {
1016
+ cursor.endParse();
1017
+ throw error;
1018
+ }
1019
+ }
999
1020
  if (this._isGreedy) {
1000
1021
  results.push(result);
1001
1022
  }
@@ -1107,7 +1128,6 @@
1107
1128
  }
1108
1129
  }
1109
1130
  parse(cursor) {
1110
- var _a;
1111
1131
  cursor.startParseWith(this);
1112
1132
  const startIndex = cursor.index;
1113
1133
  const nodes = [];
@@ -1137,7 +1157,7 @@
1137
1157
  }
1138
1158
  }
1139
1159
  if (this._trimDivider && this._hasDivider) {
1140
- const isDividerLastMatch = ((_a = cursor.leafMatch.pattern) === null || _a === void 0 ? void 0 : _a.id) === this.children[1].id;
1160
+ const isDividerLastMatch = this.children.length > 1 && nodes[nodes.length - 1].name === this.children[1].name;
1141
1161
  if (isDividerLastMatch) {
1142
1162
  const node = nodes.pop();
1143
1163
  cursor.moveTo(node.firstIndex);
@@ -1408,10 +1428,11 @@
1408
1428
  return passed;
1409
1429
  }
1410
1430
  _createNode(cursor) {
1431
+ var _a;
1411
1432
  const hasDivider = this._divider != null;
1412
1433
  if (hasDivider &&
1413
1434
  this._trimDivider &&
1414
- cursor.leafMatch.pattern === this._divider) {
1435
+ this._nodes[this._nodes.length - 1].name === ((_a = this._divider) === null || _a === void 0 ? void 0 : _a.name)) {
1415
1436
  const dividerNode = this._nodes.pop();
1416
1437
  cursor.moveTo(dividerNode.firstIndex);
1417
1438
  }
@@ -1523,10 +1544,10 @@
1523
1544
  return this._children;
1524
1545
  }
1525
1546
  get min() {
1526
- return this.children[0].min;
1547
+ return this._options.min;
1527
1548
  }
1528
1549
  get max() {
1529
- return this.children[0].max || Infinity;
1550
+ return this._options.max;
1530
1551
  }
1531
1552
  constructor(name, pattern, options = {}) {
1532
1553
  this._id = `repeat-${idIndex$3++}`;
@@ -1762,7 +1783,7 @@
1762
1783
  const tokens = [];
1763
1784
  for (const child of this._children) {
1764
1785
  tokens.push(...child.getTokens());
1765
- if (child.type !== "optional") {
1786
+ if (child.type !== "optional" && child.type !== "not") {
1766
1787
  break;
1767
1788
  }
1768
1789
  }
@@ -1784,7 +1805,7 @@
1784
1805
  const patterns = [];
1785
1806
  for (const child of this._children) {
1786
1807
  patterns.push(...child.getPatterns());
1787
- if (child.type !== "optional") {
1808
+ if (child.type !== "optional" && child.type !== "not") {
1788
1809
  break;
1789
1810
  }
1790
1811
  }
@@ -1991,8 +2012,8 @@
1991
2012
  ]);
1992
2013
 
1993
2014
  const optionalSpaces$3 = new Optional("optional-spaces", spaces$1);
1994
- const openBracket$1 = new Literal("open-bracket", "{");
1995
- const closeBracket$1 = new Literal("close-bracket", "}");
2015
+ const openBracket$1 = new Literal("repeat-open-bracket", "{");
2016
+ const closeBracket$1 = new Literal("repeat-close-bracket", "}");
1996
2017
  const comma = new Literal("comma", ",");
1997
2018
  const integer = new Regex("integer", "([1-9][0-9]*)|0");
1998
2019
  integer.setTokens(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
@@ -2029,15 +2050,15 @@
2029
2050
  const dividerComma = new Regex("divider-comma", "\\s*,\\s*");
2030
2051
  dividerComma.setTokens([", "]);
2031
2052
  const patternName$2 = name$1.clone("pattern-name");
2032
- const patterns$3 = new Options("or-patterns", [patternName$2, anonymousPattern]);
2033
- const dividerPattern = patterns$3.clone("divider-pattern");
2034
- const dividerSection = new Sequence("divider-section", [dividerComma, dividerPattern, trimFlag]);
2035
- const optionalDividerSection = new Optional("optional-divider-section", dividerSection);
2053
+ const repeatPattern = new Options("repeat-pattern", [patternName$2, anonymousPattern]);
2054
+ const repeatDividerPattern = repeatPattern.clone("repeat-divider-pattern");
2055
+ const repeatDividerSection = new Sequence("repeat-divider-section", [dividerComma, repeatDividerPattern, trimFlag]);
2056
+ const repeatOptionalDividerSection = new Optional("repeat-optional-divider-section", repeatDividerSection);
2036
2057
  const repeatLiteral = new Sequence("repeat-literal", [
2037
2058
  openParen,
2038
2059
  optionalSpaces$3,
2039
- patterns$3,
2040
- optionalDividerSection,
2060
+ repeatPattern,
2061
+ repeatOptionalDividerSection,
2041
2062
  optionalSpaces$3,
2042
2063
  closeParen,
2043
2064
  new Sequence("quantifier-section", [quantifier]),
@@ -2058,7 +2079,7 @@
2058
2079
 
2059
2080
  const patternName = name$1.clone("pattern-name");
2060
2081
  patternName.setTokens(["[PATTERN_NAME]"]);
2061
- const patterns$1 = new Options("or-patterns", [patternName, anonymousPattern]);
2082
+ const patterns$1 = new Options("options-patterns", [patternName, anonymousPattern]);
2062
2083
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2063
2084
  defaultDivider.setTokens(["|"]);
2064
2085
  const greedyDivider = new Regex("greedy-divider", "\\s*[<][|][>]\\s*");
@@ -2105,7 +2126,7 @@
2105
2126
  const body = new Optional("optional-body", new Repeat("body", bodyLine, { divider: newLine$1 }));
2106
2127
 
2107
2128
  const optionalSpaces$1 = new Optional("optional-spaces", allSpaces);
2108
- const optionalLineSpaces$1 = new Optional("options-line-spaces", lineSpaces$1);
2129
+ const optionalLineSpaces$1 = new Optional("optional-line-spaces", lineSpaces$1);
2109
2130
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
2110
2131
  importNameDivider.setTokens([", "]);
2111
2132
  const name = new Regex("import-name", "[^}\\s,]+");
@@ -2208,9 +2229,6 @@
2208
2229
  get children() {
2209
2230
  return this._children;
2210
2231
  }
2211
- get isOptional() {
2212
- return false;
2213
- }
2214
2232
  constructor(name, pattern) {
2215
2233
  this._id = `not-${idIndex++}`;
2216
2234
  this._type = "not";
@@ -2748,7 +2766,7 @@
2748
2766
  const trimDivider = repeatNode.find(n => n.name === "trim-flag") != null;
2749
2767
  const patterNode = repeatNode.children[1].type === "spaces" ? repeatNode.children[2] : repeatNode.children[1];
2750
2768
  const pattern = this._buildPattern(patterNode);
2751
- const dividerSectionNode = repeatNode.find(n => n.name === "divider-section");
2769
+ const dividerSectionNode = repeatNode.find(n => n.name === "repeat-divider-section");
2752
2770
  const options = {
2753
2771
  min: 1,
2754
2772
  max: Infinity
@@ -2865,7 +2883,7 @@
2865
2883
  .importedPatternsByName
2866
2884
  .values());
2867
2885
  const grammar = new Grammar({
2868
- params: importedValues,
2886
+ params: [...importedValues, ...this._parseContext.paramsByName.values()],
2869
2887
  originResource: this._originResource,
2870
2888
  resolveImport: this._resolveImport
2871
2889
  });