clarity-pattern-parser 3.0.16 → 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.
Files changed (168) hide show
  1. package/README.md +0 -191
  2. package/dist/ast/Node.d.ts +4 -5
  3. package/dist/index.browser.js +476 -731
  4. package/dist/index.browser.js.map +1 -1
  5. package/dist/index.d.ts +10 -18
  6. package/dist/index.esm.js +469 -716
  7. package/dist/index.esm.js.map +1 -1
  8. package/dist/index.js +476 -731
  9. package/dist/index.js.map +1 -1
  10. package/dist/patterns/And.d.ts +24 -0
  11. package/dist/patterns/Literal.d.ts +19 -0
  12. package/dist/patterns/LookAhead.d.ts +8 -0
  13. package/dist/patterns/Not.d.ts +11 -0
  14. package/dist/patterns/Or.d.ts +22 -0
  15. package/dist/patterns/Pattern.d.ts +6 -7
  16. package/dist/patterns/{RecursivePattern.d.ts → Recursive.d.ts} +4 -4
  17. package/dist/patterns/Reference.d.ts +14 -0
  18. package/dist/patterns/Regex.d.ts +21 -0
  19. package/dist/patterns/Repeat.d.ts +20 -0
  20. package/package.json +1 -1
  21. package/src/CursorHistory.ts +1 -1
  22. package/src/ast/Node.ts +20 -17
  23. package/src/ast/Visitor.ts +14 -18
  24. package/src/index.ts +17 -33
  25. package/src/patterns/And.ts +178 -0
  26. package/src/patterns/Literal.ts +91 -0
  27. package/src/patterns/Not.ts +50 -0
  28. package/src/patterns/Or.ts +132 -0
  29. package/src/patterns/Pattern.ts +31 -47
  30. package/src/patterns/Recursive.ts +92 -0
  31. package/src/patterns/{ReferencePattern.ts → Reference.ts} +32 -28
  32. package/src/patterns/Regex.ts +123 -0
  33. package/src/patterns/Repeat.ts +155 -0
  34. package/src/tests/{AndValue.test.ts → And.test.ts} +31 -41
  35. package/src/tests/CursorHistory.test.ts +6 -6
  36. package/src/tests/Cusor.test.ts +7 -10
  37. package/src/tests/Literal.test.ts +3 -5
  38. package/src/tests/LookAhead.test.ts +2 -51
  39. package/src/tests/Not.test.ts +51 -0
  40. package/src/tests/Or.test.ts +113 -0
  41. package/src/tests/Pattern.test.ts +40 -139
  42. package/src/tests/{RecursivePattern.test.ts → Recursive.test.ts} +10 -8
  43. package/src/tests/Reference.test.ts +16 -0
  44. package/src/tests/{RepeatValue.test.ts → Repeat.test.ts} +10 -42
  45. package/src/tests/TextSuggester.test.ts +20 -28
  46. package/src/tests/{NodeVisitor.test.ts → Visitor.test.ts} +42 -21
  47. package/src/tests/cssPatterns/cssValue.ts +2 -2
  48. package/src/tests/cssPatterns/divider.ts +2 -2
  49. package/src/tests/cssPatterns/hex.ts +2 -2
  50. package/src/tests/cssPatterns/method.ts +7 -9
  51. package/src/tests/cssPatterns/name.ts +2 -2
  52. package/src/tests/cssPatterns/number.ts +2 -2
  53. package/src/tests/cssPatterns/optionalSpaces.ts +1 -2
  54. package/src/tests/cssPatterns/spaces.ts +2 -2
  55. package/src/tests/cssPatterns/unit.ts +3 -3
  56. package/src/tests/cssPatterns/value.ts +2 -2
  57. package/src/tests/cssPatterns/values.ts +2 -2
  58. package/src/tests/htmlPatterns/element.ts +18 -33
  59. package/src/tests/javascriptPatterns/boolean.ts +2 -3
  60. package/src/tests/javascriptPatterns/json.ts +14 -26
  61. package/src/tests/javascriptPatterns/name.ts +3 -20
  62. package/src/tests/javascriptPatterns/number.ts +2 -2
  63. package/src/tests/javascriptPatterns/objectLiteral.ts +9 -16
  64. package/src/tests/javascriptPatterns/string.ts +26 -24
  65. package/src/tests/javascriptPatterns/unit.ts +3 -6
  66. package/src/tests/javascriptPatterns/whitespace.ts +8 -12
  67. package/src/tests/naturalLanguage/filter.ts +16 -33
  68. package/src/tests/patterns/sentence.ts +8 -8
  69. package/dist/Cursor.js +0 -105
  70. package/dist/Cursor.js.map +0 -1
  71. package/dist/CursorHistory.js +0 -104
  72. package/dist/CursorHistory.js.map +0 -1
  73. package/dist/Permutor.d.ts +0 -13
  74. package/dist/Permutor.js +0 -52
  75. package/dist/Permutor.js.map +0 -1
  76. package/dist/TextSuggester.js +0 -244
  77. package/dist/TextSuggester.js.map +0 -1
  78. package/dist/ast/CompositeNode.d.ts +0 -6
  79. package/dist/ast/CompositeNode.js +0 -17
  80. package/dist/ast/CompositeNode.js.map +0 -1
  81. package/dist/ast/Node.js +0 -16
  82. package/dist/ast/Node.js.map +0 -1
  83. package/dist/ast/NodeVisitor.d.ts +0 -31
  84. package/dist/ast/ValueNode.d.ts +0 -6
  85. package/dist/ast/ValueNode.js +0 -14
  86. package/dist/ast/ValueNode.js.map +0 -1
  87. package/dist/ast/Visitor.js +0 -209
  88. package/dist/ast/Visitor.js.map +0 -1
  89. package/dist/patterns/ParseError.js +0 -9
  90. package/dist/patterns/ParseError.js.map +0 -1
  91. package/dist/patterns/Pattern.js +0 -127
  92. package/dist/patterns/Pattern.js.map +0 -1
  93. package/dist/patterns/RecursivePattern.js +0 -65
  94. package/dist/patterns/RecursivePattern.js.map +0 -1
  95. package/dist/patterns/composite/AndComposite.d.ts +0 -22
  96. package/dist/patterns/composite/AndComposite.js +0 -117
  97. package/dist/patterns/composite/AndComposite.js.map +0 -1
  98. package/dist/patterns/composite/CompositePattern.d.ts +0 -4
  99. package/dist/patterns/composite/CompositePattern.js +0 -7
  100. package/dist/patterns/composite/CompositePattern.js.map +0 -1
  101. package/dist/patterns/composite/OptionalComposite.d.ts +0 -10
  102. package/dist/patterns/composite/OptionalComposite.js +0 -29
  103. package/dist/patterns/composite/OptionalComposite.js.map +0 -1
  104. package/dist/patterns/composite/OrComposite.d.ts +0 -16
  105. package/dist/patterns/composite/OrComposite.js +0 -69
  106. package/dist/patterns/composite/OrComposite.js.map +0 -1
  107. package/dist/patterns/composite/RepeatComposite.d.ts +0 -21
  108. package/dist/patterns/composite/RepeatComposite.js +0 -88
  109. package/dist/patterns/composite/RepeatComposite.js.map +0 -1
  110. package/dist/patterns/value/AndValue.d.ts +0 -21
  111. package/dist/patterns/value/AndValue.js +0 -118
  112. package/dist/patterns/value/AndValue.js.map +0 -1
  113. package/dist/patterns/value/AnyOfThese.d.ts +0 -18
  114. package/dist/patterns/value/AnyOfThese.js +0 -59
  115. package/dist/patterns/value/AnyOfThese.js.map +0 -1
  116. package/dist/patterns/value/Literal.d.ts +0 -20
  117. package/dist/patterns/value/Literal.js +0 -63
  118. package/dist/patterns/value/Literal.js.map +0 -1
  119. package/dist/patterns/value/NotValue.d.ts +0 -17
  120. package/dist/patterns/value/NotValue.js +0 -70
  121. package/dist/patterns/value/NotValue.js.map +0 -1
  122. package/dist/patterns/value/OptionalValue.d.ts +0 -9
  123. package/dist/patterns/value/OptionalValue.js +0 -32
  124. package/dist/patterns/value/OptionalValue.js.map +0 -1
  125. package/dist/patterns/value/OrValue.d.ts +0 -19
  126. package/dist/patterns/value/OrValue.js +0 -73
  127. package/dist/patterns/value/OrValue.js.map +0 -1
  128. package/dist/patterns/value/RegexValue.d.ts +0 -19
  129. package/dist/patterns/value/RegexValue.js +0 -69
  130. package/dist/patterns/value/RegexValue.js.map +0 -1
  131. package/dist/patterns/value/RepeatValue.d.ts +0 -19
  132. package/dist/patterns/value/RepeatValue.js +0 -89
  133. package/dist/patterns/value/RepeatValue.js.map +0 -1
  134. package/dist/patterns/value/ValuePattern.d.ts +0 -5
  135. package/dist/patterns/value/ValuePattern.js +0 -7
  136. package/dist/patterns/value/ValuePattern.js.map +0 -1
  137. package/src/Permutor.ts +0 -64
  138. package/src/ast/CompositeNode.ts +0 -26
  139. package/src/ast/ValueNode.ts +0 -28
  140. package/src/patterns/RecursivePattern.ts +0 -86
  141. package/src/patterns/composite/AndComposite.ts +0 -159
  142. package/src/patterns/composite/CompositePattern.ts +0 -7
  143. package/src/patterns/composite/OptionalComposite.ts +0 -37
  144. package/src/patterns/composite/OrComposite.ts +0 -96
  145. package/src/patterns/composite/RepeatComposite.ts +0 -130
  146. package/src/patterns/value/AndValue.ts +0 -152
  147. package/src/patterns/value/AnyOfThese.ts +0 -81
  148. package/src/patterns/value/Literal.ts +0 -92
  149. package/src/patterns/value/NotValue.ts +0 -95
  150. package/src/patterns/value/OptionalValue.ts +0 -39
  151. package/src/patterns/value/OrValue.ts +0 -103
  152. package/src/patterns/value/RegexValue.ts +0 -103
  153. package/src/patterns/value/RepeatValue.ts +0 -131
  154. package/src/patterns/value/ValuePattern.ts +0 -8
  155. package/src/tests/AndComposite.test.ts +0 -102
  156. package/src/tests/AnyOfThese.test.ts +0 -74
  157. package/src/tests/CompositeNode.test.ts +0 -33
  158. package/src/tests/NotValue.test.ts +0 -76
  159. package/src/tests/OptionalValue.test.ts +0 -50
  160. package/src/tests/OrComposite.test.ts +0 -75
  161. package/src/tests/OrValue.test.ts +0 -171
  162. package/src/tests/Permutor.test.ts +0 -30
  163. package/src/tests/ReferencePattern.test.ts +0 -24
  164. package/src/tests/RegexValue.test.ts +0 -22
  165. package/src/tests/RepeatComposite.test.ts +0 -58
  166. package/src/tests/ValueNode.test.ts +0 -24
  167. package/src/tests/javascriptPatterns/varStatement.ts +0 -0
  168. package/src/tests/readmeDemo.test.ts +0 -124
