-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathparse.ts
278 lines (239 loc) · 7.25 KB
/
parse.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
import { Parser } from "prettier";
import { createIdGenerator } from "./create-id-generator";
export const parseGoTemplate: Parser<GoNode>["parse"] = (text, options) => {
const regex =
/{{(?<startdelimiter>-|<|%|\/\*)?\s*(?<statement>(?<keyword>if|range|block|with|define|end|else|prettier-ignore-start|prettier-ignore-end)?[\s\S]*?)\s*(?<endDelimiter>-|>|%|\*\/)?}}|(?<unformattableScript><(script)((?!<)[\s\S])*>((?!<\/script)[\s\S])*?{{[\s\S]*?<\/(script)>)|(?<unformattableStyle><(style)((?!<)[\s\S])*>((?!<\/style)[\s\S])*?{{[\s\S]*?<\/(style)>)/g;
const root: GoRoot = {
type: "root",
content: text,
aliasedContent: "",
children: {},
index: 0,
contentStart: 0,
length: text.length,
};
const nodeStack: (GoBlock | GoRoot)[] = [root];
const getId = createIdGenerator();
for (let match of text.matchAll(regex)) {
const current = last(nodeStack);
const keyword = match.groups?.keyword as GoBlockKeyword | undefined;
const statement = match.groups?.statement;
const unformattable =
match.groups?.unformattableScript ?? match.groups?.unformattableStyle;
const startDelimiter = (match.groups?.startdelimiter ??
"") as GoInlineStartDelimiter;
const endDelimiter = (match.groups?.endDelimiter ??
"") as GoInlineEndDelimiter;
if (current === undefined) {
throw Error("Node stack empty.");
}
if (match.index === undefined) {
throw Error("Regex match index undefined.");
}
const id = getId();
if (unformattable) {
current.children[id] = {
id,
type: "unformattable",
index: match.index,
length: match[0].length,
content: unformattable,
parent: current,
};
continue;
}
if (statement === undefined) {
throw Error("Formattable match without statement.");
}
const inline: GoInline = {
index: match.index,
length: match[0].length,
startDelimiter,
endDelimiter,
parent: current!,
type: "inline",
statement,
id,
};
if (keyword === "end" || keyword === "prettier-ignore-end") {
if (current.type !== "block") {
throw Error("Encountered unexpected end keyword.");
}
current.length = match[0].length + match.index - current.index;
current.content = text.substring(current.contentStart, match.index);
current.aliasedContent = aliasNodeContent(current);
current.end = inline;
if (current.parent.type === "double-block") {
const firstChild = current.parent.blocks[0];
const lastChild =
current.parent.blocks[current.parent.blocks.length - 1];
current.parent.length =
lastChild.index + lastChild.length - firstChild.index;
}
nodeStack.pop();
} else if (isBlock(current) && keyword === "else") {
const nextChild: GoBlock = {
type: "block",
start: inline,
end: null,
children: {},
keyword: keyword,
index: match.index,
parent: current.parent,
contentStart: match.index + match[0].length,
content: "",
aliasedContent: "",
length: -1,
id: getId(),
startDelimiter,
endDelimiter,
};
if (isMultiBlock(current.parent)) {
current.parent.blocks.push(nextChild);
} else {
const multiBlock: GoMultiBlock = {
type: "double-block",
parent: current.parent,
index: current.index,
length: -1,
keyword,
id: current.id,
blocks: [current, nextChild],
};
nextChild.parent = multiBlock;
current.parent = multiBlock;
if ("children" in multiBlock.parent) {
multiBlock.parent.children[multiBlock.id] = multiBlock;
} else {
throw Error("Could not find child in parent.");
}
}
current.id = getId();
current.length = match[0].length + match.index - current.index;
current.content = text.substring(current.contentStart, match.index);
current.aliasedContent = aliasNodeContent(current);
nodeStack.pop();
nodeStack.push(nextChild);
} else if (keyword) {
const block: GoBlock = {
type: "block",
start: inline,
end: null,
children: {},
keyword: keyword as GoBlockKeyword,
index: match.index,
parent: current,
contentStart: match.index + match[0].length,
content: "",
aliasedContent: "",
length: -1,
id: getId(),
startDelimiter,
endDelimiter,
};
current.children[block.id] = block;
nodeStack.push(block);
} else {
current.children[inline.id] = inline;
}
}
if (!isRoot(nodeStack.pop()!)) {
throw Error("Missing end block.");
}
root.aliasedContent = aliasNodeContent(root);
return root;
};
function aliasNodeContent(current: GoBlock | GoRoot): string {
let result = current.content;
Object.entries(current.children)
.sort(([_, node1], [__, node2]) => node2.index - node1.index)
.forEach(
([id, node]) =>
(result =
result.substring(0, node.index - current.contentStart) +
id +
result.substring(node.index + node.length - current.contentStart)),
);
return result;
}
function last<T>(array: T[]): T | undefined {
return array[array.length - 1];
}
export type GoNode =
| GoRoot
| GoBlock
| GoInline
| GoMultiBlock
| GoUnformattable;
export type GoBlockKeyword =
| "if"
| "range"
| "block"
| "with"
| "define"
| "else"
| "prettier-ignore-start"
| "prettier-ignore-end"
| "end";
export type GoRoot = { type: "root" } & Omit<
GoBlock,
| "type"
| "keyword"
| "parent"
| "statement"
| "id"
| "startDelimiter"
| "endDelimiter"
| "start"
| "end"
>;
export interface GoBaseNode<Type extends string> {
id: string;
type: Type;
index: number;
length: number;
parent: GoBlock | GoRoot | GoMultiBlock;
}
export interface GoBlock extends GoBaseNode<"block">, WithDelimiter {
keyword: GoBlockKeyword;
children: {
[id: string]: GoNode;
};
start: GoInline;
end: GoInline | null;
content: string;
aliasedContent: string;
contentStart: number;
}
export interface GoMultiBlock extends GoBaseNode<"double-block"> {
blocks: (GoBlock | GoMultiBlock)[];
keyword: GoBlockKeyword;
}
export type GoSharedDelimiter = "%" | "-" | "";
export type GoInlineStartDelimiter = "<" | "/*" | GoSharedDelimiter;
export type GoInlineEndDelimiter = ">" | "*/" | GoSharedDelimiter;
export interface GoUnformattable extends GoBaseNode<"unformattable"> {
content: string;
}
export interface WithDelimiter {
startDelimiter: GoInlineStartDelimiter;
endDelimiter: GoInlineEndDelimiter;
}
export interface GoInline extends GoBaseNode<"inline">, WithDelimiter {
statement: string;
}
export function isInline(node: GoNode): node is GoInline {
return node.type === "inline";
}
export function isBlock(node: GoNode): node is GoBlock {
return node.type === "block";
}
export function isMultiBlock(node: GoNode): node is GoMultiBlock {
return node.type === "double-block";
}
export function isRoot(node: GoNode): node is GoRoot {
return node.type === "root";
}
export function isUnformattable(node: GoNode): node is GoRoot {
return node.type === "unformattable";
}