clarity-pattern-parser 9.2.4 → 10.0.0

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
@@ -2,6 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ function defaultVisitor(node) {
6
+ return node;
7
+ }
5
8
  class Node {
6
9
  get type() {
7
10
  return this._type;
@@ -30,6 +33,9 @@ class Node {
30
33
  get hasChildren() {
31
34
  return this._children.length > 0;
32
35
  }
36
+ get isLeaf() {
37
+ return !this.hasChildren;
38
+ }
33
39
  get value() {
34
40
  return this.toString();
35
41
  }
@@ -50,16 +56,22 @@ class Node {
50
56
  node._parent = null;
51
57
  }
52
58
  }
59
+ findChildIndex(node) {
60
+ return this._children.indexOf(node);
61
+ }
62
+ spliceChildren(index, deleteCount, ...items) {
63
+ const removedItems = this._children.splice(index, deleteCount, ...items);
64
+ removedItems.forEach(i => i._parent = null);
65
+ items.forEach(i => i._parent = this);
66
+ return removedItems;
67
+ }
53
68
  removeAllChildren() {
54
- this._children.forEach(c => c._parent = null);
55
- this._children.length = 0;
69
+ this.spliceChildren(0, this._children.length);
56
70
  }
57
71
  replaceChild(newNode, referenceNode) {
58
- const index = this._children.indexOf(referenceNode);
72
+ const index = this.findChildIndex(referenceNode);
59
73
  if (index > -1) {
60
- this._children.splice(index, 1, newNode);
61
- newNode._parent = this;
62
- referenceNode._parent = null;
74
+ this.spliceChildren(index, 1, newNode);
63
75
  }
64
76
  }
65
77
  replaceWith(newNode) {
@@ -73,20 +85,19 @@ class Node {
73
85
  this._children.push(newNode);
74
86
  return;
75
87
  }
76
- const index = this._children.indexOf(referenceNode);
88
+ const index = this.findChildIndex(referenceNode);
77
89
  if (index > -1) {
78
90
  this._children.splice(index, 0, newNode);
79
91
  }
80
92
  }
81
93
  appendChild(newNode) {
82
- newNode._parent = this;
83
- this._children.push(newNode);
94
+ this.append(newNode);
84
95
  }
85
- spliceChildren(index, deleteCount, ...items) {
86
- const removedItems = this._children.splice(index, deleteCount, ...items);
87
- removedItems.forEach(i => i._parent = null);
88
- items.forEach(i => i._parent = this);
89
- return removedItems;
96
+ append(...nodes) {
97
+ nodes.forEach((newNode) => {
98
+ newNode._parent = this;
99
+ this._children.push(newNode);
100
+ });
90
101
  }
91
102
  nextSibling() {
92
103
  if (this._parent == null) {
@@ -122,7 +133,7 @@ class Node {
122
133
  });
123
134
  return matches;
124
135
  }
125
- findAncester(predicate) {
136
+ findAncestor(predicate) {
126
137
  let parent = this._parent;
127
138
  while (parent != null) {
128
139
  if (predicate(parent)) {
@@ -142,6 +153,23 @@ class Node {
142
153
  callback(this);
143
154
  childrenCopy.forEach(c => c.walkDown(callback));
144
155
  }
156
+ walkBreadthFirst(callback) {
157
+ const queue = [this];
158
+ while (queue.length > 0) {
159
+ // biome-ignore lint/style/noNonNullAssertion: This will never be undefined.
160
+ const current = queue.shift();
161
+ callback(current);
162
+ queue.push(...current.children);
163
+ }
164
+ }
165
+ transform(visitors) {
166
+ const childrenCopy = this._children.slice();
167
+ const visitor = visitors[this.name] == null ? defaultVisitor : visitors[this.name];
168
+ const children = childrenCopy.map(c => c.transform(visitors));
169
+ this.removeAllChildren();
170
+ this.append(...children);
171
+ return visitor(this);
172
+ }
145
173
  flatten() {
146
174
  const nodes = [];
147
175
  this.walkDown((node) => {
@@ -187,8 +215,6 @@ class Node {
187
215
  type: this._type,
188
216
  name: this._name,
189
217
  value: this.toString(),
190
- firstIndex: this._firstIndex,
191
- lastIndex: this._lastIndex,
192
218
  startIndex: this.startIndex,
193
219
  endIndex: this.endIndex,
194
220
  children: this._children.map(c => c.toCycleFreeObject()),
@@ -197,6 +223,13 @@ class Node {
197
223
  toJson(space) {
198
224
  return JSON.stringify(this.toCycleFreeObject(), null, space);
199
225
  }
226
+ static createValueNode(name, value) {
227
+ return new Node("custom-value-node", name, 0, 0, [], value);
228
+ }
229
+ static createNode(name, children) {
230
+ const value = children.map(c => c.toString()).join("");
231
+ return new Node("custom-node", name, 0, 0, children, value);
232
+ }
200
233
  }
201
234
 
202
235
  /******************************************************************************
@@ -314,10 +347,10 @@ class CursorHistory {
314
347
  }
315
348
  }
316
349
  }
317
- recordErrorAt(firstIndex, lastIndex, pattern) {
318
- const error = new ParseError(firstIndex, lastIndex, pattern);
350
+ recordErrorAt(startIndex, endIndex, pattern) {
351
+ const error = new ParseError(startIndex, endIndex, pattern);
319
352
  this._currentError = error;
320
- if (this._furthestError === null || lastIndex > this._furthestError.endIndex) {
353
+ if (this._furthestError === null || endIndex > this._furthestError.endIndex) {
321
354
  this._furthestError = error;
322
355
  }
323
356
  if (this._isRecording) {
@@ -392,7 +425,7 @@ class Cursor {
392
425
  constructor(text) {
393
426
  this._text = text;
394
427
  this._index = 0;
395
- this._length = text.length;
428
+ this._length = [...text].length;
396
429
  this._history = new CursorHistory();
397
430
  this._stackTrace = [];
398
431
  }
@@ -432,8 +465,8 @@ class Cursor {
432
465
  recordMatch(pattern, node) {
433
466
  this._history.recordMatch(pattern, node);
434
467
  }
435
- recordErrorAt(firstIndex, lastIndex, onPattern) {
436
- this._history.recordErrorAt(firstIndex, lastIndex, onPattern);
468
+ recordErrorAt(startIndex, endIndex, onPattern) {
469
+ this._history.recordErrorAt(startIndex, endIndex, onPattern);
437
470
  }
438
471
  resolveError() {
439
472
  this._history.resolveError();
@@ -450,7 +483,8 @@ class Cursor {
450
483
  pattern,
451
484
  cursorIndex: this.index
452
485
  };
453
- if (this._stackTrace.find(t => t.pattern.id === pattern.id && this.index === t.cursorIndex)) {
486
+ const hasCycle = this._stackTrace.find(t => t.pattern.id === pattern.id && this.index === t.cursorIndex);
487
+ if (hasCycle) {
454
488
  throw new Error(`Cyclical Pattern: ${this._stackTrace.map(t => `${t.pattern.name}#${t.pattern.id}{${t.cursorIndex}}`).join(" -> ")} -> ${patternName}#${pattern.id}{${this.index}}.`);
455
489
  }
456
490
  this._history.pushStackTrace(trace);
@@ -486,8 +520,8 @@ class Literal {
486
520
  get name() {
487
521
  return this._name;
488
522
  }
489
- get value() {
490
- return this._text;
523
+ get token() {
524
+ return this._token;
491
525
  }
492
526
  get parent() {
493
527
  return this._parent;
@@ -505,15 +539,16 @@ class Literal {
505
539
  this._id = `literal-${idIndex$9++}`;
506
540
  this._type = "literal";
507
541
  this._name = name;
508
- this._text = value;
542
+ this._token = value;
509
543
  this._runes = Array.from(value);
510
544
  this._parent = null;
511
545
  this._firstIndex = 0;
512
546
  this._lastIndex = 0;
513
547
  this._endIndex = 0;
514
548
  }
515
- test(text) {
549
+ test(text, record = false) {
516
550
  const cursor = new Cursor(text);
551
+ record && cursor.startRecording();
517
552
  const ast = this.parse(cursor);
518
553
  return (ast === null || ast === void 0 ? void 0 : ast.value) === text;
519
554
  }
@@ -552,7 +587,7 @@ class Literal {
552
587
  break;
553
588
  }
554
589
  if (i + 1 === literalRuneLength) {
555
- this._lastIndex = this._firstIndex + this._text.length - 1;
590
+ this._lastIndex = this._firstIndex + this._token.length - 1;
556
591
  passed = true;
557
592
  break;
558
593
  }
@@ -565,15 +600,15 @@ class Literal {
565
600
  return passed;
566
601
  }
567
602
  _createNode() {
568
- return new Node("literal", this._name, this._firstIndex, this._lastIndex, undefined, this._text);
603
+ return new Node("literal", this._name, this._firstIndex, this._lastIndex, undefined, this._token);
569
604
  }
570
605
  clone(name = this._name) {
571
- const clone = new Literal(name, this._text);
606
+ const clone = new Literal(name, this._token);
572
607
  clone._id = this._id;
573
608
  return clone;
574
609
  }
575
610
  getTokens() {
576
- return [this._text];
611
+ return [this._token];
577
612
  }
578
613
  getTokensAfter(_lastMatched) {
579
614
  return [];
@@ -600,7 +635,7 @@ class Literal {
600
635
  return null;
601
636
  }
602
637
  isEqual(pattern) {
603
- return pattern.type === this.type && pattern._text === this._text;
638
+ return pattern.type === this.type && pattern._token === this._token;
604
639
  }
605
640
  }
606
641
 
@@ -2552,7 +2587,7 @@ class Grammar {
2552
2587
  return importBlock && importBlock.children.length > 0;
2553
2588
  }
2554
2589
  _buildPatterns(ast) {
2555
- const body = ast.find(n => n.name === "body" && n.findAncester(n => n.name === "head") == null);
2590
+ const body = ast.find(n => n.name === "body" && n.findAncestor(n => n.name === "head") == null);
2556
2591
  if (body == null) {
2557
2592
  return;
2558
2593
  }
@@ -2872,16 +2907,6 @@ class Grammar {
2872
2907
  }
2873
2908
  }
2874
2909
 
2875
- function arePatternsEqual(a, b) {
2876
- if (a === b) {
2877
- return true;
2878
- }
2879
- else if (a == null || b == null) {
2880
- return false;
2881
- }
2882
- return a.isEqual(b);
2883
- }
2884
-
2885
2910
  const kebabRegex = /-([a-z])/g; // Define the regex once
2886
2911
  function kebabToCamelCase(str) {
2887
2912
  return str.replace(kebabRegex, (_, char) => char.toUpperCase());
@@ -2910,7 +2935,6 @@ exports.Reference = Reference;
2910
2935
  exports.Regex = Regex;
2911
2936
  exports.Repeat = Repeat;
2912
2937
  exports.Sequence = Sequence;
2913
- exports.arePatternsEqual = arePatternsEqual;
2914
2938
  exports.grammar = grammar;
2915
2939
  exports.patterns = patterns;
2916
2940
  //# sourceMappingURL=index.js.map