clarity-pattern-parser 6.0.2 → 7.0.0
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/TODO.md +1 -78
- package/dist/ast/Node.d.ts +1 -0
- package/dist/grammar/Grammar.d.ts +17 -0
- package/dist/grammar/patterns/andLiteral.d.ts +2 -0
- package/dist/grammar/patterns/comment.d.ts +2 -0
- package/dist/grammar/patterns/grammar.d.ts +2 -0
- package/dist/grammar/patterns/literal.d.ts +2 -0
- package/dist/grammar/patterns/name.d.ts +2 -0
- package/dist/grammar/patterns/orLiteral.d.ts +2 -0
- package/dist/grammar/patterns/pattern.d.ts +2 -0
- package/dist/grammar/patterns/regexLiteral.d.ts +2 -0
- package/dist/grammar/patterns/repeatLiteral.d.ts +3 -0
- package/dist/grammar/patterns/spaces.d.ts +2 -0
- package/dist/grammar/patterns/statement.d.ts +2 -0
- package/dist/index.browser.js +1161 -556
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.esm.js +1159 -555
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1159 -554
- package/dist/index.js.map +1 -1
- package/dist/intellisense/AutoComplete.d.ts +2 -6
- package/dist/patterns/And.d.ts +1 -1
- package/dist/patterns/Cursor.d.ts +1 -0
- package/dist/patterns/CursorHistory.d.ts +2 -1
- package/dist/patterns/FiniteRepeat.d.ts +39 -0
- package/dist/patterns/InfiniteRepeat.d.ts +47 -0
- package/dist/patterns/Literal.d.ts +1 -1
- package/dist/patterns/Not.d.ts +1 -1
- package/dist/patterns/Or.d.ts +1 -1
- package/dist/patterns/Pattern.d.ts +1 -1
- package/dist/patterns/Reference.d.ts +1 -1
- package/dist/patterns/Regex.d.ts +1 -1
- package/dist/patterns/Repeat.d.ts +18 -22
- package/jest.config.js +0 -1
- package/jest.coverage.config.js +13 -0
- package/package.json +3 -3
- package/src/ast/Node.test.ts +15 -0
- package/src/ast/Node.ts +12 -6
- package/src/grammar/Grammar.test.ts +288 -0
- package/src/grammar/Grammar.ts +234 -0
- package/src/grammar/patterns/andLiteral.ts +8 -0
- package/src/grammar/patterns/comment.ts +3 -0
- package/src/grammar/patterns/grammar.ts +19 -0
- package/src/grammar/patterns/literal.ts +5 -0
- package/src/grammar/patterns/name.ts +3 -0
- package/src/grammar/patterns/orLiteral.ts +8 -0
- package/src/grammar/patterns/pattern.ts +13 -0
- package/src/grammar/patterns/regexLiteral.ts +4 -0
- package/src/grammar/patterns/repeatLiteral.ts +72 -0
- package/src/grammar/patterns/spaces.ts +4 -0
- package/src/grammar/patterns/statement.ts +35 -0
- package/src/grammar/spec.md +142 -0
- package/src/index.ts +6 -3
- package/src/intellisense/AutoComplete.test.ts +21 -2
- package/src/intellisense/AutoComplete.ts +14 -25
- package/src/intellisense/Suggestion.ts +0 -1
- package/src/intellisense/css/cssValue.ts +1 -1
- package/src/intellisense/css/method.ts +1 -1
- package/src/intellisense/css/values.ts +1 -1
- package/src/intellisense/javascript/Javascript.test.ts +0 -1
- package/src/intellisense/javascript/arrayLiteral.ts +1 -1
- package/src/intellisense/javascript/expression.ts +1 -1
- package/src/intellisense/javascript/invocation.ts +1 -1
- package/src/intellisense/javascript/objectLiteral.ts +1 -1
- package/src/intellisense/javascript/parameters.ts +1 -1
- package/src/intellisense/javascript/stringLiteral.ts +2 -4
- package/src/patterns/And.test.ts +5 -5
- package/src/patterns/And.ts +11 -17
- package/src/patterns/Cursor.ts +4 -0
- package/src/patterns/CursorHistory.ts +34 -5
- package/src/patterns/FiniteRepeat.test.ts +481 -0
- package/src/patterns/FiniteRepeat.ts +231 -0
- package/src/patterns/InfiniteRepeat.test.ts +296 -0
- package/src/patterns/InfiniteRepeat.ts +329 -0
- package/src/patterns/Literal.test.ts +4 -4
- package/src/patterns/Literal.ts +1 -1
- package/src/patterns/Not.test.ts +11 -11
- package/src/patterns/Not.ts +1 -1
- package/src/patterns/Or.test.ts +9 -9
- package/src/patterns/Or.ts +1 -1
- package/src/patterns/Pattern.ts +1 -1
- package/src/patterns/Reference.test.ts +8 -8
- package/src/patterns/Reference.ts +1 -1
- package/src/patterns/Regex.test.ts +4 -4
- package/src/patterns/Regex.ts +1 -1
- package/src/patterns/Repeat.test.ts +160 -165
- package/src/patterns/Repeat.ts +95 -230
package/src/patterns/Repeat.ts
CHANGED
|
@@ -1,276 +1,141 @@
|
|
|
1
1
|
import { Node } from "../ast/Node";
|
|
2
2
|
import { Cursor } from "./Cursor";
|
|
3
|
+
import { FiniteRepeat } from "./FiniteRepeat";
|
|
4
|
+
import { InfiniteRepeat } from "./InfiniteRepeat";
|
|
5
|
+
import { ParseResult } from "./ParseResult";
|
|
3
6
|
import { Pattern } from "./Pattern";
|
|
4
|
-
import { clonePatterns } from "./clonePatterns";
|
|
5
|
-
import { findPattern } from "./findPattern";
|
|
6
7
|
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
private _divider: Pattern;
|
|
14
|
-
private _isOptional: boolean;
|
|
15
|
-
private _nodes: Node[];
|
|
16
|
-
private _firstIndex: number;
|
|
17
|
-
|
|
18
|
-
get type(): string {
|
|
19
|
-
return this._type;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
get name(): string {
|
|
23
|
-
return this._name;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
get parent(): Pattern | null {
|
|
27
|
-
return this._parent;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
set parent(pattern: Pattern | null) {
|
|
31
|
-
this._parent = pattern;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
get children(): Pattern[] {
|
|
35
|
-
return this._children;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get isOptional(): boolean {
|
|
39
|
-
return this._isOptional;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
constructor(name: string, pattern: Pattern, divider?: Pattern, isOptional = false) {
|
|
43
|
-
const patterns = divider != null ? [pattern, divider] : [pattern];
|
|
44
|
-
const children: Pattern[] = clonePatterns(patterns, false);
|
|
45
|
-
this._assignChildrenToParent(children);
|
|
8
|
+
export interface RepeatOptions {
|
|
9
|
+
min?: number;
|
|
10
|
+
max?: number;
|
|
11
|
+
divider?: Pattern;
|
|
12
|
+
trimDivider?: boolean;
|
|
13
|
+
}
|
|
46
14
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
this._pattern = children[0];
|
|
53
|
-
this._divider = children[1];
|
|
54
|
-
this._firstIndex = -1
|
|
55
|
-
this._nodes = [];
|
|
56
|
-
}
|
|
15
|
+
interface InternalRepeatOptions {
|
|
16
|
+
min: number;
|
|
17
|
+
max: number;
|
|
18
|
+
divider?: Pattern;
|
|
19
|
+
}
|
|
57
20
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
21
|
+
export class Repeat implements Pattern {
|
|
22
|
+
private _repeatPattern: InfiniteRepeat | FiniteRepeat;
|
|
23
|
+
private _parent: Pattern | null;
|
|
24
|
+
private _pattern: Pattern;
|
|
25
|
+
private _options: InternalRepeatOptions;
|
|
26
|
+
private _children: Pattern[];
|
|
27
|
+
|
|
28
|
+
get type() {
|
|
29
|
+
return this._repeatPattern.type;
|
|
61
30
|
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
test(text: string) {
|
|
65
|
-
const cursor = new Cursor(text);
|
|
66
|
-
const ast = this.parse(cursor);
|
|
67
|
-
|
|
68
|
-
return ast?.value === text;
|
|
69
|
-
}
|
|
70
31
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const ast = this.parse(cursor);
|
|
74
|
-
|
|
75
|
-
return {
|
|
76
|
-
ast: ast?.value === text ? ast : null,
|
|
77
|
-
cursor
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
parse(cursor: Cursor): Node | null {
|
|
82
|
-
this._firstIndex = cursor.index;
|
|
83
|
-
this._nodes = [];
|
|
84
|
-
|
|
85
|
-
const passed = this.tryToParse(cursor);
|
|
86
|
-
|
|
87
|
-
if (passed) {
|
|
88
|
-
cursor.resolveError();
|
|
89
|
-
const node = this.createNode(cursor);
|
|
90
|
-
|
|
91
|
-
if (node != null) {
|
|
92
|
-
cursor.recordMatch(this, node);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return node;
|
|
32
|
+
get name() {
|
|
33
|
+
return this._repeatPattern.name;
|
|
96
34
|
}
|
|
97
35
|
|
|
98
|
-
|
|
99
|
-
|
|
36
|
+
get parent() {
|
|
37
|
+
return this._parent;
|
|
100
38
|
}
|
|
101
39
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
private tryToParse(cursor: Cursor): boolean {
|
|
108
|
-
let passed = false;
|
|
40
|
+
set parent(value: Pattern | null) {
|
|
41
|
+
this._parent = value;
|
|
42
|
+
}
|
|
109
43
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
44
|
+
get children() {
|
|
45
|
+
return this._children;
|
|
46
|
+
}
|
|
113
47
|
|
|
114
|
-
|
|
115
|
-
|
|
48
|
+
get isOptional() {
|
|
49
|
+
return this._repeatPattern.isOptional;
|
|
50
|
+
}
|
|
116
51
|
|
|
117
|
-
|
|
118
|
-
|
|
52
|
+
constructor(name: string, pattern: Pattern, options: RepeatOptions = {}) {
|
|
53
|
+
this._pattern = pattern;
|
|
54
|
+
this._parent = null;
|
|
55
|
+
this._options = {
|
|
56
|
+
...options,
|
|
57
|
+
min: options.min == null ? 1 : options.min,
|
|
58
|
+
max: options.max == null ? Infinity : options.max
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
if (this._options.max != Infinity) {
|
|
62
|
+
this._repeatPattern = new FiniteRepeat(name, pattern, this._options.max, this._options);
|
|
119
63
|
} else {
|
|
120
|
-
|
|
121
|
-
cursor.recordErrorAt(runningCursorIndex, this._pattern);
|
|
122
|
-
passed = false;
|
|
64
|
+
this._repeatPattern = new InfiniteRepeat(name, pattern, this._options)
|
|
123
65
|
}
|
|
124
66
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
67
|
+
this._children = [this._repeatPattern]
|
|
68
|
+
this._repeatPattern.parent = this;
|
|
69
|
+
}
|
|
128
70
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
71
|
+
parse(cursor: Cursor): Node | null {
|
|
72
|
+
return this._repeatPattern.parse(cursor);
|
|
73
|
+
}
|
|
133
74
|
|
|
134
|
-
|
|
75
|
+
exec(text: string): ParseResult {
|
|
76
|
+
return this._repeatPattern.exec(text);
|
|
77
|
+
}
|
|
135
78
|
|
|
136
|
-
|
|
137
|
-
|
|
79
|
+
test(text: string): boolean {
|
|
80
|
+
return this._repeatPattern.test(text);
|
|
81
|
+
}
|
|
138
82
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
break;
|
|
142
|
-
} else if (dividerNode != null) {
|
|
143
|
-
this._nodes.push(dividerNode);
|
|
83
|
+
clone(name = this.name, isOptional?: boolean) {
|
|
84
|
+
let min = this._options.min;
|
|
144
85
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
86
|
+
if (isOptional != null) {
|
|
87
|
+
if (isOptional) {
|
|
88
|
+
min = 0
|
|
89
|
+
} else {
|
|
90
|
+
min = Math.max(this._options.min, 1);
|
|
148
91
|
}
|
|
149
|
-
|
|
150
|
-
cursor.next();
|
|
151
|
-
}
|
|
152
92
|
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
93
|
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
private createNode(cursor: Cursor): Node | null {
|
|
160
|
-
let children: Node[] = [];
|
|
161
|
-
|
|
162
|
-
if (!this._divider) {
|
|
163
|
-
children = this._nodes;
|
|
164
|
-
} else {
|
|
165
|
-
if (this._nodes.length % 2 !== 1) {
|
|
166
|
-
const dividerNode = this._nodes[this._nodes.length - 1];
|
|
167
|
-
cursor.moveTo(dividerNode.firstIndex);
|
|
168
|
-
children = this._nodes.slice(0, this._nodes.length - 1);
|
|
169
|
-
} else {
|
|
170
|
-
children = this._nodes;
|
|
171
|
-
}
|
|
94
|
+
return new Repeat(name, this._pattern, { ...this._options, min });
|
|
172
95
|
}
|
|
173
96
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
cursor.moveTo(lastIndex);
|
|
177
|
-
|
|
178
|
-
return new Node(
|
|
179
|
-
"repeat",
|
|
180
|
-
this._name,
|
|
181
|
-
this._firstIndex,
|
|
182
|
-
lastIndex,
|
|
183
|
-
children,
|
|
184
|
-
undefined
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
private getLastValidNode(): Node | null {
|
|
189
|
-
const nodes = this._nodes.filter((node) => node !== null);
|
|
190
|
-
|
|
191
|
-
if (nodes.length === 0) {
|
|
192
|
-
return null;
|
|
97
|
+
getTokens(): string[] {
|
|
98
|
+
return this._repeatPattern.getTokens();
|
|
193
99
|
}
|
|
194
100
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
return this._pattern.getTokens();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
getTokensAfter(childReference: Pattern): string[] {
|
|
203
|
-
const patterns = this.getPatternsAfter(childReference);
|
|
204
|
-
const tokens: string[] = [];
|
|
205
|
-
|
|
206
|
-
patterns.forEach(p => tokens.push(...p.getTokens()));
|
|
207
|
-
|
|
208
|
-
return tokens;
|
|
209
|
-
}
|
|
101
|
+
getTokensAfter(_childReference: Pattern): string[] {
|
|
102
|
+
if (this._parent == null) {
|
|
103
|
+
return []
|
|
104
|
+
}
|
|
210
105
|
|
|
211
|
-
|
|
212
|
-
if (this.parent == null) {
|
|
213
|
-
return []
|
|
106
|
+
return this._parent.getTokensAfter(this);
|
|
214
107
|
}
|
|
215
108
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
return this._pattern.getPatterns();
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
getPatternsAfter(childReference: Pattern): Pattern[] {
|
|
224
|
-
let index = -1;
|
|
225
|
-
const patterns: Pattern[] = [];
|
|
109
|
+
getNextTokens(): string[] {
|
|
110
|
+
if (this._parent == null) {
|
|
111
|
+
return []
|
|
112
|
+
}
|
|
226
113
|
|
|
227
|
-
|
|
228
|
-
if (this._children[i] === childReference) {
|
|
229
|
-
index = i;
|
|
230
|
-
}
|
|
114
|
+
return this._parent.getTokensAfter(this);
|
|
231
115
|
}
|
|
232
116
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
return [];
|
|
117
|
+
getPatterns(): Pattern[] {
|
|
118
|
+
return this._repeatPattern.getPatterns();
|
|
236
119
|
}
|
|
237
120
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
121
|
+
getPatternsAfter(_childReference: Pattern): Pattern[] {
|
|
122
|
+
if (this._parent == null) {
|
|
123
|
+
return []
|
|
124
|
+
}
|
|
241
125
|
|
|
242
|
-
|
|
243
|
-
patterns.push(...this._parent.getPatternsAfter(this));
|
|
244
|
-
}
|
|
126
|
+
return this._parent.getPatternsAfter(this);
|
|
245
127
|
}
|
|
246
128
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
129
|
+
getNextPatterns(): Pattern[] {
|
|
130
|
+
if (this._parent == null) {
|
|
131
|
+
return []
|
|
132
|
+
}
|
|
251
133
|
|
|
252
|
-
|
|
253
|
-
patterns.push(this._children[0]);
|
|
254
|
-
patterns.push(...this._parent.getPatternsAfter(this));
|
|
134
|
+
return this._parent.getPatternsAfter(this);
|
|
255
135
|
}
|
|
256
136
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
getNextPatterns(): Pattern[] {
|
|
261
|
-
if (this.parent == null) {
|
|
262
|
-
return [];
|
|
137
|
+
find(predicate: (p: Pattern) => boolean): Pattern | null {
|
|
138
|
+
return this._repeatPattern.find(predicate);
|
|
263
139
|
}
|
|
264
140
|
|
|
265
|
-
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
findPattern(predicate: (p: Pattern) => boolean): Pattern | null {
|
|
269
|
-
return findPattern(this, predicate);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
clone(name = this._name, isOptional = this._isOptional): Pattern {
|
|
273
|
-
return new Repeat(name, this._pattern, this._divider, isOptional);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
141
|
+
}
|