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.
- package/dist/index.browser.js +26 -12
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +26 -12
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +26 -12
- package/dist/index.js.map +1 -1
- package/dist/patterns/Expression.d.ts +1 -0
- package/package.json +1 -1
- package/src/grammar/Grammar.ts +6 -5
- package/src/patterns/Expression.ts +26 -7
- package/src/patterns/Reference.test.ts +0 -7
- package/src/patterns/Reference.ts +1 -1
package/package.json
CHANGED
package/src/grammar/Grammar.ts
CHANGED
|
@@ -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"
|
|
276
|
-
|
|
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 === "
|
|
291
|
-
(
|
|
292
|
-
|
|
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.
|
|
100
|
-
|
|
101
|
-
|
|
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
|
-
|
|
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;
|