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.
package/dist/index.js CHANGED
@@ -223,6 +223,9 @@ class Node {
223
223
  toJson(space) {
224
224
  return JSON.stringify(this.toCycleFreeObject(), null, space);
225
225
  }
226
+ isEqual(node) {
227
+ return node.toJson(0) === this.toJson(0);
228
+ }
226
229
  static createValueNode(name, value) {
227
230
  return new Node("custom-value-node", name, 0, 0, [], value);
228
231
  }
@@ -373,6 +376,13 @@ class CursorHistory {
373
376
  }
374
377
  }
375
378
 
379
+ class CyclicalParseError extends Error {
380
+ constructor(patternId, patternName) {
381
+ super("Cyclical Parse Error");
382
+ this.patternId = patternId;
383
+ this.patternName = patternName;
384
+ }
385
+ }
376
386
  class Cursor {
377
387
  get text() {
378
388
  return this._text;
@@ -478,14 +488,13 @@ class Cursor {
478
488
  this._history.stopRecording();
479
489
  }
480
490
  startParseWith(pattern) {
481
- const patternName = pattern.name;
482
491
  const trace = {
483
492
  pattern,
484
493
  cursorIndex: this.index
485
494
  };
486
- const hasCycle = this._stackTrace.find(t => t.pattern.id === pattern.id && this.index === t.cursorIndex);
495
+ const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
487
496
  if (hasCycle) {
488
- throw new Error(`Cyclical Pattern: ${this._stackTrace.map(t => `${t.pattern.name}#${t.pattern.id}{${t.cursorIndex}}`).join(" -> ")} -> ${patternName}#${pattern.id}{${this.index}}.`);
497
+ throw new CyclicalParseError(pattern.id, pattern.name);
489
498
  }
490
499
  this._history.pushStackTrace(trace);
491
500
  this._stackTrace.push(trace);
@@ -993,7 +1002,19 @@ class Options {
993
1002
  const results = [];
994
1003
  for (const pattern of this._children) {
995
1004
  cursor.moveTo(this._firstIndex);
996
- const result = pattern.parse(cursor);
1005
+ let result = null;
1006
+ try {
1007
+ result = pattern.parse(cursor);
1008
+ }
1009
+ catch (error) {
1010
+ if (error.patternId === this._id) {
1011
+ continue;
1012
+ }
1013
+ else {
1014
+ cursor.endParse();
1015
+ throw error;
1016
+ }
1017
+ }
997
1018
  if (this._isGreedy) {
998
1019
  results.push(result);
999
1020
  }
@@ -1105,7 +1126,6 @@ class FiniteRepeat {
1105
1126
  }
1106
1127
  }
1107
1128
  parse(cursor) {
1108
- var _a;
1109
1129
  cursor.startParseWith(this);
1110
1130
  const startIndex = cursor.index;
1111
1131
  const nodes = [];
@@ -1135,7 +1155,7 @@ class FiniteRepeat {
1135
1155
  }
1136
1156
  }
1137
1157
  if (this._trimDivider && this._hasDivider) {
1138
- const isDividerLastMatch = ((_a = cursor.leafMatch.pattern) === null || _a === void 0 ? void 0 : _a.id) === this.children[1].id;
1158
+ const isDividerLastMatch = this.children.length > 1 && nodes[nodes.length - 1].name === this.children[1].name;
1139
1159
  if (isDividerLastMatch) {
1140
1160
  const node = nodes.pop();
1141
1161
  cursor.moveTo(node.firstIndex);
@@ -1406,10 +1426,11 @@ class InfiniteRepeat {
1406
1426
  return passed;
1407
1427
  }
1408
1428
  _createNode(cursor) {
1429
+ var _a;
1409
1430
  const hasDivider = this._divider != null;
1410
1431
  if (hasDivider &&
1411
1432
  this._trimDivider &&
1412
- cursor.leafMatch.pattern === this._divider) {
1433
+ this._nodes[this._nodes.length - 1].name === ((_a = this._divider) === null || _a === void 0 ? void 0 : _a.name)) {
1413
1434
  const dividerNode = this._nodes.pop();
1414
1435
  cursor.moveTo(dividerNode.firstIndex);
1415
1436
  }
@@ -1521,10 +1542,10 @@ class Repeat {
1521
1542
  return this._children;
1522
1543
  }
1523
1544
  get min() {
1524
- return this.children[0].min;
1545
+ return this._options.min;
1525
1546
  }
1526
1547
  get max() {
1527
- return this.children[0].max || Infinity;
1548
+ return this._options.max;
1528
1549
  }
1529
1550
  constructor(name, pattern, options = {}) {
1530
1551
  this._id = `repeat-${idIndex$3++}`;
@@ -1760,7 +1781,7 @@ class Sequence {
1760
1781
  const tokens = [];
1761
1782
  for (const child of this._children) {
1762
1783
  tokens.push(...child.getTokens());
1763
- if (child.type !== "optional") {
1784
+ if (child.type !== "optional" && child.type !== "not") {
1764
1785
  break;
1765
1786
  }
1766
1787
  }
@@ -1782,7 +1803,7 @@ class Sequence {
1782
1803
  const patterns = [];
1783
1804
  for (const child of this._children) {
1784
1805
  patterns.push(...child.getPatterns());
1785
- if (child.type !== "optional") {
1806
+ if (child.type !== "optional" && child.type !== "not") {
1786
1807
  break;
1787
1808
  }
1788
1809
  }
@@ -1989,8 +2010,8 @@ const anonymousPattern = new Options("anonymous-pattern", [
1989
2010
  ]);
1990
2011
 
1991
2012
  const optionalSpaces$3 = new Optional("optional-spaces", spaces$1);
1992
- const openBracket$1 = new Literal("open-bracket", "{");
1993
- const closeBracket$1 = new Literal("close-bracket", "}");
2013
+ const openBracket$1 = new Literal("repeat-open-bracket", "{");
2014
+ const closeBracket$1 = new Literal("repeat-close-bracket", "}");
1994
2015
  const comma = new Literal("comma", ",");
1995
2016
  const integer = new Regex("integer", "([1-9][0-9]*)|0");
1996
2017
  integer.setTokens(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
@@ -2027,15 +2048,15 @@ const closeParen = new Literal("repeat-close-paren", ")");
2027
2048
  const dividerComma = new Regex("divider-comma", "\\s*,\\s*");
2028
2049
  dividerComma.setTokens([", "]);
2029
2050
  const patternName$2 = name$1.clone("pattern-name");
2030
- const patterns$3 = new Options("or-patterns", [patternName$2, anonymousPattern]);
2031
- const dividerPattern = patterns$3.clone("divider-pattern");
2032
- const dividerSection = new Sequence("divider-section", [dividerComma, dividerPattern, trimFlag]);
2033
- const optionalDividerSection = new Optional("optional-divider-section", dividerSection);
2051
+ const repeatPattern = new Options("repeat-pattern", [patternName$2, anonymousPattern]);
2052
+ const repeatDividerPattern = repeatPattern.clone("repeat-divider-pattern");
2053
+ const repeatDividerSection = new Sequence("repeat-divider-section", [dividerComma, repeatDividerPattern, trimFlag]);
2054
+ const repeatOptionalDividerSection = new Optional("repeat-optional-divider-section", repeatDividerSection);
2034
2055
  const repeatLiteral = new Sequence("repeat-literal", [
2035
2056
  openParen,
2036
2057
  optionalSpaces$3,
2037
- patterns$3,
2038
- optionalDividerSection,
2058
+ repeatPattern,
2059
+ repeatOptionalDividerSection,
2039
2060
  optionalSpaces$3,
2040
2061
  closeParen,
2041
2062
  new Sequence("quantifier-section", [quantifier]),
@@ -2056,7 +2077,7 @@ const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: div
2056
2077
 
2057
2078
  const patternName = name$1.clone("pattern-name");
2058
2079
  patternName.setTokens(["[PATTERN_NAME]"]);
2059
- const patterns$1 = new Options("or-patterns", [patternName, anonymousPattern]);
2080
+ const patterns$1 = new Options("options-patterns", [patternName, anonymousPattern]);
2060
2081
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2061
2082
  defaultDivider.setTokens(["|"]);
2062
2083
  const greedyDivider = new Regex("greedy-divider", "\\s*[<][|][>]\\s*");
@@ -2103,7 +2124,7 @@ const bodyLine = new Sequence("body-line", [
2103
2124
  const body = new Optional("optional-body", new Repeat("body", bodyLine, { divider: newLine$1 }));
2104
2125
 
2105
2126
  const optionalSpaces$1 = new Optional("optional-spaces", allSpaces);
2106
- const optionalLineSpaces$1 = new Optional("options-line-spaces", lineSpaces$1);
2127
+ const optionalLineSpaces$1 = new Optional("optional-line-spaces", lineSpaces$1);
2107
2128
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
2108
2129
  importNameDivider.setTokens([", "]);
2109
2130
  const name = new Regex("import-name", "[^}\\s,]+");
@@ -2206,9 +2227,6 @@ class Not {
2206
2227
  get children() {
2207
2228
  return this._children;
2208
2229
  }
2209
- get isOptional() {
2210
- return false;
2211
- }
2212
2230
  constructor(name, pattern) {
2213
2231
  this._id = `not-${idIndex++}`;
2214
2232
  this._type = "not";
@@ -2746,7 +2764,7 @@ class Grammar {
2746
2764
  const trimDivider = repeatNode.find(n => n.name === "trim-flag") != null;
2747
2765
  const patterNode = repeatNode.children[1].type === "spaces" ? repeatNode.children[2] : repeatNode.children[1];
2748
2766
  const pattern = this._buildPattern(patterNode);
2749
- const dividerSectionNode = repeatNode.find(n => n.name === "divider-section");
2767
+ const dividerSectionNode = repeatNode.find(n => n.name === "repeat-divider-section");
2750
2768
  const options = {
2751
2769
  min: 1,
2752
2770
  max: Infinity
@@ -2863,7 +2881,7 @@ class Grammar {
2863
2881
  .importedPatternsByName
2864
2882
  .values());
2865
2883
  const grammar = new Grammar({
2866
- params: importedValues,
2884
+ params: [...importedValues, ...this._parseContext.paramsByName.values()],
2867
2885
  originResource: this._originResource,
2868
2886
  resolveImport: this._resolveImport
2869
2887
  });