clarity-pattern-parser 5.0.0 → 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 (53) hide show
  1. package/README.md +328 -38
  2. package/TODO.md +55 -1
  3. package/dist/ast/Node.d.ts +8 -2
  4. package/dist/index.browser.js +470 -205
  5. package/dist/index.browser.js.map +1 -1
  6. package/dist/index.d.ts +6 -1
  7. package/dist/index.esm.js +469 -206
  8. package/dist/index.esm.js.map +1 -1
  9. package/dist/index.js +470 -205
  10. package/dist/index.js.map +1 -1
  11. package/dist/intellisense/AutoComplete.d.ts +28 -0
  12. package/dist/intellisense/Suggestion.d.ts +11 -0
  13. package/dist/intellisense/SuggestionOption.d.ts +4 -0
  14. package/dist/patterns/And.d.ts +7 -7
  15. package/dist/patterns/Cursor.d.ts +6 -4
  16. package/dist/patterns/CursorHistory.d.ts +2 -2
  17. package/dist/patterns/Literal.d.ts +7 -8
  18. package/dist/patterns/Not.d.ts +8 -5
  19. package/dist/patterns/Or.d.ts +7 -5
  20. package/dist/patterns/Pattern.d.ts +7 -4
  21. package/dist/patterns/Reference.d.ts +10 -7
  22. package/dist/patterns/Regex.d.ts +7 -8
  23. package/dist/patterns/Repeat.d.ts +7 -7
  24. package/package.json +1 -1
  25. package/src/ast/Node.test.ts +110 -0
  26. package/src/ast/Node.ts +71 -5
  27. package/src/index.ts +14 -3
  28. package/src/intellisense/AutoComplete.test.ts +90 -12
  29. package/src/intellisense/AutoComplete.ts +66 -12
  30. package/src/intellisense/Suggestion.ts +3 -4
  31. package/src/intellisense/javascript/Javascript.test.ts +56 -56
  32. package/src/intellisense/javascript/escapedCharacter.ts +0 -1
  33. package/src/intellisense/javascript/exponent.ts +0 -2
  34. package/src/intellisense/javascript/fraction.ts +0 -2
  35. package/src/patterns/And.test.ts +63 -52
  36. package/src/patterns/And.ts +58 -36
  37. package/src/patterns/Cursor.ts +17 -14
  38. package/src/patterns/CursorHistory.ts +8 -8
  39. package/src/patterns/Literal.test.ts +70 -38
  40. package/src/patterns/Literal.ts +31 -42
  41. package/src/patterns/Not.test.ts +88 -8
  42. package/src/patterns/Not.ts +54 -14
  43. package/src/patterns/Or.test.ts +117 -13
  44. package/src/patterns/Or.ts +36 -13
  45. package/src/patterns/Pattern.ts +7 -4
  46. package/src/patterns/Reference.test.ts +117 -28
  47. package/src/patterns/Reference.ts +58 -32
  48. package/src/patterns/Regex.test.ts +67 -35
  49. package/src/patterns/Regex.ts +31 -43
  50. package/src/patterns/Repeat.test.ts +63 -41
  51. package/src/patterns/Repeat.ts +51 -38
  52. package/src/patterns/getNextPattern.test.ts +0 -39
  53. package/src/patterns/getNextPattern.ts +0 -18
package/README.md CHANGED
@@ -14,53 +14,74 @@ npm install clarity-pattern-parser
14
14
  * Or
15
15
  * Repeat
16
16
  * Reference
17
+ * Not
17
18
 
18
- The "Not" pattern is a negative look ahead and mostly used with the "And" pattern. This will be illustrated in more detail within the "Not" pattern section.
19
-
19
+ The `Not` pattern is a negative look ahead and used with the `And` pattern. This will be illustrated in more detail within the `Not` pattern section.
20
20
 
21
21
  ## Literal
