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.esm.js CHANGED
@@ -219,6 +219,9 @@ class Node {
219
219
  toJson(space) {
220
220
  return JSON.stringify(this.toCycleFreeObject(), null, space);
221
221
  }
222
+ isEqual(node) {
223
+ return node.toJson(0) === this.toJson(0);
224
+ }
222
225
  static createValueNode(name, value) {
223
226
  return new Node("custom-value-node", name, 0, 0, [], value);
224
227
  }
@@ -369,6 +372,13 @@ class CursorHistory {
369
372
  }
370
373
  }
371
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
+ }
372
382
  class Cursor {
373
383
  get text() {
374
384
  return this._text;
@@ -474,14 +484,13 @@ class Cursor {
474
484
  this._history.stopRecording();
475
485
  }
476
486
  startParseWith(pattern) {
477
- const patternName = pattern.name;
478
487
  const trace = {
479
488
  pattern,
480
489
  cursorIndex: this.index
481
490
  };
482
- 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;
483
492
  if (hasCycle) {
484
- 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);
485
494
  }
486
495
  this._history.pushStackTrace(trace);
487
496
  this._stackTrace.push(trace);
@@ -989,7 +998,19 @@ class Options {
989
998
  const results = [];
990
999
  for (const pattern of this._children) {
991
1000
  cursor.moveTo(this._firstIndex);
992
- 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
+ }
993
1014
  if (this._isGreedy) {
994
1015
  results.push(result);
995
1016
  }
@@ -1101,7 +1122,6 @@ class FiniteRepeat {
1101
1122
  }
1102
1123
  }
1103
1124
  parse(cursor) {
1104
- var _a;
1105
1125
  cursor.startParseWith(this);
1106
1126
  const startIndex = cursor.index;
1107
1127
  const nodes = [];
@@ -1131,7 +1151,7 @@ class FiniteRepeat {
1131
1151
  }
1132
1152
  }
1133
1153
  if (this._trimDivider && this._hasDivider) {
1134
- const isDividerLastMatch = ((_a = cursor.leafMatch.pattern) === null || _a === void 0 ? void 0 : _a.id) === this.children[1].id;
1154
+ const isDividerLastMatch = this.children.length > 1 && nodes[nodes.length - 1].name === this.children[1].name;
1135
1155
  if (isDividerLastMatch) {
1136
1156
  const node = nodes.pop();
1137
1157
  cursor.moveTo(node.firstIndex);
@@ -1402,10 +1422,11 @@ class InfiniteRepeat {
1402
1422
  return passed;
1403
1423
  }
1404
1424
  _createNode(cursor) {
1425
+ var _a;
1405
1426
  const hasDivider = this._divider != null;
1406
1427
  if (hasDivider &&
1407
1428
  this._trimDivider &&
1408
- cursor.leafMatch.pattern === this._divider) {
1429
+ this._nodes[this._nodes.length - 1].name === ((_a = this._divider) === null || _a === void 0 ? void 0 : _a.name)) {
1409
1430
  const dividerNode = this._nodes.pop();
1410
1431
  cursor.moveTo(dividerNode.firstIndex);
1411
1432
  }
@@ -1517,10 +1538,10 @@ class Repeat {
1517
1538
  return this._children;
1518
1539
  }
1519
1540
  get min() {
1520
- return this.children[0].min;
1541
+ return this._options.min;
1521
1542
  }
1522
1543
  get max() {
1523
- return this.children[0].max || Infinity;
1544
+ return this._options.max;
1524
1545
  }
1525
1546
  constructor(name, pattern, options = {}) {
1526
1547
  this._id = `repeat-${idIndex$3++}`;
@@ -1756,7 +1777,7 @@ class Sequence {
1756
1777
  const tokens = [];
1757
1778
  for (const child of this._children) {
1758
1779
  tokens.push(...child.getTokens());
1759
- if (child.type !== "optional") {
1780
+ if (child.type !== "optional" && child.type !== "not") {
1760
1781
  break;
1761
1782
  }
1762
1783
  }
@@ -1778,7 +1799,7 @@ class Sequence {
1778
1799
  const patterns = [];
1779
1800
  for (const child of this._children) {
1780
1801
  patterns.push(...child.getPatterns());
1781
- if (child.type !== "optional") {
1802
+ if (child.type !== "optional" && child.type !== "not") {
1782
1803
  break;
1783
1804
  }
1784
1805
  }
@@ -1985,8 +2006,8 @@ const anonymousPattern = new Options("anonymous-pattern", [
1985
2006
  ]);
1986
2007
 
1987
2008
  const optionalSpaces$3 = new Optional("optional-spaces", spaces$1);
1988
- const openBracket$1 = new Literal("open-bracket", "{");
1989
- 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", "}");
1990
2011
  const comma = new Literal("comma", ",");
1991
2012
  const integer = new Regex("integer", "([1-9][0-9]*)|0");
1992
2013
  integer.setTokens(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
@@ -2023,15 +2044,15 @@ const closeParen = new Literal("repeat-close-paren", ")");
2023
2044
  const dividerComma = new Regex("divider-comma", "\\s*,\\s*");
2024
2045
  dividerComma.setTokens([", "]);
2025
2046
  const patternName$2 = name$1.clone("pattern-name");
2026
- const patterns$3 = new Options("or-patterns", [patternName$2, anonymousPattern]);
2027
- const dividerPattern = patterns$3.clone("divider-pattern");
2028
- const dividerSection = new Sequence("divider-section", [dividerComma, dividerPattern, trimFlag]);
2029
- 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);
2030
2051
  const repeatLiteral = new Sequence("repeat-literal", [
2031
2052
  openParen,
2032
2053
  optionalSpaces$3,
2033
- patterns$3,
2034
- optionalDividerSection,
2054
+ repeatPattern,
2055
+ repeatOptionalDividerSection,
2035
2056
  optionalSpaces$3,
2036
2057
  closeParen,
2037
2058
  new Sequence("quantifier-section", [quantifier]),
@@ -2052,7 +2073,7 @@ const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: div
2052
2073
 
2053
2074
  const patternName = name$1.clone("pattern-name");
2054
2075
  patternName.setTokens(["[PATTERN_NAME]"]);
2055
- const patterns$1 = new Options("or-patterns", [patternName, anonymousPattern]);
2076
+ const patterns$1 = new Options("options-patterns", [patternName, anonymousPattern]);
2056
2077
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2057
2078
  defaultDivider.setTokens(["|"]);
2058
2079
  const greedyDivider = new Regex("greedy-divider", "\\s*[<][|][>]\\s*");
@@ -2099,7 +2120,7 @@ const bodyLine = new Sequence("body-line", [
2099
2120
  const body = new Optional("optional-body", new Repeat("body", bodyLine, { divider: newLine$1 }));
2100
2121
 
2101
2122
  const optionalSpaces$1 = new Optional("optional-spaces", allSpaces);
2102
- const optionalLineSpaces$1 = new Optional("options-line-spaces", lineSpaces$1);
2123
+ const optionalLineSpaces$1 = new Optional("optional-line-spaces", lineSpaces$1);
2103
2124
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
2104
2125
  importNameDivider.setTokens([", "]);
2105
2126
  const name = new Regex("import-name", "[^}\\s,]+");
@@ -2202,9 +2223,6 @@ class Not {
2202
2223
  get children() {
2203
2224
  return this._children;
2204
2225
  }
2205
- get isOptional() {
2206
- return false;
2207
- }
2208
2226
  constructor(name, pattern) {
2209
2227
  this._id = `not-${idIndex++}`;
2210
2228
  this._type = "not";
@@ -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
@@ -2859,7 +2877,7 @@ class Grammar {
2859
2877
  .importedPatternsByName
2860
2878
  .values());
2861
2879
  const grammar = new Grammar({
2862
- params: importedValues,
2880
+ params: [...importedValues, ...this._parseContext.paramsByName.values()],
2863
2881
  originResource: this._originResource,
2864
2882
  resolveImport: this._resolveImport
2865
2883
  });