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.js CHANGED
@@ -376,6 +376,13 @@ class CursorHistory {
376
376
  }
377
377
  }
378
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
+ }
379
386
  class Cursor {
380
387
  get text() {
381
388
  return this._text;
@@ -481,14 +488,13 @@ class Cursor {
481
488
  this._history.stopRecording();
482
489
  }
483
490
  startParseWith(pattern) {
484
- const patternName = pattern.name;
485
491
  const trace = {
486
492
  pattern,
487
493
  cursorIndex: this.index
488
494
  };
489
- 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;
490
496
  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}}.`);
497
+ throw new CyclicalParseError(pattern.id, pattern.name);
492
498
  }
493
499
  this._history.pushStackTrace(trace);
494
500
  this._stackTrace.push(trace);
@@ -996,7 +1002,19 @@ class Options {
996
1002
  const results = [];
997
1003
  for (const pattern of this._children) {
998
1004
  cursor.moveTo(this._firstIndex);
999
- 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
+ }
1000
1018
  if (this._isGreedy) {
1001
1019
  results.push(result);
1002
1020
  }
@@ -1992,8 +2010,8 @@ const anonymousPattern = new Options("anonymous-pattern", [
1992
2010
  ]);
1993
2011
 
1994
2012
  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", "}");
2013
+ const openBracket$1 = new Literal("repeat-open-bracket", "{");
2014
+ const closeBracket$1 = new Literal("repeat-close-bracket", "}");
1997
2015
  const comma = new Literal("comma", ",");
1998
2016
  const integer = new Regex("integer", "([1-9][0-9]*)|0");
1999
2017
  integer.setTokens(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
@@ -2030,15 +2048,15 @@ const closeParen = new Literal("repeat-close-paren", ")");
2030
2048
  const dividerComma = new Regex("divider-comma", "\\s*,\\s*");
2031
2049
  dividerComma.setTokens([", "]);
2032
2050
  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);
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);
2037
2055
  const repeatLiteral = new Sequence("repeat-literal", [
2038
2056
  openParen,
2039
2057
  optionalSpaces$3,
2040
- patterns$3,
2041
- optionalDividerSection,
2058
+ repeatPattern,
2059
+ repeatOptionalDividerSection,
2042
2060
  optionalSpaces$3,
2043
2061
  closeParen,
2044
2062
  new Sequence("quantifier-section", [quantifier]),
@@ -2059,7 +2077,7 @@ const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: div
2059
2077
 
2060
2078
  const patternName = name$1.clone("pattern-name");
2061
2079
  patternName.setTokens(["[PATTERN_NAME]"]);
2062
- const patterns$1 = new Options("or-patterns", [patternName, anonymousPattern]);
2080
+ const patterns$1 = new Options("options-patterns", [patternName, anonymousPattern]);
2063
2081
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2064
2082
  defaultDivider.setTokens(["|"]);
2065
2083
  const greedyDivider = new Regex("greedy-divider", "\\s*[<][|][>]\\s*");
@@ -2106,7 +2124,7 @@ const bodyLine = new Sequence("body-line", [
2106
2124
  const body = new Optional("optional-body", new Repeat("body", bodyLine, { divider: newLine$1 }));
2107
2125
 
2108
2126
  const optionalSpaces$1 = new Optional("optional-spaces", allSpaces);
2109
- const optionalLineSpaces$1 = new Optional("options-line-spaces", lineSpaces$1);
2127
+ const optionalLineSpaces$1 = new Optional("optional-line-spaces", lineSpaces$1);
2110
2128
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
2111
2129
  importNameDivider.setTokens([", "]);
2112
2130
  const name = new Regex("import-name", "[^}\\s,]+");
@@ -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