clarity-pattern-parser 3.0.9 → 3.0.11

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.
@@ -172,15 +172,17 @@ export default class TextSuggester {
172
172
  }
173
173
 
174
174
  private saveNextToken() {
175
- if (
175
+ const isCompleteMatch =
176
176
  this.patternMatch?.pattern === this.rootPattern &&
177
- this.cursor?.didSuccessfullyParse()
178
- ) {
177
+ this.cursor?.didSuccessfullyParse();
178
+ const noMatch = this.patternMatch?.astNode == null;
179
+
180
+ if (isCompleteMatch) {
179
181
  this.tokens = null;
180
182
  return;
181
183
  }
182
184
 
183
- if (this.patternMatch?.astNode == null) {
185
+ if (noMatch) {
184
186
  let options = this.rootPattern?.getTokens();
185
187
 
186
188
  options = options?.filter((option: any) => {
@@ -209,7 +211,7 @@ export default class TextSuggester {
209
211
  endIndex: this.text.length - 1,
210
212
  };
211
213
  this.error = null;
212
-
214
+
213
215
  return;
214
216
  }
215
217
 
@@ -73,6 +73,12 @@ export default class RepeatComposite extends CompositePattern {
73
73
  break;
74
74
  } else {
75
75
  this.nodes.push(node);
76
+
77
+ if (node.endIndex === this.cursor.lastIndex()) {
78
+ this._processMatch();
79
+ break;
80
+ }
81
+
76
82
  this.cursor.next();
77
83
  }
78
84
  }
@@ -74,6 +74,12 @@ export default class RepeatValue extends ValuePattern {
74
74
  break;
75
75
  } else {
76
76
  this.nodes.push(node);
77
+
78
+ if (node.endIndex === this.cursor.lastIndex()) {
79
+ this._processMatch();
80
+ break;
81
+ }
82
+
77
83
  this.cursor.next();
78
84
  }
79
85
  }
@@ -3,6 +3,8 @@ import RepeatComposite from "../patterns/composite/RepeatComposite";
3
3
  import AndComposite from "../patterns/composite/AndComposite";
4
4
  import OptionalComposite from "../patterns/composite/OptionalComposite";
5
5
  import Literal from "../patterns/value/Literal";
6
+ import Cursor from "../Cursor";
7
+ import OrComposite from "../patterns/composite/OrComposite";
6
8
 
7
9
  describe("RepeatComposite", () => {
8
10
  test("Cannot use optional patterns.", () => {
@@ -24,4 +26,21 @@ describe("RepeatComposite", () => {
24
26
 
25
27
  expect(clone.name).toBe("full-names-2");
26
28
  });
29
+
30
+ test("parse.", () => {
31
+ const a = new Literal("a", "A");
32
+ const b = new Literal("b", "B");
33
+ const space = new Literal("space", " ");
34
+ const or = new OrComposite("names", [a, b]);
35
+
36
+ const repeat = new RepeatComposite("repeat", or, space);
37
+ const result = repeat.parse(new Cursor("A B"));
38
+
39
+ expect(result?.children[0].value).toBe("A");
40
+ expect(result?.children[0].name).toBe("a");
41
+ expect(result?.children[1].value).toBe(" ");
42
+ expect(result?.children[1].name).toBe("space");
43
+ expect(result?.children[2].value).toBe("B");
44
+ expect(result?.children[2].name).toBe("b");
45
+ });
27
46
  });