@@ -1,52 +1,10 @@
1
1
  /** @jest-environment node */
2
- import ValuePattern from "../patterns/value/ValuePattern";
3
- import AndValue from "../patterns/value/AndValue";
4
- import OrValue from "../patterns/value/OrValue";
5
- import Literal from "../patterns/value/Literal";
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 AndValue("full-name", [firstName, lastName]);
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 AndValue("full-name", [firstName, lastName]);
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 OrValue("last-names", [lastName, otherLastName]);
97
- const fullName = new AndValue("full-name", [firstName, lastNames]);
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 OrValue("middle-names", [
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 OrValue("last-names", [lastName, otherLastName]);
116
- const fullName = new AndValue("full-name", [
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 OrValue("middle-names", [
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 OrValue("last-names", [lastName, otherLastName]);
139
- const fullName = new AndValue("full-name", [
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 OrValue("more-last-names", [moses, joshua]);
91
+ const moreLastNames = new Or("more-last-names", [moses, joshua]);
155
92
  const otherLastName = new Literal("other-last-name", "Smith");
156
- const lastNames = new OrValue("last-names", [
93
+ const lastNames = new Or("last-names", [
157
94
  moreLastNames,
158
95
  lastName,
159
96
  otherLastName,
160
97
  ]);
161
- const fullName = new AndValue("full-name", [firstName, lastNames]);
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 RepeatValue("middle-names", edward);
111
+ const middleName = new Repeat("middle-names", edward);
175
112
  const lastName = new Literal("lastName", "Doe");
176
- const fullName = new AndValue("full-name", [
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 RepeatValue("middle-names", edward, stewart);
129
+ const middleName = new Repeat("middle-names", edward, stewart);
197
130
  const lastName = new Literal("lastName", "Doe");
198
- const fullName = new AndValue("full-name", [
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 RepeatValue("middle-names", edward, stewart);
151
+ const middleName = new Repeat("middle-names", edward, stewart);
223
152
  const lastName = new Literal("lastName", "Doe");
224
- const fullName = new AndValue("full-name", [
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 AndValue("full-name", [
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 AndValue("full-name", [
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 AndValue("full-name", [
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 AndValue("full-name", [
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 AndValue("full-name", [
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 AndValue("full-name", [
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 { RecursivePattern, Cursor } from "../index";
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 object = literals.parse(cursor);
22
- const array = literals.parse(cursor2);
21
+ const result1 = literals.parse(cursor);
22
+ const result2 = literals.parse(cursor2);
23
23
 
24
- expect(object.name).toBe("object-literal");
25
- expect(array.name).toBe("array-literal");
26
- expect(object.toString()).toBe(json);
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 RecursivePattern("nothing");
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 RecursivePattern("nothing");
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 RepeatValue from "../patterns/value/RepeatValue";
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("RepeatValue", function () {
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 RepeatValue("johns", john);
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 RepeatValue("johns", john);
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 RepeatValue("johns", john);
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 RepeatValue("johns", john);
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 RepeatValue("johns", john);
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 RepeatValue("johns", new OptionalValue(john));
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 RepeatValue("johns", john, divider).parse(cursor);
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/value/Literal";
5
- import AndValue from "../patterns/value/AndValue";
6
- import OrValue from "../patterns/value/OrValue";
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 OrComposite from "../patterns/composite/OrComposite";
9
- import RepeatComposite from "../patterns/composite/RepeatComposite";
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 AndValue("and", [space, andLiteral, space]);
26
- const or = new AndValue("or", [space, orLiteral, space]);
23
+ const and = new And("and", [space, andLiteral, space]);
24
+ const or = new And("or", [space, orLiteral, space]);
27
25
 
28
- const booleanOperator = new OrComposite("booleanOperator", [and, or]);
29
- const flag = new OrComposite("flags", generateFlagFromList(flagNames));
30
- const group = new AndComposite("group", [
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 RecursivePattern("flag-expression"),
30
+ new Recursive("flag-expression"),
33
31
  closeParen,
34
32
  ]);
35
- const flagOrGroup = new OrComposite("flag-or-group", [flag, group]);
36
- const flagExpression = new RepeatComposite(
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 AndValue("first", [show, space, me, space, theMoney]);
182
- const second = new AndValue("second", [
183
- show,
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 OrComposite("names", [a, b]);
250
+ const or = new Or("names", [a, b]);
259
251
 
260
- const repeat = new RepeatComposite("repeat", or, space);
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 OrComposite("names", [a, b]);
266
+ const or = new Or("names", [a, b]);
275
267
 
276
- const repeat = new RepeatComposite("repeat", or, space);
268
+ const repeat = new Repeat("repeat", or, space);
277
269
 
278
270
  const result = TextSuggester.suggest("A B", repeat);
279
271
 
@@ -1,16 +1,14 @@
1
1
  /** @jest-environment node */
2
- import CompositeNode from "../ast/CompositeNode";
3
- import Visitor from "../ast/Visitor";
4
- import ValueNode from "../ast/ValueNode";
2
+ import { Visitor, Node } from "../index";
5
3
  import cssValue from "./cssPatterns/cssValue";
6
4
 
7
5
  function createContext(amount: number = 10) {
8
- const grandParent = new CompositeNode("grand-parent", "grand-parent");
9
- const parent = new CompositeNode("parent", "parent");
10
- const children: ValueNode[] = [];
6
+ const grandParent = new Node("grand-parent", "grand-parent", 0, 0);
7
+ const parent = new Node("parent", "parent", 0, 0);
8
+ const children: Node[] = [];
11
9
 
12
10
  for (let x = 0; x < amount; x++) {
13
- const child = new ValueNode("child", "child", x.toString());
11
+ const child = new Node("child", "child", 0, 0, [], x.toString());
14
12
  children.push(child);
15
13
  }
16
14
 
@@ -35,7 +33,9 @@ describe("Visitor", () => {
35
33
  });
36
34
 
37
35
  test("remove", () => {
38
- const node = cssValue.exec("linear-gradient(to left, #333, #333 50%, #eee 75%, #333 75%), linear-gradient(to bottom, #555, #555 50%, #eee 75%, #555 75%)");
36
+ const node = cssValue.exec(
37
+ "linear-gradient(to left, #333, #333 50%, #eee 75%, #333 75%), linear-gradient(to bottom, #555, #555 50%, #eee 75%, #555 75%)"
38
+ );
39
39
 
40
40
  if (node != null) {
41
41
  const visitor = new Visitor(node);
@@ -59,15 +59,19 @@ describe("Visitor", () => {
59
59
  const stringResult = visitor
60
60
  .select((n) => n.name === "child")
61
61
  .wrap((node) => {
62
- const wrapper = new CompositeNode("wrapper", "wrapper");
62
+ const wrapper = new Node("wrapper", "wrapper", 0, 0);
63
63
  wrapper.children.push(node);
64
64
  return wrapper;
65
65
  });
66
66
 
67
- let wrappers = Visitor.select(node, (n) => n.type === "wrapper")
68
- .selectedNodes;
69
- let children = Visitor.select(node, (n) => n.type === "child")
70
- .selectedNodes;
67
+ let wrappers = Visitor.select(
68
+ node,
69
+ (n) => n.type === "wrapper"
70
+ ).selectedNodes;
71
+ let children = Visitor.select(
72
+ node,
73
+ (n) => n.type === "child"
74
+ ).selectedNodes;
71
75
 
72
76
  expect(wrappers.length).toBe(10);
73
77
  expect(children.length).toBe(10);
@@ -90,13 +94,21 @@ describe("Visitor", () => {
90
94
  visitor
91
95
  .select((n) => n.type === "child")
92
96
  .prepend((n) => {
93
- const sibling = new ValueNode("sibling", "sibling", n.value || "");
97
+ const sibling = new Node(
98
+ "sibling",
99
+ "sibling",
100
+ 0,
101
+ 0,
102
+ [],
103
+ n.value || ""
104
+ );
94
105
 
95
106
  return sibling;
96
107
  });
97
108
 
98
- const siblings = visitor.select((n) => n.type === "sibling")
99
- .selectedNodes;
109
+ const siblings = visitor.select(
110
+ (n) => n.type === "sibling"
111
+ ).selectedNodes;
100
112
  const parent = visitor.select((n) => n.type === "parent")
101
113
  .selectedNodes[0];
102
114
 
@@ -174,13 +186,21 @@ describe("Visitor", () => {
174
186
  visitor
175
187
  .select((n) => n.type === "child")
176
188
  .append((n) => {
177
- const sibling = new ValueNode("sibling", "sibling", n.value || "");
189
+ const sibling = new Node(
190
+ "sibling",
191
+ "sibling",
192
+ 0,
193
+ 0,
194
+ [],
195
+ n.value || ""
196
+ );
178
197
 
179
198
  return sibling;
180
199
  });
181
200
 
182
- const siblings = visitor.select((n) => n.type === "sibling")
183
- .selectedNodes;
201
+ const siblings = visitor.select(
202
+ (n) => n.type === "sibling"
203
+ ).selectedNodes;
184
204
  const parent = visitor.select((n) => n.type === "parent")
185
205
  .selectedNodes[0];
186
206
 
@@ -288,8 +308,9 @@ describe("Visitor", () => {
288
308
 
289
309
  if (node != null) {
290
310
  const visitor = new Visitor(node);
291
- const selected = visitor.selectNode(node).deselectNode(node)
292
- .selectedNodes;
311
+ const selected = visitor
312
+ .selectNode(node)
313
+ .deselectNode(node).selectedNodes;
293
314
 
294
315
  expect(selected.length).toBe(0);
295
316
  }