clarity-pattern-parser 10.0.4 → 10.0.5

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.
@@ -5,7 +5,8 @@ import { Pattern } from "./Pattern";
5
5
  export declare class CyclicalParseError extends Error {
6
6
  readonly patternId: string;
7
7
  readonly patternName: string;
8
- constructor(patternId: string, patternName: string);
8
+ readonly cursorIndex: number;
9
+ constructor(patternId: string, patternName: string, cursorIndex: number);
9
10
  }
10
11
  export declare class Cursor {
11
12
  private _text;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-pattern-parser",
3
- "version": "10.0.4",
3
+ "version": "10.0.5",
4
4
  "description": "Parsing Library for Typescript and Javascript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -6,11 +6,13 @@ import { Pattern } from "./Pattern";
6
6
  export class CyclicalParseError extends Error {
7
7
  readonly patternId: string;
8
8
  readonly patternName: string;
9
+ readonly cursorIndex: number;
9
10
 
10
- constructor(patternId: string, patternName: string) {
11
+ constructor(patternId: string, patternName: string, cursorIndex: number) {
11
12
  super("Cyclical Parse Error");
12
13
  this.patternId = patternId;
13
14
  this.patternName = patternName;
15
+ this.cursorIndex = cursorIndex;
14
16
  }
15
17
  }
16
18
 
@@ -163,7 +165,7 @@ export class Cursor {
163
165
 
164
166
  const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
165
167
  if (hasCycle) {
166
- throw new CyclicalParseError(pattern.id, pattern.name);
168
+ throw new CyclicalParseError(pattern.id, pattern.name, this.index);
167
169
  }
168
170
 
169
171
  this._history.pushStackTrace(trace);
@@ -295,4 +295,21 @@ describe("Options", () => {
295
295
  result = expression.exec("John ? Jane : John ? Jane : John");
296
296
  expect(result.ast?.toString()).toBe("John ? Jane : John ? Jane : John");
297
297
  });
298
+
299
+ test("Deeper Cyclical Error Recorvery", () => {
300
+ const john = new Literal("john", "John");
301
+ const expressionReference = new Reference("expression");
302
+
303
+ const johns = new Sequence("johns", [john, expressionReference]);
304
+ const expression = new Options("expression", [
305
+ john,
306
+ johns,
307
+ ], true);
308
+
309
+ let result = expression.exec("John");
310
+ expect(result.ast?.toString()).toBe("John");
311
+
312
+ result = expression.exec("JohnJohnJohnJohnJohn");
313
+ expect(result.ast?.toString()).toBe("JohnJohnJohnJohnJohn");
314
+ });
298
315
  });
@@ -111,13 +111,8 @@ export class Options implements Pattern {
111
111
 
112
112
  try {
113
113
  result = pattern.parse(cursor);
114
- } catch (error: any) {
115
- if (error.patternId === this._id) {
116
- continue;
117
- } else {
118
- cursor.endParse();
119
- throw error;
120
- }
114
+ } catch {
115
+ continue;
121
116
  }
122
117
 
123
118
  if (this._isGreedy) {