clarity-pattern-parser 4.0.3 → 6.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 (133) hide show
  1. package/README.md +466 -1
  2. package/TODO.md +76 -2
  3. package/dist/ast/Node.d.ts +49 -11
  4. package/dist/ast/Visitor.d.ts +31 -31
  5. package/dist/index.browser.js +1513 -1495
  6. package/dist/index.browser.js.map +1 -1
  7. package/dist/index.d.ts +17 -17
  8. package/dist/index.esm.js +1480 -1459
  9. package/dist/index.esm.js.map +1 -1
  10. package/dist/index.js +1481 -1463
  11. package/dist/index.js.map +1 -1
  12. package/dist/intellisense/AutoComplete.d.ts +28 -0
  13. package/dist/intellisense/Suggestion.d.ts +11 -0
  14. package/dist/intellisense/SuggestionOption.d.ts +4 -0
  15. package/dist/patterns/And.d.ts +37 -24
  16. package/dist/patterns/Cursor.d.ts +37 -0
  17. package/dist/patterns/CursorHistory.d.ts +30 -0
  18. package/dist/patterns/Literal.d.ts +35 -19
  19. package/dist/patterns/Not.d.ts +29 -11
  20. package/dist/patterns/Or.d.ts +33 -22
  21. package/dist/patterns/ParseError.d.ts +6 -8
  22. package/dist/patterns/ParseResult.d.ts +6 -0
  23. package/dist/patterns/Pattern.d.ts +20 -26
  24. package/dist/patterns/Reference.d.ts +34 -12
  25. package/dist/patterns/Regex.d.ts +41 -21
  26. package/dist/patterns/Repeat.d.ts +38 -20
  27. package/dist/patterns/clonePatterns.d.ts +2 -0
  28. package/dist/patterns/filterOutNull.d.ts +2 -0
  29. package/dist/patterns/findPattern.d.ts +2 -0
  30. package/dist/patterns/getNextPattern.d.ts +2 -0
  31. package/jest.config.js +2 -1
  32. package/package.json +4 -5
  33. package/rollup.config.js +1 -1
  34. package/src/ast/Node.test.ts +364 -0
  35. package/src/ast/Node.ts +237 -23
  36. package/src/index.ts +25 -27
  37. package/src/intellisense/AutoComplete.test.ts +150 -0
  38. package/src/intellisense/AutoComplete.ts +200 -0
  39. package/src/intellisense/Suggestion.ts +12 -0
  40. package/src/intellisense/SuggestionOption.ts +4 -0
  41. package/src/{tests/cssPatterns → intellisense/css}/cssValue.ts +1 -1
  42. package/src/{tests/cssPatterns → intellisense/css}/divider.ts +2 -1
  43. package/src/intellisense/css/hex.ts +6 -0
  44. package/src/{tests/cssPatterns → intellisense/css}/method.ts +8 -9
  45. package/src/intellisense/css/name.ts +5 -0
  46. package/src/{tests/javascriptPatterns → intellisense/css}/number.ts +3 -3
  47. package/src/intellisense/css/spaces.ts +6 -0
  48. package/src/intellisense/css/unit.ts +10 -0
  49. package/src/{tests/cssPatterns → intellisense/css}/value.ts +1 -1
  50. package/src/{tests/cssPatterns → intellisense/css}/values.ts +1 -1
  51. package/src/intellisense/javascript/Javascript.test.ts +203 -0
  52. package/src/intellisense/javascript/arrayLiteral.ts +25 -0
  53. package/src/intellisense/javascript/deleteStatement.ts +14 -0
  54. package/src/intellisense/javascript/escapedCharacter.ts +49 -0
  55. package/src/intellisense/javascript/exponent.ts +24 -0
  56. package/src/intellisense/javascript/expression.ts +87 -0
  57. package/src/intellisense/javascript/expressionStatement.ts +29 -0
  58. package/src/intellisense/javascript/fraction.ts +11 -0
  59. package/src/intellisense/javascript/infixOperator.ts +36 -0
  60. package/src/intellisense/javascript/integer.ts +7 -0
  61. package/src/intellisense/javascript/invocation.ts +28 -0
  62. package/src/intellisense/javascript/literal.ts +14 -0
  63. package/src/intellisense/javascript/name.ts +3 -0
  64. package/src/intellisense/javascript/numberLiteral.ts +10 -0
  65. package/src/intellisense/javascript/objectLiteral.ts +30 -0
  66. package/src/intellisense/javascript/optionalSpaces.ts +3 -0
  67. package/src/intellisense/javascript/parameters.ts +20 -0
  68. package/src/intellisense/javascript/prefixOperator.ts +13 -0
  69. package/src/intellisense/javascript/propertyAccess.ts +23 -0
  70. package/src/intellisense/javascript/stringLiteral.ts +28 -0
  71. package/src/patterns/And.test.ts +310 -0
  72. package/src/patterns/And.ts +244 -119
  73. package/src/patterns/Cursor.test.ts +93 -0
  74. package/src/patterns/Cursor.ts +133 -0
  75. package/src/patterns/CursorHistory.test.ts +54 -0
  76. package/src/patterns/CursorHistory.ts +95 -0
  77. package/src/patterns/Literal.test.ts +166 -0
  78. package/src/patterns/Literal.ts +141 -62
  79. package/src/patterns/Not.test.ts +168 -0
  80. package/src/patterns/Not.ts +113 -32
  81. package/src/patterns/Or.test.ts +209 -0
  82. package/src/patterns/Or.ts +128 -97
  83. package/src/patterns/ParseError.ts +3 -7
  84. package/src/patterns/ParseResult.ts +7 -0
  85. package/src/patterns/Pattern.ts +21 -150
  86. package/src/patterns/Reference.test.ts +193 -0
  87. package/src/patterns/Reference.ts +114 -88
  88. package/src/patterns/Regex.test.ts +133 -0
  89. package/src/patterns/Regex.ts +117 -60
  90. package/src/patterns/Repeat.test.ts +218 -0
  91. package/src/patterns/Repeat.ts +220 -103
  92. package/src/patterns/clonePatterns.ts +5 -0
  93. package/src/patterns/filterOutNull.ts +13 -0
  94. package/src/patterns/findPattern.ts +25 -0
  95. package/src/Cursor.ts +0 -141
  96. package/src/CursorHistory.ts +0 -146
  97. package/src/TextSuggester.ts +0 -317
  98. package/src/ast/Visitor.ts +0 -271
  99. package/src/patterns/LookAhead.ts +0 -32
  100. package/src/patterns/Recursive.ts +0 -92
  101. package/src/tests/And.test.ts +0 -180
  102. package/src/tests/ComplexExamples.test.ts +0 -86
  103. package/src/tests/CssPatterns.test.ts +0 -90
  104. package/src/tests/CursorHistory.test.ts +0 -107
  105. package/src/tests/Cusor.test.ts +0 -174
  106. package/src/tests/HtmlPatterns.test.ts +0 -34
  107. package/src/tests/Literal.test.ts +0 -79
  108. package/src/tests/LookAhead.test.ts +0 -44
  109. package/src/tests/Not.test.ts +0 -51
  110. package/src/tests/Or.test.ts +0 -113
  111. package/src/tests/Pattern.test.ts +0 -290
  112. package/src/tests/Recursive.test.ts +0 -64
  113. package/src/tests/Reference.test.ts +0 -16
  114. package/src/tests/Repeat.test.ts +0 -75
  115. package/src/tests/SpeedTest.test.ts +0 -31
  116. package/src/tests/TextSuggester.test.ts +0 -297
  117. package/src/tests/Visitor.test.ts +0 -331
  118. package/src/tests/cssPatterns/hex.ts +0 -5
  119. package/src/tests/cssPatterns/name.ts +0 -5
  120. package/src/tests/cssPatterns/number.ts +0 -8
  121. package/src/tests/cssPatterns/spaces.ts +0 -5
  122. package/src/tests/cssPatterns/unit.ts +0 -8
  123. package/src/tests/htmlPatterns/element.ts +0 -49
  124. package/src/tests/javascriptPatterns/boolean.ts +0 -10
  125. package/src/tests/javascriptPatterns/json.ts +0 -67
  126. package/src/tests/javascriptPatterns/name.ts +0 -5
  127. package/src/tests/javascriptPatterns/objectLiteral.ts +0 -40
  128. package/src/tests/javascriptPatterns/string.ts +0 -84
  129. package/src/tests/javascriptPatterns/unit.ts +0 -8
  130. package/src/tests/javascriptPatterns/whitespace.ts +0 -44
  131. package/src/tests/naturalLanguage/filter.ts +0 -37
  132. package/src/tests/patterns/sentence.ts +0 -37
  133. /package/src/{tests/cssPatterns → intellisense/css}/optionalSpaces.ts +0 -0