22
- The "Literal" pattern uses a string literal to match patterns.
22
+ The `Literal` pattern uses a string literal to match patterns.
23
23
  ```ts
24
24
  import { Literal } from "clarity-pattern-parser";
25
25
 
26
26
  const firstName = new Literal("first-name", "John");
27
- const { ast } = firstName.parseText("John");
28
27
 
29
- ast.type // ==> "literal"
30
- ast.name // ==> "first-name"
31
- ast.value // ==> "John"
28
+ const { ast } = firstName.exec("John");
29
+
30
+ ast.toJson(2)
31
+ ```
32
+ ```json
33
+ {
34
+ "type": "literal",
35
+ "name": "first-name",
36
+ "value": "John",
37
+ "firstIndex": 0,
38
+ "lastIndex": 3,
39
+ "startIndex": 0,
40
+ "endIndex": 4,
41
+ "children": []
42
+ }
32
43
  ```
33
44
 
34
45
  ## Regex
35
- The "Regex" pattern uses regular expressions to match patterns
46
+ The `Regex` pattern uses regular expressions to match patterns.
36
47
  ```ts
37
48
  import { Regex } from "clarity-pattern-parser";
38
49
 
39
50
  const digits = new Regex("digits", "\\d+");
40
- const { ast } = digit.parseText("12");
41
51
 
42
- ast.type // ==> "regex"
43
- ast.name // ==> "digits"
44
- ast.value // ==> "12"
52
+ const { ast } = digits.exec("12");
53
+
54
+ ast.toJson(2);
55
+ ```
56
+ ```json
57
+ {
58
+ "type": "regex",
59
+ "name": "digits",
60
+ "value": "12",
61
+ "firstIndex": 0,
62
+ "lastIndex": 1,
63
+ "startIndex": 0,
64
+ "endIndex": 2,
65
+ "children": []
66
+ }
45
67
  ```
46
68
 
47
69
  ### Regex Caveats
48
70
  Do not use "^" at the beginning or "$" at the end of your regular expression. If you are creating a regular expression that is concerned about the beginning and end of the text you should probably just use a regular expression.
49
71
 
50
72
  ## And
51
- The "And" pattern is a way to make a sequence pattern. And accepts any other patterns as children, even other "And" patterns.
73
+ The `And` pattern is a way to make a sequence pattern. `And` accepts all other patterns as children.
52
74
  ```ts
53
75
  import { And, Literal } from "clarity-pattern-parser";
54
76
 
55
77
  const jane = new Literal("first-name", "Jane");
56
78
  const space = new Literal("space", " ");
57
79
  const doe = new Literal("last-name", "Doe");
58
-
59
80
  const fullName = new And("full-name", [jane, space, doe]);
60
81
 
61
- const { ast } = fullName.parseText("Jane Doe");
82
+ const { ast } = fullName.exec("Jane Doe");
62
83
 
63
- ast.toJson(); // Look Below for output
84
+ ast.toJson(2); // Look Below for output
64
85
  ```
65
86
 
66
87
  ```json
@@ -108,29 +129,31 @@ ast.toJson(); // Look Below for output
108
129
  ```
109
130
 
110
131
  ## Or
111
- The "Or" pattern mathes any of the patterns given to the constructor.
132
+ The `Or` pattern mathes any of the patterns given to the constructor.
112
133
  ```ts
113
134
  import { Or, Literal } from "clarity-pattern-parser";
114
135
 
115
136
  const jane = new Literal("jane", "Jane");
116
137
  const john = new Literal("john", "John");
117
138
  const firstName = new Or("first-name", [jane, john]);
139
+ const { ast }= firstName.exec("Jane");
118
140
 
119
- let ast= firstName.parseText("Jane").ast;
120
-
121
- ast.type // ==> "literal"
122
- ast.name // ==> "jane"
123
- ast.value // ==> "Jane"
124
-
125
- ast = firstName.parseText("John").ast;
126
-
127
- ast.type // ==> "literal"
128
- ast.name // ==> "john"
129
- ast.value // ==> "John"
141
+ ast.toJson(2)
142
+ ```
143
+ ```json
144
+ {
145
+ "type": "literal",
146
+ "name": "jane",
147
+ "value": "Jane",
148
+ "firstIndex": 0,
149
+ "lastIndex": 3,
150
+ "startIndex": 0,
151
+ "endIndex": 4,
152
+ "children": []
153
+ }
130
154
  ```
