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