clarity-pattern-parser 11.2.2 → 11.3.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.
package/dist/index.js CHANGED
@@ -5,7 +5,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  function defaultVisitor(node) {
6
6
  return node;
7
7
  }
8
+ let idIndex$b = 0;
8
9
  class Node {
10
+ get id() {
11
+ return this._id;
12
+ }
9
13
  get type() {
10
14
  return this._type;
11
15
  }
@@ -40,6 +44,7 @@ class Node {
40
44
  return !this.hasChildren;
41
45
  }
42
46
  constructor(type, name, firstIndex, lastIndex, children = [], value = "") {
47
+ this._id = String(idIndex$b++);
43
48
  this._type = type;
44
49
  this._name = name;
45
50
  this._firstIndex = firstIndex;
@@ -200,7 +205,8 @@ class Node {
200
205
  }
201
206
  }
202
207
  clone() {
203
- return new Node(this._type, this._name, this._firstIndex, this._lastIndex, this._children.map((c) => c.clone()), this._value);
208
+ const node = new Node(this._type, this._name, this._firstIndex, this._lastIndex, this._children.map((c) => c.clone()), this._value);
209
+ return node;
204
210
  }
205
211
  normalize(startIndex = this._firstIndex) {
206
212
  let length = 0;
@@ -228,6 +234,7 @@ class Node {
228
234
  }
229
235
  toCycleFreeObject() {
230
236
  return {
237
+ id: this._id,
231
238
  type: this._type,
232
239
  name: this._name,
233
240
  value: this.toString(),
@@ -240,7 +247,12 @@ class Node {
240
247
  return JSON.stringify(this.toCycleFreeObject(), null, space);
241
248
  }
242
249
  isEqual(node) {
243
- return node.toJson(0) === this.toJson(0);
250
+ return node._type === this._type &&
251
+ node._name === this._name &&
252
+ node._firstIndex === this._firstIndex &&
253
+ node._lastIndex === this._lastIndex &&
254
+ node._value === this._value &&
255
+ this._children.every((child, index) => child.isEqual(node._children[index]));
244
256
  }
245
257
  static createValueNode(type, name, value = "") {
246
258
  return new Node(type, name, 0, 0, [], value);
@@ -352,14 +364,15 @@ class CursorHistory {
352
364
  return this._patterns;
353
365
  }
354
366
  recordMatch(pattern, node) {
367
+ const record = {
368
+ pattern,
369
+ ast: node,
370
+ error: null
371
+ };
355
372
  if (this._isRecording) {
356
373
  this._patterns.push(pattern);
357
374
  this._nodes.push(node);
358
- this._records.push({
359
- pattern,
360
- ast: node,
361
- error: null
362
- });
375
+ this._records.push(record);
363
376
  }
364
377
  this._rootMatch.pattern = pattern;
365
378
  this._rootMatch.node = node;
@@ -393,28 +406,29 @@ class CursorHistory {
393
406
  }
394
407
  recordErrorAt(startIndex, lastIndex, pattern) {
395
408
  const error = new ParseError(startIndex, lastIndex, pattern);
409
+ const record = {
410
+ pattern,
411
+ ast: null,
412
+ error
413
+ };
396
414
  this._currentError = error;
397
415
  if (this._furthestError === null || lastIndex > this._furthestError.lastIndex) {
398
416
  this._furthestError = error;
399
417
  }
400
418
  if (this._isRecording) {
401
419
  this._errors.push(error);
402
- this.records.push({
403
- pattern,
404
- ast: null,
405
- error
406
- });
420
+ this.records.push(record);
407
421
  }
408
422
  }
423
+ resolveError() {
424
+ this._currentError = null;
425
+ }
409
426
  startRecording() {
410
427
  this._isRecording = true;
411
428
  }
412
429
  stopRecording() {
413
430
  this._isRecording = false;
414
431
  }
415
- resolveError() {
416
- this._currentError = null;
417
- }
418
432
  }
419
433
 
420
434
  class Cursor {
@@ -511,8 +525,8 @@ class Cursor {
511
525
  recordMatch(pattern, node) {
512
526
  this._history.recordMatch(pattern, node);
513
527
  }
514
- recordErrorAt(startIndex, endIndex, onPattern) {
515
- this._history.recordErrorAt(startIndex, endIndex, onPattern);
528
+ recordErrorAt(startIndex, lastIndex, onPattern) {
529
+ this._history.recordErrorAt(startIndex, lastIndex, onPattern);
516
530
  }
517
531
  resolveError() {
518
532
  this._history.resolveError();
@@ -896,7 +910,7 @@ class Reference {
896
910
  for (let pattern of this._recursiveAncestors) {
897
911
  if (pattern.startedOnIndex === this.startedOnIndex) {
898
912
  depth++;
899
- if (depth > 0) {
913
+ if (depth > 1) {
900
914
  return true;
901
915
  }
902
916
  }
@@ -3237,7 +3251,9 @@ class TakeUntil {
3237
3251
  if (foundMatch) {
3238
3252
  cursor.moveTo(cursorIndex - 1);
3239
3253
  const value = cursor.getChars(this.startedOnIndex, cursorIndex - 1);
3240
- return Node.createValueNode(this._type, this._name, value);
3254
+ const node = Node.createValueNode(this._type, this._name, value);
3255
+ cursor.recordMatch(this, node);
3256
+ return node;
3241
3257
  }
3242
3258
  else {
3243
3259
  cursor.moveTo(this.startedOnIndex);