clarity-pattern-parser 6.0.0 → 6.0.2

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 (46) hide show
  1. package/TODO.md +9 -7
  2. package/dist/index.browser.js +72 -22
  3. package/dist/index.browser.js.map +1 -1
  4. package/dist/index.esm.js +72 -22
  5. package/dist/index.esm.js.map +1 -1
  6. package/dist/index.js +72 -22
  7. package/dist/index.js.map +1 -1
  8. package/dist/intellisense/AutoComplete.d.ts +12 -6
  9. package/dist/intellisense/Suggestion.d.ts +1 -2
  10. package/dist/patterns/And.d.ts +1 -0
  11. package/dist/patterns/Literal.d.ts +1 -0
  12. package/dist/patterns/Not.d.ts +1 -0
  13. package/dist/patterns/Or.d.ts +1 -0
  14. package/dist/patterns/Pattern.d.ts +2 -1
  15. package/dist/patterns/Reference.d.ts +1 -0
  16. package/dist/patterns/Regex.d.ts +1 -0
  17. package/dist/patterns/Repeat.d.ts +1 -0
  18. package/package.json +1 -1
  19. package/src/ast/Node.test.ts +6 -0
  20. package/src/intellisense/AutoComplete.test.ts +104 -37
  21. package/src/intellisense/AutoComplete.ts +51 -24
  22. package/src/intellisense/Suggestion.ts +1 -1
  23. package/src/intellisense/javascript/Javascript.test.ts +35 -11
  24. package/src/intellisense/javascript/{expressionStatement.ts → assignment.ts} +7 -8
  25. package/src/intellisense/javascript/expression.ts +44 -26
  26. package/src/intellisense/javascript/infixOperator.ts +6 -2
  27. package/src/intellisense/javascript/keywords.ts +3 -0
  28. package/src/intellisense/javascript/objectAccess.ts +9 -0
  29. package/src/intellisense/javascript/objectLiteral.ts +3 -3
  30. package/src/intellisense/javascript/propertyAccess.ts +8 -3
  31. package/src/intellisense/javascript/stringLiteral.ts +16 -8
  32. package/src/patterns/And.test.ts +13 -0
  33. package/src/patterns/And.ts +14 -0
  34. package/src/patterns/Literal.test.ts +9 -0
  35. package/src/patterns/Literal.ts +4 -0
  36. package/src/patterns/Not.test.ts +11 -0
  37. package/src/patterns/Not.ts +4 -0
  38. package/src/patterns/Or.test.ts +11 -0
  39. package/src/patterns/Or.ts +10 -0
  40. package/src/patterns/Pattern.ts +2 -1
  41. package/src/patterns/Reference.test.ts +10 -0
  42. package/src/patterns/Reference.ts +4 -0
  43. package/src/patterns/Regex.test.ts +9 -0
  44. package/src/patterns/Regex.ts +4 -0
  45. package/src/patterns/Repeat.test.ts +9 -0
  46. package/src/patterns/Repeat.ts +4 -0
@@ -6,7 +6,7 @@ import { name } from "./name";
6
6
 
7
7
  const dotPropertyAccess = new And("dot-property-access", [
8
8
  new Literal("period", "."),
9
- name
9
+ name.clone("property-name")
10
10
  ]);
11
11
 
