clarity-pattern-parser 9.2.5 → 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) => {
@@ -191,6 +219,13 @@ class Node {
191
219
  toJson(space) {
192
220
  return JSON.stringify(this.toCycleFreeObject(), null, space);
193
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
+ }
194
229
  }
195
230
 
196
231
  /******************************************************************************
@@ -308,10 +343,10 @@ class CursorHistory {
308
343
  }
309
344
  }
310
345
  }
311
- recordErrorAt(firstIndex, lastIndex, pattern) {
312
- const error = new ParseError(firstIndex, lastIndex, pattern);
346
+ recordErrorAt(startIndex, endIndex, pattern) {
347
+ const error = new ParseError(startIndex, endIndex, pattern);
313
348
  this._currentError = error;
314
- if (this._furthestError === null || lastIndex > this._furthestError.endIndex) {
349
+ if (this._furthestError === null || endIndex > this._furthestError.endIndex) {
315
350
  this._furthestError = error;
316
351
  }
317
352
  if (this._isRecording) {
@@ -386,7 +421,7 @@ class Cursor {
386
421
  constructor(text) {
387
422
  this._text = text;
388
423
  this._index = 0;
389
- this._length = text.length;
424
+ this._length = [...text].length;
390
425
  this._history = new CursorHistory();
391
426
  this._stackTrace = [];
392
427
  }
@@ -426,8 +461,8 @@ class Cursor {
426
461
  recordMatch(pattern, node) {
427
462
  this._history.recordMatch(pattern, node);
428
463
  }
429
- recordErrorAt(firstIndex, lastIndex, onPattern) {
430
- this._history.recordErrorAt(firstIndex, lastIndex, onPattern);
464
+ recordErrorAt(startIndex, endIndex, onPattern) {
465
+ this._history.recordErrorAt(startIndex, endIndex, onPattern);
431
466
  }
432
467
  resolveError() {
433
468
  this._history.resolveError();
@@ -444,7 +479,8 @@ class Cursor {
444
479
  pattern,
445
480
  cursorIndex: this.index
446
481
  };
447
- 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) {
448
484
  throw new Error(`Cyclical Pattern: ${this._stackTrace.map(t => `${t.pattern.name}#${t.pattern.id}{${t.cursorIndex}}`).join(" -> ")} -> ${patternName}#${pattern.id}{${this.index}}.`);
449
485
  }
450
486
  this._history.pushStackTrace(trace);
@@ -480,8 +516,8 @@ class Literal {
480
516
  get name() {
481
517
  return this._name;
482
518
  }
483
- get value() {
484
- return this._text;
519
+ get token() {
520
+ return this._token;
485
521
  }
486
522
  get parent() {
487
523
  return this._parent;
@@ -499,15 +535,16 @@ class Literal {
499
535
  this._id = `literal-${idIndex$9++}`;
500
536
  this._type = "literal";
501
537
  this._name = name;
502
- this._text = value;
538
+ this._token = value;
503
539
  this._runes = Array.from(value);
504
540
  this._parent = null;
505
541
  this._firstIndex = 0;
506
542
  this._lastIndex = 0;
507
543
  this._endIndex = 0;
508
544
  }
509
- test(text) {
545
+ test(text, record = false) {
510
546
  const cursor = new Cursor(text);
547
+ record && cursor.startRecording();
511
548
  const ast = this.parse(cursor);
512
549
  return (ast === null || ast === void 0 ? void 0 : ast.value) === text;
513
550
  }
@@ -546,7 +583,7 @@ class Literal {
546
583
  break;
547
584
  }
548
585
  if (i + 1 === literalRuneLength) {
549
- this._lastIndex = this._firstIndex + this._text.length - 1;
586
+ this._lastIndex = this._firstIndex + this._token.length - 1;
550
587
  passed = true;
551
588
  break;
552
589
  }
@@ -559,15 +596,15 @@ class Literal {
559
596
  return passed;
560
597
  }
561
598
  _createNode() {
562
- 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);
563
600
  }
564
601
  clone(name = this._name) {
565
- const clone = new Literal(name, this._text);
602
+ const clone = new Literal(name, this._token);
566
603
  clone._id = this._id;
567
604
  return clone;
568
605
  }
569
606
  getTokens() {
570
- return [this._text];
607
+ return [this._token];
571
608
  }
572
609
  getTokensAfter(_lastMatched) {
573
610
  return [];
@@ -594,7 +631,7 @@ class Literal {
594
631
  return null;
595
632
  }
596
633
  isEqual(pattern) {
597
- return pattern.type === this.type && pattern._text === this._text;
634
+ return pattern.type === this.type && pattern._token === this._token;
598
635
  }
599
636
  }
600
637
 
@@ -2546,7 +2583,7 @@ class Grammar {
2546
2583
  return importBlock && importBlock.children.length > 0;
2547
2584
  }
2548
2585
  _buildPatterns(ast) {
2549
- 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);
2550
2587
  if (body == null) {
2551
2588
  return;
2552
2589
  }
@@ -2866,16 +2903,6 @@ class Grammar {
2866
2903
  }
2867
2904
  }
2868
2905
 
2869
- function arePatternsEqual(a, b) {
2870
- if (a === b) {
2871
- return true;
2872
- }
2873
- else if (a == null || b == null) {
2874
- return false;
2875
- }
2876
- return a.isEqual(b);
2877
- }
2878
-
2879
2906
  const kebabRegex = /-([a-z])/g; // Define the regex once
2880
2907
  function kebabToCamelCase(str) {
2881
2908
  return str.replace(kebabRegex, (_, char) => char.toUpperCase());
@@ -2890,5 +2917,5 @@ function patterns(strings, ...values) {
2890
2917
  return result;
2891
2918
  }
2892
2919
 
2893
- 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 };
2894
2921
  //# sourceMappingURL=index.esm.js.map