clarity-pattern-parser 11.0.7 → 11.0.8

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.
@@ -51,7 +51,6 @@ export declare class Expression implements Pattern {
51
51
  private _tryToMatchAtom;
52
52
  private _tryToMatchPostfix;
53
53
  private _tryToMatchBinary;
54
- private _isBeyondRecursiveAllowance;
55
54
  test(text: string, record?: boolean): boolean;
56
55
  exec(text: string, record?: boolean): ParseResult;
57
56
  getTokens(): string[];
@@ -23,7 +23,6 @@ export declare class Options implements Pattern {
23
23
  exec(text: string, record?: boolean): ParseResult;
24
24
  parse(cursor: Cursor): Node | null;
25
25
  private _tryToParse;
26
- private _isBeyondRecursiveAllowance;
27
26
  getTokens(): string[];
28
27
  getTokensAfter(_childReference: Pattern): string[];
29
28
  getNextTokens(): string[];
@@ -11,6 +11,8 @@ export declare class Reference implements Pattern {
11
11
  private _pattern;
12
12
  private _children;
13
13
  private _firstIndex;
14
+ private _cachedAncestors;
15
+ private _recursiveAncestors;
14
16
  get id(): string;
15
17
  get type(): string;
16
18
  get name(): string;
@@ -22,6 +24,8 @@ export declare class Reference implements Pattern {
22
24
  test(text: string, record?: boolean): boolean;
23
25
  exec(text: string, record?: boolean): ParseResult;
24
26
  parse(cursor: Cursor): Node | null;
27
+ private _cacheAncestors;
28
+ private _isBeyondRecursiveAllowance;
25
29
  getReferencePatternSafely(): Pattern;
26
30
  private _findPattern;
27
31
  private _isValidPattern;
@@ -24,7 +24,6 @@ export declare class Sequence implements Pattern {
24
24
  parse(cursor: Cursor): Node | null;
25
25
  private tryToParse;
26
26
  private getLastValidNode;
27
- private _isBeyondRecursiveAllowance;
28
27
  private areRemainingPatternsOptional;
29
28
  private createNode;
30
29
  getTokens(): string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-pattern-parser",
3
- "version": "11.0.7",
3
+ "version": "11.0.8",
4
4
  "description": "Parsing Library for Typescript and Javascript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -263,11 +263,6 @@ export class Expression implements Pattern {
263
263
 
264
264
 
265
265
  private _tryToParse(cursor: Cursor): Node | null {
266
- if (this._isBeyondRecursiveAllowance()) {
267
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
268
- return null;
269
- }
270
-
271
266
  this._shouldStopParsing = false;
272
267
 
273
268
  while (true) {
@@ -426,25 +421,6 @@ export class Expression implements Pattern {
426
421
  }
427
422
  }
428
423
 
429
-
430
- private _isBeyondRecursiveAllowance() {
431
- let depth = 0;
432
- let pattern: Pattern | null = this;
433
-
434
- while (pattern != null) {
435
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
436
- depth++;
437
- }
438
-
439
- if (depth > 2) {
440
- return true;
441
- }
442
- pattern = pattern.parent;
443
- }
444
-
445
- return false;
446
- }
447
-
448
424
  test(text: string, record = false): boolean {
449
425
  return testPattern(this, text, record);
450
426
  }
@@ -7,6 +7,7 @@ import { Pattern } from "./Pattern";
7
7
  import { Optional } from "./Optional";
8
8
  import { Regex } from "./Regex";
9
9
  import { Reference } from "./Reference";
10
+ import { Expression } from "./Expression";
10
11
 
11
12
  describe("Options", () => {
12
13
  test("Empty Options", () => {
@@ -283,7 +284,7 @@ describe("Options", () => {
283
284
  const space = new Regex("space", "\\s+");
284
285
  const expressionReference = new Reference("expression");
285
286
  const ternary = new Sequence("ternary", [expressionReference, space, questionMark, space, expressionReference, space, colon, space, expressionReference]);
286
- const expression = new Options("expression", [names, ternary], true);
287
+ const expression = new Expression("expression", [names, ternary]);
287
288
 
288
289
  let result = expression.exec("John");
289
290
  expect(result.ast?.toString()).toBe("John");
@@ -94,10 +94,6 @@ export class Options implements Pattern {
94
94
  }
95
95
 
96
96
  private _tryToParse(cursor: Cursor): Node | null {
97
- if (this._isBeyondRecursiveAllowance()) {
98
- return null;
99
- }
100
-
101
97
  const results: (Node | null)[] = [];
102
98
 
103
99
  for (const pattern of this._children) {
@@ -123,24 +119,6 @@ export class Options implements Pattern {
123
119
  return nonNullResults[0] || null;
124
120
  }
125
121
 
126
- private _isBeyondRecursiveAllowance() {
127
- let depth = 0;
128
- let pattern: Pattern | null = this;
129
-
130
- while (pattern != null) {
131
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
132
- depth++;
133
- }
134
-
135
- if (depth > 2) {
136
- return true;
137
- }
138
- pattern = pattern.parent;
139
- }
140
-
141
- return false;
142
- }
143
-
144
122
  getTokens(): string[] {
145
123
  const tokens: string[] = [];
146
124
 
@@ -18,6 +18,8 @@ export class Reference implements Pattern {
18
18
  private _pattern: Pattern | null;
19
19
  private _children: Pattern[];
20
20
  private _firstIndex: number;
21
+ private _cachedAncestors: boolean;
22
+ private _recursiveAncestors: Reference[];
21
23
 
22
24
  get id(): string {
23
25
  return this._id;
@@ -56,6 +58,8 @@ export class Reference implements Pattern {
56
58
  this._cachedPattern = null;
57
59
  this._children = [];
58
60
  this._firstIndex = 0;
61
+ this._cachedAncestors = false;
62
+ this._recursiveAncestors = [];
59
63
  }
60
64
 
61
65
  test(text: string, record = false): boolean {
@@ -68,9 +72,46 @@ export class Reference implements Pattern {
68
72
 
69
73
  parse(cursor: Cursor): Node | null {
70
74
  this._firstIndex = cursor.index;
75
+ this._cacheAncestors();
76
+
77
+ if (this._isBeyondRecursiveAllowance()) {
78
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
79
+ return null;
80
+ }
81
+
71
82
  return this.getReferencePatternSafely().parse(cursor);
72
83
  }
73
84
 
85
+ private _cacheAncestors() {
86
+ if (!this._cachedAncestors) {
87
+ let pattern: Pattern | null = this.parent;
88
+
89
+ while (pattern != null) {
90
+ if (pattern.type === this.type && pattern.id === this._id) {
91
+ this._recursiveAncestors.push(pattern as Reference);
92
+ }
93
+ pattern = pattern.parent;
94
+ }
95
+ }
96
+
97
+ }
98
+
99
+ private _isBeyondRecursiveAllowance() {
100
+ let depth = 0;
101
+
102
+ for (let pattern of this._recursiveAncestors) {
103
+ if (pattern._firstIndex === this._firstIndex) {
104
+ depth++;
105
+
106
+ if (depth > 2) {
107
+ return true;
108
+ }
109
+ }
110
+ }
111
+
112
+ return false;
113
+ }
114
+
74
115
  getReferencePatternSafely(): Pattern {
75
116
  if (this._pattern === null) {
76
117
  let pattern: Pattern | null = null;
@@ -98,11 +98,6 @@ export class Sequence implements Pattern {
98
98
  }
99
99
 
100
100
  private tryToParse(cursor: Cursor): boolean {
101
- if (this._isBeyondRecursiveAllowance()) {
102
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
103
- return false;
104
- }
105
-
106
101
  let passed = false;
107
102
 
108
103
  for (let i = 0; i < this._children.length; i++) {
@@ -173,24 +168,6 @@ export class Sequence implements Pattern {
173
168
  return nodes[nodes.length - 1];
174
169
  }
175
170
 
176
- private _isBeyondRecursiveAllowance() {
177
- let depth = 0;
178
- let pattern: Pattern | null = this;
179
-
180
- while (pattern != null) {
181
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
182
- depth++;
183
- }
184
-
185
- if (depth > 1) {
186
- return true;
187
- }
188
- pattern = pattern.parent;
189
- }
190
-
191
- return false;
192
- }
193
-
194
171
  private areRemainingPatternsOptional(fromIndex: number): boolean {
195
172
  const startOnIndex = fromIndex + 1;
196
173
  const length = this._children.length;