-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
nodes.ts
345 lines (282 loc) · 9.14 KB
/
nodes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import type { Filterable } from "../filterManager/filters";
import type { Parser } from "./parser";
import type { TagAccessor, TagPath, RoundInfo } from "./types";
import type { Delimiters } from "./delimiters";
import type { Optic } from "./optics";
import { id } from "../utils";
import { run, dictFunction, dictForget } from "./optics";
import { Status } from "./types";
export interface ASTNode extends Filterable {
toString(): string | null;
isReady(): boolean;
evaluate(
parser: Parser,
tagAccessor: TagAccessor,
tagPath: TagPath,
): ASTNode[];
}
export const nodesAreReady = (accu: boolean, node: ASTNode): boolean =>
accu && node.isReady();
const joinNodes = (nodes: ASTNode[]) =>
nodes.map((node) => node.toString()).join("");
const joinRepr = (nodes: ASTNode[]) =>
nodes.map((node) => node.toReprString()).join("");
const leadingTrailingNewlineOrBr = /^(?:<br(?: ?\/)?>|\n)|(?:<br(?: ?\/)?>|\n)$/gu;
const stripLineBreaks = (text: string): string =>
text.replace(leadingTrailingNewlineOrBr, "");
export class TagNode implements ASTNode {
readonly fullKey: string;
readonly key: string;
readonly num: number | null;
readonly fullOccur: number;
readonly occur: number;
readonly delimiters: Delimiters;
protected _inlineNodes: ASTNode[];
readonly hasInline: boolean;
protected _blockNodes: ASTNode[];
readonly hasBlock: boolean;
protected _inlineOptics: Optic[] = [];
protected _inlineGetter: (i: any) => any = id;
protected _blockOptics: Optic[] = [];
protected _blockGetter: (i: any) => any = id;
constructor(
fullKey: string,
key: string,
num: number | null,
fullOccur: number,
occur: number,
delimiters: Delimiters,
inlineNodes: ASTNode[],
hasInline: boolean,
blockNodes: ASTNode[],
hasBlock: boolean,
) {
this.fullKey = fullKey;
this.key = key;
this.num = num;
this.fullOccur = fullOccur;
this.occur = occur;
this.delimiters = delimiters;
this._inlineNodes = inlineNodes;
this.hasInline = hasInline;
this._blockNodes = blockNodes;
this.hasBlock = hasBlock;
}
/******************** NODES ********************/
get inlineNodes(): ASTNode[] {
return this._inlineNodes;
}
get blockNodes(): ASTNode[] {
return this._blockNodes;
}
get innerNodes(): ASTNode[] {
return this.hasBlock ? this.blockNodes : this.inlineNodes;
}
protected set internalNodes(nodes: ASTNode[]) {
if (this.hasBlock) {
this._blockNodes = nodes;
} else {
this._inlineNodes = nodes;
}
}
/******************** TEXT ********************/
get inlineText(): string {
return joinNodes(this.inlineNodes);
}
get blockText(): string {
return stripLineBreaks(joinNodes(this.blockNodes));
}
get text(): string {
return this.hasBlock ? this.blockText : this.inlineText;
}
/******************** OPTICS ********************/
set inlineOptics(op: Optic[]) {
this._inlineOptics = op;
this._inlineGetter = run(op, dictForget, id);
}
get inlineOptics(): Optic[] {
return this._inlineOptics;
}
set blockOptics(op: Optic[]) {
this._blockOptics = op;
this._blockGetter = run(op, dictForget, id);
}
get blockOptics(): Optic[] {
return this._blockOptics;
}
set optics(op: Optic[]) {
if (this.hasBlock) {
this.blockOptics = op;
} else {
this.inlineOptics = op;
}
}
get optics(): Optic[] {
return this.hasBlock ? this.blockOptics : this.inlineOptics;
}
get inlineGetter(): (i: any) => any {
return this._inlineGetter;
}
get blockGetter(): (i: any) => any {
return this._blockGetter;
}
get getter(): (i: any) => any {
return this.hasBlock ? this.blockGetter : this.inlineGetter;
}
/******************** VALUES ********************/
get inlineValues() {
return this.inlineGetter(this.inlineText);
}
get blockValues() {
return this.blockGetter(this.blockText);
}
get values() {
return this.hasBlock ? this.blockValues : this.inlineValues;
}
/******************** TRAVERSE ********************/
inlineTraverse(f: (x: unknown) => unknown) {
return run(this.inlineOptics, dictFunction, f)(this.inlineText);
}
blockTraverse(f: (x: unknown) => unknown) {
return run(this.blockOptics, dictFunction, f)(this.blockText);
}
traverse(f: (x: unknown) => unknown) {
return this.hasBlock ? this.blockTraverse(f) : this.inlineTraverse(f);
}
/******************** STRINGIFY ********************/
// implementation detail
protected wrapWithDelimiters(infix: string, wrapped?: string): string {
return `${this.delimiters.open}${infix}${this.fullKey}${
wrapped ? this.delimiters.sep + wrapped : ""
}${this.delimiters.close}`;
}
protected wrapBlock(block: string, inline?: string): string {
return `${this.wrapWithDelimiters(
"#",
inline,
)}${block}${this.wrapWithDelimiters("/")}`;
}
protected baseToString(
getInline: () => string,
getBlock: () => string,
): string {
return this.hasInline
? this.hasBlock
? this.wrapBlock(getBlock(), getInline())
: this.wrapWithDelimiters("", getInline())
: this.hasBlock
? this.wrapBlock(getBlock())
: this.wrapWithDelimiters("");
}
toString(): string {
return this.baseToString(
() => this.inlineText,
() => this.blockText,
);
}
toReprString(): string {
return this.baseToString(
() => joinRepr(this.inlineNodes),
() => joinRepr(this.blockNodes),
);
}
isReady(): boolean {
return false;
}
evaluate(
parser: Parser,
tagAccessor: TagAccessor,
tagPath: TagPath,
afterCapture = false,
): ASTNode[] {
const tagProcessor = tagAccessor(this.key);
const depth = tagPath.length - 1;
const useCapture = tagProcessor.getOptions().capture;
const innerEvaluate = (node: ASTNode, index: number) =>
node.evaluate(parser, tagAccessor, [...tagPath, index]);
const shouldEvaluateInnerNodes = !useCapture && !afterCapture;
const operatesAsCapture = useCapture && !afterCapture;
if (shouldEvaluateInnerNodes) {
this.innerNodes.splice(
0,
this.innerNodes.length,
...this.innerNodes.flatMap(innerEvaluate),
);
}
const allReady = this.innerNodes.reduce(nodesAreReady, true);
this.inlineOptics = tagProcessor.getOptions().inlineOptics;
this.blockOptics = tagProcessor.getOptions().blockOptics;
const roundInfo: RoundInfo = {
path: tagPath,
depth: depth,
ready: allReady,
isCapture: operatesAsCapture,
};
const filterOutput = tagProcessor.execute(this, roundInfo);
let result;
switch (filterOutput.status) {
case Status.Ready:
result =
typeof filterOutput.result === "string"
? [new TextNode(filterOutput.result as string)]
: ((filterOutput.result as unknown) as ASTNode[]);
break;
case Status.NotReady:
result = operatesAsCapture ? this.innerNodes : [this];
break;
case Status.ContinueTags:
result = parser
.rawParse(filterOutput.result as string)
.flatMap(innerEvaluate);
break;
case Status.ContainsTags:
result = parser.rawParse(filterOutput.result as string);
break;
}
if (operatesAsCapture) {
this.internalNodes = result;
return this.evaluate(parser, tagAccessor, tagPath, true);
}
return result;
}
}
class SimpleNode implements ASTNode {
isReady(): boolean {
// should never happen
return true;
}
evaluate(): ASTNode[] {
return [this];
}
toReprString(): string {
return this.toString() ?? "";
}
}
export class TextNode extends SimpleNode {
readonly text: string;
constructor(text: string) {
super();
this.text = text;
}
toString(): string | null {
return this.text;
}
}
export class EscapedNode extends SimpleNode {
readonly escaped: string;
constructor(escaped: string) {
super();
this.escaped = escaped;
}
toString(): string | null {
return this.escaped.length === 1 ? this.escaped : this.escaped.slice(1);
}
toReprString(): string {
return this.escaped;
}
}
export class DocSeparatorNode extends SimpleNode {
toString(): string | null {
return null;
}
}