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/ast/Node.d.ts
CHANGED
|
@@ -23,22 +23,27 @@ export declare class Node {
|
|
|
23
23
|
get parent(): Node | null;
|
|
24
24
|
get children(): readonly Node[];
|
|
25
25
|
get hasChildren(): boolean;
|
|
26
|
+
get isLeaf(): boolean;
|
|
26
27
|
get value(): string;
|
|
27
28
|
constructor(type: string, name: string, firstIndex: number, lastIndex: number, children?: Node[], value?: string);
|
|
28
29
|
removeChild(node: Node): void;
|
|
30
|
+
findChildIndex(node: Node): number;
|
|
31
|
+
spliceChildren(index: number, deleteCount: number, ...items: Node[]): Node[];
|
|
29
32
|
removeAllChildren(): void;
|
|
30
33
|
replaceChild(newNode: Node, referenceNode: Node): void;
|
|
31
34
|
replaceWith(newNode: Node): void;
|
|
32
35
|
insertBefore(newNode: Node, referenceNode: Node | null): void;
|
|
33
36
|
appendChild(newNode: Node): void;
|
|
34
|
-
|
|
37
|
+
append(...nodes: Node[]): void;
|
|
35
38
|
nextSibling(): Node | null;
|
|
36
39
|
previousSibling(): Node | null;
|
|
37
40
|
find(predicate: (node: Node) => boolean): Node | null;
|
|
38
41
|
findAll(predicate: (node: Node) => boolean): Node[];
|
|
39
|
-
|
|
42
|
+
findAncestor(predicate: (node: Node) => boolean): Node | null;
|
|
40
43
|
walkUp(callback: (node: Node) => void): void;
|
|
41
44
|
walkDown(callback: (node: Node) => void): void;
|
|
45
|
+
walkBreadthFirst(callback: (node: Node) => void): void;
|
|
46
|
+
transform(visitors: Record<string, (node: Node) => Node>): Node;
|
|
42
47
|
flatten(): Node[];
|
|
43
48
|
reduce(): void;
|
|
44
49
|
remove(): void;
|
|
@@ -47,4 +52,6 @@ export declare class Node {
|
|
|
47
52
|
toString(): string;
|
|
48
53
|
toCycleFreeObject(): CycleFreeNode;
|
|
49
54
|
toJson(space?: number): string;
|
|
55
|
+
static createValueNode(name: string, value: string): Node;
|
|
56
|
+
static createNode(name: string, children: Node[]): Node;
|
|
50
57
|
}
|
package/dist/index.browser.js
CHANGED
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.clarityPatternParser = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
+
function defaultVisitor(node) {
|
|
8
|
+
return node;
|
|
9
|
+
}
|
|
7
10
|
class Node {
|
|
8
11
|
get type() {
|
|
9
12
|
return this._type;
|
|
@@ -32,6 +35,9 @@
|
|
|
32
35
|
get hasChildren() {
|
|
33
36
|
return this._children.length > 0;
|
|
34
37
|
}
|
|
38
|
+
get isLeaf() {
|
|
39
|
+
return !this.hasChildren;
|
|
40
|
+
}
|
|
35
41
|
get value() {
|
|
36
42
|
return this.toString();
|
|
37
43
|
}
|
|
@@ -52,16 +58,22 @@
|
|
|
52
58
|
node._parent = null;
|
|
53
59
|
}
|
|
54
60
|
}
|
|
61
|
+
findChildIndex(node) {
|
|
62
|
+
return this._children.indexOf(node);
|
|
63
|
+
}
|
|
64
|
+
spliceChildren(index, deleteCount, ...items) {
|
|
65
|
+
const removedItems = this._children.splice(index, deleteCount, ...items);
|
|
66
|
+
removedItems.forEach(i => i._parent = null);
|
|
67
|
+
items.forEach(i => i._parent = this);
|
|
68
|
+
return removedItems;
|
|
69
|
+
}
|
|
55
70
|
removeAllChildren() {
|
|
56
|
-
this.
|
|
57
|
-
this._children.length = 0;
|
|
71
|
+
this.spliceChildren(0, this._children.length);
|
|
58
72
|
}
|
|
59
73
|
replaceChild(newNode, referenceNode) {
|
|
60
|
-
const index = this.
|
|
74
|
+
const index = this.findChildIndex(referenceNode);
|
|
61
75
|
if (index > -1) {
|
|
62
|
-
this.
|
|
63
|
-
newNode._parent = this;
|
|
64
|
-
referenceNode._parent = null;
|
|
76
|
+
this.spliceChildren(index, 1, newNode);
|
|
65
77
|
}
|
|
66
78
|
}
|
|
67
79
|
replaceWith(newNode) {
|
|
@@ -75,20 +87,19 @@
|
|
|
75
87
|
this._children.push(newNode);
|
|
76
88
|
return;
|
|
77
89
|
}
|
|
78
|
-
const index = this.
|
|
90
|
+
const index = this.findChildIndex(referenceNode);
|
|
79
91
|
if (index > -1) {
|
|
80
92
|
this._children.splice(index, 0, newNode);
|
|
81
93
|
}
|
|
82
94
|
}
|
|
83
95
|
appendChild(newNode) {
|
|
84
|
-
newNode
|
|
85
|
-
this._children.push(newNode);
|
|
96
|
+
this.append(newNode);
|
|
86
97
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
98
|
+
append(...nodes) {
|
|
99
|
+
nodes.forEach((newNode) => {
|
|
100
|
+
newNode._parent = this;
|
|
101
|
+
this._children.push(newNode);
|
|
102
|
+
});
|
|
92
103
|
}
|
|
93
104
|
nextSibling() {
|
|
94
105
|
if (this._parent == null) {
|
|
@@ -124,7 +135,7 @@
|
|
|
124
135
|
});
|
|
125
136
|
return matches;
|
|
126
137
|
}
|
|
127
|
-
|
|
138
|
+
findAncestor(predicate) {
|
|
128
139
|
let parent = this._parent;
|
|
129
140
|
while (parent != null) {
|
|
130
141
|
if (predicate(parent)) {
|
|
@@ -144,6 +155,23 @@
|
|
|
144
155
|
callback(this);
|
|
145
156
|
childrenCopy.forEach(c => c.walkDown(callback));
|
|
146
157
|
}
|
|
158
|
+
walkBreadthFirst(callback) {
|
|
159
|
+
const queue = [this];
|
|
160
|
+
while (queue.length > 0) {
|
|
161
|
+
// biome-ignore lint/style/noNonNullAssertion: This will never be undefined.
|
|
162
|
+
const current = queue.shift();
|
|
163
|
+
callback(current);
|
|
164
|
+
queue.push(...current.children);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
transform(visitors) {
|
|
168
|
+
const childrenCopy = this._children.slice();
|
|
169
|
+
const visitor = visitors[this.name] == null ? defaultVisitor : visitors[this.name];
|
|
170
|
+
const children = childrenCopy.map(c => c.transform(visitors));
|
|
171
|
+
this.removeAllChildren();
|
|
172
|
+
this.append(...children);
|
|
173
|
+
return visitor(this);
|
|
174
|
+
}
|
|
147
175
|
flatten() {
|
|
148
176
|
const nodes = [];
|
|
149
177
|
this.walkDown((node) => {
|
|
@@ -197,6 +225,13 @@
|
|
|
197
225
|
toJson(space) {
|
|
198
226
|
return JSON.stringify(this.toCycleFreeObject(), null, space);
|
|
199
227
|
}
|
|
228
|
+
static createValueNode(name, value) {
|
|
229
|
+
return new Node("custom-value-node", name, 0, 0, [], value);
|
|
230
|
+
}
|
|
231
|
+
static createNode(name, children) {
|
|
232
|
+
const value = children.map(c => c.toString()).join("");
|
|
233
|
+
return new Node("custom-node", name, 0, 0, children, value);
|
|
234
|
+
}
|
|
200
235
|
}
|
|
201
236
|
|
|
202
237
|
/******************************************************************************
|
|
@@ -314,10 +349,10 @@
|
|
|
314
349
|
}
|
|
315
350
|
}
|
|
316
351
|
}
|
|
317
|
-
recordErrorAt(
|
|
318
|
-
const error = new ParseError(
|
|
352
|
+
recordErrorAt(startIndex, endIndex, pattern) {
|
|
353
|
+
const error = new ParseError(startIndex, endIndex, pattern);
|
|
319
354
|
this._currentError = error;
|
|
320
|
-
if (this._furthestError === null ||
|
|
355
|
+
if (this._furthestError === null || endIndex > this._furthestError.endIndex) {
|
|
321
356
|
this._furthestError = error;
|
|
322
357
|
}
|
|
323
358
|
if (this._isRecording) {
|
|
@@ -392,7 +427,7 @@
|
|
|
392
427
|
constructor(text) {
|
|
393
428
|
this._text = text;
|
|
394
429
|
this._index = 0;
|
|
395
|
-
this._length = text.length;
|
|
430
|
+
this._length = [...text].length;
|
|
396
431
|
this._history = new CursorHistory();
|
|
397
432
|
this._stackTrace = [];
|
|
398
433
|
}
|
|
@@ -432,8 +467,8 @@
|
|
|
432
467
|
recordMatch(pattern, node) {
|
|
433
468
|
this._history.recordMatch(pattern, node);
|
|
434
469
|
}
|
|
435
|
-
recordErrorAt(
|
|
436
|
-
this._history.recordErrorAt(
|
|
470
|
+
recordErrorAt(startIndex, endIndex, onPattern) {
|
|
471
|
+
this._history.recordErrorAt(startIndex, endIndex, onPattern);
|
|
437
472
|
}
|
|
438
473
|
resolveError() {
|
|
439
474
|
this._history.resolveError();
|
|
@@ -450,7 +485,8 @@
|
|
|
450
485
|
pattern,
|
|
451
486
|
cursorIndex: this.index
|
|
452
487
|
};
|
|
453
|
-
|
|
488
|
+
const hasCycle = this._stackTrace.find(t => t.pattern.id === pattern.id && this.index === t.cursorIndex);
|
|
489
|
+
if (hasCycle) {
|
|
454
490
|
throw new Error(`Cyclical Pattern: ${this._stackTrace.map(t => `${t.pattern.name}#${t.pattern.id}{${t.cursorIndex}}`).join(" -> ")} -> ${patternName}#${pattern.id}{${this.index}}.`);
|
|
455
491
|
}
|
|
456
492
|
this._history.pushStackTrace(trace);
|
|
@@ -486,8 +522,8 @@
|
|
|
486
522
|
get name() {
|
|
487
523
|
return this._name;
|
|
488
524
|
}
|
|
489
|
-
get
|
|
490
|
-
return this.
|
|
525
|
+
get token() {
|
|
526
|
+
return this._token;
|
|
491
527
|
}
|
|
492
528
|
get parent() {
|
|
493
529
|
return this._parent;
|
|
@@ -505,15 +541,16 @@
|
|
|
505
541
|
this._id = `literal-${idIndex$9++}`;
|
|
506
542
|
this._type = "literal";
|
|
507
543
|
this._name = name;
|
|
508
|
-
this.
|
|
544
|
+
this._token = value;
|
|
509
545
|
this._runes = Array.from(value);
|
|
510
546
|
this._parent = null;
|
|
511
547
|
this._firstIndex = 0;
|
|
512
548
|
this._lastIndex = 0;
|
|
513
549
|
this._endIndex = 0;
|
|
514
550
|
}
|
|
515
|
-
test(text) {
|
|
551
|
+
test(text, record = false) {
|
|
516
552
|
const cursor = new Cursor(text);
|
|
553
|
+
record && cursor.startRecording();
|
|
517
554
|
const ast = this.parse(cursor);
|
|
518
555
|
return (ast === null || ast === void 0 ? void 0 : ast.value) === text;
|
|
519
556
|
}
|
|
@@ -552,7 +589,7 @@
|
|
|
552
589
|
break;
|
|
553
590
|
}
|
|
554
591
|
if (i + 1 === literalRuneLength) {
|
|
555
|
-
this._lastIndex = this._firstIndex + this.
|
|
592
|
+
this._lastIndex = this._firstIndex + this._token.length - 1;
|
|
556
593
|
passed = true;
|
|
557
594
|
break;
|
|
558
595
|
}
|
|
@@ -565,15 +602,15 @@
|
|
|
565
602
|
return passed;
|
|
566
603
|
}
|
|
567
604
|
_createNode() {
|
|
568
|
-
return new Node("literal", this._name, this._firstIndex, this._lastIndex, undefined, this.
|
|
605
|
+
return new Node("literal", this._name, this._firstIndex, this._lastIndex, undefined, this._token);
|
|
569
606
|
}
|
|
570
607
|
clone(name = this._name) {
|
|
571
|
-
const clone = new Literal(name, this.
|
|
608
|
+
const clone = new Literal(name, this._token);
|
|
572
609
|
clone._id = this._id;
|
|
573
610
|
return clone;
|
|
574
611
|
}
|
|
575
612
|
getTokens() {
|
|
576
|
-
return [this.
|
|
613
|
+
return [this._token];
|
|
577
614
|
}
|
|
578
615
|
getTokensAfter(_lastMatched) {
|
|
579
616
|
return [];
|
|
@@ -600,7 +637,7 @@
|
|
|
600
637
|
return null;
|
|
601
638
|
}
|
|
602
639
|
isEqual(pattern) {
|
|
603
|
-
return pattern.type === this.type && pattern.
|
|
640
|
+
return pattern.type === this.type && pattern._token === this._token;
|
|
604
641
|
}
|
|
605
642
|
}
|
|
606
643
|
|
|
@@ -615,7 +652,7 @@
|
|
|
615
652
|
get name() {
|
|
616
653
|
return this._name;
|
|
617
654
|
}
|
|
618
|
-
get
|
|
655
|
+
get regex() {
|
|
619
656
|
return this._originalRegexString;
|
|
620
657
|
}
|
|
621
658
|
get parent() {
|
|
@@ -2552,7 +2589,7 @@
|
|
|
2552
2589
|
return importBlock && importBlock.children.length > 0;
|
|
2553
2590
|
}
|
|
2554
2591
|
_buildPatterns(ast) {
|
|
2555
|
-
const body = ast.find(n => n.name === "body" && n.
|
|
2592
|
+
const body = ast.find(n => n.name === "body" && n.findAncestor(n => n.name === "head") == null);
|
|
2556
2593
|
if (body == null) {
|
|
2557
2594
|
return;
|
|
2558
2595
|
}
|
|
@@ -2872,16 +2909,6 @@
|
|
|
2872
2909
|
}
|
|
2873
2910
|
}
|
|
2874
2911
|
|
|
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
2912
|
const kebabRegex = /-([a-z])/g; // Define the regex once
|
|
2886
2913
|
function kebabToCamelCase(str) {
|
|
2887
2914
|
return str.replace(kebabRegex, (_, char) => char.toUpperCase());
|
|
@@ -2910,7 +2937,6 @@
|
|
|
2910
2937
|
exports.Regex = Regex;
|
|
2911
2938
|
exports.Repeat = Repeat;
|
|
2912
2939
|
exports.Sequence = Sequence;
|
|
2913
|
-
exports.arePatternsEqual = arePatternsEqual;
|
|
2914
2940
|
exports.grammar = grammar;
|
|
2915
2941
|
exports.patterns = patterns;
|
|
2916
2942
|
|