clarity-pattern-parser 11.1.4 → 11.2.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/grammar/Grammar.d.ts +2 -0
- package/dist/index.browser.js +98 -49
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +98 -49
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +98 -49
- package/dist/index.js.map +1 -1
- package/dist/patterns/Context.d.ts +1 -0
- package/package.json +1 -1
- package/src/grammar/Grammar.test.ts +82 -0
- package/src/grammar/Grammar.ts +113 -48
- package/src/grammar/patterns/import.ts +13 -1
- package/src/grammar/patterns/literals.ts +1 -0
- package/src/grammar/patterns/pattern.ts +3 -1
- package/src/grammar/patterns/takeUtilLiteral.ts +21 -0
- package/src/grammar/patterns.test.ts +6 -6
- package/src/grammar/patterns.ts +1 -1
- package/src/patterns/Context.ts +7 -0
- package/src/patterns/TakeUntil.test.ts +62 -0
- package/src/patterns/TakeUntil.ts +166 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { Node } from "../ast/Node";
|
|
2
|
+
import { Cursor } from "./Cursor";
|
|
3
|
+
import { execPattern } from "./execPattern";
|
|
4
|
+
import { ParseResult } from "./ParseResult";
|
|
5
|
+
import { Pattern } from "./Pattern";
|
|
6
|
+
import { testPattern } from "./testPattern";
|
|
7
|
+
|
|
8
|
+
let idIndex = 0;
|
|
9
|
+
|
|
10
|
+
export class TakeUntil implements Pattern {
|
|
11
|
+
private _id: string;
|
|
12
|
+
private _type: string;
|
|
13
|
+
private _name: string;
|
|
14
|
+
private _parent: Pattern | null;
|
|
15
|
+
private _children: Pattern[];
|
|
16
|
+
private _startedOnIndex: number;
|
|
17
|
+
private _terminatingPattern: Pattern;
|
|
18
|
+
private _tokens: string[];
|
|
19
|
+
|
|
20
|
+
get id(): string {
|
|
21
|
+
return this._id;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get type(): string {
|
|
25
|
+
return this._type;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get name(): string {
|
|
29
|
+
return this._name;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get children(): Pattern[] {
|
|
33
|
+
return this._children;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get parent(): Pattern | null {
|
|
37
|
+
return this._parent;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
set parent(pattern: Pattern | null) {
|
|
41
|
+
this._parent = pattern;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get startedOnIndex(): number {
|
|
45
|
+
return this._startedOnIndex;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
constructor(name: string, terminatingPattern: Pattern) {
|
|
49
|
+
this._id = String(idIndex++);
|
|
50
|
+
this._type = "take-until";
|
|
51
|
+
this._name = name;
|
|
52
|
+
this._parent = null;
|
|
53
|
+
this._terminatingPattern = terminatingPattern;
|
|
54
|
+
this._children = [this._terminatingPattern];
|
|
55
|
+
this._tokens = [];
|
|
56
|
+
this._startedOnIndex = 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
parse(cursor: Cursor): Node | null {
|
|
60
|
+
let cursorIndex = cursor.index;
|
|
61
|
+
let foundMatch = false;
|
|
62
|
+
this._startedOnIndex = cursor.index;
|
|
63
|
+
|
|
64
|
+
let terminatingResult = this._terminatingPattern.parse(cursor);
|
|
65
|
+
|
|
66
|
+
if (terminatingResult == null) {
|
|
67
|
+
foundMatch = true;
|
|
68
|
+
|
|
69
|
+
cursor.moveTo(cursorIndex);
|
|
70
|
+
cursorIndex += 1;
|
|
71
|
+
cursor.hasNext() && cursor.next();
|
|
72
|
+
|
|
73
|
+
cursor.resolveError();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
while (true) {
|
|
77
|
+
terminatingResult = this._terminatingPattern.parse(cursor);
|
|
78
|
+
|
|
79
|
+
if (terminatingResult == null) {
|
|
80
|
+
cursor.moveTo(cursorIndex);
|
|
81
|
+
cursorIndex += 1;
|
|
82
|
+
|
|
83
|
+
if (cursor.hasNext()) {
|
|
84
|
+
cursor.next();
|
|
85
|
+
} else {
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
cursor.resolveError();
|
|
90
|
+
} else {
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (foundMatch) {
|
|
96
|
+
cursor.moveTo(cursorIndex - 1);
|
|
97
|
+
const value = cursor.getChars(this.startedOnIndex, cursorIndex - 1);
|
|
98
|
+
return Node.createValueNode(this._type, this._name, value);
|
|
99
|
+
} else {
|
|
100
|
+
cursor.resolveError();
|
|
101
|
+
cursor.moveTo(this.startedOnIndex);
|
|
102
|
+
cursor.recordErrorAt(this._startedOnIndex, this._startedOnIndex, this);
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
exec(text: string, record?: boolean | undefined): ParseResult {
|
|
108
|
+
return execPattern(this, text, record);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
test(text: string, record?: boolean | undefined): boolean {
|
|
112
|
+
return testPattern(this, text, record);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
clone(name = this.name): Pattern {
|
|
116
|
+
const clone = new TakeUntil(name, this._terminatingPattern);
|
|
117
|
+
clone._id = this._id;
|
|
118
|
+
|
|
119
|
+
return clone;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
getTokens() {
|
|
123
|
+
return this._tokens;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
getTokensAfter(_childReference: Pattern): string[] {
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
getNextTokens(): string[] {
|
|
131
|
+
if (this.parent == null) {
|
|
132
|
+
return [];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return this.parent.getTokensAfter(this);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
getPatterns(): Pattern[] {
|
|
139
|
+
return [this];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
getPatternsAfter(_childReference: Pattern): Pattern[] {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
getNextPatterns(): Pattern[] {
|
|
147
|
+
if (this.parent == null) {
|
|
148
|
+
return [];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return this.parent.getPatternsAfter(this);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
find(_predicate: (p: Pattern) => boolean): Pattern | null {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
setTokens(tokens: string[]) {
|
|
159
|
+
this._tokens = tokens;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
isEqual(pattern: Pattern): boolean {
|
|
163
|
+
return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
}
|