clarity-pattern-parser 10.2.13 → 10.2.14

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.
@@ -10,6 +10,7 @@ export declare class Context implements Pattern {
10
10
  private _children;
11
11
  private _pattern;
12
12
  private _patterns;
13
+ shouldCompactAst: boolean;
13
14
  get id(): string;
14
15
  get type(): string;
15
16
  get name(): string;
@@ -18,6 +18,7 @@ export declare class ExpressionPattern implements Pattern {
18
18
  private _binaryAssociation;
19
19
  private _precedenceMap;
20
20
  private _binaryNames;
21
+ shouldCompactAst: boolean;
21
22
  get id(): string;
22
23
  get type(): string;
23
24
  get name(): string;
@@ -18,6 +18,7 @@ export declare class FiniteRepeat implements Pattern {
18
18
  private _min;
19
19
  private _max;
20
20
  private _trimDivider;
21
+ shouldCompactAst: boolean;
21
22
  get id(): string;
22
23
  get type(): string;
23
24
  get name(): string;
@@ -19,6 +19,7 @@ export declare class InfiniteRepeat implements Pattern {
19
19
  private _firstIndex;
20
20
  private _min;
21
21
  private _trimDivider;
22
+ shouldCompactAst: boolean;
22
23
  get id(): string;
23
24
  get type(): string;
24
25
  get name(): string;
@@ -12,6 +12,7 @@ export declare class Literal implements Pattern {
12
12
  private _firstIndex;
13
13
  private _lastIndex;
14
14
  private _endIndex;
15
+ shouldCompactAst: boolean;
15
16
  get id(): string;
16
17
  get type(): string;
17
18
  get name(): string;
@@ -8,6 +8,7 @@ export declare class Not implements Pattern {
8
8
  private _name;
9
9
  private _parent;
10
10
  private _children;
11
+ shouldCompactAst: boolean;
11
12
  get id(): string;
12
13
  get type(): string;
13
14
  get name(): string;
@@ -8,6 +8,7 @@ export declare class Optional implements Pattern {
8
8
  private _name;
9
9
  private _parent;
10
10
  private _children;
11
+ shouldCompactAst: boolean;
11
12
  get id(): string;
12
13
  get type(): string;
13
14
  get name(): string;
@@ -10,6 +10,7 @@ export declare class Options implements Pattern {
10
10
  private _children;
11
11
  private _isGreedy;
12
12
  private _firstIndex;
13
+ shouldCompactAst: boolean;
13
14
  get id(): string;
14
15
  get type(): string;
15
16
  get name(): string;
@@ -5,6 +5,7 @@ export interface Pattern {
5
5
  id: string;
6
6
  type: string;
7
7
  name: string;
8
+ shouldCompactAst: boolean;
8
9
  parent: Pattern | null;
9
10
  children: Pattern[];
10
11
  parse(cursor: Cursor): Node | null;
@@ -10,6 +10,7 @@ export declare class Reference implements Pattern {
10
10
  private _cachedPattern;
11
11
  private _pattern;
12
12
  private _children;
13
+ shouldCompactAst: boolean;
13
14
  get id(): string;
14
15
  get type(): string;
15
16
  get name(): string;
@@ -14,6 +14,7 @@ export declare class Regex implements Pattern {
14
14
  private _firstIndex;
15
15
  private _substring;
16
16
  private _tokens;
17
+ shouldCompactAst: boolean;
17
18
  get id(): string;
18
19
  get type(): string;
19
20
  get name(): string;
@@ -15,6 +15,7 @@ export declare class Repeat implements Pattern {
15
15
  private _pattern;
16
16
  private _options;
17
17
  private _children;
18
+ shouldCompactAst: boolean;
18
19
  get id(): string;
19
20
  get type(): string;
20
21
  get name(): string;
@@ -9,6 +9,7 @@ export declare class Sequence implements Pattern {
9
9
  private _children;
10
10
  private _nodes;
11
11
  private _firstIndex;
12
+ shouldCompactAst: boolean;
12
13
  get id(): string;
13
14
  get type(): string;
14
15
  get name(): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-pattern-parser",
3
- "version": "10.2.13",
3
+ "version": "10.2.14",
4
4
  "description": "Parsing Library for Typescript and Javascript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
package/src/ast/Node.ts CHANGED
@@ -309,6 +309,11 @@ export class Node {
309
309
  return length;
310
310
  }
311
311
 
312
+ compact(){
313
+ this._value = this.toString();
314
+ this._children.length = 0;
315
+ }
316
+
312
317
  toString(): string {
313
318
  if (this._children.length === 0) {
314
319
  return this._value;
@@ -14,6 +14,8 @@ export class Context implements Pattern {
14
14
  private _pattern: Pattern;
15
15
  private _patterns: Record<string, Pattern>;
16
16
 
17
+ shouldCompactAst = false;
18
+
17
19
  get id(): string {
18
20
  return this._id;
19
21
  }
@@ -76,6 +78,7 @@ export class Context implements Pattern {
76
78
  clone(name = this._name): Pattern {
77
79
  const clone = new Context(name, this._pattern, Object.values(this._patterns));
78
80
  clone._id = this._id;
81
+ clone.shouldCompactAst = this.shouldCompactAst;
79
82
  return clone;
80
83
  }
81
84
 
@@ -35,6 +35,8 @@ export class ExpressionPattern implements Pattern {
35
35
  private _precedenceMap: Record<string, number>;
36
36
  private _binaryNames: string[];
37
37
 
38
+ shouldCompactAst = false;
39
+
38
40
  get id(): string {
39
41
  return this._id;
40
42
  }
@@ -514,6 +516,7 @@ export class ExpressionPattern implements Pattern {
514
516
  clone(name = this._name): Pattern {
515
517
  const clone = new ExpressionPattern(name, this._originalPatterns);
516
518
  clone._id = this._id;
519
+ clone.shouldCompactAst = this.shouldCompactAst;
517
520
  return clone;
518
521
  }
519
522
 
@@ -24,6 +24,8 @@ export class FiniteRepeat implements Pattern {
24
24
  private _max: number;
25
25
  private _trimDivider: boolean;
26
26
 
27
+ shouldCompactAst = false;
28
+
27
29
  get id() {
28
30
  return this._id;
29
31
  }
@@ -140,7 +142,13 @@ export class FiniteRepeat implements Pattern {
140
142
  cursor.resolveError();
141
143
  cursor.moveTo(lastIndex);
142
144
 
143
- return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
145
+ const node = new Node(this._type, this.name, firstIndex, lastIndex, nodes);
146
+
147
+ if (this.shouldCompactAst) {
148
+ node.compact();
149
+ }
150
+
151
+ return node;
144
152
  }
145
153
 
146
154
  test(text: string): boolean {
@@ -178,6 +186,7 @@ export class FiniteRepeat implements Pattern {
178
186
  );
179
187
 
180
188
  clone._id = this._id;
189
+ clone.shouldCompactAst = this.shouldCompactAst;
181
190
 
182
191
  return clone;
183
192
  }
@@ -26,6 +26,8 @@ export class InfiniteRepeat implements Pattern {
26
26
  private _min: number;
27
27
  private _trimDivider: boolean;
28
28
 
29
+ shouldCompactAst = false;
30
+
29
31
  get id(): string {
30
32
  return this._id;
31
33
  }
@@ -118,6 +120,10 @@ export class InfiniteRepeat implements Pattern {
118
120
  if (node != null) {
119
121
  cursor.moveTo(node.lastIndex);
120
122
  cursor.recordMatch(this, node);
123
+
124
+ if (this.shouldCompactAst) {
125
+ node.compact();
126
+ }
121
127
  }
122
128
 
123
129
  return node;
@@ -352,6 +358,7 @@ export class InfiniteRepeat implements Pattern {
352
358
  );
353
359
 
354
360
  clone._id = this._id;
361
+ clone.shouldCompactAst = this.shouldCompactAst;
355
362
 
356
363
  return clone;
357
364
  }
@@ -16,6 +16,8 @@ export class Literal implements Pattern {
16
16
  private _lastIndex: number;
17
17
  private _endIndex: number;
18
18
 
19
+ shouldCompactAst = false;
20
+
19
21
  get id(): string {
20
22
  return this._id;
21
23
  }
@@ -141,6 +143,7 @@ export class Literal implements Pattern {
141
143
  clone(name = this._name): Pattern {
142
144
  const clone = new Literal(name, this._token);
143
145
  clone._id = this._id;
146
+ clone.shouldCompactAst = this.shouldCompactAst;
144
147
  return clone;
145
148
  }
146
149
 
@@ -12,6 +12,8 @@ export class Not implements Pattern {
12
12
  private _parent: Pattern | null;
13
13
  private _children: Pattern[];
14
14
 
15
+ shouldCompactAst = false;
16
+
15
17
  get id(): string {
16
18
  return this._id;
17
19
  }
@@ -12,6 +12,8 @@ export class Optional implements Pattern {
12
12
  private _parent: Pattern | null;
13
13
  private _children: Pattern[];
14
14
 
15
+ shouldCompactAst = false;
16
+
15
17
  get id(): string {
16
18
  return this._id;
17
19
  }
@@ -74,15 +76,20 @@ export class Optional implements Pattern {
74
76
 
75
77
  return null;
76
78
  } else {
79
+ if (node != null && this.shouldCompactAst) {
80
+ node.compact();
81
+ }
82
+
77
83
  return node;
78
84
  }
79
85
 
80
86
  }
81
87
 
82
88
  clone(name = this._name): Pattern {
83
- const optional = new Optional(name, this._children[0]);
84
- optional._id = this._id;
85
- return optional;
89
+ const clone = new Optional(name, this._children[0]);
90
+ clone._id = this._id;
91
+ clone.shouldCompactAst = this.shouldCompactAst;
92
+ return clone;
86
93
  }
87
94
 
88
95
  getTokens(): string[] {
@@ -23,6 +23,8 @@ export class Options implements Pattern {
23
23
  private _isGreedy: boolean;
24
24
  private _firstIndex: number;
25
25
 
26
+ shouldCompactAst = false;
27
+
26
28
  get id(): string {
27
29
  return this._id;
28
30
  }
@@ -102,6 +104,11 @@ export class Options implements Pattern {
102
104
  if (node != null) {
103
105
  cursor.moveTo(node.lastIndex);
104
106
  cursor.resolveError();
107
+
108
+ if (this.shouldCompactAst) {
109
+ node.compact();
110
+ }
111
+
105
112
  return node;
106
113
  }
107
114
 
@@ -202,9 +209,10 @@ export class Options implements Pattern {
202
209
  }
203
210
 
204
211
  clone(name = this._name): Pattern {
205
- const or = new Options(name, this._children, this._isGreedy);
206
- or._id = this._id;
207
- return or;
212
+ const clone = new Options(name, this._children, this._isGreedy);
213
+ clone._id = this._id;
214
+ clone.shouldCompactAst = this.shouldCompactAst;
215
+ return clone;
208
216
  }
209
217
 
210
218
  isEqual(pattern: Options): boolean {
@@ -6,6 +6,7 @@ export interface Pattern {
6
6
  id: string;
7
7
  type: string;
8
8
  name: string;
9
+ shouldCompactAst: boolean;
9
10
  parent: Pattern | null;
10
11
  children: Pattern[];
11
12
 
@@ -16,6 +16,8 @@ export class Reference implements Pattern {
16
16
  private _pattern: Pattern | null;
17
17
  private _children: Pattern[];
18
18
 
19
+ shouldCompactAst = false;
20
+
19
21
  get id(): string {
20
22
  return this._id;
21
23
  }
@@ -196,6 +198,7 @@ export class Reference implements Pattern {
196
198
  clone(name = this._name): Pattern {
197
199
  const clone = new Reference(name);
198
200
  clone._id = this._id;
201
+ clone.shouldCompactAst = this.shouldCompactAst;
199
202
 
200
203
  // Optimize future clones, by caching the pattern we already found.
201
204
  if (this._pattern != null) {
@@ -18,6 +18,8 @@ export class Regex implements Pattern {
18
18
  private _substring = "";
19
19
  private _tokens: string[] = [];
20
20
 
21
+ shouldCompactAst = false;
22
+
21
23
  get id(): string {
22
24
  return this._id;
23
25
  }
@@ -30,7 +32,7 @@ export class Regex implements Pattern {
30
32
  return this._name;
31
33
  }
32
34
 
33
- get regex(): string{
35
+ get regex(): string {
34
36
  return this._originalRegexString;
35
37
  }
36
38
 
@@ -145,8 +147,9 @@ export class Regex implements Pattern {
145
147
  clone(name = this._name) {
146
148
  const clone = new Regex(name, this._originalRegexString);
147
149
  clone._tokens = this._tokens.slice();
148
-
149
150
  clone._id = this._id;
151
+ clone.shouldCompactAst = this.shouldCompactAst;
152
+
150
153
  return clone;
151
154
  }
152
155
 
@@ -28,6 +28,8 @@ export class Repeat implements Pattern {
28
28
  private _options: InternalRepeatOptions;
29
29
  private _children: Pattern[];
30
30
 
31
+ shouldCompactAst = false;
32
+
31
33
  get id() {
32
34
  return this._id;
33
35
  }
@@ -76,6 +78,7 @@ export class Repeat implements Pattern {
76
78
  this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
77
79
  }
78
80
 
81
+ this._repeatPattern.shouldCompactAst = this.shouldCompactAst;
79
82
  this._children = [this._repeatPattern];
80
83
  this._repeatPattern.parent = this;
81
84
  }
@@ -97,6 +100,7 @@ export class Repeat implements Pattern {
97
100
  const clone = new Repeat(name, this._pattern, { ...this._options, min });
98
101
 
99
102
  clone._id = this._id;
103
+ clone.shouldCompactAst = this.shouldCompactAst;
100
104
  return clone;
101
105
  }
102
106
 
@@ -12,6 +12,8 @@ export class RightAssociatedPattern implements Pattern {
12
12
  private _parent: Pattern | null;
13
13
  private _children: Pattern[];
14
14
 
15
+ shouldCompactAst = false;
16
+
15
17
  get id(): string {
16
18
  return this._id;
17
19
  }
@@ -19,6 +19,8 @@ export class Sequence implements Pattern {
19
19
  private _nodes: (Node | null)[];
20
20
  private _firstIndex: number;
21
21
 
22
+ shouldCompactAst = false;
23
+
22
24
  get id(): string {
23
25
  return this._id;
24
26
  }
@@ -100,6 +102,10 @@ export class Sequence implements Pattern {
100
102
 
101
103
  if (node !== null) {
102
104
  cursor.recordMatch(this, node);
105
+
106
+ if (this.shouldCompactAst) {
107
+ node.compact();
108
+ }
103
109
  }
104
110
 
105
111
  return node;
@@ -323,6 +329,7 @@ export class Sequence implements Pattern {
323
329
  clone(name = this._name): Pattern {
324
330
  const clone = new Sequence(name, this._children);
325
331
  clone._id = this._id;
332
+ clone.shouldCompactAst = this.shouldCompactAst;
326
333
 
327
334
  return clone;
328
335
  }