clarity-pattern-parser 11.0.23 → 11.0.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.
@@ -529,20 +529,31 @@
529
529
 
530
530
  function execPattern(pattern, text, record = false) {
531
531
  const cursor = new Cursor(text);
532
+ if (cursor.length === 0) {
533
+ return { ast: null, cursor };
534
+ }
532
535
  record && cursor.startRecording();
533
- const ast = pattern.parse(cursor);
534
- const isMatch = (ast === null || ast === void 0 ? void 0 : ast.value.length) === text.length;
536
+ let ast = pattern.parse(cursor);
537
+ const resultLength = ast == null ? 0 : ast.value.length;
538
+ if (ast != null) {
539
+ const isMatch = ast.value === text;
540
+ if (!isMatch && !cursor.hasError) {
541
+ ast = null;
542
+ cursor.recordErrorAt(resultLength, cursor.length, pattern);
543
+ }
544
+ }
545
+ else {
546
+ cursor.recordErrorAt(resultLength, cursor.length, pattern);
547
+ }
535
548
  return {
536
- ast: isMatch ? ast : null,
549
+ ast: ast,
537
550
  cursor
538
551
  };
539
552
  }
540
553
 
541
554
  function testPattern(pattern, text, record = false) {
542
- const cursor = new Cursor(text);
543
- record && cursor.startRecording();
544
- const ast = pattern.parse(cursor);
545
- return (ast === null || ast === void 0 ? void 0 : ast.value.length) === text.length;
555
+ const result = execPattern(pattern, text, record);
556
+ return !result.cursor.hasError;
546
557
  }
547
558
 
548
559
  let idIndex$9 = 0;
@@ -606,9 +617,6 @@
606
617
  _tryToParse(cursor) {
607
618
  let passed = false;
608
619
  const literalRuneLength = this._runes.length;
609
- if (!cursor.hasNext) {
610
- return false;
611
- }
612
620
  for (let i = 0; i < literalRuneLength; i++) {
613
621
  const literalRune = this._runes[i];
614
622
  const cursorRune = cursor.currentChar;
@@ -1992,19 +2000,11 @@
1992
2000
  this._children = [pattern.clone()];
1993
2001
  this._children[0].parent = this;
1994
2002
  }
1995
- test(text) {
1996
- const cursor = new Cursor(text);
1997
- this.parse(cursor);
1998
- return !cursor.hasError;
2003
+ test(text, record = false) {
2004
+ return testPattern(this, text, record);
1999
2005
  }
2000
2006
  exec(text, record = false) {
2001
- const cursor = new Cursor(text);
2002
- record && cursor.startRecording();
2003
- const ast = this.parse(cursor);
2004
- return {
2005
- ast: (ast === null || ast === void 0 ? void 0 : ast.value) === text ? ast : null,
2006
- cursor
2007
- };
2007
+ return execPattern(this, text, record);
2008
2008
  }
2009
2009
  parse(cursor) {
2010
2010
  const firstIndex = cursor.index;
@@ -2824,12 +2824,15 @@
2824
2824
  }
2825
2825
  return pattern.name === this.name;
2826
2826
  }
2827
- parse(cursor) {
2828
- this._firstIndex = cursor.index;
2827
+ build() {
2829
2828
  if (!this._hasOrganized) {
2830
2829
  this._hasOrganized = true;
2831
2830
  this._organizePatterns(this._originalPatterns);
2832
2831
  }
2832
+ }
2833
+ parse(cursor) {
2834
+ this._firstIndex = cursor.index;
2835
+ this.build();
2833
2836
  // If there are not any atom nodes then nothing can be found.
2834
2837
  if (this._atomPatterns.length < 1) {
2835
2838
  cursor.moveTo(this._firstIndex);