clarity-pattern-parser 3.0.15 → 4.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/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/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/LookAhead.ts +32 -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/Reference.ts +127 -0
- 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 +44 -0
- package/src/tests/Not.test.ts +51 -0
- package/src/tests/Or.test.ts +113 -0
- package/src/tests/Pattern.test.ts +41 -141
- 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 +20 -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 -24
- 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 -153
- 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 -69
- 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/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);
|
|
@@ -211,22 +140,17 @@ describe("ValuePattern", () => {
|
|
|
211
140
|
expect(tokens[1]).toBe("Doe");
|
|
212
141
|
|
|
213
142
|
tokens = fullName.children[1].children[1].getNextTokens();
|
|
214
|
-
expect(tokens.length).toBe(
|
|
143
|
+
expect(tokens.length).toBe(1);
|
|
215
144
|
expect(tokens[0]).toBe("Edward");
|
|
216
|
-
expect(tokens[1]).toBe("Doe");
|
|
217
145
|
});
|
|
218
146
|
|
|
219
147
|
test("getNextTokens, has child and at the beginning.", () => {
|
|
220
148
|
const firstName = new Literal("first-name", "John");
|
|
221
149
|
const edward = new Literal("edward", "Edward");
|
|
222
150
|
const stewart = new Literal("stewart", "Stewart");
|
|
223
|
-
const middleName = new
|
|
151
|
+
const middleName = new Repeat("middle-names", edward, stewart);
|
|
224
152
|
const lastName = new Literal("lastName", "Doe");
|
|
225
|
-
const fullName = new
|
|
226
|
-
firstName,
|
|
227
|
-
middleName,
|
|
228
|
-
lastName,
|
|
229
|
-
]);
|
|
153
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
230
154
|
|
|
231
155
|
let tokens = fullName.getTokens();
|
|
232
156
|
expect(tokens.length).toBe(1);
|
|
@@ -242,14 +166,10 @@ describe("ValuePattern", () => {
|
|
|
242
166
|
});
|
|
243
167
|
|
|
244
168
|
test("getNextTokens, and with optional start.", () => {
|
|
245
|
-
const firstName = new Literal("first-name", "John");
|
|
169
|
+
const firstName = new Literal("first-name", "John", true);
|
|
246
170
|
const middleName = new Literal("middle-name", "Edward");
|
|
247
171
|
const lastName = new Literal("last-name", "Doe");
|
|
248
|
-
const fullName = new
|
|
249
|
-
new OptionalValue(firstName),
|
|
250
|
-
middleName,
|
|
251
|
-
lastName,
|
|
252
|
-
]);
|
|
172
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
253
173
|
|
|
254
174
|
let tokens = fullName.getTokens();
|
|
255
175
|
expect(tokens.length).toBe(2);
|
|
@@ -259,13 +179,9 @@ describe("ValuePattern", () => {
|
|
|
259
179
|
|
|
260
180
|
test("getNextTokens, and with optional middle.", () => {
|
|
261
181
|
const firstName = new Literal("first-name", "John");
|
|
262
|
-
const middleName = new Literal("middle-name", "Edward");
|
|
182
|
+
const middleName = new Literal("middle-name", "Edward", true);
|
|
263
183
|
const lastName = new Literal("last-name", "Doe");
|
|
264
|
-
const fullName = new
|
|
265
|
-
firstName,
|
|
266
|
-
new OptionalValue(middleName),
|
|
267
|
-
lastName,
|
|
268
|
-
]);
|
|
184
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
269
185
|
|
|
270
186
|
let tokens = fullName.children[0].getNextTokens();
|
|
271
187
|
expect(tokens.length).toBe(2);
|
|
@@ -283,12 +199,8 @@ describe("ValuePattern", () => {
|
|
|
283
199
|
test("getNextTokens, and with optional last.", () => {
|
|
284
200
|
const firstName = new Literal("first-name", "John");
|
|
285
201
|
const middleName = new Literal("middle-name", "Edward");
|
|
286
|
-
const lastName = new Literal("last-name", "Doe");
|
|
287
|
-
const fullName = new
|
|
288
|
-
firstName,
|
|
289
|
-
middleName,
|
|
290
|
-
new OptionalValue(lastName),
|
|
291
|
-
]);
|
|
202
|
+
const lastName = new Literal("last-name", "Doe", true);
|
|
203
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
292
204
|
|
|
293
205
|
let tokens = fullName.children[0].getNextTokens();
|
|
294
206
|
expect(tokens.length).toBe(1);
|
|
@@ -303,14 +215,10 @@ describe("ValuePattern", () => {
|
|
|
303
215
|
});
|
|
304
216
|
|
|
305
217
|
test("getNextTokens, first two optional.", () => {
|
|
306
|
-
const firstName = new Literal("first-name", "John");
|
|
307
|
-
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);
|
|
308
220
|
const lastName = new Literal("last-name", "Doe");
|
|
309
|
-
const fullName = new
|
|
310
|
-
new OptionalValue(firstName),
|
|
311
|
-
new OptionalValue(middleName),
|
|
312
|
-
lastName,
|
|
313
|
-
]);
|
|
221
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
314
222
|
|
|
315
223
|
let tokens = fullName.getTokens();
|
|
316
224
|
expect(tokens.length).toBe(3);
|
|
@@ -333,13 +241,9 @@ describe("ValuePattern", () => {
|
|
|
333
241
|
|
|
334
242
|
test("getNextTokens, last two optional.", () => {
|
|
335
243
|
const firstName = new Literal("first-name", "John");
|
|
336
|
-
const middleName = new Literal("middle-name", "Edward");
|
|
337
|
-
const lastName = new Literal("last-name", "Doe");
|
|
338
|
-
const fullName = new
|
|
339
|
-
firstName,
|
|
340
|
-
new OptionalValue(middleName),
|
|
341
|
-
new OptionalValue(lastName),
|
|
342
|
-
]);
|
|
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]);
|
|
343
247
|
|
|
344
248
|
let tokens = fullName.getTokens();
|
|
345
249
|
expect(tokens.length).toBe(1);
|
|
@@ -359,15 +263,11 @@ describe("ValuePattern", () => {
|
|
|
359
263
|
});
|
|
360
264
|
|
|
361
265
|
test("getNextTokens, all three optional.", () => {
|
|
362
|
-
const firstName = new Literal("first-name", "John");
|
|
363
|
-
const middleName = new Literal("middle-name", "Edward");
|
|
364
|
-
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);
|
|
365
269
|
|
|
366
|
-
const fullName = new
|
|
367
|
-
new OptionalValue(firstName),
|
|
368
|
-
new OptionalValue(middleName),
|
|
369
|
-
new OptionalValue(lastName),
|
|
370
|
-
]);
|
|
270
|
+
const fullName = new And("full-name", [firstName, middleName, lastName]);
|
|
371
271
|
|
|
372
272
|
let tokens = fullName.getTokens();
|
|
373
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
|
|