clarity-pattern-parser 10.1.23 → 10.1.25

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
@@ -266,9 +266,11 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
266
266
  };
267
267
 
268
268
  class ParseError {
269
- constructor(startIndex, endIndex, pattern) {
269
+ constructor(startIndex, lastIndex, pattern) {
270
+ this.firstIndex = startIndex;
270
271
  this.startIndex = startIndex;
271
- this.endIndex = endIndex;
272
+ this.lastIndex = lastIndex;
273
+ this.endIndex = lastIndex + 1;
272
274
  this.pattern = pattern;
273
275
  }
274
276
  }
@@ -358,7 +360,7 @@ class CursorHistory {
358
360
  recordErrorAt(startIndex, endIndex, pattern) {
359
361
  const error = new ParseError(startIndex, endIndex, pattern);
360
362
  this._currentError = error;
361
- if (this._furthestError === null || endIndex > this._furthestError.endIndex) {
363
+ if (this._furthestError === null || endIndex > this._furthestError.lastIndex) {
362
364
  this._furthestError = error;
363
365
  }
364
366
  if (this._isRecording) {
@@ -993,7 +995,7 @@ class Options {
993
995
  }
994
996
  constructor(name, options, isGreedy = false) {
995
997
  if (options.length === 0) {
996
- throw new Error("Need at least one pattern with an 'or' pattern.");
998
+ throw new Error("Need at least one pattern with an 'options' pattern.");
997
999
  }
998
1000
  const children = clonePatterns(options);
999
1001
  this._assignChildrenToParent(children);
@@ -2383,14 +2385,14 @@ class AutoComplete {
2383
2385
  const startIndex = options.reduce((lowestIndex, o) => {
2384
2386
  return Math.min(lowestIndex, o.startIndex);
2385
2387
  }, Infinity);
2386
- const endIndex = cursor.getLastIndex() + 1;
2387
- error = new ParseError(startIndex, endIndex, this._pattern);
2388
+ const lastIndex = cursor.getLastIndex() + 1;
2389
+ error = new ParseError(startIndex, lastIndex, this._pattern);
2388
2390
  errorAtIndex = startIndex;
2389
2391
  }
2390
2392
  else if (!isComplete && options.length === 0 && ast != null) {
2391
2393
  const startIndex = ast.endIndex;
2392
- const endIndex = cursor.getLastIndex() + 1;
2393
- error = new ParseError(startIndex, endIndex, this._pattern);
2394
+ const lastIndex = cursor.getLastIndex() + 1;
2395
+ error = new ParseError(startIndex, lastIndex, this._pattern);
2394
2396
  errorAtIndex = startIndex;
2395
2397
  }
2396
2398
  else if (!isComplete && this._cursor.hasError && this._cursor.furthestError != null) {
@@ -2410,18 +2412,18 @@ class AutoComplete {
2410
2412
  const furthestError = cursor.furthestError;
2411
2413
  const furthestMatch = cursor.allMatchedNodes[cursor.allMatchedNodes.length - 1];
2412
2414
  if (furthestError && furthestMatch) {
2413
- if (furthestError.endIndex > furthestMatch.endIndex) {
2415
+ if (furthestError.lastIndex > furthestMatch.endIndex) {
2414
2416
  return furthestMatch.endIndex;
2415
2417
  }
2416
2418
  else {
2417
- return furthestError.endIndex;
2419
+ return furthestError.lastIndex;
2418
2420
  }
2419
2421
  }
2420
2422
  if (furthestError == null && furthestMatch != null) {
2421
2423
  return furthestMatch.endIndex;
2422
2424
  }
2423
2425
  if (furthestMatch == null && furthestError != null) {
2424
- return furthestError.endIndex;
2426
+ return furthestError.lastIndex;
2425
2427
  }
2426
2428
  return 0;
2427
2429
  }
@@ -2442,11 +2444,24 @@ class AutoComplete {
2442
2444
  }
2443
2445
  _getOptionsFromErrors() {
2444
2446
  // These errored because the length of the string.
2445
- const errors = this._cursor.errors.filter(e => e.endIndex === this._cursor.length);
2447
+ const errors = this._cursor.errors.filter(e => e.lastIndex === this._cursor.length);
2446
2448
  const suggestions = errors.map(e => {
2447
2449
  const tokens = this._getTokensForPattern(e.pattern);
2448
- const adjustedTokens = tokens.map(t => t.slice(e.endIndex - e.startIndex));
2449
- return this._createSuggestions(e.endIndex, adjustedTokens);
2450
+ const adjustedTokens = new Set();
2451
+ const currentText = this._cursor.getChars(e.startIndex, e.lastIndex);
2452
+ tokens.forEach((token) => {
2453
+ if (token.startsWith(currentText) && token.length > currentText.length) {
2454
+ const difference = token.length - currentText.length;
2455
+ const suggestedText = token.slice(-difference);
2456
+ adjustedTokens.add(suggestedText);
2457
+ }
2458
+ });
2459
+ return Array.from(adjustedTokens).map(t => {
2460
+ return {
2461
+ text: t,
2462
+ startIndex: e.lastIndex,
2463
+ };
2464
+ });
2450
2465
  });
2451
2466
  return suggestions.flat();
2452
2467
  }
@@ -2621,31 +2636,38 @@ class Context {
2621
2636
  }
2622
2637
  clone(name = this._name) {
2623
2638
  const clone = new Context(name, this._pattern, Object.values(this._patterns));
2639
+ clone._id = this._id;
2624
2640
  return clone;
2625
2641
  }
2626
2642
  getTokens() {
2627
2643
  return this._pattern.getTokens();
2628
2644
  }
2629
- getTokensAfter(childReference) {
2630
- if (this.parent == null) {
2645
+ getTokensAfter(_childReference) {
2646
+ if (this._parent == null) {
2631
2647
  return [];
2632
2648
  }
2633
- return this._pattern.getTokensAfter(childReference);
2649
+ return this._parent.getTokensAfter(this);
2634
2650
  }
2635
2651
  getNextTokens() {
2636
- return this._pattern.getNextTokens();
2652
+ if (this._parent == null) {
2653
+ return [];
2654
+ }
2655
+ return this._parent.getTokensAfter(this);
2637
2656
  }
2638
2657
  getPatterns() {
2639
2658
  return this._pattern.getPatterns();
2640
2659
  }
2641
- getPatternsAfter(childReference) {
2642
- if (this.parent == null) {
2660
+ getPatternsAfter(_childReference) {
2661
+ if (this._parent == null) {
2643
2662
  return [];
2644
2663
  }
2645
- return this.parent.getPatternsAfter(childReference);
2664
+ return this._parent.getPatternsAfter(this);
2646
2665
  }
2647
2666
  getNextPatterns() {
2648
- return this._pattern.getNextPatterns();
2667
+ if (this._parent == null) {
2668
+ return [];
2669
+ }
2670
+ return this._parent.getPatternsAfter(this);
2649
2671
  }
2650
2672
  find(predicate) {
2651
2673
  return this._pattern.find(predicate);