clarity-pattern-parser 9.2.5 → 10.0.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/ast/Node.d.ts +9 -2
- package/dist/index.browser.js +70 -44
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.esm.js +71 -44
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +70 -44
- package/dist/index.js.map +1 -1
- package/dist/patterns/Cursor.d.ts +1 -1
- package/dist/patterns/CursorHistory.d.ts +1 -1
- package/dist/patterns/Literal.d.ts +3 -3
- package/dist/patterns/Pattern.d.ts +1 -1
- package/dist/patterns/Regex.d.ts +1 -1
- package/dist/types.d.ts +797 -0
- package/package.json +1 -1
- package/src/ast/Node.test.ts +60 -24
- package/src/ast/Node.ts +75 -28
- package/src/grammar/Grammar.test.ts +26 -25
- package/src/grammar/Grammar.ts +1 -1
- package/src/index.ts +0 -2
- package/src/patterns/Cursor.ts +5 -4
- package/src/patterns/CursorHistory.ts +3 -3
- package/src/patterns/FiniteRepeat.test.ts +4 -5
- package/src/patterns/InfiniteRepeat.test.ts +1 -2
- package/src/patterns/Literal.ts +12 -11
- package/src/patterns/ParseError.ts +1 -1
- package/src/patterns/Pattern.ts +1 -1
- package/src/patterns/Regex.ts +1 -1
- package/src/patterns/Repeat.test.ts +2 -3
- package/src/patterns/arePatternsEqual.ts +0 -12
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.
|
|
55
|
-
this._children.length = 0;
|
|
69
|
+
this.spliceChildren(0, this._children.length);
|
|
56
70
|
}
|
|
57
71
|
replaceChild(newNode, referenceNode) {
|
|
58
|
-
const index = this.
|
|
72
|
+
const index = this.findChildIndex(referenceNode);
|
|
59
73
|
if (index > -1) {
|
|
60
|
-
this.
|
|
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.
|
|
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
|
|
83
|
-
this._children.push(newNode);
|
|
94
|
+
this.append(newNode);
|
|
84
95
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
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) => {
|
|
@@ -195,6 +223,13 @@ class Node {
|
|
|
195
223
|
toJson(space) {
|
|
196
224
|
return JSON.stringify(this.toCycleFreeObject(), null, space);
|
|
197
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
|
+
}
|
|
198
233
|
}
|
|
199
234
|
|
|
200
235
|
/******************************************************************************
|
|
@@ -312,10 +347,10 @@ class CursorHistory {
|
|
|
312
347
|
}
|
|
313
348
|
}
|
|
314
349
|
}
|
|
315
|
-
recordErrorAt(
|
|
316
|
-
const error = new ParseError(
|
|
350
|
+
recordErrorAt(startIndex, endIndex, pattern) {
|
|
351
|
+
const error = new ParseError(startIndex, endIndex, pattern);
|
|
317
352
|
this._currentError = error;
|
|
318
|
-
if (this._furthestError === null ||
|
|
353
|
+
if (this._furthestError === null || endIndex > this._furthestError.endIndex) {
|
|
319
354
|
this._furthestError = error;
|
|
320
355
|
}
|
|
321
356
|
if (this._isRecording) {
|
|
@@ -390,7 +425,7 @@ class Cursor {
|
|
|
390
425
|
constructor(text) {
|
|
391
426
|
this._text = text;
|
|
392
427
|
this._index = 0;
|
|
393
|
-
this._length = text.length;
|
|
428
|
+
this._length = [...text].length;
|
|
394
429
|
this._history = new CursorHistory();
|
|
395
430
|
this._stackTrace = [];
|
|
396
431
|
}
|
|
@@ -430,8 +465,8 @@ class Cursor {
|
|
|
430
465
|
recordMatch(pattern, node) {
|
|
431
466
|
this._history.recordMatch(pattern, node);
|
|
432
467
|
}
|
|
433
|
-
recordErrorAt(
|
|
434
|
-
this._history.recordErrorAt(
|
|
468
|
+
recordErrorAt(startIndex, endIndex, onPattern) {
|
|
469
|
+
this._history.recordErrorAt(startIndex, endIndex, onPattern);
|
|
435
470
|
}
|
|
436
471
|
resolveError() {
|
|
437
472
|
this._history.resolveError();
|
|
@@ -448,7 +483,8 @@ class Cursor {
|
|
|
448
483
|
pattern,
|
|
449
484
|
cursorIndex: this.index
|
|
450
485
|
};
|
|
451
|
-
|
|
486
|
+
const hasCycle = this._stackTrace.find(t => t.pattern.id === pattern.id && this.index === t.cursorIndex);
|
|
487
|
+
if (hasCycle) {
|
|
452
488
|
throw new Error(`Cyclical Pattern: ${this._stackTrace.map(t => `${t.pattern.name}#${t.pattern.id}{${t.cursorIndex}}`).join(" -> ")} -> ${patternName}#${pattern.id}{${this.index}}.`);
|
|
453
489
|
}
|
|
454
490
|
this._history.pushStackTrace(trace);
|
|
@@ -484,8 +520,8 @@ class Literal {
|
|
|
484
520
|
get name() {
|
|
485
521
|
return this._name;
|
|
486
522
|
}
|
|
487
|
-
get
|
|
488
|
-
return this.
|
|
523
|
+
get token() {
|
|
524
|
+
return this._token;
|
|
489
525
|
}
|
|
490
526
|
get parent() {
|
|
491
527
|
return this._parent;
|
|
@@ -503,15 +539,16 @@ class Literal {
|
|
|
503
539
|
this._id = `literal-${idIndex$9++}`;
|
|
504
540
|
this._type = "literal";
|
|
505
541
|
this._name = name;
|
|
506
|
-
this.
|
|
542
|
+
this._token = value;
|
|
507
543
|
this._runes = Array.from(value);
|
|
508
544
|
this._parent = null;
|
|
509
545
|
this._firstIndex = 0;
|
|
510
546
|
this._lastIndex = 0;
|
|
511
547
|
this._endIndex = 0;
|
|
512
548
|
}
|
|
513
|
-
test(text) {
|
|
549
|
+
test(text, record = false) {
|
|
514
550
|
const cursor = new Cursor(text);
|
|
551
|
+
record && cursor.startRecording();
|
|
515
552
|
const ast = this.parse(cursor);
|
|
516
553
|
return (ast === null || ast === void 0 ? void 0 : ast.value) === text;
|
|
517
554
|
}
|
|
@@ -550,7 +587,7 @@ class Literal {
|
|
|
550
587
|
break;
|
|
551
588
|
}
|
|
552
589
|
if (i + 1 === literalRuneLength) {
|
|
553
|
-
this._lastIndex = this._firstIndex + this.
|
|
590
|
+
this._lastIndex = this._firstIndex + this._token.length - 1;
|
|
554
591
|
passed = true;
|
|
555
592
|
break;
|
|
556
593
|
}
|
|
@@ -563,15 +600,15 @@ class Literal {
|
|
|
563
600
|
return passed;
|
|
564
601
|
}
|
|
565
602
|
_createNode() {
|
|
566
|
-
return new Node("literal", this._name, this._firstIndex, this._lastIndex, undefined, this.
|
|
603
|
+
return new Node("literal", this._name, this._firstIndex, this._lastIndex, undefined, this._token);
|
|
567
604
|
}
|
|
568
605
|
clone(name = this._name) {
|
|
569
|
-
const clone = new Literal(name, this.
|
|
606
|
+
const clone = new Literal(name, this._token);
|
|
570
607
|
clone._id = this._id;
|
|
571
608
|
return clone;
|
|
572
609
|
}
|
|
573
610
|
getTokens() {
|
|
574
|
-
return [this.
|
|
611
|
+
return [this._token];
|
|
575
612
|
}
|
|
576
613
|
getTokensAfter(_lastMatched) {
|
|
577
614
|
return [];
|
|
@@ -598,7 +635,7 @@ class Literal {
|
|
|
598
635
|
return null;
|
|
599
636
|
}
|
|
600
637
|
isEqual(pattern) {
|
|
601
|
-
return pattern.type === this.type && pattern.
|
|
638
|
+
return pattern.type === this.type && pattern._token === this._token;
|
|
602
639
|
}
|
|
603
640
|
}
|
|
604
641
|
|
|
@@ -613,7 +650,7 @@ class Regex {
|
|
|
613
650
|
get name() {
|
|
614
651
|
return this._name;
|
|
615
652
|
}
|
|
616
|
-
get
|
|
653
|
+
get regex() {
|
|
617
654
|
return this._originalRegexString;
|
|
618
655
|
}
|
|
619
656
|
get parent() {
|
|
@@ -2550,7 +2587,7 @@ class Grammar {
|
|
|
2550
2587
|
return importBlock && importBlock.children.length > 0;
|
|
2551
2588
|
}
|
|
2552
2589
|
_buildPatterns(ast) {
|
|
2553
|
-
const body = ast.find(n => n.name === "body" && n.
|
|
2590
|
+
const body = ast.find(n => n.name === "body" && n.findAncestor(n => n.name === "head") == null);
|
|
2554
2591
|
if (body == null) {
|
|
2555
2592
|
return;
|
|
2556
2593
|
}
|
|
@@ -2870,16 +2907,6 @@ class Grammar {
|
|
|
2870
2907
|
}
|
|
2871
2908
|
}
|
|
2872
2909
|
|
|
2873
|
-
function arePatternsEqual(a, b) {
|
|
2874
|
-
if (a === b) {
|
|
2875
|
-
return true;
|
|
2876
|
-
}
|
|
2877
|
-
else if (a == null || b == null) {
|
|
2878
|
-
return false;
|
|
2879
|
-
}
|
|
2880
|
-
return a.isEqual(b);
|
|
2881
|
-
}
|
|
2882
|
-
|
|
2883
2910
|
const kebabRegex = /-([a-z])/g; // Define the regex once
|
|
2884
2911
|
function kebabToCamelCase(str) {
|
|
2885
2912
|
return str.replace(kebabRegex, (_, char) => char.toUpperCase());
|
|
@@ -2908,7 +2935,6 @@ exports.Reference = Reference;
|
|
|
2908
2935
|
exports.Regex = Regex;
|
|
2909
2936
|
exports.Repeat = Repeat;
|
|
2910
2937
|
exports.Sequence = Sequence;
|
|
2911
|
-
exports.arePatternsEqual = arePatternsEqual;
|
|
2912
2938
|
exports.grammar = grammar;
|
|
2913
2939
|
exports.patterns = patterns;
|
|
2914
2940
|
//# sourceMappingURL=index.js.map
|