@@ -0,0 +1,364 @@
1
+ import { Node } from "./Node";
2
+
3
+ describe("Node", () => {
4
+ test("Create", () => {
5
+ const node = new Node("type", "name", 0, 0, [], "value");
6
+
7
+ expect(node.type).toBe("type");
8
+ expect(node.name).toBe("name");
9
+ expect(node.value).toBe("value");
10
+ expect(node.firstIndex).toBe(0);
11
+ expect(node.lastIndex).toBe(0);
12
+ expect(node.startIndex).toBe(0);
13
+ expect(node.endIndex).toBe(1);
14
+ expect(node.parent).toBeNull();
15
+ expect(node.children.length).toBe(0);
16
+ });
17
+
18
+ test("Properties", () => {
19
+ const child = new Node("child", "child", 0, 0, undefined, "Content")
20
+ const parent = new Node("parent", "parent", 0, 0, [
21
+ child,
22
+ ]);
23
+
24
+ expect(parent.hasChildren).toBeTruthy();
25
+ expect(child.parent).toBe(parent);
26
+ expect(parent.children[0]).toBe(child);
27
+ expect(child.type).toBe("child");
28
+ expect(parent.value).toBe("Content");
29
+ expect(child.value).toBe("Content");
30
+ expect(child.name).toBe("child");
31
+ expect(child.firstIndex).toBe(0);
32
+ expect(child.lastIndex).toBe(0);
33
+ expect(child.startIndex).toBe(0);
34
+ expect(child.endIndex).toBe(1);
35
+ });
36
+
37
+ test("Create Tree", () => {
38
+ const child = new Node("child", "child", 0, 0, [], "Child");
39
+ const parent = new Node("parent", "parent", 0, 0, [child]);
40
+
41
+ expect(parent.value).toBe(child.value);
42
+ });
43
+
44
+ test("Remove Child", () => {
45
+ const child = new Node("child", "child", 0, 0, [], "Child");
46
+ const parent = new Node("parent", "parent", 0, 0, [child]);
47
+
48
+ parent.removeChild(child);
49
+
50
+ expect(parent.children.length).toBe(0);
51
+ expect(child.parent).toBeNull();
52
+ expect(parent.value).toBe("");
53
+ expect(child.value).toBe("Child");
54
+ });
55
+
56
+ test("Remove all Child", () => {
57
+ const a = new Node("a", "a", 0, 0, [], "A");
58
+ const b = new Node("b", "b", 0, 0, [], "B");
59
+ const parent = new Node("parent", "parent", 0, 0, [a, b], "Fallback Value");
60
+
61
+ expect(parent.children.length).toBe(2);
62
+ expect(a.parent).toBe(parent);
63
+ expect(b.parent).toBe(parent);
64
+ expect(parent.value).toBe("AB");
65
+ expect(a.value).toBe("A");
66
+ expect(b.value).toBe("B");
67
+
68
+ parent.removeAllChildren();
69
+
70
+ expect(parent.children.length).toBe(0);
71
+ expect(a.parent).toBeNull();
72
+ expect(b.parent).toBeNull();
73
+ expect(parent.value).toBe("Fallback Value");
74
+ expect(a.value).toBe("A");
75
+ expect(b.value).toBe("B");
76
+ });
77
+
78
+ test("Replace Child", () => {
79
+ const a = new Node("a", "a", 0, 0, [], "A");
80
+ const b = new Node("b", "b", 0, 0, [], "B");
81
+ const parent = new Node("parent", "parent", 0, 0, [a]);
82
+
83
+ parent.replaceChild(b, a);
84
+
85
+ expect(parent.children.length).toBe(1);
86
+ expect(a.parent).toBeNull();
87
+ expect(b.parent).toBe(parent);
88
+ expect(a.value).toBe("A");
89
+ expect(b.value).toBe("B");
90
+ });
91
+
92
+ test("Insert Child", () => {
93
+ const a = new Node("a", "a", 0, 0, [], "A");
94
+ const b = new Node("b", "b", 0, 0, [], "B");
95
+ const parent = new Node("parent", "parent", 0, 0, [a]);
96
+
97
+ expect(parent.value).toBe("A");
98
+
99
+ parent.insertBefore(b, a);
100
+
101
+ expect(parent.children.length).toBe(2);
102
+ expect(parent.value).toBe("BA")
103
+ expect(parent.children[0]).toBe(b);
104
+ expect(parent.children[1]).toBe(a);
105
+ expect(a.parent).toBe(parent);
106
+ expect(b.parent).toBe(parent);
107
+ expect(a.value).toBe("A");
108
+ expect(b.value).toBe("B");
109
+ });
110
+
111
+ test("Insert Child With Null Reference", () => {
112
+ const a = new Node("a", "a", 0, 0, [], "A");
113
+ const b = new Node("b", "b", 0, 0, [], "B");
114
+ const parent = new Node("parent", "parent", 0, 0, [a]);
115
+
116
+ expect(parent.value).toBe("A");
117
+
118
+ parent.insertBefore(b, null);
119
+
120
+ expect(parent.children.length).toBe(2);
121
+ expect(parent.value).toBe("AB")
122
+ expect(parent.children[0]).toBe(a);
123
+ expect(parent.children[1]).toBe(b);
124
+ expect(a.parent).toBe(parent);
125
+ expect(b.parent).toBe(parent);
126
+ expect(a.value).toBe("A");
127
+ expect(b.value).toBe("B");
128
+ });
129
+
130
+ test("Append Child", () => {
131
+ const a = new Node("a", "a", 0, 0, [], "A");
132
+ const b = new Node("b", "b", 0, 0, [], "B");
133
+ const parent = new Node("parent", "parent", 0, 0, [a]);
134
+
135
+ expect(parent.value).toBe("A");
136
+
137
+ parent.appendChild(b);
138
+
139
+ expect(parent.children.length).toBe(2);
140
+ expect(parent.value).toBe("AB")
141
+ expect(parent.children[0]).toBe(a);
142
+ expect(parent.children[1]).toBe(b);
143
+ expect(a.parent).toBe(parent);
144
+ expect(b.parent).toBe(parent);
145
+ expect(a.value).toBe("A");
146
+ expect(b.value).toBe("B");
147
+ });
148
+
149
+ test("Splice Children", () => {
150
+ const a = new Node("a", "a", 0, 0, [], "A");
151
+ const b = new Node("b", "b", 0, 0, [], "B");
152
+ const parent = new Node("parent", "parent", 0, 0, [a]);
153
+
154
+ expect(parent.value).toBe("A");
155
+
156
+ parent.spliceChildren(0, 0, b);
157
+
158
+ expect(parent.children.length).toBe(2);
159
+ expect(parent.value).toBe("BA")
160
+ expect(parent.children[0]).toBe(b);
161
+ expect(parent.children[1]).toBe(a);
162
+ expect(a.parent).toBe(parent);
163
+ expect(b.parent).toBe(parent);
164
+ expect(a.value).toBe("A");
165
+ expect(b.value).toBe("B");
166
+ });
167
+
168
+ test("Splice Children (Replace)", () => {
169
+ const a = new Node("a", "a", 0, 0, [], "A");
170
+ const b = new Node("b", "b", 0, 0, [], "B");
171
+ const parent = new Node("parent", "parent", 0, 0, [a]);
172
+
173
+ expect(parent.value).toBe("A");
174
+
175
+ parent.spliceChildren(0, 1, b);
176
+
177
+ expect(parent.children.length).toBe(1);
178
+ expect(parent.value).toBe("B")
179
+ expect(parent.children[0]).toBe(b);
180
+ expect(a.parent).toBeNull();
181
+ expect(b.parent).toBe(parent);
182
+ expect(a.value).toBe("A");
183
+ expect(b.value).toBe("B");
184
+ });
185
+
186
+ test("Next Sibling", () => {
187
+ const a = new Node("a", "a", 0, 0, [], "A");
188
+ const b = new Node("b", "b", 0, 0, [], "B");
189
+ new Node("parent", "parent", 0, 0, [a, b]);
190
+
191
+ const nextSibling = a.nextSibling()
192
+
193
+ expect(nextSibling).toBe(b);
194
+ });
195
+
196
+ test("Next Sibling (No Parent)", () => {
197
+ const a = new Node("a", "a", 0, 0, [], "A");
198
+
199
+ const nextSibling = a.nextSibling()
200
+ expect(nextSibling).toBeNull;
201
+ });
202
+
203
+ test("Next Sibling (Last Child)", () => {
204
+ const a = new Node("a", "a", 0, 0, [], "A");
205
+ const b = new Node("b", "b", 0, 0, [], "B");
206
+ new Node("parent", "parent", 0, 0, [a, b]);
207
+
208
+ const nextSibling = b.nextSibling()
209
+ expect(nextSibling).toBeNull;
210
+ });
211
+
212
+ test("Previous Sibling", () => {
213
+ const a = new Node("a", "a", 0, 0, [], "A");
214
+ const b = new Node("b", "b", 0, 0, [], "B");
215
+ new Node("parent", "parent", 0, 0, [a, b]);
216
+
217
+ const previousSibling = b.previousSibling()
218
+
219
+ expect(previousSibling).toBe(a);
220
+ });
221
+
222
+ test("Previous Sibling (No Parent)", () => {
223
+ const a = new Node("a", "a", 0, 0, [], "A");
224
+ const previousSibling = a.previousSibling()
225
+ expect(previousSibling).toBeNull();
226
+ });
227
+
228
+ test("Previous Sibling (First Child)", () => {
229
+ const a = new Node("a", "a", 0, 0, [], "A");
230
+ const b = new Node("b", "b", 0, 0, [], "B");
231
+ new Node("parent", "parent", 0, 0, [a, b]);
232
+
233
+ const previousSibling = a.previousSibling()
234
+ expect(previousSibling).toBeNull;
235
+ });
236
+
237
+ test("Find", () => {
238
+ const a = new Node("a", "a", 0, 0, [], "A");
239
+ const b = new Node("b", "b", 0, 0, [], "B");
240
+ const parent = new Node("parent", "parent", 0, 0, [a, b]);
241
+
242
+ const result = parent.find(n => n.name === "a");
243
+
244
+ expect(result).toBe(a)
245
+ });
246
+
247
+ test("Walk Down", () => {
248
+ const result: Node[] = [];
249
+ const a = new Node("a", "a", 0, 0, [], "A");
250
+ const b = new Node("b", "b", 0, 0, [], "B");
251
+ const parent = new Node("parent", "parent", 0, 0, [a, b]);
252
+
253
+ parent.walkDown((n) => {
254
+ result.push(n)
255
+ });
256
+
257
+ const expected = [parent, a, b];
258
+
259
+ expect(result).toEqual(expected);
260
+ });
261
+
262
+ test("Walk Up", () => {
263
+ const result: Node[] = [];
264
+ const a = new Node("a", "a", 0, 0, [], "A");
265
+ const b = new Node("b", "b", 0, 0, [], "B");
266
+ const parent = new Node("parent", "parent", 0, 0, [a, b]);
267
+
268
+ parent.walkUp((n) => {
269
+ result.push(n)
270
+ });
271
+
272
+ const expected = [a, b, parent];
273
+
274
+ expect(result).toEqual(expected);
275
+ });
276
+
277
+ test("Clone", () => {
278
+ const a = new Node("a", "a", 0, 0, [], "A");
279
+ const b = new Node("b", "b", 0, 0, [], "B");
280
+ const parent = new Node("parent", "parent", 0, 0, [a, b]);
281
+ const clone = parent.clone();
282
+
283
+ expect(clone).toEqual(parent)
284
+ });
285
+
286
+ test("Turn Into JSON", () => {
287
+ const a = new Node("a", "a", 0, 0, [], "A");
288
+ const b = new Node("b", "b", 0, 0, [], "B");
289
+ const parent = new Node("parent", "parent", 0, 0, [a, b]);
290
+ const result = parent.toJson();
291
+ const expected = JSON.stringify({
292
+ type: "parent",
293
+ name: "parent",
294
+ value: "AB",
295
+ firstIndex: 0,
296
+ lastIndex: 0,
297
+ startIndex: 0,
298
+ endIndex: 1,
299
+ children: [{
300
+ type: "a",
301
+ name: "a",
302
+ value: "A",
303
+ firstIndex: 0,
304
+ lastIndex: 0,
305
+ startIndex: 0,
306
+ endIndex: 1,
307
+ children: [],
308
+ }, {
309
+ type: "b",
310
+ name: "b",
311
+ value: "B",
312
+ firstIndex: 0,
313
+ lastIndex: 0,
314
+ startIndex: 0,
315
+ endIndex: 1,
316
+ children: [],
317
+ }],
318
+ });
319
+
320
+ expect(result).toEqual(expected)
321
+ });
322
+
323
+ test("Reduce", () => {
324
+ const parent = new Node("parent", "parent", 0, 6, [
325
+ new Node("child", "child", 0, 6, undefined, "Content")
326
+ ]);
327
+
328
+ parent.reduce();
329
+
330
+ expect(parent.hasChildren).toBeFalsy();
331
+ expect(parent.value).toBe("Content")
332
+ });
333
+
334
+ test("Flatten", () => {
335
+ const a = new Node("a", "a", 0, 0, [], "A");
336
+ const b = new Node("b", "b", 1, 1, [], "B");
337
+ const c = new Node("c", "c", 2, 2, [], "C");
338
+
339
+ const parent = new Node("parent", "parent", 0, 1, [
340
+ a,
341
+ b,
342
+ ]);
343
+
344
+ const grandParent = new Node("grand-parent", "grand-parent", 0, 2, [
345
+ parent,
346
+ c,
347
+ ]);
348
+
349
+ const nodes = grandParent.flatten();
350
+ const expected = [a, b, c];
351
+
352
+ expect(nodes).toEqual(expected)
353
+ });
354
+
355
+ test("Find Ancester", () => {
356
+ const child = new Node("child", "child", 0, 0, []);
357
+ const parent = new Node("parent", "parent", 0, 0, [child]);
358
+ const grandParent = new Node("grand-parent", "grand-parent", 0, 0, [parent]);
359
+ const result = child.findAncester(p => p.name === "grand-parent")
360
+
361
+ expect(result).toBe(grandParent)
362
+ });
363
+
364
+ });
package/src/ast/Node.ts CHANGED
@@ -1,39 +1,253 @@
1
- export default class Node {
2
- public type: string;
3
- public name: string;
4
- public startIndex: number;
5
- public endIndex: number;
6
- public children: Node[];
7
- public value: string;
1
+ export interface CycleFreeNode {
2
+ type: string;
3
+ name: string;
4
+ firstIndex: number;
5
+ lastIndex: number;
6
+ startIndex: number;
7
+ endIndex: number;
8
+ value: string;
9
+ children: CycleFreeNode[];
10
+ }
11
+
12
+ export class Node {
13
+ private _type: string;
14
+ private _name: string;
15
+ private _firstIndex: number;
16
+ private _lastIndex: number;
17
+ private _parent: Node | null;
18
+ private _children: Node[];
19
+ private _value: string;
20
+
21
+ get type() {
22
+ return this._type;
23
+ }
24
+
25
+ get name() {
26
+ return this._name;
27
+ }
28
+
29
+ get firstIndex() {
30
+ return this._firstIndex;
31
+ }
32
+
33
+ get lastIndex() {
34
+ return this._lastIndex;
35
+ }
36
+
37
+ get startIndex() {
38
+ return this._firstIndex;
39
+ }
40
+
41
+ get endIndex() {
42
+ return this._lastIndex + 1;
43
+ }
44
+
45
+ get parent() {
46
+ return this._parent;
47
+ }
48
+
49
+ get children(): readonly Node[] {
50
+ return this._children;
51
+ }
52
+
53
+ get hasChildren(): boolean {
54
+ return this._children.length > 0;
55
+ }
56
+
57
+ get value() {
58
+ return this.toString();
59
+ }
8
60
 
9
61
  constructor(
10
62
  type: string,
11
63
  name: string,
12
- startIndex: number,
13
- endIndex: number,
64
+ firstIndex: number,
65
+ lastIndex: number,
14
66
  children: Node[] = [],
15
67
  value: string = ""
16
68
  ) {
17
- this.type = type;
18
- this.name = name;
19
- this.startIndex = startIndex;
20
- this.endIndex = endIndex;
21
- this.children = children;
22
- this.value = value;
69
+ this._type = type;
70
+ this._name = name;
71
+ this._firstIndex = firstIndex;
72
+ this._lastIndex = lastIndex;
73
+ this._parent = null;
74
+ this._children = children;
75
+ this._value = value;
76
+
77
+ this._children.forEach(c => c._parent = this)
78
+ }
79
+
80
+ removeChild(node: Node) {
81
+ const index = this._children.indexOf(node);
82
+
83
+ if (index > -1) {
84
+ this._children.splice(index, 1);
85
+ node._parent = null;
86
+ }
87
+ }
88
+
89
+ removeAllChildren() {
90
+ this._children.forEach(c => c._parent = null);
91
+ this._children.length = 0;
92
+ }
93
+
94
+ replaceChild(newNode: Node, referenceNode: Node) {
95
+ const index = this._children.indexOf(referenceNode);
96
+
97
+ if (index > -1) {
98
+ this._children.splice(index, 1, newNode);
99
+ newNode._parent = this;
100
+ referenceNode._parent = null;
101
+ }
102
+ }
103
+
104
+ insertBefore(newNode: Node, referenceNode: Node | null) {
105
+ newNode._parent = this;
106
+
107
+ if (referenceNode == null) {
108
+ this._children.push(newNode);
109
+ return;
110
+ }
111
+
112
+ const index = this._children.indexOf(referenceNode);
113
+
114
+ if (index > -1) {
115
+ this._children.splice(index, 0, newNode);
116
+ }
117
+ }
118
+
119
+ appendChild(newNode: Node) {
120
+ newNode._parent = this;
121
+ this._children.push(newNode);
122
+ }
123
+
124
+ spliceChildren(index: number, deleteCount: number, ...items: Node[]) {
125
+ const removedItems = this._children.splice(index, deleteCount, ...items);
126
+
127
+ removedItems.forEach(i => i._parent = null);
128
+ items.forEach(i => i._parent = this);
129
+
130
+ return removedItems;
131
+ }
132
+
133
+ nextSibling() {
134
+ if (this._parent == null) {
135
+ return null;
136
+ }
137
+
138
+ const children = this._parent._children;
139
+ const index = children.indexOf(this);
140
+
141
+ if (index > -1 && index < children.length - 1) {
142
+ return children[index + 1]
143
+ }
144
+
145
+ return null
146
+ }
147
+
148
+ previousSibling() {
149
+ if (this._parent == null) {
150
+ return null;
151
+ }
152
+
153
+ const children = this._parent._children;
154
+ const index = children.indexOf(this);
155
+
156
+ if (index > -1 && index > 0) {
157
+ return children[index - 1]
158
+ }
159
+
160
+ return null
161
+ }
162
+
163
+ find(predicate: (node: Node) => boolean): Node | null {
164
+ return this.findAll(predicate)[0] || null
165
+ }
166
+
167
+ findAll(predicate: (node: Node) => boolean): Node[] {
168
+ const matches: Node[] = [];
169
+ this.walkUp(n => {
170
+ if (predicate(n)) { matches.push(n); }
171
+ })
172
+
173
+ return matches;
174
+ }
175
+
176
+ findAncester(predicate: (node: Node) => boolean){
177
+ let parent = this._parent;
178
+
179
+ while(parent != null){
180
+ if (predicate(parent)){
181
+ return parent;
182
+ }
183
+
184
+ parent = parent._parent;
185
+ }
186
+
187
+ return null;
188
+ }
189
+
190
+ walkUp(callback: (node: Node) => void) {
191
+ this.children.forEach(c => c.walkUp(callback))
192
+ callback(this);
193
+ }
194
+
195
+ walkDown(callback: (node: Node) => void) {
196
+ callback(this);
197
+ this.children.forEach(c => c.walkDown(callback))
198
+ }
199
+
200
+ flatten(){
201
+ const nodes: Node[] = [];
202
+
203
+ this.walkDown((node: Node)=>{
204
+ if (!node.hasChildren){
205
+ nodes.push(node);
206
+ }
207
+ });
208
+
209
+ return nodes;
210
+ }
211
+
212
+ reduce() {
213
+ const value = this.toString();
214
+ this.removeAllChildren();
215
+ this._value = value;
23
216
  }
24
217
 
25
218
  clone(): Node {
26
219
  return new Node(
27
- this.type,
28
- this.name,
29
- this.startIndex,
30
- this.endIndex,
31
- this.children.map((c) => c.clone()),
32
- this.value
220
+ this._type,
221
+ this._name,
222
+ this._firstIndex,
223
+ this._lastIndex,
224
+ this._children.map((c) => c.clone()),
225
+ this._value
33
226
  );
34
227
  }
35
228
 
36
- toString() {
37
- return this.value;
229
+ toString(): string {
230
+ if (this._children.length === 0) {
231
+ return this._value;
232
+ }
233
+
234
+ return this._children.map(c => c.toString()).join("")
235
+ }
236
+
237
+ toCycleFreeObject(): CycleFreeNode {
238
+ return {
239
+ type: this._type,
240
+ name: this._name,
241
+ value: this.toString(),
242
+ firstIndex: this._firstIndex,
243
+ lastIndex: this._lastIndex,
244
+ startIndex: this.startIndex,
245
+ endIndex: this.endIndex,
246
+ children: this._children.map(c => c.toCycleFreeObject()),
247
+ }
248
+ }
249
+
250
+ toJson(space?: number): string {
251
+ return JSON.stringify(this.toCycleFreeObject(), null, space)
38
252
  }
39
253
  }
package/src/index.ts CHANGED
@@ -1,38 +1,36 @@
1
- import Node from "./ast/Node";
2
- import Cursor from "./Cursor";
3
- import Regex from "./patterns/Regex";
4
- import And from "./patterns/And";
5
- import Literal from "./patterns/Literal";
6
- import LookAhead from "./patterns/LookAhead";
7
- import Not from "./patterns/Not";
8
- import Or from "./patterns/Or";
9
- import Repeat from "./patterns/Repeat";
10
- import ParseError from "./patterns/ParseError";
11
- import Pattern from "./patterns/Pattern";
12
- import Recursive from "./patterns/Recursive";
13
- import Reference from "./patterns/Reference";
14
- import Visitor from "./ast/Visitor";
15
- export { default as TextSuggester } from "./TextSuggester";
16
- export type {
17
- SuggestionError,
18
- SuggestionMatch,
19
- SuggestionResult,
20
- Token,
21
- } from "./TextSuggester";
1
+ import { Node } from "./ast/Node";
2
+ import { Cursor } from "./patterns/Cursor";
3
+ import { Regex } from "./patterns/Regex";
4
+ import { And } from "./patterns/And";
5
+ import { Literal } from "./patterns/Literal";
6
+ import { Not } from "./patterns/Not";
7
+ import { Or } from "./patterns/Or";
8
+ import { Repeat } from "./patterns/Repeat";
9
+ import { ParseError } from "./patterns/ParseError";
10
+ import { Pattern } from "./patterns/Pattern";
11
+ import { Reference } from "./patterns/Reference";
12
+ import { AutoComplete } from './intellisense/AutoComplete';
13
+ import { CursorHistory, Match } from "./patterns/CursorHistory";
14
+ import { ParseResult } from "./patterns/ParseResult";
15
+ import { Suggestion } from "./intellisense/Suggestion";
16
+ import { SuggestionOption } from "./intellisense/SuggestionOption";
22
17
 
23
18
  export {
24
19
  Node,
25
- Cursor,
26
- Regex,
20
+ AutoComplete,
21
+ Suggestion,
22
+ SuggestionOption,
27
23
  And,
24
+ Cursor,
25
+ CursorHistory,
26
+ Match,
28
27
  Literal,
29
28
  Not,
30
29
  Or,
31
- Repeat,
32
30
  ParseError,
31
+ ParseResult,
33
32
  Pattern,
34
- Recursive,
35
33
  Reference,
36
- Visitor,
37
- LookAhead,
34
+ Regex,
35
+ Repeat,
38
36
  };