clarity-pattern-parser 3.0.16 → 4.0.1
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/README.md +0 -191
- package/dist/ast/Node.d.ts +4 -5
- package/dist/index.browser.js +476 -731
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +10 -18
- package/dist/index.esm.js +469 -716
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +476 -731
- package/dist/index.js.map +1 -1
- package/dist/patterns/And.d.ts +24 -0
- package/dist/patterns/Literal.d.ts +19 -0
- package/dist/patterns/LookAhead.d.ts +8 -0
- package/dist/patterns/Not.d.ts +11 -0
- package/dist/patterns/Or.d.ts +22 -0
- package/dist/patterns/Pattern.d.ts +6 -7
- package/dist/patterns/{RecursivePattern.d.ts → Recursive.d.ts} +4 -4
- package/dist/patterns/Reference.d.ts +14 -0
- package/dist/patterns/Regex.d.ts +21 -0
- package/dist/patterns/Repeat.d.ts +20 -0
- package/package.json +1 -1
- package/src/CursorHistory.ts +1 -1
- package/src/TextSuggester.ts +1 -1
- package/src/ast/Node.ts +20 -17
- package/src/ast/Visitor.ts +14 -18
- package/src/index.ts +17 -33
- package/src/patterns/And.ts +178 -0
- package/src/patterns/Literal.ts +91 -0
- package/src/patterns/Not.ts +50 -0
- package/src/patterns/Or.ts +132 -0
- package/src/patterns/Pattern.ts +31 -47
- package/src/patterns/Recursive.ts +92 -0
- package/src/patterns/{ReferencePattern.ts → Reference.ts} +32 -28
- package/src/patterns/Regex.ts +123 -0
- package/src/patterns/Repeat.ts +155 -0
- package/src/tests/{AndValue.test.ts → And.test.ts} +31 -41
- package/src/tests/CursorHistory.test.ts +6 -6
- package/src/tests/Cusor.test.ts +7 -10
- package/src/tests/Literal.test.ts +3 -5
- package/src/tests/LookAhead.test.ts +2 -51
- package/src/tests/Not.test.ts +51 -0
- package/src/tests/Or.test.ts +113 -0
- package/src/tests/Pattern.test.ts +40 -139
- package/src/tests/{RecursivePattern.test.ts → Recursive.test.ts} +10 -8
- package/src/tests/Reference.test.ts +16 -0
- package/src/tests/{RepeatValue.test.ts → Repeat.test.ts} +10 -42
- package/src/tests/TextSuggester.test.ts +30 -28
- package/src/tests/{NodeVisitor.test.ts → Visitor.test.ts} +42 -21
- package/src/tests/cssPatterns/cssValue.ts +2 -2
- package/src/tests/cssPatterns/divider.ts +2 -2
- package/src/tests/cssPatterns/hex.ts +2 -2
- package/src/tests/cssPatterns/method.ts +7 -9
- package/src/tests/cssPatterns/name.ts +2 -2
- package/src/tests/cssPatterns/number.ts +2 -2
- package/src/tests/cssPatterns/optionalSpaces.ts +1 -2
- package/src/tests/cssPatterns/spaces.ts +2 -2
- package/src/tests/cssPatterns/unit.ts +3 -3
- package/src/tests/cssPatterns/value.ts +2 -2
- package/src/tests/cssPatterns/values.ts +2 -2
- package/src/tests/htmlPatterns/element.ts +18 -33
- package/src/tests/javascriptPatterns/boolean.ts +2 -3
- package/src/tests/javascriptPatterns/json.ts +14 -26
- package/src/tests/javascriptPatterns/name.ts +3 -20
- package/src/tests/javascriptPatterns/number.ts +2 -2
- package/src/tests/javascriptPatterns/objectLiteral.ts +9 -16
- package/src/tests/javascriptPatterns/string.ts +26 -24
- package/src/tests/javascriptPatterns/unit.ts +3 -6
- package/src/tests/javascriptPatterns/whitespace.ts +8 -12
- package/src/tests/naturalLanguage/filter.ts +16 -33
- package/src/tests/patterns/sentence.ts +8 -8
- package/dist/Cursor.js +0 -105
- package/dist/Cursor.js.map +0 -1
- package/dist/CursorHistory.js +0 -104
- package/dist/CursorHistory.js.map +0 -1
- package/dist/Permutor.d.ts +0 -13
- package/dist/Permutor.js +0 -52
- package/dist/Permutor.js.map +0 -1
- package/dist/TextSuggester.js +0 -244
- package/dist/TextSuggester.js.map +0 -1
- package/dist/ast/CompositeNode.d.ts +0 -6
- package/dist/ast/CompositeNode.js +0 -17
- package/dist/ast/CompositeNode.js.map +0 -1
- package/dist/ast/Node.js +0 -16
- package/dist/ast/Node.js.map +0 -1
- package/dist/ast/NodeVisitor.d.ts +0 -31
- package/dist/ast/ValueNode.d.ts +0 -6
- package/dist/ast/ValueNode.js +0 -14
- package/dist/ast/ValueNode.js.map +0 -1
- package/dist/ast/Visitor.js +0 -209
- package/dist/ast/Visitor.js.map +0 -1
- package/dist/patterns/ParseError.js +0 -9
- package/dist/patterns/ParseError.js.map +0 -1
- package/dist/patterns/Pattern.js +0 -127
- package/dist/patterns/Pattern.js.map +0 -1
- package/dist/patterns/RecursivePattern.js +0 -65
- package/dist/patterns/RecursivePattern.js.map +0 -1
- package/dist/patterns/composite/AndComposite.d.ts +0 -22
- package/dist/patterns/composite/AndComposite.js +0 -117
- package/dist/patterns/composite/AndComposite.js.map +0 -1
- package/dist/patterns/composite/CompositePattern.d.ts +0 -4
- package/dist/patterns/composite/CompositePattern.js +0 -7
- package/dist/patterns/composite/CompositePattern.js.map +0 -1
- package/dist/patterns/composite/OptionalComposite.d.ts +0 -10
- package/dist/patterns/composite/OptionalComposite.js +0 -29
- package/dist/patterns/composite/OptionalComposite.js.map +0 -1
- package/dist/patterns/composite/OrComposite.d.ts +0 -16
- package/dist/patterns/composite/OrComposite.js +0 -69
- package/dist/patterns/composite/OrComposite.js.map +0 -1
- package/dist/patterns/composite/RepeatComposite.d.ts +0 -21
- package/dist/patterns/composite/RepeatComposite.js +0 -88
- package/dist/patterns/composite/RepeatComposite.js.map +0 -1
- package/dist/patterns/value/AndValue.d.ts +0 -21
- package/dist/patterns/value/AndValue.js +0 -118
- package/dist/patterns/value/AndValue.js.map +0 -1
- package/dist/patterns/value/AnyOfThese.d.ts +0 -18
- package/dist/patterns/value/AnyOfThese.js +0 -59
- package/dist/patterns/value/AnyOfThese.js.map +0 -1
- package/dist/patterns/value/Literal.d.ts +0 -20
- package/dist/patterns/value/Literal.js +0 -63
- package/dist/patterns/value/Literal.js.map +0 -1
- package/dist/patterns/value/NotValue.d.ts +0 -17
- package/dist/patterns/value/NotValue.js +0 -70
- package/dist/patterns/value/NotValue.js.map +0 -1
- package/dist/patterns/value/OptionalValue.d.ts +0 -9
- package/dist/patterns/value/OptionalValue.js +0 -32
- package/dist/patterns/value/OptionalValue.js.map +0 -1
- package/dist/patterns/value/OrValue.d.ts +0 -19
- package/dist/patterns/value/OrValue.js +0 -73
- package/dist/patterns/value/OrValue.js.map +0 -1
- package/dist/patterns/value/RegexValue.d.ts +0 -19
- package/dist/patterns/value/RegexValue.js +0 -69
- package/dist/patterns/value/RegexValue.js.map +0 -1
- package/dist/patterns/value/RepeatValue.d.ts +0 -19
- package/dist/patterns/value/RepeatValue.js +0 -89
- package/dist/patterns/value/RepeatValue.js.map +0 -1
- package/dist/patterns/value/ValuePattern.d.ts +0 -5
- package/dist/patterns/value/ValuePattern.js +0 -7
- package/dist/patterns/value/ValuePattern.js.map +0 -1
- package/src/Permutor.ts +0 -64
- package/src/ast/CompositeNode.ts +0 -26
- package/src/ast/ValueNode.ts +0 -28
- package/src/patterns/RecursivePattern.ts +0 -86
- package/src/patterns/composite/AndComposite.ts +0 -159
- package/src/patterns/composite/CompositePattern.ts +0 -7
- package/src/patterns/composite/OptionalComposite.ts +0 -37
- package/src/patterns/composite/OrComposite.ts +0 -96
- package/src/patterns/composite/RepeatComposite.ts +0 -130
- package/src/patterns/value/AndValue.ts +0 -152
- package/src/patterns/value/AnyOfThese.ts +0 -81
- package/src/patterns/value/Literal.ts +0 -92
- package/src/patterns/value/NotValue.ts +0 -95
- package/src/patterns/value/OptionalValue.ts +0 -39
- package/src/patterns/value/OrValue.ts +0 -103
- package/src/patterns/value/RegexValue.ts +0 -103
- package/src/patterns/value/RepeatValue.ts +0 -131
- package/src/patterns/value/ValuePattern.ts +0 -8
- package/src/tests/AndComposite.test.ts +0 -102
- package/src/tests/AnyOfThese.test.ts +0 -74
- package/src/tests/CompositeNode.test.ts +0 -33
- package/src/tests/NotValue.test.ts +0 -76
- package/src/tests/OptionalValue.test.ts +0 -50
- package/src/tests/OrComposite.test.ts +0 -75
- package/src/tests/OrValue.test.ts +0 -171
- package/src/tests/Permutor.test.ts +0 -30
- package/src/tests/ReferencePattern.test.ts +0 -24
- package/src/tests/RegexValue.test.ts +0 -22
- package/src/tests/RepeatComposite.test.ts +0 -58
- package/src/tests/ValueNode.test.ts +0 -24
- package/src/tests/javascriptPatterns/varStatement.ts +0 -0
- package/src/tests/readmeDemo.test.ts +0 -124
|
@@ -1,52 +1,10 @@
|
|
|
1
1
|
/** @jest-environment node */
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import RepeatValue from "../patterns/value/RepeatValue";
|
|
7
|
-
import OptionalValue from "../patterns/value/OptionalValue";
|
|
8
|
-
|
|
9
|
-
describe("ValuePattern", () => {
|
|
10
|
-
test("parse.", () => {
|
|
11
|
-
const valueValuePattern = new (ValuePattern as any)(
|
|
12
|
-
"pattern-type",
|
|
13
|
-
"pattern"
|
|
14
|
-
);
|
|
15
|
-
expect(() => {
|
|
16
|
-
valueValuePattern.parse();
|
|
17
|
-
}).toThrow();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("clone.", () => {
|
|
21
|
-
const valueValuePattern = new (ValuePattern as any)(
|
|
22
|
-
"pattern-type",
|
|
23
|
-
"pattern"
|
|
24
|
-
);
|
|
25
|
-
expect(() => {
|
|
26
|
-
valueValuePattern.clone();
|
|
27
|
-
}).toThrow();
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("getPossibilities.", () => {
|
|
31
|
-
const valueValuePattern = new (ValuePattern as any)(
|
|
32
|
-
"pattern-type",
|
|
33
|
-
"pattern"
|
|
34
|
-
);
|
|
35
|
-
expect(() => {
|
|
36
|
-
valueValuePattern.getPossibilities();
|
|
37
|
-
}).toThrow();
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test("limited arguments.", () => {
|
|
41
|
-
new (ValuePattern as any)(undefined, "name");
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test("no arguments.", () => {
|
|
45
|
-
expect(() => {
|
|
46
|
-
new (ValuePattern as any)();
|
|
47
|
-
}).toThrow();
|
|
48
|
-
});
|
|
2
|
+
import And from "../patterns/And";
|
|
3
|
+
import Or from "../patterns/Or";
|
|
4
|
+
import Literal from "../patterns/Literal";
|
|
5
|
+
import Repeat from "../patterns/Repeat";
|
|
49
6
|
|
|
7
|
+
describe("Pattern", () => {
|
|
50
8
|
test("set parent.", () => {
|
|
51
9
|
const parent = new Literal("pattern-type", "pattern");
|
|
52
10
|
const child = new Literal("pattern-type", "pattern");
|
|
@@ -54,17 +12,10 @@ describe("ValuePattern", () => {
|
|
|
54
12
|
child.parent = parent;
|
|
55
13
|
});
|
|
56
14
|
|
|
57
|
-
test("set invalid parent.", () => {
|
|
58
|
-
const child = new Literal("pattern-type", "pattern");
|
|
59
|
-
(child as any).parent = "";
|
|
60
|
-
|
|
61
|
-
expect(child.parent).toBe(null);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
15
|
test("getTokens", () => {
|
|
65
16
|
const firstName = new Literal("first-name", "John");
|
|
66
17
|
const lastName = new Literal("last-name", "Doe");
|
|
67
|
-
const fullName = new
|
|
18
|
+
const fullName = new And("full-name", [firstName, lastName]);
|
|
68
19
|
|
|
69
20
|
let tokens = firstName.getTokens();
|
|
70
21
|
expect(tokens.length).toBe(1);
|
|
@@ -82,7 +33,7 @@ describe("ValuePattern", () => {
|
|
|
82
33
|
test("getNextToken one deep.", () => {
|
|
83
34
|
const firstName = new Literal("first-name", "John");
|
|
84
35
|
const lastName = new Literal("last-name", "Doe");
|
|
85
|
-
const fullName = new
|
|
36
|
+
const fullName = new And("full-name", [firstName, lastName]);
|
|
86
37
|
|
|
87
38
|
const tokens = fullName.children[0].getNextTokens();
|
|
88
39
|
expect(tokens.length).toBe(1);
|
|
@@ -93,8 +44,8 @@ describe("ValuePattern", () => {
|
|
|
93
44
|
const firstName = new Literal("first-name", "John");
|
|
94
45
|
const lastName = new Literal("last-name", "Doe");
|
|
95
46
|
const otherLastName = new Literal("other-last-name", "Smith");
|
|
96
|
-
const lastNames = new
|
|
97
|
-
const fullName = new
|
|
47
|
+
const lastNames = new Or("last-names", [lastName, otherLastName]);
|
|
48
|
+
const fullName = new And("full-name", [firstName, lastNames]);
|
|
98
49
|
|
|
99
50
|
const tokens = fullName.children[0].getNextTokens();
|
|
100
51
|
expect(tokens.length).toBe(2);
|
|
@@ -107,17 +58,10 @@ describe("ValuePattern", () => {
|
|
|
107
58
|
const lastName = new Literal("last-name", "Doe");
|
|
108
59
|
const middleName = new Literal("middle-name", "Moses");
|
|
109
60
|
const otherMiddleName = new Literal("other-middle-name", "Joshua");
|
|
110
|
-
const middleNames = new
|
|
111
|
-
middleName,
|
|
112
|
-
otherMiddleName,
|
|
113
|
-
]);
|
|
61
|
+
const middleNames = new Or("middle-names", [middleName, otherMiddleName]);
|
|
114
62
|
const otherLastName = new Literal("other-last-name", "Smith");
|
|
115
|
-
const lastNames = new
|
|
116
|
-
const fullName = new
|
|
117
|
-
firstName,
|
|
118
|
-
middleNames,
|
|
119
|
-
lastNames,
|
|
120
|
-
]);
|
|
63
|
+
const lastNames = new Or("last-names", [lastName, otherLastName]);
|
|
64
|
+
const fullName = new And("full-name", [firstName, middleNames, lastNames]);
|
|
121
65
|
|
|
122
66
|
const tokens = fullName.children[1].children[1].getNextTokens();
|
|
123
67
|
expect(tokens.length).toBe(2);
|
|
@@ -130,17 +74,10 @@ describe("ValuePattern", () => {
|
|
|
130
74
|
const lastName = new Literal("last-name", "Doe");
|
|
131
75
|
const middleName = new Literal("middle-name", "Moses");
|
|
132
76
|
const otherMiddleName = new Literal("other-middle-name", "Joshua");
|
|
133
|
-
const middleNames = new
|
|
134
|
-
middleName,
|
|
135
|
-
otherMiddleName,
|
|
136
|
-
]);
|
|
77
|
+
const middleNames = new Or("middle-names", [middleName, otherMiddleName]);
|
|
137
78
|
const otherLastName = new Literal("other-last-name", "Smith");
|
|
138
|
-
const lastNames = new
|
|
139
|
-
const fullName = new
|
|
140
|
-
firstName,
|
|
141
|
-
middleNames,
|
|
142
|
-
lastNames,
|
|
143
|
-
]);
|
|
79
|
+
const lastNames = new Or("last-names", [lastName, otherLastName]);
|
|
80
|
+
const fullName = new And("full-name", [firstName, middleNames, lastNames]);
|
|
144
81
|
|
|
145
82
|
const tokens = fullName.children[2].children[1].getNextTokens();
|
|
146
83
|
expect(tokens.length).toBe(0);
|
|
@@ -151,14 +88,14 @@ describe("ValuePattern", () => {
|
|
|
151
88
|
const lastName = new Literal("last-name", "Doe");
|
|
152
89
|
const moses = new Literal("moses", "Moses");
|
|
153
90
|
const joshua = new Literal("other-middle-name", "Joshua");
|
|
154
|
-
const moreLastNames = new
|
|
91
|
+
const moreLastNames = new Or("more-last-names", [moses, joshua]);
|
|
155
92
|
const otherLastName = new Literal("other-last-name", "Smith");
|
|
156
|
-
const lastNames = new
|
|
93
|
+
const lastNames = new Or("last-names", [
|
|
157
94
|
moreLastNames,
|
|
158
95
|
lastName,
|
|
159
96
|
otherLastName,
|
|
160
97
|
]);
|
|
161
|
-
const fullName = new
|
|
98
|
+
const fullName = new And("full-name", [firstName, lastNames]);
|
|
162
99
|
|
|
163
100
|
const tokens = fullName.children[0].getNextTokens();
|
|
164
101
|
expect(tokens.length).toBe(4);
|
|
@@ -171,13 +108,9 @@ describe("ValuePattern", () => {
|
|
|
171
108
|
test("getNextTokens, with repeat.", () => {
|
|
172
109
|
const firstName = new Literal("first-name", "John");
|
|
173
110
|
const edward = new Literal("edward", "Edward");
|
|
174
|
-
const middleName = new
|
|
111
|
+
const middleName = new Repeat("middle-names", edward);
|
|
175
112
|
const lastName = new Literal("lastName", "Doe");
|
|
176
|
-
const fullName = new
|
|
177
|
-
firstName,
|
|
178
|
-
middleName,
|
|
179
|
-
lastName,
|
|
180
|
-
]);
|
|
113
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
181
114
|
|
|
182
115
|
let tokens = fullName.children[0].getNextTokens();
|
|
183
116
|
expect(tokens.length).toBe(1);
|
|
@@ -193,13 +126,9 @@ describe("ValuePattern", () => {
|
|
|
193
126
|
const firstName = new Literal("first-name", "John");
|
|
194
127
|
const edward = new Literal("edward", "Edward");
|
|
195
128
|
const stewart = new Literal("stewart", "Stewart");
|
|
196
|
-
const middleName = new
|
|
129
|
+
const middleName = new Repeat("middle-names", edward, stewart);
|
|
197
130
|
const lastName = new Literal("lastName", "Doe");
|
|
198
|
-
const fullName = new
|
|
199
|
-
firstName,
|
|
200
|
-
middleName,
|
|
201
|
-
lastName,
|
|
202
|
-
]);
|
|
131
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
203
132
|
|
|
204
133
|
let tokens = fullName.children[0].getNextTokens();
|
|
205
134
|
expect(tokens.length).toBe(1);
|
|
@@ -219,13 +148,9 @@ describe("ValuePattern", () => {
|
|
|
219
148
|
const firstName = new Literal("first-name", "John");
|
|
220
149
|
const edward = new Literal("edward", "Edward");
|
|
221
150
|
const stewart = new Literal("stewart", "Stewart");
|
|
222
|
-
const middleName = new
|
|
151
|
+
const middleName = new Repeat("middle-names", edward, stewart);
|
|
223
152
|
const lastName = new Literal("lastName", "Doe");
|
|
224
|
-
const fullName = new
|
|
225
|
-
firstName,
|
|
226
|
-
middleName,
|
|
227
|
-
lastName,
|
|
228
|
-
]);
|
|
153
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
229
154
|
|
|
230
155
|
let tokens = fullName.getTokens();
|
|
231
156
|
expect(tokens.length).toBe(1);
|
|
@@ -241,14 +166,10 @@ describe("ValuePattern", () => {
|
|
|
241
166
|
});
|
|
242
167
|
|
|
243
168
|
test("getNextTokens, and with optional start.", () => {
|
|
244
|
-
const firstName = new Literal("first-name", "John");
|
|
169
|
+
const firstName = new Literal("first-name", "John", true);
|
|
245
170
|
const middleName = new Literal("middle-name", "Edward");
|
|
246
171
|
const lastName = new Literal("last-name", "Doe");
|
|
247
|
-
const fullName = new
|
|
248
|
-
new OptionalValue(firstName),
|
|
249
|
-
middleName,
|
|
250
|
-
lastName,
|
|
251
|
-
]);
|
|
172
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
252
173
|
|
|
253
174
|
let tokens = fullName.getTokens();
|
|
254
175
|
expect(tokens.length).toBe(2);
|
|
@@ -258,13 +179,9 @@ describe("ValuePattern", () => {
|
|
|
258
179
|
|
|
259
180
|
test("getNextTokens, and with optional middle.", () => {
|
|
260
181
|
const firstName = new Literal("first-name", "John");
|
|
261
|
-
const middleName = new Literal("middle-name", "Edward");
|
|
182
|
+
const middleName = new Literal("middle-name", "Edward", true);
|
|
262
183
|
const lastName = new Literal("last-name", "Doe");
|
|
263
|
-
const fullName = new
|
|
264
|
-
firstName,
|
|
265
|
-
new OptionalValue(middleName),
|
|
266
|
-
lastName,
|
|
267
|
-
]);
|
|
184
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
268
185
|
|
|
269
186
|
let tokens = fullName.children[0].getNextTokens();
|
|
270
187
|
expect(tokens.length).toBe(2);
|
|
@@ -282,12 +199,8 @@ describe("ValuePattern", () => {
|
|
|
282
199
|
test("getNextTokens, and with optional last.", () => {
|
|
283
200
|
const firstName = new Literal("first-name", "John");
|
|
284
201
|
const middleName = new Literal("middle-name", "Edward");
|
|
285
|
-
const lastName = new Literal("last-name", "Doe");
|
|
286
|
-
const fullName = new
|
|
287
|
-
firstName,
|
|
288
|
-
middleName,
|
|
289
|
-
new OptionalValue(lastName),
|
|
290
|
-
]);
|
|
202
|
+
const lastName = new Literal("last-name", "Doe", true);
|
|
203
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
291
204
|
|
|
292
205
|
let tokens = fullName.children[0].getNextTokens();
|
|
293
206
|
expect(tokens.length).toBe(1);
|
|
@@ -302,14 +215,10 @@ describe("ValuePattern", () => {
|
|
|
302
215
|
});
|
|
303
216
|
|
|
304
217
|
test("getNextTokens, first two optional.", () => {
|
|
305
|
-
const firstName = new Literal("first-name", "John");
|
|
306
|
-
const middleName = new Literal("middle-name", "Edward");
|
|
218
|
+
const firstName = new Literal("first-name", "John", true);
|
|
219
|
+
const middleName = new Literal("middle-name", "Edward", true);
|
|
307
220
|
const lastName = new Literal("last-name", "Doe");
|
|
308
|
-
const fullName = new
|
|
309
|
-
new OptionalValue(firstName),
|
|
310
|
-
new OptionalValue(middleName),
|
|
311
|
-
lastName,
|
|
312
|
-
]);
|
|
221
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
313
222
|
|
|
314
223
|
let tokens = fullName.getTokens();
|
|
315
224
|
expect(tokens.length).toBe(3);
|
|
@@ -332,13 +241,9 @@ describe("ValuePattern", () => {
|
|
|
332
241
|
|
|
333
242
|
test("getNextTokens, last two optional.", () => {
|
|
334
243
|
const firstName = new Literal("first-name", "John");
|
|
335
|
-
const middleName = new Literal("middle-name", "Edward");
|
|
336
|
-
const lastName = new Literal("last-name", "Doe");
|
|
337
|
-
const fullName = new
|
|
338
|
-
firstName,
|
|
339
|
-
new OptionalValue(middleName),
|
|
340
|
-
new OptionalValue(lastName),
|
|
341
|
-
]);
|
|
244
|
+
const middleName = new Literal("middle-name", "Edward", true);
|
|
245
|
+
const lastName = new Literal("last-name", "Doe", true);
|
|
246
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
342
247
|
|
|
343
248
|
let tokens = fullName.getTokens();
|
|
344
249
|
expect(tokens.length).toBe(1);
|
|
@@ -358,15 +263,11 @@ describe("ValuePattern", () => {
|
|
|
358
263
|
});
|
|
359
264
|
|
|
360
265
|
test("getNextTokens, all three optional.", () => {
|
|
361
|
-
const firstName = new Literal("first-name", "John");
|
|
362
|
-
const middleName = new Literal("middle-name", "Edward");
|
|
363
|
-
const lastName = new Literal("last-name", "Doe");
|
|
266
|
+
const firstName = new Literal("first-name", "John", true);
|
|
267
|
+
const middleName = new Literal("middle-name", "Edward", true);
|
|
268
|
+
const lastName = new Literal("last-name", "Doe", true);
|
|
364
269
|
|
|
365
|
-
const fullName = new
|
|
366
|
-
new OptionalValue(firstName),
|
|
367
|
-
new OptionalValue(middleName),
|
|
368
|
-
new OptionalValue(lastName),
|
|
369
|
-
]);
|
|
270
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
370
271
|
|
|
371
272
|
let tokens = fullName.getTokens();
|
|
372
273
|
expect(tokens.length).toBe(3);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** @jest-environment node */
|
|
2
|
-
import {
|
|
2
|
+
import { Recursive, Cursor } from "../index";
|
|
3
3
|
import literals from "./javascriptPatterns/json";
|
|
4
4
|
|
|
5
5
|
describe("RecursivePattern", () => {
|
|
@@ -18,23 +18,25 @@ describe("RecursivePattern", () => {
|
|
|
18
18
|
const cursor = new Cursor(json);
|
|
19
19
|
const cursor2 = new Cursor(JSON.stringify([{ foo: "bar" }]));
|
|
20
20
|
|
|
21
|
-
const
|
|
22
|
-
const
|
|
21
|
+
const result1 = literals.parse(cursor);
|
|
22
|
+
const result2 = literals.parse(cursor2);
|
|
23
23
|
|
|
24
|
-
expect(
|
|
25
|
-
expect(
|
|
26
|
-
expect(
|
|
24
|
+
expect(result1?.name).toBe("literals");
|
|
25
|
+
expect(result1?.children[0].name).toBe("object-literal");
|
|
26
|
+
expect(result2?.name).toBe("literals");
|
|
27
|
+
expect(result2?.children[0].name).toBe("array-literal");
|
|
28
|
+
expect(result1?.toString()).toBe(json);
|
|
27
29
|
});
|
|
28
30
|
|
|
29
31
|
test("No pattern", () => {
|
|
30
|
-
const node = new
|
|
32
|
+
const node = new Recursive("nothing");
|
|
31
33
|
const result = node.exec("Some string.");
|
|
32
34
|
|
|
33
35
|
expect(result).toBe(null);
|
|
34
36
|
});
|
|
35
37
|
|
|
36
38
|
test("clone.", () => {
|
|
37
|
-
const node = new
|
|
39
|
+
const node = new Recursive("nothing");
|
|
38
40
|
const clone = node.clone();
|
|
39
41
|
|
|
40
42
|
expect(node.name).toBe(clone.name);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Cursor, Reference, And, Literal, Or } from "../index";
|
|
2
|
+
|
|
3
|
+
describe("ReferencePattern", () => {
|
|
4
|
+
test("Reference to node in different branch.", () => {
|
|
5
|
+
const a = new Literal("a", "a");
|
|
6
|
+
const b = new Literal("b", "b");
|
|
7
|
+
const a_b_c = new And("a-and-b-and-c", [a, b, new Reference("c")]);
|
|
8
|
+
const c = new Literal("c", "c");
|
|
9
|
+
const b_c = new And("b-and-c", [b, c]);
|
|
10
|
+
const main = new Or("main", [a_b_c, b_c]);
|
|
11
|
+
|
|
12
|
+
const result = main.parse(new Cursor("abc"));
|
|
13
|
+
|
|
14
|
+
expect(result).not.toBeNull();
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -1,43 +1,11 @@
|
|
|
1
1
|
/** @jest-environment node */
|
|
2
|
-
import
|
|
3
|
-
import Literal from "../patterns/value/Literal";
|
|
4
|
-
import OptionalValue from "../patterns/value/OptionalValue";
|
|
5
|
-
import Cursor from "../Cursor";
|
|
2
|
+
import {Repeat, Cursor, Literal} from "../index";
|
|
6
3
|
|
|
7
|
-
describe("
|
|
8
|
-
test("Empty Constructor.", () => {
|
|
9
|
-
expect(() => {
|
|
10
|
-
new (RepeatValue as any)();
|
|
11
|
-
}).toThrow();
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
test("Invalid name.", () => {
|
|
15
|
-
expect(() => {
|
|
16
|
-
new (RepeatValue as any)([], new Literal("blah", "Blah"));
|
|
17
|
-
}).toThrow();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("No patterns", () => {
|
|
21
|
-
expect(() => {
|
|
22
|
-
new (RepeatValue as any)("and-value");
|
|
23
|
-
}).toThrow();
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test("Empty patterns", () => {
|
|
27
|
-
expect(() => {
|
|
28
|
-
new (RepeatValue as any)("and-value", null);
|
|
29
|
-
}).toThrow();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("Invalid patterns", () => {
|
|
33
|
-
expect(() => {
|
|
34
|
-
new (RepeatValue as any)("and-value", {});
|
|
35
|
-
}).toThrow();
|
|
36
|
-
});
|
|
4
|
+
describe("Repeat", function () {
|
|
37
5
|
|
|
38
6
|
test("No Match", () => {
|
|
39
7
|
const john = new Literal("john", "John");
|
|
40
|
-
const johns = new
|
|
8
|
+
const johns = new Repeat("johns", john);
|
|
41
9
|
const cursor = new Cursor("JaneJane");
|
|
42
10
|
|
|
43
11
|
johns.parse(cursor);
|
|
@@ -46,7 +14,7 @@ describe("RepeatValue", function () {
|
|
|
46
14
|
|
|
47
15
|
test("Success, one John", () => {
|
|
48
16
|
const john = new Literal("john", "John");
|
|
49
|
-
const johns = new
|
|
17
|
+
const johns = new Repeat("johns", john);
|
|
50
18
|
const cursor = new Cursor("John");
|
|
51
19
|
const node = johns.parse(cursor);
|
|
52
20
|
|
|
@@ -58,7 +26,7 @@ describe("RepeatValue", function () {
|
|
|
58
26
|
|
|
59
27
|
test("Success with a terminating match.", () => {
|
|
60
28
|
const john = new Literal("john", "John");
|
|
61
|
-
const johns = new
|
|
29
|
+
const johns = new Repeat("johns", john);
|
|
62
30
|
const cursor = new Cursor("JohnJohnJane");
|
|
63
31
|
const node = johns.parse(cursor);
|
|
64
32
|
|
|
@@ -71,7 +39,7 @@ describe("RepeatValue", function () {
|
|
|
71
39
|
|
|
72
40
|
test("Bad cursor.", () => {
|
|
73
41
|
const john = new Literal("john", "John");
|
|
74
|
-
const johns = new
|
|
42
|
+
const johns = new Repeat("johns", john);
|
|
75
43
|
|
|
76
44
|
expect(() => {
|
|
77
45
|
(johns as any).parse("");
|
|
@@ -80,17 +48,17 @@ describe("RepeatValue", function () {
|
|
|
80
48
|
|
|
81
49
|
test("Clone.", () => {
|
|
82
50
|
const john = new Literal("john", "John");
|
|
83
|
-
const johns = new
|
|
51
|
+
const johns = new Repeat("johns", john);
|
|
84
52
|
const clone = johns.clone();
|
|
85
53
|
|
|
86
54
|
expect(johns.name).toBe(clone.name);
|
|
87
55
|
});
|
|
88
56
|
|
|
89
57
|
test("Try Optional.", () => {
|
|
90
|
-
const john = new Literal("john", "John");
|
|
58
|
+
const john = new Literal("john", "John", true);
|
|
91
59
|
|
|
92
60
|
expect(() => {
|
|
93
|
-
new
|
|
61
|
+
new Repeat("johns", john);
|
|
94
62
|
});
|
|
95
63
|
});
|
|
96
64
|
|
|
@@ -99,7 +67,7 @@ describe("RepeatValue", function () {
|
|
|
99
67
|
const john = new Literal("john", "John");
|
|
100
68
|
const divider = new Literal("divider", ",");
|
|
101
69
|
|
|
102
|
-
const node = new
|
|
70
|
+
const node = new Repeat("johns", john, divider).parse(cursor);
|
|
103
71
|
|
|
104
72
|
expect(node?.name).toBe("johns");
|
|
105
73
|
expect(node?.value).toBe("John,John");
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
/** @jest-environment node */
|
|
2
2
|
import TextSuggester from "../TextSuggester";
|
|
3
3
|
import sentence from "./patterns/sentence";
|
|
4
|
-
import Literal from "../patterns/
|
|
5
|
-
import
|
|
6
|
-
import
|
|
4
|
+
import Literal from "../patterns/Literal";
|
|
5
|
+
import And from "../patterns/And";
|
|
6
|
+
import Or from "../patterns/Or";
|
|
7
7
|
import json from "./javascriptPatterns/json";
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import AndComposite from "../patterns/composite/AndComposite";
|
|
11
|
-
import RecursivePattern from "../patterns/RecursivePattern";
|
|
8
|
+
import Repeat from "../patterns/Repeat";
|
|
9
|
+
import Recursive from "../patterns/Recursive";
|
|
12
10
|
|
|
13
11
|
function generateFlagFromList(flagNames: string[]) {
|
|
14
12
|
return flagNames.map((flagName) => {
|
|
@@ -22,18 +20,18 @@ function generateExpression(flagNames: string[]) {
|
|
|
22
20
|
const andLiteral = new Literal("and-literal", "AND");
|
|
23
21
|
const space = new Literal("space", " ");
|
|
24
22
|
const orLiteral = new Literal("or-literal", "OR");
|
|
25
|
-
const and = new
|
|
26
|
-
const or = new
|
|
23
|
+
const and = new And("and", [space, andLiteral, space]);
|
|
24
|
+
const or = new And("or", [space, orLiteral, space]);
|
|
27
25
|
|
|
28
|
-
const booleanOperator = new
|
|
29
|
-
const flag = new
|
|
30
|
-
const group = new
|
|
26
|
+
const booleanOperator = new Or("booleanOperator", [and, or]);
|
|
27
|
+
const flag = new Or("flags", generateFlagFromList(flagNames));
|
|
28
|
+
const group = new And("group", [
|
|
31
29
|
openParen,
|
|
32
|
-
new
|
|
30
|
+
new Recursive("flag-expression"),
|
|
33
31
|
closeParen,
|
|
34
32
|
]);
|
|
35
|
-
const flagOrGroup = new
|
|
36
|
-
const flagExpression = new
|
|
33
|
+
const flagOrGroup = new Or("flag-or-group", [flag, group]);
|
|
34
|
+
const flagExpression = new Repeat(
|
|
37
35
|
"flag-expression",
|
|
38
36
|
flagOrGroup,
|
|
39
37
|
booleanOperator
|
|
@@ -178,15 +176,9 @@ describe("TextInspector", () => {
|
|
|
178
176
|
const theMoney = new Literal("the-money", "the money");
|
|
179
177
|
const yourLicense = new Literal("your-license", "your license");
|
|
180
178
|
const space = new Literal("space", " ");
|
|
181
|
-
const first = new
|
|
182
|
-
const second = new
|
|
183
|
-
|
|
184
|
-
space,
|
|
185
|
-
me,
|
|
186
|
-
space,
|
|
187
|
-
yourLicense,
|
|
188
|
-
]);
|
|
189
|
-
const either = new OrValue("either", [first, second]);
|
|
179
|
+
const first = new And("first", [show, space, me, space, theMoney]);
|
|
180
|
+
const second = new And("second", [show, space, me, space, yourLicense]);
|
|
181
|
+
const either = new Or("either", [first, second]);
|
|
190
182
|
|
|
191
183
|
const inspection = TextSuggester.suggest("Show me ", either);
|
|
192
184
|
|
|
@@ -255,9 +247,9 @@ describe("TextInspector", () => {
|
|
|
255
247
|
const a = new Literal("a", "A");
|
|
256
248
|
const b = new Literal("b", "B");
|
|
257
249
|
const space = new Literal("space", " ");
|
|
258
|
-
const or = new
|
|
250
|
+
const or = new Or("names", [a, b]);
|
|
259
251
|
|
|
260
|
-
const repeat = new
|
|
252
|
+
const repeat = new Repeat("repeat", or, space);
|
|
261
253
|
|
|
262
254
|
const result = TextSuggester.suggest("A B ", repeat);
|
|
263
255
|
|
|
@@ -271,9 +263,9 @@ describe("TextInspector", () => {
|
|
|
271
263
|
const a = new Literal("a", "A");
|
|
272
264
|
const b = new Literal("b", "B");
|
|
273
265
|
const space = new Literal("space", " ");
|
|
274
|
-
const or = new
|
|
266
|
+
const or = new Or("names", [a, b]);
|
|
275
267
|
|
|
276
|
-
const repeat = new
|
|
268
|
+
const repeat = new Repeat("repeat", or, space);
|
|
277
269
|
|
|
278
270
|
const result = TextSuggester.suggest("A B", repeat);
|
|
279
271
|
|
|
@@ -292,4 +284,14 @@ describe("TextInspector", () => {
|
|
|
292
284
|
expect(result.options.values[2]).toBe("FlagZ");
|
|
293
285
|
expect(result.options.values[3]).toBe("(");
|
|
294
286
|
});
|
|
287
|
+
|
|
288
|
+
test("Do not match on first partial token.", () => {
|
|
289
|
+
const expression = generateExpression(["FlagX", "FlagY", "FlagZ"]);
|
|
290
|
+
const result = TextSuggester.suggest("l", expression);
|
|
291
|
+
|
|
292
|
+
expect(result.options).toBe(null);
|
|
293
|
+
expect(result.error?.startIndex).toBe(0);
|
|
294
|
+
expect(result.error?.endIndex).toBe(0);
|
|
295
|
+
expect(result.error?.text).toBe("l");
|
|
296
|
+
});
|
|
295
297
|
});
|