clarity-pattern-parser 10.2.4 → 10.2.5

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.
@@ -14,6 +14,7 @@ export declare class ExpressionPattern implements Pattern {
14
14
  private _binaryPatterns;
15
15
  private _recursivePatterns;
16
16
  private _recursiveNames;
17
+ private _endsInRecursion;
17
18
  private _binaryAssociation;
18
19
  private _precedenceMap;
19
20
  private _binaryNames;
@@ -35,6 +36,7 @@ export declare class ExpressionPattern implements Pattern {
35
36
  private _isRecursive;
36
37
  private _isRecursivePattern;
37
38
  private _extractRecursiveTail;
39
+ private _endsWithRecursion;
38
40
  parse(cursor: Cursor): Node | null;
39
41
  private _tryToParse;
40
42
  test(text: string): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-pattern-parser",
3
- "version": "10.2.4",
3
+ "version": "10.2.5",
4
4
  "description": "Parsing Library for Typescript and Javascript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -30,6 +30,7 @@ export class ExpressionPattern implements Pattern {
30
30
  private _binaryPatterns: Pattern[];
31
31
  private _recursivePatterns: Pattern[];
32
32
  private _recursiveNames: string[];
33
+ private _endsInRecursion: boolean[];
33
34
  private _binaryAssociation: Association[];
34
35
  private _precedenceMap: Record<string, number>;
35
36
  private _binaryNames: string[];
@@ -84,6 +85,7 @@ export class ExpressionPattern implements Pattern {
84
85
  this._binaryPatterns = [];
85
86
  this._recursivePatterns = [];
86
87
  this._recursiveNames = [];
88
+ this._endsInRecursion = [];
87
89
  this._binaryNames = [];
88
90
  this._binaryAssociation = [];
89
91
  this._precedenceMap = {};
@@ -121,6 +123,7 @@ export class ExpressionPattern implements Pattern {
121
123
 
122
124
  this._recursivePatterns.push(tail);
123
125
  this._recursiveNames.push(name);
126
+ this._endsInRecursion.push(this._endsWithRecursion(pattern));
124
127
  finalPatterns.push(tail);
125
128
  } else {
126
129
  const clone = pattern.clone();
@@ -188,6 +191,18 @@ export class ExpressionPattern implements Pattern {
188
191
  return new Sequence(`${pattern.name}-tail`, pattern.children.slice(1));
189
192
  }
190
193
 
194
+ private _endsWithRecursion(pattern: Pattern) {
195
+ if (pattern.type === "right-associated") {
196
+ pattern = pattern.children[0];
197
+ }
198
+
199
+ const lastChild = pattern.children[pattern.children.length - 1];
200
+ return pattern.type === "sequence" &&
201
+ pattern.children.length > 1 &&
202
+ lastChild.type === "reference" &&
203
+ lastChild.name === this.name;
204
+ }
205
+
191
206
  parse(cursor: Cursor): Node | null {
192
207
  // This is a cache to help with speed
193
208
  this._firstIndex = cursor.index;
@@ -214,11 +229,11 @@ export class ExpressionPattern implements Pattern {
214
229
  return null;
215
230
  }
216
231
 
217
-
232
+
218
233
  let lastUnaryNode: Node | null = null;
219
234
  let lastBinaryNode: Node | null = null;
220
235
  let onIndex = cursor.index;
221
-
236
+
222
237
  outer: while (true) {
223
238
  cursor.resolveError();
224
239
  onIndex = cursor.index;
@@ -261,13 +276,22 @@ export class ExpressionPattern implements Pattern {
261
276
  if (lastBinaryNode != null && lastUnaryNode != null) {
262
277
  lastBinaryNode.appendChild(lastUnaryNode);
263
278
  }
264
-
265
- const frontExpression = lastBinaryNode == null ? lastUnaryNode as Node : lastBinaryNode.findRoot();
279
+
266
280
  const name = this._recursiveNames[i];
267
- const recursiveNode = createNode(name, [frontExpression, ...node.children]);
268
281
 
269
- recursiveNode.normalize(this._firstIndex);
270
- return recursiveNode;
282
+ if (this._endsInRecursion[i]) {
283
+ const frontExpression = lastBinaryNode == null ? lastUnaryNode as Node : lastBinaryNode.findRoot();
284
+ const recursiveNode = createNode(name, [frontExpression, ...node.children]);
285
+
286
+ recursiveNode.normalize(this._firstIndex);
287
+
288
+ return recursiveNode;
289
+ } else {
290
+ const recursiveNode = createNode(name, [lastUnaryNode, ...node.children]);
291
+ recursiveNode.normalize(this._firstIndex);
292
+ lastUnaryNode = recursiveNode;
293
+ break;
294
+ }
271
295
  }
272
296
 
273
297
  cursor.moveTo(onIndex);
@@ -298,7 +322,7 @@ export class ExpressionPattern implements Pattern {
298
322
  lastBinaryNode = node;
299
323
  } else if (lastBinaryNode != null && lastUnaryNode != null && delimiterNode != null) {
300
324
  const precedence = this._precedenceMap[name];
301
- const lastPrecendece = lastBinaryNode == null ? 0 : this._precedenceMap[lastBinaryNode.name];
325
+ const lastPrecendece = lastBinaryNode == null ? 0 : this._precedenceMap[lastBinaryNode.name] || -1;
302
326
  const association = this._binaryAssociation[i];
303
327
 
304
328
  if (precedence === lastPrecendece && association === Association.right) {