clarity-pattern-parser 10.1.21 → 10.1.23
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 +30 -9
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +30 -9
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +30 -9
- package/dist/index.js.map +1 -1
- package/dist/patterns/Reference.d.ts +1 -1
- package/dist/patterns/isRecursivePattern.d.ts +2 -0
- package/package.json +1 -1
- package/src/patterns/Context.ts +8 -1
- package/src/patterns/ExpressionPattern.ts +11 -3
- package/src/patterns/Options.ts +3 -2
- package/src/patterns/Reference.ts +4 -4
- package/src/patterns/Sequence.ts +3 -3
- package/src/patterns/isRecursivePattern.ts +21 -0
|
@@ -20,7 +20,7 @@ export declare class Reference implements Pattern {
|
|
|
20
20
|
test(text: string): boolean;
|
|
21
21
|
exec(text: string, record?: boolean): ParseResult;
|
|
22
22
|
parse(cursor: Cursor): Node | null;
|
|
23
|
-
|
|
23
|
+
getReferencePatternSafely(): Pattern;
|
|
24
24
|
private _findPattern;
|
|
25
25
|
private _isValidPattern;
|
|
26
26
|
private _getRoot;
|
package/package.json
CHANGED
package/src/patterns/Context.ts
CHANGED
|
@@ -84,6 +84,9 @@ export class Context implements Pattern {
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
getTokensAfter(childReference: Pattern): string[] {
|
|
87
|
+
if (this.parent == null) {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
87
90
|
return this._pattern.getTokensAfter(childReference);
|
|
88
91
|
}
|
|
89
92
|
|
|
@@ -96,7 +99,11 @@ export class Context implements Pattern {
|
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
getPatternsAfter(childReference: Pattern): Pattern[] {
|
|
99
|
-
|
|
102
|
+
if (this.parent == null) {
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return this.parent.getPatternsAfter(childReference);
|
|
100
107
|
}
|
|
101
108
|
|
|
102
109
|
getNextPatterns(): Pattern[] {
|
|
@@ -41,17 +41,23 @@ export class ExpressionPattern implements Pattern {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
get children(): Pattern[] {
|
|
44
|
-
return
|
|
44
|
+
return this._patterns;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
constructor(name: string, patterns: []) {
|
|
47
|
+
constructor(name: string, patterns: Pattern[]) {
|
|
48
48
|
this._id = `expression-${indexId++}`;
|
|
49
49
|
this._type = "expression";
|
|
50
|
+
this._name = name;
|
|
50
51
|
this._unaryPatterns = [];
|
|
51
52
|
this._binaryPatterns = [];
|
|
53
|
+
|
|
54
|
+
this._patterns.forEach(p => p.parent = this);
|
|
55
|
+
this._patterns = this._organizePatterns(patterns);
|
|
56
|
+
|
|
52
57
|
}
|
|
53
58
|
|
|
54
|
-
private _organizePatterns() {
|
|
59
|
+
private _organizePatterns(patterns: Pattern[]) {
|
|
60
|
+
const finalPatterns = [];
|
|
55
61
|
this._patterns.forEach((pattern) => {
|
|
56
62
|
if (this._isBinary(pattern)) {
|
|
57
63
|
this._binaryPatterns.push(pattern);
|
|
@@ -59,6 +65,8 @@ export class ExpressionPattern implements Pattern {
|
|
|
59
65
|
this._unaryPatterns.push();
|
|
60
66
|
}
|
|
61
67
|
});
|
|
68
|
+
|
|
69
|
+
return finalPatterns;
|
|
62
70
|
}
|
|
63
71
|
|
|
64
72
|
private _isBinary(pattern: Pattern) {
|
package/src/patterns/Options.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { clonePatterns } from "./clonePatterns";
|
|
|
5
5
|
import { findPattern } from "./findPattern";
|
|
6
6
|
import { ParseResult } from "./ParseResult";
|
|
7
7
|
import { DepthCache } from './DepthCache';
|
|
8
|
+
import { isRecursivePattern } from "./isRecursivePattern";
|
|
8
9
|
|
|
9
10
|
/*
|
|
10
11
|
The following is created to reduce the overhead of recursion check.
|
|
@@ -143,7 +144,7 @@ export class Options implements Pattern {
|
|
|
143
144
|
const tokens: string[] = [];
|
|
144
145
|
|
|
145
146
|
for (const pattern of this._children) {
|
|
146
|
-
if (pattern
|
|
147
|
+
if (isRecursivePattern(pattern)) {
|
|
147
148
|
continue;
|
|
148
149
|
}
|
|
149
150
|
tokens.push(...pattern.getTokens());
|
|
@@ -172,7 +173,7 @@ export class Options implements Pattern {
|
|
|
172
173
|
const patterns: Pattern[] = [];
|
|
173
174
|
|
|
174
175
|
for (const pattern of this._children) {
|
|
175
|
-
if (pattern
|
|
176
|
+
if (isRecursivePattern(pattern)) {
|
|
176
177
|
continue;
|
|
177
178
|
}
|
|
178
179
|
patterns.push(...pattern.getPatterns());
|
|
@@ -70,10 +70,10 @@ export class Reference implements Pattern {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
parse(cursor: Cursor): Node | null {
|
|
73
|
-
return this.
|
|
73
|
+
return this.getReferencePatternSafely().parse(cursor);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
getReferencePatternSafely(): Pattern {
|
|
77
77
|
if (this._pattern === null) {
|
|
78
78
|
let pattern: Pattern | null = null;
|
|
79
79
|
|
|
@@ -150,7 +150,7 @@ export class Reference implements Pattern {
|
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
getTokens(): string[] {
|
|
153
|
-
return this.
|
|
153
|
+
return this.getReferencePatternSafely().getTokens();
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
getTokensAfter(_lastMatched: Pattern): string[] {
|
|
@@ -170,7 +170,7 @@ export class Reference implements Pattern {
|
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
getPatterns(): Pattern[] {
|
|
173
|
-
return this.
|
|
173
|
+
return this.getReferencePatternSafely().getPatterns();
|
|
174
174
|
}
|
|
175
175
|
|
|
176
176
|
getPatternsAfter(_childReference: Pattern): Pattern[] {
|
package/src/patterns/Sequence.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { clonePatterns } from "./clonePatterns";
|
|
|
5
5
|
import { filterOutNull } from "./filterOutNull";
|
|
6
6
|
import { findPattern } from "./findPattern";
|
|
7
7
|
import { DepthCache } from "./DepthCache";
|
|
8
|
+
import { isRecursivePattern } from "./isRecursivePattern";
|
|
8
9
|
|
|
9
10
|
const depthCache = new DepthCache();
|
|
10
11
|
let idIndex = 0;
|
|
@@ -217,7 +218,7 @@ export class Sequence implements Pattern {
|
|
|
217
218
|
const tokens: string[] = [];
|
|
218
219
|
|
|
219
220
|
for (const pattern of this._children) {
|
|
220
|
-
if (pattern
|
|
221
|
+
if (isRecursivePattern(pattern) && pattern === this._children[0]) {
|
|
221
222
|
return tokens;
|
|
222
223
|
}
|
|
223
224
|
|
|
@@ -251,8 +252,7 @@ export class Sequence implements Pattern {
|
|
|
251
252
|
const patterns: Pattern[] = [];
|
|
252
253
|
|
|
253
254
|
for (const pattern of this._children) {
|
|
254
|
-
|
|
255
|
-
if (pattern.type === "reference" && pattern.name === this.name && pattern === this.children[0]) {
|
|
255
|
+
if (isRecursivePattern(pattern) && pattern === this._children[0]) {
|
|
256
256
|
return patterns;
|
|
257
257
|
}
|
|
258
258
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Pattern } from "./Pattern";
|
|
2
|
+
import { Reference } from "./Reference";
|
|
3
|
+
|
|
4
|
+
export function isRecursivePattern(pattern: Pattern) {
|
|
5
|
+
let onPattern = pattern.parent;
|
|
6
|
+
let depth = 0;
|
|
7
|
+
|
|
8
|
+
while (onPattern != null) {
|
|
9
|
+
if (onPattern.id === pattern.id) {
|
|
10
|
+
depth++;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
onPattern = onPattern.parent;
|
|
14
|
+
|
|
15
|
+
if (depth > 1){
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return false;
|
|
21
|
+
}
|