131
-
132
155
  ## Repeat
133
- The "Repeat" patterns allows you to match repeating patterns with, or without a divider.
156
+ The `Repeat` patterns allows you to match repeating patterns with, or without a divider.
134
157
 
135
158
  For example you may want to match a pattern like so.
136
159
  ```
@@ -141,10 +164,10 @@ Here is the code to do so.
141
164
  import { Repeat, Literal, Regex } from "clarity-pattern-parser";
142
165
 
143
166
  const digit = new Regex("digit", "\\d+");
144
- const comma = new Literal("comma", ",");
145
- const numberList = new Repeat("number-list", digit, comma);
167
+ const commaDivider = new Literal("comma", ",");
168
+ const numberList = new Repeat("number-list", digit, commaDivider);
146
169
 
147
- const ast = numberList.parseText("1,2,3").ast;
170
+ const ast = numberList.exec("1,2,3").ast;
148
171
 
149
172
  ast.type // ==> "repeat"
150
173
  ast.name // ==> "number-list"
@@ -157,16 +180,16 @@ ast.children[3].value // ==> ","
157
180
  ast.children[4].value // ==> "3"
158
181
  ```
159
182
 
160
- If there is a trailing divider without a the repeating pattern, it will not include the trailing divider as part of the result. Here is an example.
183
+ If there is a trailing divider without the repeating pattern, it will not include the trailing divider as part of the result. Here is an example.
161
184
 
162
185
  ```ts
163
186
  import { Repeat, Literal, Regex } from "clarity-pattern-parser";
164
187
 
165
188
  const digit = new Regex("digit", "\\d+");
166
- const comma = new Literal("comma", ",");
167
- const numberList = new Repeat("number-list", digit, comma);
189
+ const commaDivider = new Literal("comma", ",");
190
+ const numberList = new Repeat("number-list", digit, commaDivider);
168
191
 
169
- const ast = numberList.parseText("1,2,").ast;
192
+ const ast = numberList.exec("1,2,").ast;
170
193
 
171
194
  ast.type // ==> "repeat"
172
195
  ast.name // ==> "number-list"
@@ -178,5 +201,272 @@ ast.children[2].value // ==> "2"
178
201
  ast.children.length // ==> 3
179
202
  ```
180
203
 
