clarity-pattern-parser 11.0.23 → 11.0.24

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
@@ -523,20 +523,31 @@ class Cursor {
523
523
 
524
524
  function execPattern(pattern, text, record = false) {
525
525
  const cursor = new Cursor(text);
526
+ if (cursor.length === 0) {
527
+ return { ast: null, cursor };
528
+ }
526
529
  record && cursor.startRecording();
527
- const ast = pattern.parse(cursor);
528
- const isMatch = (ast === null || ast === void 0 ? void 0 : ast.value.length) === text.length;
530
+ let ast = pattern.parse(cursor);
531
+ const resultLength = ast == null ? 0 : ast.value.length;
532
+ if (ast != null) {
533
+ const isMatch = ast.value === text;
534
+ if (!isMatch && !cursor.hasError) {
535
+ ast = null;
536
+ cursor.recordErrorAt(resultLength, cursor.length, pattern);
537
+ }
538
+ }
539
+ else {
540
+ cursor.recordErrorAt(resultLength, cursor.length, pattern);
541
+ }
529
542
  return {
530
- ast: isMatch ? ast : null,
543
+ ast: ast,
531
544
  cursor
532
545
  };
533
546
  }
534
547
 
535
548
  function testPattern(pattern, text, record = false) {
536
- const cursor = new Cursor(text);
537
- record && cursor.startRecording();
538
- const ast = pattern.parse(cursor);
539
- return (ast === null || ast === void 0 ? void 0 : ast.value.length) === text.length;
549
+ const result = execPattern(pattern, text, record);
550
+ return !result.cursor.hasError;
540
551
  }
541
552
 
542
553
  let idIndex$9 = 0;
@@ -600,9 +611,6 @@ class Literal {
600
611
  _tryToParse(cursor) {
601
612
  let passed = false;
602
613
  const literalRuneLength = this._runes.length;
603
- if (!cursor.hasNext) {
604
- return false;
605
- }
606
614
  for (let i = 0; i < literalRuneLength; i++) {
607
615
  const literalRune = this._runes[i];
608
616
  const cursorRune = cursor.currentChar;
@@ -1986,19 +1994,11 @@ class Optional {
1986
1994
  this._children = [pattern.clone()];
1987
1995
  this._children[0].parent = this;
1988
1996
  }
1989
- test(text) {
1990
- const cursor = new Cursor(text);
1991
- this.parse(cursor);
1992
- return !cursor.hasError;
1997
+ test(text, record = false) {
1998
+ return testPattern(this, text, record);
1993
1999
  }
1994
2000
  exec(text, record = false) {
1995
- const cursor = new Cursor(text);
1996
- record && cursor.startRecording();
1997
- const ast = this.parse(cursor);
1998
- return {
1999
- ast: (ast === null || ast === void 0 ? void 0 : ast.value) === text ? ast : null,
2000
- cursor
2001
- };
2001
+ return execPattern(this, text, record);
2002
2002
  }
2003
2003
  parse(cursor) {
2004
2004
  const firstIndex = cursor.index;