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.js CHANGED
@@ -376,6 +376,14 @@ class CursorHistory {
376
376
  }
377
377
  }
378
378
 
379
+ class CyclicalParseError extends Error {
380
+ constructor(patternId, patternName, cursorIndex) {
381
+ super("Cyclical Parse Error");
382
+ this.patternId = patternId;
383
+ this.patternName = patternName;
384
+ this.cursorIndex = cursorIndex;
385
+ }
386
+ }
379
387
  class Cursor {
380
388
  get text() {
381
389
  return this._text;
@@ -481,14 +489,13 @@ class Cursor {
481
489
  this._history.stopRecording();
482
490
  }
483
491
  startParseWith(pattern) {
484
- const patternName = pattern.name;
485
492
  const trace = {
486
493
  pattern,
487
494
  cursorIndex: this.index
488
495
  };
489
- const hasCycle = this._stackTrace.find(t => t.pattern.id === pattern.id && this.index === t.cursorIndex);
496
+ const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
490
497
  if (hasCycle) {
491
- throw new Error(`Cyclical Pattern: ${this._stackTrace.map(t => `${t.pattern.name}#${t.pattern.id}{${t.cursorIndex}}`).join(" -> ")} -> ${patternName}#${pattern.id}{${this.index}}.`);
498
+ throw new CyclicalParseError(pattern.id, pattern.name, this.index);
492
499
  }
493
500
  this._history.pushStackTrace(trace);
494
501
  this._stackTrace.push(trace);
@@ -996,7 +1003,13 @@ class Options {
996
1003
  const results = [];
997
1004
  for (const pattern of this._children) {
998
1005
  cursor.moveTo(this._firstIndex);
999
- const result = pattern.parse(cursor);
1006
+ let result = null;
1007
+ try {
1008
+ result = pattern.parse(cursor);
1009
+ }
1010
+ catch (_a) {
1011
+ continue;
1012
+ }
1000
1013
  if (this._isGreedy) {
1001
1014
  results.push(result);
1002
1015
  }
@@ -1992,8 +2005,8 @@ const anonymousPattern = new Options("anonymous-pattern", [
1992
2005
  ]);
1993
2006
 
1994
2007
  const optionalSpaces$3 = new Optional("optional-spaces", spaces$1);
1995
- const openBracket$1 = new Literal("open-bracket", "{");
1996
- const closeBracket$1 = new Literal("close-bracket", "}");
2008
+ const openBracket$1 = new Literal("repeat-open-bracket", "{");
2009
+ const closeBracket$1 = new Literal("repeat-close-bracket", "}");
1997
2010
  const comma = new Literal("comma", ",");
1998
2011
  const integer = new Regex("integer", "([1-9][0-9]*)|0");
1999
2012
  integer.setTokens(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
@@ -2030,15 +2043,15 @@ const closeParen = new Literal("repeat-close-paren", ")");
2030
2043
  const dividerComma = new Regex("divider-comma", "\\s*,\\s*");
2031
2044
  dividerComma.setTokens([", "]);
2032
2045
  const patternName$2 = name$1.clone("pattern-name");
2033
- const patterns$3 = new Options("or-patterns", [patternName$2, anonymousPattern]);
2034
- const dividerPattern = patterns$3.clone("divider-pattern");
2035
- const dividerSection = new Sequence("divider-section", [dividerComma, dividerPattern, trimFlag]);
2036
- const optionalDividerSection = new Optional("optional-divider-section", dividerSection);
2046
+ const repeatPattern = new Options("repeat-pattern", [patternName$2, anonymousPattern]);
2047
+ const repeatDividerPattern = repeatPattern.clone("repeat-divider-pattern");
2048
+ const repeatDividerSection = new Sequence("repeat-divider-section", [dividerComma, repeatDividerPattern, trimFlag]);
2049
+ const repeatOptionalDividerSection = new Optional("repeat-optional-divider-section", repeatDividerSection);
2037
2050
  const repeatLiteral = new Sequence("repeat-literal", [
2038
2051
  openParen,
2039
2052
  optionalSpaces$3,
2040
- patterns$3,
2041
- optionalDividerSection,
2053
+ repeatPattern,
2054
+ repeatOptionalDividerSection,
2042
2055
  optionalSpaces$3,
2043
2056
  closeParen,
2044
2057
  new Sequence("quantifier-section", [quantifier]),
@@ -2059,7 +2072,7 @@ const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: div
2059
2072
 
2060
2073
  const patternName = name$1.clone("pattern-name");
2061
2074
  patternName.setTokens(["[PATTERN_NAME]"]);
2062
- const patterns$1 = new Options("or-patterns", [patternName, anonymousPattern]);
2075
+ const patterns$1 = new Options("options-patterns", [patternName, anonymousPattern]);
2063
2076
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2064
2077
  defaultDivider.setTokens(["|"]);
2065
2078
  const greedyDivider = new Regex("greedy-divider", "\\s*[<][|][>]\\s*");
@@ -2106,7 +2119,7 @@ const bodyLine = new Sequence("body-line", [
2106
2119
  const body = new Optional("optional-body", new Repeat("body", bodyLine, { divider: newLine$1 }));
2107
2120
 
2108
2121
  const optionalSpaces$1 = new Optional("optional-spaces", allSpaces);
2109
- const optionalLineSpaces$1 = new Optional("options-line-spaces", lineSpaces$1);
2122
+ const optionalLineSpaces$1 = new Optional("optional-line-spaces", lineSpaces$1);
2110
2123
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
2111
2124
  importNameDivider.setTokens([", "]);
2112
2125
  const name = new Regex("import-name", "[^}\\s,]+");
@@ -2746,7 +2759,7 @@ class Grammar {
2746
2759
  const trimDivider = repeatNode.find(n => n.name === "trim-flag") != null;
2747
2760
  const patterNode = repeatNode.children[1].type === "spaces" ? repeatNode.children[2] : repeatNode.children[1];
2748
2761
  const pattern = this._buildPattern(patterNode);
2749
- const dividerSectionNode = repeatNode.find(n => n.name === "divider-section");
2762
+ const dividerSectionNode = repeatNode.find(n => n.name === "repeat-divider-section");
2750
2763
  const options = {
2751
2764
  min: 1,
2752
2765
  max: Infinity