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