181
- T
204
+ ## Reference
205
+ Reference is a way to handle cyclical patterns. An example of this would be arrays within arrays. Lets say we want to make a pattern that matches an array that can store numbers and arrays.
206
+ ```
207
+ [[1, [1]], 1, 2, 3]
208
+ ```
209
+ Here is an example of using `Reference` to parse this pattern.
210
+ ```ts
211
+ import { Regex, Literal, Or, Repeat, And, Reference } from "clarity-pattern-parser";
212
+
213
+ const integer = new Regex("integer", "\\d+");
214
+ const commaDivider = new Regex("comma-divider", "\\s*,\\s*");
215
+
216
+ const openBracket = new Literal("open-bracket", "[");
217
+ const closeBracket = new Literal("close-bracket", "]");
218
+ const item = new Or("item", [integer, new Reference("array")]);
219
+ const items = new Repeat("items", item, commaDivider);
220
+
221
+ const array = new And("array", [openBracket, items, closeBracket]);
222
+ const { ast } = array.exec("[[1, [1]], 1, 2, 3]");
223
+
224
+ ast.toJson();
225
+ ```
226
+ ```json
227
+ {
228
+ "type": "and",
229
+ "name": "array",
230
+ "value": "[[1, [1]], 1, 2, 3]",
231
+ "firstIndex": 0,
232
+ "lastIndex": 18,
233
+ "startIndex": 0,
234
+ "endIndex": 19,
235
+ "children": [
236
+ {
237
+ "type": "literal",
238
+ "name": "open-bracket",
239
+ "value": "[",
240
+ "firstIndex": 0,
241
+ "lastIndex": 0,
242
+ "startIndex": 0,
243
+ "endIndex": 1,
244
+ "children": []
245
+ },
246
+ {
247
+ "type": "repeat",
248
+ "name": "items",
249
+ "value": "[1, [1]], 1, 2, 3",
250
+ "firstIndex": 1,
251
+ "lastIndex": 17,
252
+ "startIndex": 1,
253
+ "endIndex": 18,
254
+ "children": [
255
+ {
256
+ "type": "and",
257
+ "name": "array",
258
+ "value": "[1, [1]]",
259
+ "firstIndex": 1,
260
+ "lastIndex": 8,
261
+ "startIndex": 1,
262
+ "endIndex": 9,
263
+ "children": [
264
+ {
265
+ "type": "literal",
266
+ "name": "open-bracket",
267
+ "value": "[",
268
+ "firstIndex": 1,
269
+ "lastIndex": 1,
270
+ "startIndex": 1,
271
+ "endIndex": 2,
272
+ "children": []
273
+ },
274
+ {
275
+ "type": "repeat",
276
+ "name": "items",
277
+ "value": "1, [1]",
278
+ "firstIndex": 2,
279
+ "lastIndex": 7,
280
+ "startIndex": 2,
281
+ "endIndex": 8,
282
+ "children": [
283
+ {
284
+ "type": "regex",
285
+ "name": "integer",
286
+ "value": "1",
287
+ "firstIndex": 2,
288
+ "lastIndex": 2,
289
+ "startIndex": 2,
290
+ "endIndex": 3,
291
+ "children": []
292
+ },
293
+ {
294
+ "type": "regex",
295
+ "name": "comma-divider",
296
+ "value": ", ",
297
+ "firstIndex": 3,
298
+ "lastIndex": 4,
299
+ "startIndex": 3,
300
+ "endIndex": 5,
301
+ "children": []
302
+ },
303
+ {
304
+ "type": "and",
305
+ "name": "array",
306
+ "value": "[1]",
307
+ "firstIndex": 5,
308
+ "lastIndex": 7,
309
+ "startIndex": 5,
310
+ "endIndex": 8,
311
+ "children": [
312
+ {
313
+ "type": "literal",
314
+ "name": "open-bracket",
315
+ "value": "[",
316
+ "firstIndex": 5,
317
+ "lastIndex": 5,
318
+ "startIndex": 5,
319
+ "endIndex": 6,
320
+ "children": []
321
+ },
322
+ {
323
+ "type": "repeat",
324
+ "name": "items",
325
+ "value": "1",
326
+ "firstIndex": 6,
327
+ "lastIndex": 6,
328
+ "startIndex": 6,
329
+ "endIndex": 7,
330
+ "children": [
331
+ {
332
+ "type": "regex",
333
+ "name": "integer",
334
+ "value": "1",
335
+ "firstIndex": 6,
336
+ "lastIndex": 6,
337
+ "startIndex": 6,
338
+ "endIndex": 7,
339
+ "children": []
340
+ }
341
+ ]
342
+ },
343
+ {
344
+ "type": "literal",
345
+ "name": "close-bracket",
346
+ "value": "]",
347
+ "firstIndex": 7,
348
+ "lastIndex": 7,
349
+ "startIndex": 7,
350
+ "endIndex": 8,
351
+ "children": []
352
+ }
353
+ ]
354
+ }
355
+ ]
356
+ },
357
+ {
358
+ "type": "literal",
359
+ "name": "close-bracket",
360
+ "value": "]",
361
+ "firstIndex": 8,
362
+ "lastIndex": 8,
363
+ "startIndex": 8,
364
+ "endIndex": 9,
365
+ "children": []
366
+ }
367
+ ]
368
+ },
369
+ {
370
+ "type": "regex",
371
+ "name": "comma-divider",
372
+ "value": ", ",
373
+ "firstIndex": 9,
374
+ "lastIndex": 10,
375
+ "startIndex": 9,
376
+ "endIndex": 11,
377
+ "children": []
378
+ },
379
+ {
380
+ "type": "regex",
381
+ "name": "integer",
382
+ "value": "1",
383
+ "firstIndex": 11,
384
+ "lastIndex": 11,
385
+ "startIndex": 11,
386
+ "endIndex": 12,
387
+ "children": []
388
+ },
389
+ {
390
+ "type": "regex",
391
+ "name": "comma-divider",
392
+ "value": ", ",
393
+ "firstIndex": 12,
394
+ "lastIndex": 13,
395
+ "startIndex": 12,
396
+ "endIndex": 14,
397
+ "children": []
398
+ },
399
+ {
400
+ "type": "regex",
401
+ "name": "integer",
402
+ "value": "2",
403
+ "firstIndex": 14,
404
+ "lastIndex": 14,
405
+ "startIndex": 14,
406
+ "endIndex": 15,
407
+ "children": []
408
+ },
409
+ {
410
+ "type": "regex",
411
+ "name": "comma-divider",
412
+ "value": ", ",
413
+ "firstIndex": 15,
414
+ "lastIndex": 16,
415
+ "startIndex": 15,
416
+ "endIndex": 17,
417
+ "children": []
418
+ },
419
+ {
420
+ "type": "regex",
421
+ "name": "integer",
422
+ "value": "3",
423
+ "firstIndex": 17,
424
+ "lastIndex": 17,
425
+ "startIndex": 17,
426
+ "endIndex": 18,
427
+ "children": []
428
+ }
429
+ ]
430
+ },
431
+ {
432
+ "type": "literal",
433
+ "name": "close-bracket",
434
+ "value": "]",
435
+ "firstIndex": 18,
436
+ "lastIndex": 18,
437
+ "startIndex": 18,
438
+ "endIndex": 19,
439
+ "children": []
440
+ }
441
+ ]
442
+ }
443
+ ```
444
+ The `Reference` pattern traverses the pattern composition to find the pattern that matches the one given to it at construction. It will then clone that pattern and tell that pattern to parse the text. If it cannot find the pattern with the given name, it will throw a runtime error.
445
+ ## Not
446
+
447
+ ## Intellisense
448
+ Because the patterns are composed in a tree and the cursor remembers what patterns matched last, we can ask what tokens are next. We will discuss how you can use clarity-pattern-parser for text auto complete and other interesting approaches for intellisense.
449
+
450
+ ## GetTokens
451
+ The `getTokens` method allow you to ask the pattern what tokens it is looking for. The Regex pattern was the only pattern that didn't already intrinsically know what patterns it was looking for, and we solved this by adding a `setTokens` to its class. This allows you to define a regexp that can capture infinitely many patterns, but suggest a finite set. We will discuss this further in the setTokens section. For now we will demonstrate what `getTokens` does.
452
+
453
+ ```ts
454
+ import { Or, Literal } from "clarity-pattern-parser";
455
+
456
+ const jane = new Literal("jane", "Jane");
457
+ const john = new Literal("john", "John");
458
+ const jack = new Literal("jack", "Jack");
459
+ const jill = new Literal("jill", "Jill");
460
+
461
+ const names = new Or("names", [jane, john, jack, jill]);
462
+
463
+ names.getTokens();
464
+ ```
465
+ ```json
466
+ ["Jane", "John", "Jack", "Jill"]
467
+ ```
468
+ ## GetNextTokens
469
+
470
+ ## SetTokens
471
+
182
472
  ## Error Handling