12
12
  const bracketPropertyAccess = new And("bracket-property-access", [
@@ -15,9 +15,14 @@ const bracketPropertyAccess = new And("bracket-property-access", [
15
15
  new Literal("close-square-bracket", "]"),
16
16
  ]);
17
17
 
18
- const propertyAccess = new Or("property-access", [
18
+ const propertyAccessTypes = new Or("property-access-types", [
19
19
  dotPropertyAccess,
20
- bracketPropertyAccess,
20
+ bracketPropertyAccess
21
+ ]);
22
+
23
+ const propertyAccess = new And("property-access", [
24
+ propertyAccessTypes,
25
+ new Reference("property-access", true)
21
26
  ]);
22
27
 
23
28
  export { propertyAccess }
@@ -7,19 +7,27 @@ import { escapedCharacter } from "./escapedCharacter";
7
7
 
8
8
  const doubleQuoteStringLiteral = new And("double-string-literal", [
9
9
  new Literal("double-quote", "\""),
10
- new Repeat("characters", new Or("characters", [
11
- new Regex("normal-characters", "[^\\\"]+"),
12
- escapedCharacter
13
- ])),
10
+ new Repeat("characters",
11
+ new Or("characters", [
12
+ new Regex("normal-characters", "[^\\\"]+"),
13
+ escapedCharacter
14
+ ]),
15
+ undefined,
16
+ true
17
+ ),
14
18
  new Literal("double-quote", "\""),
15
19
  ]);
16
20
 
17
21
  const singleQuoteStringLiteral = new And("single-string-literal", [
18
22
  new Literal("single-quote", "'"),
19
- new Repeat("characters", new Or("characters", [
20
- new Regex("normal-characters", "[^\\']+"),
21
- escapedCharacter
22
- ])),
23
+ new Repeat("characters",
24
+ new Or("characters", [
25
+ new Regex("normal-characters", "[^\\']+"),
26
+ escapedCharacter
27
+ ]),
28
+ undefined,
29
+ true
30
+ ),
23
31
  new Literal("single-quote", "'"),
24
32
  ]);
25
33
 
@@ -289,6 +289,19 @@ describe("And", () => {
289
289
  expect(tokens.length).toBe(0);
290
290
  });
291
291
 
292
+ test("Get Patterns", () => {
293
+ const sequence = new And("sequence", [
294
+ new Literal("a", "A", true),
295
+ new Literal("b", "B"),
296
+ ], true);
297
+ const tokens = sequence.getPatterns();
298
+ const a = sequence.findPattern(p=>p.name === "a");
299
+ const b = sequence.findPattern(p=>p.name === "b");
300
+ const expected = [a, b]
301
+
302
+ expect(tokens).toEqual(expected);
303
+ });
304
+
292
305
  test("Get Next Patterns", () => {
293
306
  const sequence = new And("sequence", [new Literal("a", "A")]);
294
307
  const parent = new And("parent", [sequence, new Literal("b", "B")]);
@@ -234,6 +234,20 @@ export class And implements Pattern {
234
234
  return this.parent.getTokensAfter(this);
235
235
  }
236
236
 
237
+ getPatterns(): Pattern[] {
238
+ const patterns: Pattern[] = [];
239
+
240
+ for (const pattern of this._children) {
241
+ patterns.push(...pattern.getPatterns());
242
+
243
+ if (!pattern.isOptional) {
244
+ break;
245
+ }
246
+ }
247
+
248
+ return patterns;
249
+ }
250
+
237
251
  getPatternsAfter(childReference: Pattern): Pattern[] {
238
252
  let nextSibling: Pattern | null = null;
239
253
  let nextSiblingIndex = -1;
@@ -132,6 +132,15 @@ describe("Literal", () => {
132
132
  expect(tokens.length).toBe(0);
133
133
  });
134
134
 
135
+ test("Get Patterns", () => {
136
+ const a = new Literal("a", "A");
137
+
138
+ const tokens = a.getPatterns();
139
+ const expectedTokens = [a];
140
+
141
+ expect(tokens).toEqual(expectedTokens);
142
+ });
143
+
135
144
  test("Get Next Patterns", () => {
136
145
  const sequence = new And("sequence", [new Literal("a", "A")]);
137
146
  const parent = new And("parent", [sequence, new Literal("b", "B")]);
@@ -151,6 +151,10 @@ export class Literal implements Pattern {
151
151
  return this.parent.getTokensAfter(this);
152
152
  }
153
153
 
154
+ getPatterns(): Pattern[] {
155
+ return [this];
156
+ }
157
+
154
158
  getPatternsAfter(): Pattern[] {
155
159
  return []
156
160
  }
@@ -128,6 +128,17 @@ describe("Not", () => {
128
128
  expect(child).not.toBeNull();
129
129
  });
130
130
 
131
+ test("Get Patterns", () => {
132
+ const notAboutUs = new Not("not-about-us", new Literal("about-us", "About Us"));
133
+ const sequence = new And("sequence", [notAboutUs, new Literal("about-them", "About Them")]);
134
+
135
+ const cloneNotAboutUs = sequence.findPattern(p => p.name === "not-about-us") as Pattern;
136
+ const nextPatterns = cloneNotAboutUs.getPatterns();
137
+ const expected = [sequence.findPattern(p=>p.name === "about-them")];
138
+
139
+ expect(nextPatterns).toEqual(expected);
140
+ });
141
+
131
142
  test("Get Next Patterns", () => {
132
143
  const notAboutUs = new Not("not-about-us", new Literal("about-us", "About Us"));
133
144
  const sequence = new And("sequence", [notAboutUs, new Literal("about-them", "About Them")]);
@@ -106,6 +106,10 @@ export class Not implements Pattern {
106
106
  return this.parent.getTokensAfter(this);
107
107
  }
108
108
 
109
+ getPatterns(): Pattern[] {
110
+ return [...this.getNextPatterns().map(p => p.getPatterns()).flat()];
111
+ }
112
+
109
113
  getPatternsAfter(_childReference: Pattern): Pattern[] {
110
114
  const parent = this._parent;
111
115
 
@@ -153,6 +153,17 @@ describe("Or", () => {
153
153
  expect(tokens[0]).toBe("C");
154
154
  });
155
155
 
156
+ test("Get Patterns", () => {
157
+ const aOrB = new Or("a-b", [new Literal("a", "A"), new Literal("b", "B")]);
158
+ const patterns = aOrB.getPatterns();
159
+ const expected = [
160
+ aOrB.findPattern(p => p.name === "a"),
161
+ aOrB.findPattern(p => p.name === "b")
162
+ ];
163
+
164
+ expect(patterns).toEqual(expected);
165
+ });
166
+
156
167
  test("Get Patterns After", () => {
157
168
  const sequence = new And("sequence", [
158
169
  new Or("a-or-b", [
@@ -136,6 +136,16 @@ export class Or implements Pattern {
136
136
  return this._parent.getTokensAfter(this);
137
137
  }
138
138
 
139
+ getPatterns(): Pattern[] {
140
+ const patterns: Pattern[] = [];
141
+
142
+ for (const pattern of this._children) {
143
+ patterns.push(...pattern.getPatterns());
144
+ }
145
+
146
+ return patterns;
147
+ }
148
+
139
149
  getPatternsAfter(_childReference: Pattern): Pattern[] {
140
150
  if (this._parent === null) {
141
151
  return [];
@@ -15,8 +15,9 @@ export interface Pattern {
15
15
  clone(name?: string, isOptional?: boolean): Pattern;
16
16
  getTokens(): string[];
17
17
  getTokensAfter(childReference: Pattern): string[];
18
+ getNextTokens(): string[];
19
+ getPatterns(): Pattern[];
18
20
  getPatternsAfter(childReference: Pattern): Pattern[];
19
21
  getNextPatterns(): Pattern[];
20
- getNextTokens(): string[];
21
22
  findPattern(predicate: (p: Pattern) => boolean): Pattern | null;
22
23
  }
@@ -152,6 +152,16 @@ describe("Reference", () => {
152
152
  expect(tokens).toEqual([])
153
153
  });
154
154
 
155
+ test("Get Patterns", () => {
156
+ const value = createValuePattern();
157
+ const ref = findPattern(value, (p) => p.type === "reference");
158
+ const patterns = ref?.getPatterns() || [];
159
+
160
+ expect(patterns.length).toBe(2);
161
+ expect(patterns[0].name).toBe("number");
162
+ expect(patterns[1].name).toBe("open-bracket");
163
+ });
164
+
155
165
  test("Get Patterns After", () => {
156
166
  const value = createValuePattern();
157
167
  const reference = value.findPattern(p => p.type === "reference") as Pattern;
@@ -127,6 +127,10 @@ export class Reference implements Pattern {
127
127
  return this.parent.getTokensAfter(this);
128
128
  }
129
129
 
130
+ getPatterns(): Pattern[] {
131
+ return this._getPatternSafely().getPatterns();
132
+ }
133
+
130
134
  getPatternsAfter(_childReference: Pattern): Pattern[] {
131
135
  if (this._parent == null) {
132
136
  return [];
@@ -99,6 +99,15 @@ describe("Regex", () => {
99
99
  expect(tokens).toEqual([]);
100
100
  });
101
101
 
102
+ test("Get Patterns", () => {
103
+ const a = new Regex("a", "A");
104
+
105
+ const tokens = a.getPatterns();
106
+ const expectedTokens = [a];
107
+
108
+ expect(tokens).toEqual(expectedTokens);
109
+ });
110
+
102
111
  test("Get Patterns After", () => {
103
112
  const a = new Regex("a", "A")
104
113
  const patterns = a.getPatternsAfter(new Literal("bogus", "bogus"));
@@ -157,6 +157,10 @@ export class Regex implements Pattern {
157
157
  return this.parent.getTokensAfter(this);
158
158
  }
159
159
 
160
+ getPatterns(): Pattern[] {
161
+ return [this];
162
+ }
163
+
160
164
  getPatternsAfter(_childReference: Pattern): Pattern[] {
161
165
  return [];
162
166
  }
@@ -198,6 +198,15 @@ describe("Repeat", () => {
198
198
  expect(digitClone).not.toBeNull();
199
199
  });
200
200
 
201
+ test("Get Patterns", () => {
202
+ const a = new Literal("a", "A");
203
+ const manyA = new Repeat("number", a);
204
+ const patterns = manyA.getPatterns();
205
+ const expected = [manyA.findPattern(p => p.name === "a")];
206
+
207
+ expect(patterns).toEqual(expected)
208
+ });
209
+
201
210
  test("Get Next Patterns", () => {
202
211
  const integer = new Repeat("integer", new Regex("digit", "\\d"));
203
212
  const parent = new And("parent", [integer, new Literal("pow", "!")]);
@@ -216,6 +216,10 @@ export class Repeat implements Pattern {
216
216
  return this.parent.getTokensAfter(this);
217
217
  }
218
218
 
219
+ getPatterns(): Pattern[] {
220
+ return this._pattern.getPatterns();
221
+ }
222
+
219
223
  getPatternsAfter(childReference: Pattern): Pattern[] {
220
224
  let index = -1;
221
225
  const patterns: Pattern[] = [];