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.d.ts CHANGED
@@ -17,6 +17,5 @@ import { Reference } from "./patterns/Reference";
17
17
  import { CursorHistory, Match } from "./patterns/CursorHistory";
18
18
  import { ParseResult } from "./patterns/ParseResult";
19
19
  import { grammar } from "./grammar/patterns/grammar";
20
- import { arePatternsEqual } from "./patterns/arePatternsEqual";
21
20
  import { patterns } from "./grammar/patterns";
22
- export { Node, Grammar, AutoComplete, AutoCompleteOptions, Suggestion, SuggestionOption, Sequence, Cursor, CursorHistory, Match, Literal, Not, Options, Optional, ParseError, ParseResult, Pattern, Reference, Regex, Repeat, grammar, arePatternsEqual, patterns, };
21
+ export { Node, Grammar, AutoComplete, AutoCompleteOptions, Suggestion, SuggestionOption, Sequence, Cursor, CursorHistory, Match, Literal, Not, Options, Optional, ParseError, ParseResult, Pattern, Reference, Regex, Repeat, grammar, patterns, };
package/dist/index.esm.js CHANGED
@@ -1,3 +1,6 @@
1
+ function defaultVisitor(node) {
2
+ return node;
3
+ }
1
4
  class Node {
2
5
  get type() {
3
6
  return this._type;
@@ -26,6 +29,9 @@ class Node {
26
29
  get hasChildren() {
27
30
  return this._children.length > 0;
28
31
  }
32
+ get isLeaf() {
33
+ return !this.hasChildren;
34
+ }
29
35
  get value() {
30
36
  return this.toString();
31
37
  }
@@ -46,16 +52,22 @@ class Node {
46
52
  node._parent = null;
47
53
  }
48
54
  }
55
+ findChildIndex(node) {
56
+ return this._children.indexOf(node);
57
+ }
58
+ spliceChildren(index, deleteCount, ...items) {
59
+ const removedItems = this._children.splice(index, deleteCount, ...items);
60
+ removedItems.forEach(i => i._parent = null);
61
+ items.forEach(i => i._parent = this);
62
+ return removedItems;
63
+ }
49
64
  removeAllChildren() {
50
- this._children.forEach(c => c._parent = null);
51
- this._children.length = 0;
65
+ this.spliceChildren(0, this._children.length);
52
66
  }
53
67
  replaceChild(newNode, referenceNode) {
54
- const index = this._children.indexOf(referenceNode);
68
+ const index = this.findChildIndex(referenceNode);
55
69
  if (index > -1) {
56
- this._children.splice(index, 1, newNode);
57
- newNode._parent = this;
58
- referenceNode._parent = null;
70
+ this.spliceChildren(index, 1, newNode);
59
71
  }
60
72
  }
61
73
  replaceWith(newNode) {
@@ -69,20 +81,19 @@ class Node {
69
81
  this._children.push(newNode);
70
82
  return;
71
83
  }
72
- const index = this._children.indexOf(referenceNode);
84
+ const index = this.findChildIndex(referenceNode);
73
85
  if (index > -1) {
74
86
  this._children.splice(index, 0, newNode);
75
87
  }
76
88
  }
77
89
  appendChild(newNode) {
78
- newNode._parent = this;
79
- this._children.push(newNode);
90
+ this.append(newNode);
80
91
  }
81
- spliceChildren(index, deleteCount, ...items) {
82
- const removedItems = this._children.splice(index, deleteCount, ...items);
83
- removedItems.forEach(i => i._parent = null);
84
- items.forEach(i => i._parent = this);
85
- return removedItems;
92
+ append(...nodes) {
93
+ nodes.forEach((newNode) => {
94
+ newNode._parent = this;
95
+ this._children.push(newNode);
96
+ });
86
97
  }
87
98
  nextSibling() {
88
99
  if (this._parent == null) {
@@ -118,7 +129,7 @@ class Node {
118
129
  });
119
130
  return matches;
120
131
  }
121
- findAncester(predicate) {
132
+ findAncestor(predicate) {
122
133
  let parent = this._parent;
123
134
  while (parent != null) {
124
135
  if (predicate(parent)) {
@@ -138,6 +149,23 @@ class Node {
138
149
  callback(this);
139
150
  childrenCopy.forEach(c => c.walkDown(callback));
140
151
  }
152
+ walkBreadthFirst(callback) {
153
+ const queue = [this];
154
+ while (queue.length > 0) {
155
+ // biome-ignore lint/style/noNonNullAssertion: This will never be undefined.
156
+ const current = queue.shift();
157
+ callback(current);
158
+ queue.push(...current.children);
159
+ }
160
+ }
161
+ transform(visitors) {
162
+ const childrenCopy = this._children.slice();
163
+ const visitor = visitors[this.name] == null ? defaultVisitor : visitors[this.name];
164
+ const children = childrenCopy.map(c => c.transform(visitors));
165
+ this.removeAllChildren();
166
+ this.append(...children);
167
+ return visitor(this);
168
+ }
141
169
  flatten() {
142
170
  const nodes = [];
143
171
  this.walkDown((node) => {
@@ -183,8 +211,6 @@ class Node {
183
211
  type: this._type,
184
212
  name: this._name,
185
213
  value: this.toString(),
186
- firstIndex: this._firstIndex,
187
- lastIndex: this._lastIndex,
188
214
  startIndex: this.startIndex,
189
215
  endIndex: this.endIndex,
190
216
  children: this._children.map(c => c.toCycleFreeObject()),
@@ -193,6 +219,13 @@ class Node {
193
219
  toJson(space) {
194
220
  return JSON.stringify(this.toCycleFreeObject(), null, space);
195
221
  }
222
+ static createValueNode(name, value) {
223
+ return new Node("custom-value-node", name, 0, 0, [], value);
224
+ }
225
+ static createNode(name, children) {
226
+ const value = children.map(c => c.toString()).join("");
227
+ return new Node("custom-node", name, 0, 0, children, value);
228
+ }
196
229
  }
197
230
 
198
231
  /******************************************************************************
@@ -310,10 +343,10 @@ class CursorHistory {
310
343
  }
311
344
  }
312
345
  }
313
- recordErrorAt(firstIndex, lastIndex, pattern) {
314
- const error = new ParseError(firstIndex, lastIndex, pattern);
346
+ recordErrorAt(startIndex, endIndex, pattern) {
347
+ const error = new ParseError(startIndex, endIndex, pattern);
315
348
  this._currentError = error;
316
- if (this._furthestError === null || lastIndex > this._furthestError.endIndex) {
349
+ if (this._furthestError === null || endIndex > this._furthestError.endIndex) {
317
350
  this._furthestError = error;
318
351
  }
319
352
  if (this._isRecording) {
@@ -388,7 +421,7 @@ class Cursor {
388
421
  constructor(text) {
389
422
  this._text = text;
390
423
  this._index = 0;
391
- this._length = text.length;
424
+ this._length = [...text].length;
392
425
  this._history = new CursorHistory();
393
426
  this._stackTrace = [];
394
427
  }
@@ -428,8 +461,8 @@ class Cursor {
428
461
  recordMatch(pattern, node) {
429
462
  this._history.recordMatch(pattern, node);
430
463
  }
431
- recordErrorAt(firstIndex, lastIndex, onPattern) {
432
- this._history.recordErrorAt(firstIndex, lastIndex, onPattern);
464
+ recordErrorAt(startIndex, endIndex, onPattern) {
465
+ this._history.recordErrorAt(startIndex, endIndex, onPattern);
433
466
  }
434
467
  resolveError() {
435
468
  this._history.resolveError();
@@ -446,7 +479,8 @@ class Cursor {
446
479
  pattern,
447
480
  cursorIndex: this.index
448
481
  };
449
- if (this._stackTrace.find(t => t.pattern.id === pattern.id && this.index === t.cursorIndex)) {
482
+ const hasCycle = this._stackTrace.find(t => t.pattern.id === pattern.id && this.index === t.cursorIndex);
483
+ if (hasCycle) {
450
484
  throw new Error(`Cyclical Pattern: ${this._stackTrace.map(t => `${t.pattern.name}#${t.pattern.id}{${t.cursorIndex}}`).join(" -> ")} -> ${patternName}#${pattern.id}{${this.index}}.`);
451
485
  }
452
486
  this._history.pushStackTrace(trace);
@@ -482,8 +516,8 @@ class Literal {
482
516
  get name() {
483
517
  return this._name;
484
518
  }
485
- get value() {
486
- return this._text;
519
+ get token() {
520
+ return this._token;
487
521
  }
488
522
  get parent() {
489
523
  return this._parent;
@@ -501,15 +535,16 @@ class Literal {
501
535
  this._id = `literal-${idIndex$9++}`;
502
536
  this._type = "literal";
503
537
  this._name = name;
504
- this._text = value;
538
+ this._token = value;
505
539
  this._runes = Array.from(value);
506
540
  this._parent = null;
507
541
  this._firstIndex = 0;
508
542
  this._lastIndex = 0;
509
543
  this._endIndex = 0;
510
544
  }
511
- test(text) {
545
+ test(text, record = false) {
512
546
  const cursor = new Cursor(text);
547
+ record && cursor.startRecording();
513
548
  const ast = this.parse(cursor);
514
549
  return (ast === null || ast === void 0 ? void 0 : ast.value) === text;
515
550
  }
@@ -548,7 +583,7 @@ class Literal {
548
583
  break;
549
584
  }
550
585
  if (i + 1 === literalRuneLength) {
551
- this._lastIndex = this._firstIndex + this._text.length - 1;
586
+ this._lastIndex = this._firstIndex + this._token.length - 1;
552
587
  passed = true;
553
588
  break;
554
589
  }
@@ -561,15 +596,15 @@ class Literal {
561
596
  return passed;
562
597
  }
563
598
  _createNode() {
564
- return new Node("literal", this._name, this._firstIndex, this._lastIndex, undefined, this._text);
599
+ return new Node("literal", this._name, this._firstIndex, this._lastIndex, undefined, this._token);
565
600
  }
566
601
  clone(name = this._name) {
567
- const clone = new Literal(name, this._text);
602
+ const clone = new Literal(name, this._token);
568
603
  clone._id = this._id;
569
604
  return clone;
570
605
  }
571
606
  getTokens() {
572
- return [this._text];
607
+ return [this._token];
573
608
  }
574
609
  getTokensAfter(_lastMatched) {
575
610
  return [];
@@ -596,7 +631,7 @@ class Literal {
596
631
  return null;
597
632
  }
598
633
  isEqual(pattern) {
599
- return pattern.type === this.type && pattern._text === this._text;
634
+ return pattern.type === this.type && pattern._token === this._token;
600
635
  }
601
636
  }
602
637
 
@@ -2548,7 +2583,7 @@ class Grammar {
2548
2583
  return importBlock && importBlock.children.length > 0;
2549
2584
  }
2550
2585
  _buildPatterns(ast) {
2551
- const body = ast.find(n => n.name === "body" && n.findAncester(n => n.name === "head") == null);
2586
+ const body = ast.find(n => n.name === "body" && n.findAncestor(n => n.name === "head") == null);
2552
2587
  if (body == null) {
2553
2588
  return;
2554
2589
  }
@@ -2868,16 +2903,6 @@ class Grammar {
2868
2903
  }
2869
2904
  }
2870
2905
 
2871
- function arePatternsEqual(a, b) {
2872
- if (a === b) {
2873
- return true;
2874
- }
2875
- else if (a == null || b == null) {
2876
- return false;
2877
- }
2878
- return a.isEqual(b);
2879
- }
2880
-
2881
2906
  const kebabRegex = /-([a-z])/g; // Define the regex once
2882
2907
  function kebabToCamelCase(str) {
2883
2908
  return str.replace(kebabRegex, (_, char) => char.toUpperCase());
@@ -2892,5 +2917,5 @@ function patterns(strings, ...values) {
2892
2917
  return result;
2893
2918
  }
2894
2919
 
2895
- export { AutoComplete, Cursor, CursorHistory, Grammar, Literal, Node, Not, Optional, Options, ParseError, Reference, Regex, Repeat, Sequence, arePatternsEqual, grammar, patterns };
2920
+ export { AutoComplete, Cursor, CursorHistory, Grammar, Literal, Node, Not, Optional, Options, ParseError, Reference, Regex, Repeat, Sequence, grammar, patterns };
2896
2921
  //# sourceMappingURL=index.esm.js.map