clarity-pattern-parser 10.2.3 → 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.3",
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,13 @@ export class ExpressionPattern implements Pattern {
214
229
  return null;
215
230
  }
216
231
 
232
+
217
233
  let lastUnaryNode: Node | null = null;
218
234
  let lastBinaryNode: Node | null = null;
219
235
  let onIndex = cursor.index;
220
236
 
221
237
  outer: while (true) {
238
+ cursor.resolveError();
222
239
  onIndex = cursor.index;
223
240
 
224
241
  for (let i = 0; i < this._unaryPatterns.length; i++) {
@@ -259,18 +276,28 @@ export class ExpressionPattern implements Pattern {
259
276
  if (lastBinaryNode != null && lastUnaryNode != null) {
260
277
  lastBinaryNode.appendChild(lastUnaryNode);
261
278
  }
262
-
263
- const frontExpression = lastBinaryNode == null ? lastUnaryNode as Node : lastBinaryNode.findRoot();
279
+
264
280
  const name = this._recursiveNames[i];
265
- const recursiveNode = createNode(name, [frontExpression, ...node.children]);
266
281
 
267
- recursiveNode.normalize(this._firstIndex);
268
- 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
+ }
269
295
  }
270
296
 
271
297
  cursor.moveTo(onIndex);
272
298
  }
273
299
 
300
+ cursor.resolveError();
274
301
  onIndex = cursor.index;
275
302
  for (let i = 0; i < this._binaryPatterns.length; i++) {
276
303
  cursor.moveTo(onIndex);
@@ -295,7 +322,7 @@ export class ExpressionPattern implements Pattern {
295
322
  lastBinaryNode = node;
296
323
  } else if (lastBinaryNode != null && lastUnaryNode != null && delimiterNode != null) {
297
324
  const precedence = this._precedenceMap[name];
298
- const lastPrecendece = lastBinaryNode == null ? 0 : this._precedenceMap[lastBinaryNode.name];
325
+ const lastPrecendece = lastBinaryNode == null ? 0 : this._precedenceMap[lastBinaryNode.name] || -1;
299
326
  const association = this._binaryAssociation[i];
300
327
 
301
328
  if (precedence === lastPrecendece && association === Association.right) {