clarity-pattern-parser 11.0.16 → 11.0.19

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.
@@ -21,6 +21,7 @@ export declare class Expression implements Pattern {
21
21
  private _precedenceMap;
22
22
  private _shouldStopParsing;
23
23
  private _precedenceTree;
24
+ private _hasOrganized;
24
25
  get id(): string;
25
26
  get type(): string;
26
27
  get name(): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-pattern-parser",
3
- "version": "11.0.16",
3
+ "version": "11.0.19",
4
4
  "description": "Parsing Library for Typescript and Javascript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -272,8 +272,8 @@ export class Grammar {
272
272
  }
273
273
 
274
274
  private _isRecursive(name: string, pattern: Pattern) {
275
- if (pattern.type === "right-associated" && this._isRecursivePattern(name, pattern.children[0])) {
276
- return true;
275
+ if (pattern.type === "right-associated") {
276
+ pattern = pattern.children[0];
277
277
  }
278
278
 
279
279
  return this._isRecursivePattern(name, pattern);
@@ -287,9 +287,10 @@ export class Grammar {
287
287
  const firstChild = pattern.children[0];
288
288
  const lastChild = pattern.children[pattern.children.length - 1];
289
289
  const isLongEnough = pattern.children.length >= 2;
290
- return pattern.type === "sequence" && isLongEnough &&
291
- (firstChild.type === "reference" && firstChild.name === name) ||
292
- (lastChild.type === "reference" && lastChild.name === name);
290
+ return pattern.type === "reference" ||
291
+ (pattern.type === "sequence" && isLongEnough &&
292
+ (firstChild.type === "reference" && firstChild.name === name) ||
293
+ (lastChild.type === "reference" && lastChild.name === name));
293
294
  }
294
295
 
295
296
  private _buildPattern(node: Node): Pattern {
@@ -7,6 +7,7 @@ import { Sequence } from "./Sequence";
7
7
  import { Association, PrecedenceTree } from './PrecedenceTree';
8
8
  import { testPattern } from "./testPattern";
9
9
  import { execPattern } from "./execPattern";
10
+ import { Reference } from "./Reference";
10
11
 
11
12
  let indexId = 0;
12
13
 
@@ -29,6 +30,7 @@ export class Expression implements Pattern {
29
30
  private _precedenceMap: Record<string, number>;
30
31
  private _shouldStopParsing: boolean;
31
32
  private _precedenceTree: PrecedenceTree;
33
+ private _hasOrganized: boolean;
32
34
 
33
35
  get id(): string {
34
36
  return this._id;
@@ -94,13 +96,10 @@ export class Expression implements Pattern {
94
96
  this._associationMap = {};
95
97
  this._precedenceMap = {};
96
98
  this._originalPatterns = patterns;
97
- this._patterns = this._organizePatterns(patterns);
98
99
  this._shouldStopParsing = false;
99
- this._precedenceTree = new PrecedenceTree(this._precedenceMap, this._associationMap);
100
-
101
- if (this._atomPatterns.length === 0) {
102
- throw new Error("Need at least one terminating pattern with an 'expression' pattern.");
103
- }
100
+ this._hasOrganized = false;
101
+ this._patterns = [];
102
+ this._precedenceTree = new PrecedenceTree({}, {});
104
103
  }
105
104
 
106
105
  private _organizePatterns(patterns: Pattern[]) {
@@ -152,6 +151,9 @@ export class Expression implements Pattern {
152
151
  }
153
152
  });
154
153
 
154
+ this._patterns = finalPatterns;
155
+ this._precedenceTree = new PrecedenceTree(this._precedenceMap, this._associationMap);
156
+
155
157
  return finalPatterns;
156
158
  }
157
159
 
@@ -227,7 +229,11 @@ export class Expression implements Pattern {
227
229
 
228
230
  private _unwrapAssociationIfNecessary(pattern: Pattern) {
229
231
  if (pattern.type === "right-associated") {
230
- return pattern.children[0];
232
+ pattern = pattern.children[0];
233
+ }
234
+
235
+ if (pattern.type === "reference") {
236
+ pattern = (pattern as Reference).getReferencePatternSafely();
231
237
  }
232
238
 
233
239
  return pattern;
@@ -246,6 +252,19 @@ export class Expression implements Pattern {
246
252
 
247
253
  parse(cursor: Cursor): Node | null {
248
254
  this._firstIndex = cursor.index;
255
+
256
+ if (!this._hasOrganized) {
257
+ this._hasOrganized = true;
258
+ this._organizePatterns(this._originalPatterns);
259
+ }
260
+
261
+ // If there are not any atom nodes then nothing can be found.
262
+ if (this._atomPatterns.length < 1) {
263
+ cursor.moveTo(this._firstIndex);
264
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
265
+ return null;
266
+ }
267
+
249
268
  const node = this._tryToParse(cursor);
250
269
 
251
270
  if (node != null) {
@@ -97,13 +97,6 @@ describe("Reference", () => {
97
97
  expect(result).toBeNull();
98
98
  });
99
99
 
100
- test("Test With Match", () => {
101
- const value = createValuePattern();
102
- const reference = findPattern(value, p => p.type === "reference") as Reference;
103
- const result = reference.test("[1]");
104
- expect(result).toBeTruthy();
105
- });
106
-
107
100
  test("Test No Match", () => {
108
101
  const value = createValuePattern();
109
102
  const reference = findPattern(value, p => p.type === "reference") as Reference;
@@ -104,7 +104,7 @@ export class Reference implements Pattern {
104
104
  if (pattern._firstIndex === this._firstIndex) {
105
105
  depth++;
106
106
 
107
- if (depth > 1) {
107
+ if (depth > 0) {
108
108
  return true;
109
109
  }
110
110
  }