clarity-pattern-parser 10.0.3 → 10.0.5

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
@@ -372,6 +372,14 @@ class CursorHistory {
372
372
  }
373
373
  }
374
374
 
375
+ class CyclicalParseError extends Error {
376
+ constructor(patternId, patternName, cursorIndex) {
377
+ super("Cyclical Parse Error");
378
+ this.patternId = patternId;
379
+ this.patternName = patternName;
380
+ this.cursorIndex = cursorIndex;
381
+ }
382
+ }
375
383
  class Cursor {
376
384
  get text() {
377
385
  return this._text;
@@ -477,14 +485,13 @@ class Cursor {
477
485
  this._history.stopRecording();
478
486
  }
479
487
  startParseWith(pattern) {
480
- const patternName = pattern.name;
481
488
  const trace = {
482
489
  pattern,
483
490
  cursorIndex: this.index
484
491
  };
485
- const hasCycle = this._stackTrace.find(t => t.pattern.id === pattern.id && this.index === t.cursorIndex);
492
+ const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
486
493
  if (hasCycle) {
487
- throw new Error(`Cyclical Pattern: ${this._stackTrace.map(t => `${t.pattern.name}#${t.pattern.id}{${t.cursorIndex}}`).join(" -> ")} -> ${patternName}#${pattern.id}{${this.index}}.`);
494
+ throw new CyclicalParseError(pattern.id, pattern.name, this.index);
488
495
  }
489
496
  this._history.pushStackTrace(trace);
490
497
  this._stackTrace.push(trace);
@@ -992,7 +999,13 @@ class Options {
992
999
  const results = [];
993
1000
  for (const pattern of this._children) {
994
1001
  cursor.moveTo(this._firstIndex);
995
- const result = pattern.parse(cursor);
1002
+ let result = null;
1003
+ try {
1004
+ result = pattern.parse(cursor);
1005
+ }
1006
+ catch (_a) {
1007
+ continue;
1008
+ }
996
1009
  if (this._isGreedy) {
997
1010
  results.push(result);
998
1011
  }
@@ -1988,8 +2001,8 @@ const anonymousPattern = new Options("anonymous-pattern", [
1988
2001
  ]);
1989
2002
 
1990
2003
  const optionalSpaces$3 = new Optional("optional-spaces", spaces$1);
1991
- const openBracket$1 = new Literal("open-bracket", "{");
1992
- const closeBracket$1 = new Literal("close-bracket", "}");
2004
+ const openBracket$1 = new Literal("repeat-open-bracket", "{");
2005
+ const closeBracket$1 = new Literal("repeat-close-bracket", "}");
1993
2006
  const comma = new Literal("comma", ",");
1994
2007
  const integer = new Regex("integer", "([1-9][0-9]*)|0");
1995
2008
  integer.setTokens(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
@@ -2026,15 +2039,15 @@ const closeParen = new Literal("repeat-close-paren", ")");
2026
2039
  const dividerComma = new Regex("divider-comma", "\\s*,\\s*");
2027
2040
  dividerComma.setTokens([", "]);
2028
2041
  const patternName$2 = name$1.clone("pattern-name");
2029
- const patterns$3 = new Options("or-patterns", [patternName$2, anonymousPattern]);
2030
- const dividerPattern = patterns$3.clone("divider-pattern");
2031
- const dividerSection = new Sequence("divider-section", [dividerComma, dividerPattern, trimFlag]);
2032
- const optionalDividerSection = new Optional("optional-divider-section", dividerSection);
2042
+ const repeatPattern = new Options("repeat-pattern", [patternName$2, anonymousPattern]);
2043
+ const repeatDividerPattern = repeatPattern.clone("repeat-divider-pattern");
2044
+ const repeatDividerSection = new Sequence("repeat-divider-section", [dividerComma, repeatDividerPattern, trimFlag]);
2045
+ const repeatOptionalDividerSection = new Optional("repeat-optional-divider-section", repeatDividerSection);
2033
2046
  const repeatLiteral = new Sequence("repeat-literal", [
2034
2047
  openParen,
2035
2048
  optionalSpaces$3,
2036
- patterns$3,
2037
- optionalDividerSection,
2049
+ repeatPattern,
2050
+ repeatOptionalDividerSection,
2038
2051
  optionalSpaces$3,
2039
2052
  closeParen,
2040
2053
  new Sequence("quantifier-section", [quantifier]),
@@ -2055,7 +2068,7 @@ const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: div
2055
2068
 
2056
2069
  const patternName = name$1.clone("pattern-name");
2057
2070
  patternName.setTokens(["[PATTERN_NAME]"]);
2058
- const patterns$1 = new Options("or-patterns", [patternName, anonymousPattern]);
2071
+ const patterns$1 = new Options("options-patterns", [patternName, anonymousPattern]);
2059
2072
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2060
2073
  defaultDivider.setTokens(["|"]);
2061
2074
  const greedyDivider = new Regex("greedy-divider", "\\s*[<][|][>]\\s*");
@@ -2102,7 +2115,7 @@ const bodyLine = new Sequence("body-line", [
2102
2115
  const body = new Optional("optional-body", new Repeat("body", bodyLine, { divider: newLine$1 }));
2103
2116
 
2104
2117
  const optionalSpaces$1 = new Optional("optional-spaces", allSpaces);
2105
- const optionalLineSpaces$1 = new Optional("options-line-spaces", lineSpaces$1);
2118
+ const optionalLineSpaces$1 = new Optional("optional-line-spaces", lineSpaces$1);
2106
2119
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
2107
2120
  importNameDivider.setTokens([", "]);
2108
2121
  const name = new Regex("import-name", "[^}\\s,]+");
@@ -2742,7 +2755,7 @@ class Grammar {
2742
2755
  const trimDivider = repeatNode.find(n => n.name === "trim-flag") != null;
2743
2756
  const patterNode = repeatNode.children[1].type === "spaces" ? repeatNode.children[2] : repeatNode.children[1];
2744
2757
  const pattern = this._buildPattern(patterNode);
2745
- const dividerSectionNode = repeatNode.find(n => n.name === "divider-section");
2758
+ const dividerSectionNode = repeatNode.find(n => n.name === "repeat-divider-section");
2746
2759
  const options = {
2747
2760
  min: 1,
2748
2761
  max: Infinity