clarity-pattern-parser 10.0.3 → 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.esm.js CHANGED
@@ -372,6 +372,13 @@ class CursorHistory {
372
372
  }
373
373
  }
374
374
 
375
+ class CyclicalParseError extends Error {
376
+ constructor(patternId, patternName) {
377
+ super("Cyclical Parse Error");
378
+ this.patternId = patternId;
379
+ this.patternName = patternName;
380
+ }
381
+ }
375
382
  class Cursor {
376
383
  get text() {
377
384
  return this._text;
@@ -477,14 +484,13 @@ class Cursor {
477
484
  this._history.stopRecording();
478
485
  }
479
486
  startParseWith(pattern) {
480
- const patternName = pattern.name;
481
487
  const trace = {
482
488
  pattern,
483
489
  cursorIndex: this.index
484
490
  };
485
- const hasCycle = this._stackTrace.find(t => t.pattern.id === pattern.id && this.index === t.cursorIndex);
491
+ const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
486
492
  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}}.`);
493
+ throw new CyclicalParseError(pattern.id, pattern.name);
488
494
  }
489
495
  this._history.pushStackTrace(trace);
490
496
  this._stackTrace.push(trace);
@@ -992,7 +998,19 @@ class Options {
992
998
  const results = [];
993
999
  for (const pattern of this._children) {
994
1000
  cursor.moveTo(this._firstIndex);
995
- const result = pattern.parse(cursor);
1001
+ let result = null;
1002
+ try {
1003
+ result = pattern.parse(cursor);
1004
+ }
1005
+ catch (error) {
1006
+ if (error.patternId === this._id) {
1007
+ continue;
1008
+ }
1009
+ else {
1010
+ cursor.endParse();
1011
+ throw error;
1012
+ }
1013
+ }
996
1014
  if (this._isGreedy) {
997
1015
  results.push(result);
998
1016
  }
@@ -1988,8 +2006,8 @@ const anonymousPattern = new Options("anonymous-pattern", [
1988
2006
  ]);
1989
2007
 
1990
2008
  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", "}");
2009
+ const openBracket$1 = new Literal("repeat-open-bracket", "{");
2010
+ const closeBracket$1 = new Literal("repeat-close-bracket", "}");
1993
2011
  const comma = new Literal("comma", ",");
1994
2012
  const integer = new Regex("integer", "([1-9][0-9]*)|0");
1995
2013
  integer.setTokens(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
@@ -2026,15 +2044,15 @@ const closeParen = new Literal("repeat-close-paren", ")");
2026
2044
  const dividerComma = new Regex("divider-comma", "\\s*,\\s*");
2027
2045
  dividerComma.setTokens([", "]);
2028
2046
  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);
2047
+ const repeatPattern = new Options("repeat-pattern", [patternName$2, anonymousPattern]);
2048
+ const repeatDividerPattern = repeatPattern.clone("repeat-divider-pattern");
2049
+ const repeatDividerSection = new Sequence("repeat-divider-section", [dividerComma, repeatDividerPattern, trimFlag]);
2050
+ const repeatOptionalDividerSection = new Optional("repeat-optional-divider-section", repeatDividerSection);
2033
2051
  const repeatLiteral = new Sequence("repeat-literal", [
2034
2052
  openParen,
2035
2053
  optionalSpaces$3,
2036
- patterns$3,
2037
- optionalDividerSection,
2054
+ repeatPattern,
2055
+ repeatOptionalDividerSection,
2038
2056
  optionalSpaces$3,
2039
2057
  closeParen,
2040
2058
  new Sequence("quantifier-section", [quantifier]),
@@ -2055,7 +2073,7 @@ const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: div
2055
2073
 
2056
2074
  const patternName = name$1.clone("pattern-name");
2057
2075
  patternName.setTokens(["[PATTERN_NAME]"]);
2058
- const patterns$1 = new Options("or-patterns", [patternName, anonymousPattern]);
2076
+ const patterns$1 = new Options("options-patterns", [patternName, anonymousPattern]);
2059
2077
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2060
2078
  defaultDivider.setTokens(["|"]);
2061
2079
  const greedyDivider = new Regex("greedy-divider", "\\s*[<][|][>]\\s*");
@@ -2102,7 +2120,7 @@ const bodyLine = new Sequence("body-line", [
2102
2120
  const body = new Optional("optional-body", new Repeat("body", bodyLine, { divider: newLine$1 }));
2103
2121
 
2104
2122
  const optionalSpaces$1 = new Optional("optional-spaces", allSpaces);
2105
- const optionalLineSpaces$1 = new Optional("options-line-spaces", lineSpaces$1);
2123
+ const optionalLineSpaces$1 = new Optional("optional-line-spaces", lineSpaces$1);
2106
2124
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
2107
2125
  importNameDivider.setTokens([", "]);
2108
2126
  const name = new Regex("import-name", "[^}\\s,]+");
@@ -2742,7 +2760,7 @@ class Grammar {
2742
2760
  const trimDivider = repeatNode.find(n => n.name === "trim-flag") != null;
2743
2761
  const patterNode = repeatNode.children[1].type === "spaces" ? repeatNode.children[2] : repeatNode.children[1];
2744
2762
  const pattern = this._buildPattern(patterNode);
2745
- const dividerSectionNode = repeatNode.find(n => n.name === "divider-section");
2763
+ const dividerSectionNode = repeatNode.find(n => n.name === "repeat-divider-section");
2746
2764
  const options = {
2747
2765
  min: 1,
2748
2766
  max: Infinity