package/TODO.md CHANGED
@@ -9,7 +9,8 @@ close-paren = ")"
9
9
  argument = integer | method
10
10
  arguments = argument* divider
11
11
  not-integer = !integer
12
- method = method-name open-paren arguments close-paren
12
+ optional-integer = integer?
13
+ method = method-name & open-paren & arguments & close-paren
13
14
 
14
15
  integer.tokens = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
15
16
  integer.enableContextualTokenAggregation
@@ -18,5 +19,58 @@ arguments.shouldReduceAst
18
19
  export (method, arguments)
19
20
  ```
20
21
 
22
+ Using Grammar for a simple JSON Parser
23
+ ```
24
+ # Number Integer
25
+ integer = /([1-9][0-9])*|0/
26
+ digit = /\d+/
27
+
28
+ # Number Fraction
29
+ period = "."
30
+ fraction = period & digit
31
+
32
+ # Number Exponent
33
+ e = "e" | "E"
34
+ sign = "+" | "-"
35
+ number-exponent = e & sign? & digit
36
+
37
+ # Number
38
+ number = integer & fraction? & number-exponent?
39
+
40
+ # String Unicode
41
+ hex-digit = /[0-9a-fA-F]/
42
+ unicode = "u" & hex-digit & hex-digit & hex-digit & hex-digit
43
+ special-character =
44
+ "'" |
45
+ "\"" |
46
+ "\\" |
47
+ "/" |
48
+ "b" |
49
+ "f" |
50
+ "n" |
51
+ "r" |
52
+ "t" |
53
+ unicode
54
+
55
+ normal-character = /[^\\"]+/
56
+ escaped-character = "\\" & special-character
57
+ character = normal-character | special-character
58
+ characters = character*
59
+
60
+ # String
61
+ string = "\"" & characters & "\""
62
+
63
+ # Boolean
64
+ boolean = "true" | "false"
65
+
66
+ spaces /\s+/
67
+
68
+ array-item = number | boolean | string | array | object
69
+ array-items = array-items* /\s+,\s+/
70
+ array = "[" & spaces? & array-items & spaces? & "]"
71
+
72
+
73
+ ```
74
+
21
75
  // Other file
22
76
  import (method, arguments) from "./method.grammar"
@@ -24,6 +24,7 @@ export declare class Node {
24
24
  get endIndex(): number;
25
25
  get parent(): Node | null;
26
26
  get children(): readonly Node[];
27
+ get hasChildren(): boolean;
27
28
  get value(): string;
28
29
  constructor(type: string, name: string, firstIndex: number, lastIndex: number, children?: Node[], value?: string);
29
30
  removeChild(node: Node): void;
@@ -32,10 +33,15 @@ export declare class Node {
32
33
  insertBefore(newNode: Node, referenceNode: Node | null): void;
33
34
  appendChild(newNode: Node): void;
34
35
  spliceChildren(index: number, deleteCount: number, ...items: Node[]): Node[];
35
- find(isMatch: (node: Node) => boolean): Node | null;
36
- findAll(isMatch: (node: Node) => boolean): Node[];
36
+ nextSibling(): Node | null;
37
+ previousSibling(): Node | null;
38
+ find(predicate: (node: Node) => boolean): Node | null;
39
+ findAll(predicate: (node: Node) => boolean): Node[];
40
+ findAncester(predicate: (node: Node) => boolean): Node | null;
37
41
  walkUp(callback: (node: Node) => void): void;
38
42
  walkDown(callback: (node: Node) => void): void;
43
+ flatten(): Node[];
44
+ reduce(): void;
39
45
  clone(): Node;
40
46
  toString(): string;
41
47
  toCycleFreeObject(): CycleFreeNode;