clarity-pattern-parser 10.1.17 → 10.1.21

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
@@ -1048,8 +1048,11 @@ class Options {
1048
1048
  }
1049
1049
  getTokens() {
1050
1050
  const tokens = [];
1051
- for (const child of this._children) {
1052
- tokens.push(...child.getTokens());
1051
+ for (const pattern of this._children) {
1052
+ if (pattern.type === "reference" && pattern.name === this.name) {
1053
+ continue;
1054
+ }
1055
+ tokens.push(...pattern.getTokens());
1053
1056
  }
1054
1057
  return tokens;
1055
1058
  }
@@ -1068,6 +1071,9 @@ class Options {
1068
1071
  getPatterns() {
1069
1072
  const patterns = [];
1070
1073
  for (const pattern of this._children) {
1074
+ if (pattern.type === "reference" && pattern.name === this.name) {
1075
+ continue;
1076
+ }
1071
1077
  patterns.push(...pattern.getPatterns());
1072
1078
  }
1073
1079
  return patterns;
@@ -1795,9 +1801,12 @@ class Sequence {
1795
1801
  }
1796
1802
  getTokens() {
1797
1803
  const tokens = [];
1798
- for (const child of this._children) {
1799
- tokens.push(...child.getTokens());
1800
- if (child.type !== "optional" && child.type !== "not") {
1804
+ for (const pattern of this._children) {
1805
+ if (pattern.type === "reference" && pattern.name === this.name && pattern === this.children[0]) {
1806
+ return tokens;
1807
+ }
1808
+ tokens.push(...pattern.getTokens());
1809
+ if (pattern.type !== "optional" && pattern.type !== "not") {
1801
1810
  break;
1802
1811
  }
1803
1812
  }
@@ -1817,9 +1826,12 @@ class Sequence {
1817
1826
  }
1818
1827
  getPatterns() {
1819
1828
  const patterns = [];
1820
- for (const child of this._children) {
1821
- patterns.push(...child.getPatterns());
1822
- if (child.type !== "optional" && child.type !== "not") {
1829
+ for (const pattern of this._children) {
1830
+ if (pattern.type === "reference" && pattern.name === this.name && pattern === this.children[0]) {
1831
+ return patterns;
1832
+ }
1833
+ patterns.push(...pattern.getPatterns());
1834
+ if (pattern.type !== "optional" && pattern.type !== "not") {
1823
1835
  break;
1824
1836
  }
1825
1837
  }
@@ -2367,9 +2379,8 @@ class AutoComplete {
2367
2379
  errorAtIndex = startIndex;
2368
2380
  }
2369
2381
  else if (!isComplete && this._cursor.hasError && this._cursor.furthestError != null) {
2370
- errorAtIndex = this._cursor.furthestError.endIndex;
2371
- error = this._cursor.furthestError;
2372
- errorAtIndex = options.reduce((errorAtIndex, option) => Math.max(errorAtIndex, option.startIndex), errorAtIndex);
2382
+ errorAtIndex = this.getFurthestPosition(cursor);
2383
+ error = new ParseError(errorAtIndex, errorAtIndex, this._pattern);
2373
2384
  }
2374
2385
  return {
2375
2386
  isComplete: isComplete,
@@ -2380,6 +2391,25 @@ class AutoComplete {
2380
2391
  ast,
2381
2392
  };
2382
2393
  }
2394
+ getFurthestPosition(cursor) {
2395
+ const furthestError = cursor.furthestError;
2396
+ const furthestMatch = cursor.allMatchedNodes[cursor.allMatchedNodes.length - 1];
2397
+ if (furthestError && furthestMatch) {
2398
+ if (furthestError.endIndex > furthestMatch.endIndex) {
2399
+ return furthestMatch.endIndex;
2400
+ }
2401
+ else {
2402
+ return furthestError.endIndex;
2403
+ }
2404
+ }
2405
+ if (furthestError == null && furthestMatch != null) {
2406
+ return furthestMatch.endIndex;
2407
+ }
2408
+ if (furthestMatch == null && furthestError != null) {
2409
+ return furthestError.endIndex;
2410
+ }
2411
+ return 0;
2412
+ }
2383
2413
  suggestFor(text) {
2384
2414
  return this.suggestForWithCursor(new Cursor(text));
2385
2415
  }