clarity-pattern-parser 8.4.15 → 9.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 +4 -1
- package/dist/grammar/Grammar.d.ts +18 -10
- package/dist/grammar/patterns/andLiteral.d.ts +2 -0
- package/dist/grammar/patterns/anonymousPattern.d.ts +2 -0
- package/dist/grammar/patterns/inlinePattern.d.ts +1 -0
- package/dist/grammar/patterns/literals.d.ts +3 -0
- package/dist/grammar/patterns/pattern.d.ts +2 -2
- package/dist/grammar/patterns.d.ts +2 -0
- package/dist/index.browser.js +471 -184
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.esm.js +470 -185
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +471 -184
- package/dist/index.js.map +1 -1
- package/dist/patterns/And.d.ts +4 -1
- package/dist/patterns/Cursor.d.ts +5 -0
- package/dist/patterns/CursorHistory.d.ts +7 -0
- package/dist/patterns/FiniteRepeat.d.ts +4 -1
- package/dist/patterns/InfiniteRepeat.d.ts +5 -4
- package/dist/patterns/Literal.d.ts +6 -5
- package/dist/patterns/Not.d.ts +5 -4
- package/dist/patterns/Or.d.ts +5 -4
- package/dist/patterns/Pattern.d.ts +4 -2
- package/dist/patterns/Reference.d.ts +5 -4
- package/dist/patterns/Regex.d.ts +5 -4
- package/dist/patterns/Repeat.d.ts +3 -0
- package/dist/patterns/arePatternsEqual.d.ts +2 -0
- package/package.json +1 -1
- package/src/grammar/Grammar.test.ts +117 -74
- package/src/grammar/Grammar.ts +241 -158
- package/src/grammar/patterns/anonymousPattern.ts +23 -0
- package/src/grammar/patterns/body.ts +9 -6
- package/src/grammar/patterns/comment.ts +3 -2
- package/src/grammar/patterns/grammar.ts +15 -12
- package/src/grammar/patterns/import.ts +18 -12
- package/src/grammar/patterns/literal.ts +2 -1
- package/src/grammar/patterns/literals.ts +20 -0
- package/src/grammar/patterns/optionsLiteral.ts +19 -0
- package/src/grammar/patterns/pattern.ts +23 -9
- package/src/grammar/patterns/regexLiteral.ts +1 -0
- package/src/grammar/patterns/repeatLiteral.ts +30 -25
- package/src/grammar/patterns/sequenceLiteral.ts +24 -0
- package/src/grammar/patterns/spaces.ts +8 -6
- package/src/grammar/patterns/statement.ts +8 -20
- package/src/grammar/patterns.test.ts +38 -0
- package/src/grammar/patterns.ts +24 -0
- package/src/grammar/spec.md +4 -12
- package/src/index.ts +11 -5
- package/src/intellisense/AutoComplete.test.ts +41 -40
- package/src/intellisense/css/method.ts +2 -2
- package/src/intellisense/css/unit.ts +2 -2
- package/src/intellisense/css/value.ts +1 -1
- package/src/intellisense/javascript/Javascript.test.ts +31 -32
- package/src/intellisense/javascript/arrayLiteral.ts +7 -6
- package/src/intellisense/javascript/assignment.ts +6 -6
- package/src/intellisense/javascript/deleteStatement.ts +2 -2
- package/src/intellisense/javascript/escapedCharacter.ts +6 -6
- package/src/intellisense/javascript/exponent.ts +6 -6
- package/src/intellisense/javascript/expression.ts +18 -17
- package/src/intellisense/javascript/fraction.ts +3 -3
- package/src/intellisense/javascript/infixOperator.ts +10 -10
- package/src/intellisense/javascript/integer.ts +1 -1
- package/src/intellisense/javascript/invocation.ts +8 -7
- package/src/intellisense/javascript/literal.ts +3 -3
- package/src/intellisense/javascript/numberLiteral.ts +5 -4
- package/src/intellisense/javascript/objectAccess.ts +2 -3
- package/src/intellisense/javascript/objectLiteral.ts +8 -7
- package/src/intellisense/javascript/optionalSpaces.ts +2 -1
- package/src/intellisense/javascript/parameters.ts +5 -5
- package/src/intellisense/javascript/prefixOperator.ts +3 -4
- package/src/intellisense/javascript/propertyAccess.ts +9 -8
- package/src/intellisense/javascript/stringLiteral.ts +14 -15
- package/src/patterns/Cursor.ts +42 -4
- package/src/patterns/CursorHistory.ts +20 -4
- package/src/patterns/FiniteRepeat.test.ts +52 -51
- package/src/patterns/FiniteRepeat.ts +60 -38
- package/src/patterns/InfiniteRepeat.test.ts +36 -49
- package/src/patterns/InfiniteRepeat.ts +70 -37
- package/src/patterns/Literal.test.ts +16 -27
- package/src/patterns/Literal.ts +34 -27
- package/src/patterns/Not.test.ts +7 -7
- package/src/patterns/Not.ts +24 -6
- package/src/patterns/Optional.test.ts +164 -0
- package/src/patterns/Optional.ts +143 -0
- package/src/patterns/{Or.test.ts → Options.test.ts} +51 -49
- package/src/patterns/{Or.ts → Options.ts} +32 -23
- package/src/patterns/Pattern.ts +6 -5
- package/src/patterns/Reference.test.ts +21 -22
- package/src/patterns/Reference.ts +26 -15
- package/src/patterns/Regex.test.ts +15 -15
- package/src/patterns/Regex.ts +29 -19
- package/src/patterns/Repeat.test.ts +12 -22
- package/src/patterns/Repeat.ts +22 -21
- package/src/patterns/{And.test.ts → Sequence.test.ts} +78 -78
- package/src/patterns/{And.ts → Sequence.ts} +40 -29
- package/src/patterns/arePatternsEqual.ts +12 -0
- package/src/patterns/clonePatterns.ts +2 -2
- package/src/grammar/patterns/andLiteral.ts +0 -8
- package/src/grammar/patterns/orLiteral.ts +0 -8
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Sequence } from "../patterns/Sequence";
|
|
2
|
+
import { arePatternsEqual } from "../patterns/arePatternsEqual";
|
|
2
3
|
import { Literal } from "../patterns/Literal";
|
|
3
4
|
import { Not } from "../patterns/Not";
|
|
4
|
-
import {
|
|
5
|
+
import { Options } from "../patterns/Options";
|
|
5
6
|
import { Pattern } from "../patterns/Pattern";
|
|
7
|
+
import { Reference } from "../patterns/Reference";
|
|
6
8
|
import { Regex } from "../patterns/Regex";
|
|
7
9
|
import { Repeat } from "../patterns/Repeat";
|
|
8
10
|
import { Grammar } from "./Grammar";
|
|
11
|
+
import { Optional } from "../patterns/Optional";
|
|
9
12
|
|
|
10
13
|
describe("Grammar", () => {
|
|
11
14
|
test("Literal", () => {
|
|
@@ -14,10 +17,10 @@ describe("Grammar", () => {
|
|
|
14
17
|
`;
|
|
15
18
|
|
|
16
19
|
const patterns = Grammar.parseString(expression);
|
|
17
|
-
const namePattern = patterns
|
|
20
|
+
const namePattern = patterns["name"];
|
|
18
21
|
const expected = new Literal("name", "John");
|
|
19
22
|
|
|
20
|
-
expect(namePattern).
|
|
23
|
+
expect(arePatternsEqual(namePattern, expected)).toBeTruthy();
|
|
21
24
|
});
|
|
22
25
|
|
|
23
26
|
test("Literal With Escaped Characters", () => {
|
|
@@ -25,10 +28,10 @@ describe("Grammar", () => {
|
|
|
25
28
|
chars = "\\n\\r\\t\\b\\f\\v\\0\\x00\\u0000\\"\\\\"
|
|
26
29
|
`;
|
|
27
30
|
const patterns = Grammar.parseString(expression);
|
|
28
|
-
const namePattern = patterns
|
|
31
|
+
const namePattern = patterns["chars"];
|
|
29
32
|
const expected = new Literal("chars", "\n\r\t\b\f\v\0\x00\u0000\"\\");
|
|
30
33
|
|
|
31
|
-
expect(namePattern).
|
|
34
|
+
expect(arePatternsEqual(namePattern, expected)).toBeTruthy();
|
|
32
35
|
});
|
|
33
36
|
|
|
34
37
|
test("Literal With Escaped Quotes", () => {
|
|
@@ -36,10 +39,10 @@ describe("Grammar", () => {
|
|
|
36
39
|
content = "With Con\\"tent"
|
|
37
40
|
`;
|
|
38
41
|
const patterns = Grammar.parseString(expression);
|
|
39
|
-
const namePattern = patterns
|
|
42
|
+
const namePattern = patterns["content"];
|
|
40
43
|
const expected = new Literal("content", "With Con\"tent");
|
|
41
44
|
|
|
42
|
-
expect(namePattern).
|
|
45
|
+
expect(arePatternsEqual(namePattern, expected)).toBeTruthy();
|
|
43
46
|
});
|
|
44
47
|
|
|
45
48
|
test("Regex", () => {
|
|
@@ -48,10 +51,10 @@ describe("Grammar", () => {
|
|
|
48
51
|
`;
|
|
49
52
|
|
|
50
53
|
const patterns = Grammar.parseString(expression);
|
|
51
|
-
const pattern = patterns
|
|
54
|
+
const pattern = patterns["name"];
|
|
52
55
|
const name = new Regex("name", "\\w");
|
|
53
56
|
|
|
54
|
-
expect(pattern).
|
|
57
|
+
expect(arePatternsEqual(pattern, name)).toBeTruthy();
|
|
55
58
|
});
|
|
56
59
|
|
|
57
60
|
test("Or", () => {
|
|
@@ -62,12 +65,12 @@ describe("Grammar", () => {
|
|
|
62
65
|
`;
|
|
63
66
|
|
|
64
67
|
const patterns = Grammar.parseString(expression);
|
|
65
|
-
const pattern = patterns
|
|
68
|
+
const pattern = patterns["names"];
|
|
66
69
|
const john = new Literal("john", "John");
|
|
67
70
|
const jane = new Literal("jane", "Jane");
|
|
68
|
-
const names = new
|
|
71
|
+
const names = new Options("names", [john, jane], true);
|
|
69
72
|
|
|
70
|
-
expect(pattern).
|
|
73
|
+
expect(arePatternsEqual(pattern, names)).toBeTruthy();
|
|
71
74
|
});
|
|
72
75
|
|
|
73
76
|
test("And", () => {
|
|
@@ -75,17 +78,17 @@ describe("Grammar", () => {
|
|
|
75
78
|
space = " "
|
|
76
79
|
first-name = /\\w/
|
|
77
80
|
last-name = /\\w/
|
|
78
|
-
full-name = first-name
|
|
81
|
+
full-name = first-name + space + last-name
|
|
79
82
|
`;
|
|
80
83
|
|
|
81
84
|
const patterns = Grammar.parseString(expression);
|
|
82
|
-
const pattern = patterns
|
|
85
|
+
const pattern = patterns["full-name"];
|
|
83
86
|
const space = new Literal("space", " ");
|
|
84
87
|
const firstName = new Regex("first-name", "\\w");
|
|
85
88
|
const lastName = new Regex("last-name", "\\w");
|
|
86
|
-
const fullName = new
|
|
89
|
+
const fullName = new Sequence("full-name", [firstName, space, lastName]);
|
|
87
90
|
|
|
88
|
-
expect(pattern).
|
|
91
|
+
expect(arePatternsEqual(pattern, fullName)).toBeTruthy();
|
|
89
92
|
});
|
|
90
93
|
|
|
91
94
|
test("And With Optional Pattern", () => {
|
|
@@ -94,20 +97,20 @@ describe("Grammar", () => {
|
|
|
94
97
|
first-name = /\\w/
|
|
95
98
|
last-name = /\\w/
|
|
96
99
|
middle-name = /\\w/
|
|
97
|
-
middle-name-with-space = middle-name
|
|
98
|
-
full-name = first-name
|
|
100
|
+
middle-name-with-space = middle-name + space
|
|
101
|
+
full-name = first-name + space + middle-name-with-space? + last-name
|
|
99
102
|
`;
|
|
100
103
|
|
|
101
104
|
const patterns = Grammar.parseString(expression);
|
|
102
|
-
const pattern = patterns
|
|
105
|
+
const pattern = patterns["full-name"];
|
|
103
106
|
const space = new Literal("space", " ");
|
|
104
107
|
const firstName = new Regex("first-name", "\\w");
|
|
105
108
|
const lastName = new Regex("last-name", "\\w");
|
|
106
109
|
const middleName = new Regex("middle-name", "\\w");
|
|
107
|
-
const middleNameWithSpace = new
|
|
108
|
-
const fullName = new
|
|
110
|
+
const middleNameWithSpace = new Optional("optional-middle-name-with-space", new Sequence("middle-name-with-space", [middleName, space]));
|
|
111
|
+
const fullName = new Sequence("full-name", [firstName, space, middleNameWithSpace, lastName]);
|
|
109
112
|
|
|
110
|
-
expect(pattern).
|
|
113
|
+
expect(arePatternsEqual(pattern, fullName)).toBeTruthy();
|
|
111
114
|
});
|
|
112
115
|
|
|
113
116
|
test("And With Not Pattern", () => {
|
|
@@ -117,22 +120,21 @@ describe("Grammar", () => {
|
|
|
117
120
|
last-name = /\\w/
|
|
118
121
|
middle-name = /\\w/
|
|
119
122
|
jack = "Jack"
|
|
120
|
-
middle-name-with-space = middle-name
|
|
121
|
-
full-name = !jack
|
|
123
|
+
middle-name-with-space = middle-name + space
|
|
124
|
+
full-name = !jack + first-name + space + middle-name-with-space? + last-name
|
|
122
125
|
`;
|
|
123
126
|
|
|
124
127
|
const patterns = Grammar.parseString(expression);
|
|
125
|
-
const pattern = patterns
|
|
128
|
+
const pattern = patterns["full-name"];
|
|
126
129
|
const space = new Literal("space", " ");
|
|
127
130
|
const firstName = new Regex("first-name", "\\w");
|
|
128
131
|
const lastName = new Regex("last-name", "\\w");
|
|
129
132
|
const middleName = new Regex("middle-name", "\\w");
|
|
130
133
|
const jack = new Literal("jack", "Jack");
|
|
131
134
|
const notJack = new Not("not-jack", jack);
|
|
132
|
-
const middleNameWithSpace = new
|
|
133
|
-
const fullName = new
|
|
134
|
-
|
|
135
|
-
expect(pattern).toEqual(fullName);
|
|
135
|
+
const middleNameWithSpace = new Optional("optional-middle-name-with-space", new Sequence("middle-name-with-space", [middleName, space]));
|
|
136
|
+
const fullName = new Sequence("full-name", [notJack, firstName, space, middleNameWithSpace, lastName]);
|
|
137
|
+
expect(arePatternsEqual(pattern, fullName)).toBeTruthy();
|
|
136
138
|
});
|
|
137
139
|
|
|
138
140
|
test("Repeat", () => {
|
|
@@ -142,11 +144,11 @@ describe("Grammar", () => {
|
|
|
142
144
|
`;
|
|
143
145
|
|
|
144
146
|
const patterns = Grammar.parseString(expression);
|
|
145
|
-
const pattern = patterns
|
|
147
|
+
const pattern = patterns["digits"];
|
|
146
148
|
const digit = new Regex("digit", "\\d");
|
|
147
149
|
const digits = new Repeat("digits", digit);
|
|
148
150
|
|
|
149
|
-
expect(pattern).
|
|
151
|
+
expect(arePatternsEqual(pattern, digits)).toBeTruthy();
|
|
150
152
|
});
|
|
151
153
|
|
|
152
154
|
test("Repeat Zero Or More", () => {
|
|
@@ -156,11 +158,11 @@ describe("Grammar", () => {
|
|
|
156
158
|
`;
|
|
157
159
|
|
|
158
160
|
const patterns = Grammar.parseString(expression);
|
|
159
|
-
const pattern = patterns
|
|
161
|
+
const pattern = patterns["digits"];
|
|
160
162
|
const digit = new Regex("digit", "\\d");
|
|
161
|
-
const digits = new Repeat("digits", digit, { min: 0 });
|
|
163
|
+
const digits = new Optional("digits", new Repeat("digits", digit, { min: 0 }));
|
|
162
164
|
|
|
163
|
-
expect(pattern).
|
|
165
|
+
expect(arePatternsEqual(pattern, digits)).toBeTruthy();
|
|
164
166
|
});
|
|
165
167
|
|
|
166
168
|
test("Repeat Lower Limit", () => {
|
|
@@ -170,11 +172,11 @@ describe("Grammar", () => {
|
|
|
170
172
|
`;
|
|
171
173
|
|
|
172
174
|
const patterns = Grammar.parseString(expression);
|
|
173
|
-
const pattern = patterns
|
|
175
|
+
const pattern = patterns["digits"];
|
|
174
176
|
const digit = new Regex("digit", "\\d+");
|
|
175
177
|
const digits = new Repeat("digits", digit, { min: 1 });
|
|
176
178
|
|
|
177
|
-
expect(pattern).
|
|
179
|
+
expect(arePatternsEqual(pattern, digits)).toBeTruthy();
|
|
178
180
|
});
|
|
179
181
|
|
|
180
182
|
test("Repeat Bounded", () => {
|
|
@@ -184,11 +186,10 @@ describe("Grammar", () => {
|
|
|
184
186
|
`;
|
|
185
187
|
|
|
186
188
|
const patterns = Grammar.parseString(expression);
|
|
187
|
-
const pattern = patterns
|
|
189
|
+
const pattern = patterns["digits"];
|
|
188
190
|
const digit = new Regex("digit", "\\d+");
|
|
189
191
|
const digits = new Repeat("digits", digit, { min: 1, max: 3 });
|
|
190
|
-
|
|
191
|
-
expect(pattern).toEqual(digits);
|
|
192
|
+
expect(arePatternsEqual(pattern, digits)).toBeTruthy();
|
|
192
193
|
});
|
|
193
194
|
|
|
194
195
|
test("Repeat Upper Limit", () => {
|
|
@@ -198,11 +199,11 @@ describe("Grammar", () => {
|
|
|
198
199
|
`;
|
|
199
200
|
|
|
200
201
|
const patterns = Grammar.parseString(expression);
|
|
201
|
-
const pattern = patterns
|
|
202
|
+
const pattern = patterns["digits"];
|
|
202
203
|
const digit = new Regex("digit", "\\d+");
|
|
203
204
|
const digits = new Repeat("digits", digit, { min: 0, max: 3 });
|
|
204
205
|
|
|
205
|
-
expect(pattern).
|
|
206
|
+
expect(arePatternsEqual(pattern, digits)).toBeTruthy();
|
|
206
207
|
});
|
|
207
208
|
|
|
208
209
|
test("Repeat Exact", () => {
|
|
@@ -212,11 +213,11 @@ describe("Grammar", () => {
|
|
|
212
213
|
`;
|
|
213
214
|
|
|
214
215
|
const patterns = Grammar.parseString(expression);
|
|
215
|
-
const pattern = patterns
|
|
216
|
+
const pattern = patterns["digits"];
|
|
216
217
|
const digit = new Regex("digit", "\\d+");
|
|
217
218
|
const digits = new Repeat("digits", digit, { min: 3, max: 3 });
|
|
218
219
|
|
|
219
|
-
expect(pattern).
|
|
220
|
+
expect(arePatternsEqual(pattern, digits)).toBeTruthy();
|
|
220
221
|
});
|
|
221
222
|
|
|
222
223
|
test("Repeat Divider", () => {
|
|
@@ -227,28 +228,44 @@ describe("Grammar", () => {
|
|
|
227
228
|
`;
|
|
228
229
|
|
|
229
230
|
const patterns = Grammar.parseString(expression);
|
|
230
|
-
const pattern = patterns
|
|
231
|
+
const pattern = patterns["digits"];
|
|
231
232
|
const digit = new Regex("digit", "\\d+");
|
|
232
233
|
const divider = new Literal("comma", ",");
|
|
233
234
|
const digits = new Repeat("digits", digit, { divider, min: 3, max: 3 });
|
|
234
235
|
|
|
235
|
-
expect(pattern).
|
|
236
|
+
expect(arePatternsEqual(pattern, digits)).toBeTruthy();
|
|
236
237
|
});
|
|
237
238
|
|
|
238
239
|
test("Repeat Divider With Trim Divider", () => {
|
|
239
240
|
const expression = `
|
|
240
241
|
digit = /\\d+/
|
|
241
242
|
comma = ","
|
|
242
|
-
digits = (digit, comma)
|
|
243
|
+
digits = (digit, comma trim)+
|
|
243
244
|
`;
|
|
244
245
|
|
|
245
246
|
const patterns = Grammar.parseString(expression);
|
|
246
|
-
const pattern = patterns
|
|
247
|
+
const pattern = patterns["digits"];
|
|
248
|
+
const digit = new Regex("digit", "\\d+");
|
|
249
|
+
const divider = new Literal("comma", ",");
|
|
250
|
+
const digits = new Repeat("digits", digit, { divider, min: 1, trimDivider: true });
|
|
251
|
+
debugger;
|
|
252
|
+
expect(arePatternsEqual(pattern, digits)).toBeTruthy();
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test("Repeat Divider With Trim Divider And Bounds", () => {
|
|
256
|
+
const expression = `
|
|
257
|
+
digit = /\\d+/
|
|
258
|
+
comma = ","
|
|
259
|
+
digits = (digit, comma trim){3, 3}
|
|
260
|
+
`;
|
|
261
|
+
|
|
262
|
+
const patterns = Grammar.parseString(expression);
|
|
263
|
+
const pattern = patterns["digits"];
|
|
247
264
|
const digit = new Regex("digit", "\\d+");
|
|
248
265
|
const divider = new Literal("comma", ",");
|
|
249
266
|
const digits = new Repeat("digits", digit, { divider, min: 3, max: 3, trimDivider: true });
|
|
250
267
|
|
|
251
|
-
expect(pattern).
|
|
268
|
+
expect(arePatternsEqual(pattern, digits)).toBeTruthy();
|
|
252
269
|
});
|
|
253
270
|
|
|
254
271
|
test("Reference", () => {
|
|
@@ -259,15 +276,15 @@ describe("Grammar", () => {
|
|
|
259
276
|
close-bracket = "]"
|
|
260
277
|
spaces = /\\s+/
|
|
261
278
|
items = digit | array
|
|
262
|
-
array-items = (items, divider)*
|
|
263
|
-
array = open-bracket
|
|
279
|
+
array-items = (items, divider trim)*
|
|
280
|
+
array = open-bracket + spaces? + array-items + spaces? + close-bracket
|
|
264
281
|
`;
|
|
265
282
|
|
|
266
283
|
const patterns = Grammar.parseString(expression);
|
|
267
|
-
const pattern = patterns
|
|
284
|
+
const pattern = patterns["array"] as Pattern;
|
|
268
285
|
|
|
269
286
|
let text = "[1, []]";
|
|
270
|
-
let result = pattern.exec(text);
|
|
287
|
+
let result = pattern.exec(text, true);
|
|
271
288
|
|
|
272
289
|
expect(result.ast?.value).toEqual("[1, []]");
|
|
273
290
|
});
|
|
@@ -280,14 +297,14 @@ describe("Grammar", () => {
|
|
|
280
297
|
|
|
281
298
|
const patterns = Grammar.parseString(expression);
|
|
282
299
|
|
|
283
|
-
const name = patterns
|
|
300
|
+
const name = patterns["name"];
|
|
284
301
|
const expectedName = new Regex("name", "regex");
|
|
285
302
|
|
|
286
|
-
const alias = patterns
|
|
303
|
+
const alias = patterns["alias"];
|
|
287
304
|
const expectedAlias = new Regex("alias", "regex");
|
|
288
305
|
|
|
289
|
-
expect(name).
|
|
290
|
-
expect(alias).
|
|
306
|
+
expect(arePatternsEqual(name, expectedName)).toBeTruthy();
|
|
307
|
+
expect(arePatternsEqual(alias, expectedAlias)).toBeTruthy();
|
|
291
308
|
});
|
|
292
309
|
|
|
293
310
|
test("Bad Grammar At Beginning", () => {
|
|
@@ -306,7 +323,7 @@ describe("Grammar", () => {
|
|
|
306
323
|
age = /()
|
|
307
324
|
`;
|
|
308
325
|
Grammar.parseString(expression);
|
|
309
|
-
}).
|
|
326
|
+
}).toThrow();
|
|
310
327
|
|
|
311
328
|
});
|
|
312
329
|
|
|
@@ -316,15 +333,15 @@ describe("Grammar", () => {
|
|
|
316
333
|
import { first-name } from "some/path/to/file.cpat"
|
|
317
334
|
last-name = "Doe"
|
|
318
335
|
space = " "
|
|
319
|
-
full-name = first-name
|
|
320
|
-
|
|
336
|
+
full-name = first-name + space + last-name
|
|
337
|
+
`;
|
|
321
338
|
function resolveImport(resource: string) {
|
|
322
339
|
expect(resource).toBe("some/path/to/file.cpat");
|
|
323
340
|
return Promise.resolve({ expression: importExpression, resource });
|
|
324
341
|
}
|
|
325
342
|
|
|
326
343
|
const patterns = await Grammar.parse(expression, { resolveImport });
|
|
327
|
-
const fullname = patterns
|
|
344
|
+
const fullname = patterns["full-name"] as Pattern;
|
|
328
345
|
const result = fullname.exec("John Doe");
|
|
329
346
|
|
|
330
347
|
expect(result?.ast?.value).toBe("John Doe");
|
|
@@ -332,7 +349,7 @@ describe("Grammar", () => {
|
|
|
332
349
|
|
|
333
350
|
test("Imports", async () => {
|
|
334
351
|
const importExpression = `first-name = "John"`;
|
|
335
|
-
const spaceExpression = `space = " "
|
|
352
|
+
const spaceExpression = `space = " "`;
|
|
336
353
|
|
|
337
354
|
const pathMap: Record<string, string> = {
|
|
338
355
|
"space.cpat": spaceExpression,
|
|
@@ -343,14 +360,14 @@ describe("Grammar", () => {
|
|
|
343
360
|
import { first-name } from "first-name.cpat"
|
|
344
361
|
import { space } from "space.cpat"
|
|
345
362
|
last-name = "Doe"
|
|
346
|
-
full-name = first-name
|
|
347
|
-
|
|
363
|
+
full-name = first-name + space + last-name
|
|
364
|
+
`;
|
|
348
365
|
function resolveImport(resource: string) {
|
|
349
366
|
return Promise.resolve({ expression: pathMap[resource], resource });
|
|
350
367
|
}
|
|
351
368
|
|
|
352
369
|
const patterns = await Grammar.parse(expression, { resolveImport });
|
|
353
|
-
const fullname = patterns
|
|
370
|
+
const fullname = patterns["full-name"] as Pattern;
|
|
354
371
|
const result = fullname.exec("John Doe");
|
|
355
372
|
expect(result?.ast?.value).toBe("John Doe");
|
|
356
373
|
});
|
|
@@ -360,13 +377,13 @@ describe("Grammar", () => {
|
|
|
360
377
|
const spaceExpression = `
|
|
361
378
|
use params { custom-space }
|
|
362
379
|
space = custom-space
|
|
363
|
-
|
|
380
|
+
`;
|
|
364
381
|
const expression = `
|
|
365
382
|
import { first-name } from "first-name.cpat"
|
|
366
383
|
import { space } from "space.cpat" with params { custom-space = " " }
|
|
367
384
|
last-name = "Doe"
|
|
368
|
-
full-name = first-name
|
|
369
|
-
|
|
385
|
+
full-name = first-name + space + last-name
|
|
386
|
+
`;
|
|
370
387
|
|
|
371
388
|
const pathMap: Record<string, string> = {
|
|
372
389
|
"space.cpat": spaceExpression,
|
|
@@ -378,7 +395,7 @@ describe("Grammar", () => {
|
|
|
378
395
|
}
|
|
379
396
|
|
|
380
397
|
const patterns = await Grammar.parse(expression, { resolveImport });
|
|
381
|
-
const fullname = patterns
|
|
398
|
+
const fullname = patterns["full-name"] as Pattern;
|
|
382
399
|
const result = fullname.exec("John Doe");
|
|
383
400
|
expect(result?.ast?.value).toBe("John Doe");
|
|
384
401
|
});
|
|
@@ -391,7 +408,7 @@ describe("Grammar", () => {
|
|
|
391
408
|
}
|
|
392
409
|
|
|
393
410
|
name
|
|
394
|
-
|
|
411
|
+
`;
|
|
395
412
|
|
|
396
413
|
const resource1 = `
|
|
397
414
|
use-this = "Use This"
|
|
@@ -413,7 +430,7 @@ describe("Grammar", () => {
|
|
|
413
430
|
return Promise.resolve({ expression: pathMap[resource], resource });
|
|
414
431
|
}
|
|
415
432
|
const patterns = await Grammar.parse(expression, { resolveImport });
|
|
416
|
-
const pattern = patterns
|
|
433
|
+
const pattern = patterns["name"] as Literal;
|
|
417
434
|
|
|
418
435
|
const result = pattern.exec("Use This");
|
|
419
436
|
|
|
@@ -427,7 +444,7 @@ describe("Grammar", () => {
|
|
|
427
444
|
param = alias
|
|
428
445
|
}
|
|
429
446
|
name = export-value
|
|
430
|
-
|
|
447
|
+
`;
|
|
431
448
|
|
|
432
449
|
const resource1 = `
|
|
433
450
|
value = "Value"
|
|
@@ -438,7 +455,7 @@ describe("Grammar", () => {
|
|
|
438
455
|
param
|
|
439
456
|
}
|
|
440
457
|
export-value = param
|
|
441
|
-
|
|
458
|
+
`;
|
|
442
459
|
|
|
443
460
|
const pathMap: Record<string, string> = {
|
|
444
461
|
"resource1": resource1,
|
|
@@ -449,9 +466,35 @@ describe("Grammar", () => {
|
|
|
449
466
|
return Promise.resolve({ expression: pathMap[resource], resource });
|
|
450
467
|
}
|
|
451
468
|
const patterns = await Grammar.parse(expression, { resolveImport });
|
|
452
|
-
const pattern = patterns
|
|
469
|
+
const pattern = patterns["name"] as Literal;
|
|
453
470
|
|
|
454
471
|
const result = pattern.exec("Value");
|
|
455
472
|
expect(result.ast?.value).toBe("Value");
|
|
456
473
|
});
|
|
474
|
+
|
|
475
|
+
test("Anonymous Patterns", () => {
|
|
476
|
+
const expression = `
|
|
477
|
+
complex-expression = !"NOT_THIS" + "Text"? + /regex/ + ("Text" <|> /regex/ <|> (pattern)+) + (pattern | pattern)
|
|
478
|
+
`;
|
|
479
|
+
|
|
480
|
+
const patterns = Grammar.parseString(expression);
|
|
481
|
+
const expected = new Sequence("complex-expression", [
|
|
482
|
+
new Not("not-NOT_THIS", new Literal("NOT_THIS", "NOT_THIS")),
|
|
483
|
+
new Optional("Text", new Literal("Text", "Text")),
|
|
484
|
+
new Regex("regex", "regex"),
|
|
485
|
+
new Options("anonymous", [
|
|
486
|
+
new Literal("Text", "Text"),
|
|
487
|
+
new Regex("regex", "regex"),
|
|
488
|
+
new Repeat("anonymous", new Reference("pattern")),
|
|
489
|
+
],
|
|
490
|
+
true
|
|
491
|
+
),
|
|
492
|
+
new Options("anonymous", [
|
|
493
|
+
new Reference("pattern"),
|
|
494
|
+
new Reference("pattern")
|
|
495
|
+
])
|
|
496
|
+
]);
|
|
497
|
+
|
|
498
|
+
expect(arePatternsEqual(patterns["complex-expression"], expected)).toBeTruthy();
|
|
499
|
+
});
|
|
457
500
|
});
|