clarity-pattern-parser 9.2.5 → 10.0.1

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.
@@ -106,11 +106,11 @@ export class CursorHistory {
106
106
  }
107
107
  }
108
108
 
109
- recordErrorAt(firstIndex: number, lastIndex: number, pattern: Pattern): void {
110
- const error = new ParseError(firstIndex, lastIndex, pattern);
109
+ recordErrorAt(startIndex: number, endIndex: number, pattern: Pattern): void {
110
+ const error = new ParseError(startIndex, endIndex, pattern);
111
111
  this._currentError = error;
112
112
 
113
- if (this._furthestError === null || lastIndex > this._furthestError.endIndex) {
113
+ if (this._furthestError === null || endIndex > this._furthestError.endIndex) {
114
114
  this._furthestError = error;
115
115
  }
116
116
 
@@ -4,7 +4,6 @@ import { Regex } from "./Regex";
4
4
  import { Node } from "../ast/Node";
5
5
  import { Literal } from "./Literal";
6
6
  import { Sequence } from "./Sequence";
7
- import { arePatternsEqual } from "./arePatternsEqual";
8
7
  import { Optional } from "./Optional";
9
8
 
10
9
  describe("BoundedRepeat", () => {
@@ -287,22 +286,22 @@ describe("BoundedRepeat", () => {
287
286
  let clone = numbers.clone();
288
287
  let expected = new FiniteRepeat("numbers", new Regex("number", "\\d"), { min: 0, max: 3 });
289
288
 
290
- expect(arePatternsEqual(clone, expected)).toBeTruthy();
289
+ expect(clone.isEqual(expected)).toBeTruthy();
291
290
 
292
291
  clone = numbers.clone("cloned-numbers");
293
292
  expected = new FiniteRepeat("cloned-numbers", new Regex("number", "\\d"), { min: 0, max: 3 });
294
293
 
295
- expect(arePatternsEqual(clone, expected)).toBeTruthy();
294
+ expect(clone.isEqual(expected)).toBeTruthy();
296
295
 
297
296
  clone = numbers.clone("cloned-numbers");
298
297
  expected = new FiniteRepeat("cloned-numbers", new Regex("number", "\\d"), { min: 0, max: 3 });
299
298
 
300
- expect(arePatternsEqual(clone, expected)).toBeTruthy();
299
+ expect(clone.isEqual(expected)).toBeTruthy();
301
300
 
302
301
  clone = numbers.clone("cloned-numbers");
303
302
  expected = new FiniteRepeat("cloned-numbers", new Regex("number", "\\d"), { min: 0, max: 3 });
304
303
 
305
- expect(arePatternsEqual(clone, expected)).toBeTruthy();
304
+ expect(clone.isEqual(expected)).toBeTruthy();
306
305
  });
307
306
 
308
307
  test("Get Tokens", () => {
@@ -6,7 +6,6 @@ import { Literal } from "./Literal";
6
6
  import { Pattern } from "./Pattern";
7
7
  import { Regex } from "./Regex";
8
8
  import { InfiniteRepeat } from "./InfiniteRepeat";
9
- import { arePatternsEqual } from "./arePatternsEqual";
10
9
  import { Optional } from "./Optional";
11
10
 
12
11
  describe("InfiniteRepeat", () => {
@@ -254,7 +253,7 @@ describe("InfiniteRepeat", () => {
254
253
  let clone = numbers.clone();
255
254
  let expected = new InfiniteRepeat("numbers", new Regex("number", "\\d"), { min: 3, divider: new Literal("divider", "divider"), trimDivider: true });
256
255
 
257
- expect(arePatternsEqual(clone, expected)).toBeTruthy();
256
+ expect(clone.isEqual(expected)).toBeTruthy();
258
257
  });
259
258
 
260
259
  test("No Results, min is 0", () => {
@@ -10,7 +10,7 @@ export class Literal implements Pattern {
10
10
  private _type: string;
11
11
  private _name: string;
12
12
  private _parent: Pattern | null;
13
- private _text: string;
13
+ private _token: string;
14
14
  private _runes: string[];
15
15
  private _firstIndex: number;
16
16
  private _lastIndex: number;
@@ -28,8 +28,8 @@ export class Literal implements Pattern {
28
28
  return this._name;
29
29
  }
30
30
 
31
- get value(): string {
32
- return this._text;
31
+ get token(): string {
32
+ return this._token;
33
33
  }
34
34
 
35
35
  get parent(): Pattern | null {
@@ -52,7 +52,7 @@ export class Literal implements Pattern {
52
52
  this._id = `literal-${idIndex++}`;
53
53
  this._type = "literal";
54
54
  this._name = name;
55
- this._text = value;
55
+ this._token = value;
56
56
  this._runes = Array.from(value);
57
57
  this._parent = null;
58
58
  this._firstIndex = 0;
@@ -60,8 +60,10 @@ export class Literal implements Pattern {
60
60
  this._endIndex = 0;
61
61
  }
62
62
 
63
- test(text: string) {
63
+ test(text: string, record = false) {
64
64
  const cursor = new Cursor(text);
65
+ record && cursor.startRecording();
66
+
65
67
  const ast = this.parse(cursor);
66
68
 
67
69
  return ast?.value === text;
@@ -83,7 +85,6 @@ export class Literal implements Pattern {
83
85
  cursor.startParseWith(this);
84
86
 
85
87
  this._firstIndex = cursor.index;
86
-
87
88
  const passed = this._tryToParse(cursor);
88
89
 
89
90
  if (passed) {
@@ -114,7 +115,7 @@ export class Literal implements Pattern {
114
115
  }
115
116
 
116
117
  if (i + 1 === literalRuneLength) {
117
- this._lastIndex = this._firstIndex + this._text.length - 1;
118
+ this._lastIndex = this._firstIndex + this._token.length - 1;
118
119
  passed = true;
119
120
  break;
120
121
  }
@@ -137,18 +138,18 @@ export class Literal implements Pattern {
137
138
  this._firstIndex,
138
139
  this._lastIndex,
139
140
  undefined,
140
- this._text
141
+ this._token
141
142
  );
142
143
  }
143
144
 
144
145
  clone(name = this._name): Pattern {
145
- const clone = new Literal(name, this._text);
146
+ const clone = new Literal(name, this._token);
146
147
  clone._id = this._id;
147
148
  return clone;
148
149
  }
149
150
 
150
151
  getTokens(): string[] {
151
- return [this._text];
152
+ return [this._token];
152
153
  }
153
154
 
154
155
  getTokensAfter(_lastMatched: Pattern): string[] {
@@ -184,6 +185,6 @@ export class Literal implements Pattern {
184
185
  }
185
186
 
186
187
  isEqual(pattern: Literal) {
187
- return pattern.type === this.type && pattern._text === this._text;
188
+ return pattern.type === this.type && pattern._token === this._token;
188
189
  }
189
190
  }
@@ -1,7 +1,7 @@
1
1
  import { Pattern } from "./Pattern";
2
2
 
3
3
  export class ParseError {
4
- public startIndex: number
4
+ public startIndex: number;
5
5
  public endIndex: number;
6
6
  public pattern: Pattern;
7
7
 
@@ -19,6 +19,6 @@ export interface Pattern {
19
19
  getPatterns(): Pattern[];
20
20
  getPatternsAfter(childReference: Pattern): Pattern[];
21
21
  getNextPatterns(): Pattern[];
22
- find(predicate: (p: Pattern) => boolean): Pattern | null;
22
+ find(predicate: (pattern: Pattern) => boolean): Pattern | null;
23
23
  isEqual(pattern: Pattern): boolean;
24
24
  }
@@ -30,7 +30,7 @@ export class Regex implements Pattern {
30
30
  return this._name;
31
31
  }
32
32
 
33
- get value(): string{
33
+ get regex(): string{
34
34
  return this._originalRegexString;
35
35
  }
36
36
 
@@ -1,6 +1,5 @@
1
1
  import { Node } from "../ast/Node";
2
2
  import { Sequence } from "./Sequence";
3
- import { arePatternsEqual } from "./arePatternsEqual";
4
3
  import { Cursor } from "./Cursor";
5
4
  import { InfiniteRepeat } from "./InfiniteRepeat";
6
5
  import { Literal } from "./Literal";
@@ -202,11 +201,11 @@ describe("Repeat", () => {
202
201
  let repeatClone = repeat.clone();
203
202
  let expected = new Repeat("numbers", number);
204
203
 
205
- expect(arePatternsEqual(repeatClone, expected)).toBeTruthy();
204
+ expect(repeatClone.isEqual(expected)).toBeTruthy();
206
205
 
207
206
  repeatClone = repeat.clone("new-name");
208
207
  expected = new Repeat("new-name", number);
209
208
 
210
- expect(arePatternsEqual(repeatClone, expected)).toBeTruthy();
209
+ expect(repeatClone.isEqual(expected)).toBeTruthy();
211
210
  });
212
211
  });
@@ -1,12 +0,0 @@
1
- import { Literal } from "./Literal";
2
- import { Pattern } from "./Pattern";
3
-
4
- export function arePatternsEqual(a?: Pattern | null, b?: Pattern | null): boolean {
5
- if (a === b) {
6
- return true;
7
- } else if (a == null || b == null) {
8
- return false;
9
- }
10
-
11
- return a.isEqual(b);
12
- }