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