-
Notifications
You must be signed in to change notification settings - Fork 0
/
compileNode.ts
332 lines (274 loc) · 8.33 KB
/
compileNode.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
import {
ContainerNode,
countNodes,
IFunctionNode,
isBlankNode,
ISelectCaseNode,
ISelectNode,
isSelectNode,
ITextNode,
Node,
} from '../parser';
import {visitNode} from './visitNode';
import {die, jsonStringify} from '../misc';
import {pluralCategories, PluralCategory, RuntimeMethod} from '@mfml/runtime';
import {compilePropertyName} from '@smikhalevski/codegen';
export interface INodeCompilerOptions {
/**
* The key that is used as the default for `select`.
*
* @default `"other"`
*/
otherSelectCaseKey?: string;
/**
* The name of the temporary variable used by `plural`, `select` and `selectordinal` to store the detected index.
*/
indexVarName: string;
/**
* The name of the variable that holds a locale or a source code of a literal locale string.
*/
localeSrc: string;
/**
* Returns the name of the the variable that holds an argument value.
*/
provideArgumentVarName(argumentName: string): string;
/**
* Triggered if a function node was rendered.
*
* @param node The node that was rendered.
*/
onFunctionUsed?(node: IFunctionNode): void;
/**
* Triggered if a select node was rendered.
*
* @param node The node that was rendered.
*/
onSelectUsed?(node: ISelectNode): void;
/**
* Triggered if a runtime method usage was rendered.
*
* @param runtimeMethod The name of the used runtime method.
* @param indexVarUsed `true` if the method used an index var to store temporary runtime result.
*/
onRuntimeMethodUsed?(runtimeMethod: RuntimeMethod, indexVarUsed: boolean): void;
}
/**
* Compiles an AST node to a source code. This may return an empty string if an AST nodes doesn't describe any
* meaningful value.
*
* @param node The node to compile.
* @param options The compiler options.
* @param [attributeMode = false] If `true` then attribute runtime methods are used.
*/
export function compileNode(node: Node, options: Readonly<INodeCompilerOptions>, attributeMode = false): string {
const {
localeSrc,
otherSelectCaseKey = 'other',
provideArgumentVarName,
onFunctionUsed,
onRuntimeMethodUsed,
} = options;
let src = '';
let separatorRequired = false;
const enterChild = () => {
if (separatorRequired) {
src += ',';
}
separatorRequired = true;
};
const enterBlock = () => {
separatorRequired = false;
};
const compileFragment = (node: ContainerNode, next: () => void) => {
const count = countNodes(node.children);
if (count === 0) {
return;
}
if (count === 1) {
next();
return;
}
enterChild();
if (count === 2) {
const runtimeMethod = attributeMode ? RuntimeMethod.ATTRIBUTE_FRAGMENT : RuntimeMethod.FRAGMENT;
onRuntimeMethodUsed?.(runtimeMethod, false);
src += runtimeMethod + '(';
enterBlock();
next();
src += ')';
}
};
visitNode(node, {
fragment: compileFragment,
selectCase: compileFragment,
element(node, nextAttributes, nextChildren) {
enterChild();
onRuntimeMethodUsed?.(RuntimeMethod.ELEMENT, false);
src += RuntimeMethod.ELEMENT + '(' + jsonStringify(node.tagName);
if (node.attributes.length > 0) {
src += ',{';
enterBlock();
attributeMode = true;
nextAttributes();
attributeMode = false;
src += '}';
} else if (countNodes(node.children) !== 0) {
src += ',null';
}
nextChildren();
src += ')';
},
attribute(node, next) {
enterChild();
src += compilePropertyName(node.name) + ':';
if (node.children.length === 0) {
src += 'true';
return;
}
if (countNodes(node.children) === 0) {
src += '""';
return;
}
enterBlock();
compileFragment(node, next);
},
text(node: ITextNode) {
if (isBlankNode(node)) {
return;
}
enterChild();
src += jsonStringify(node.value);
},
argument(node) {
enterChild();
const runtimeMethod = attributeMode ? RuntimeMethod.ATTRIBUTE_ARGUMENT : RuntimeMethod.ARGUMENT;
onRuntimeMethodUsed?.(runtimeMethod, false);
src += runtimeMethod + `(${localeSrc},${provideArgumentVarName(node.name)})`;
},
function(node, next) {
enterChild();
const runtimeMethod = attributeMode ? RuntimeMethod.ATTRIBUTE_FUNCTION : RuntimeMethod.FUNCTION;
onFunctionUsed?.(node);
onRuntimeMethodUsed?.(runtimeMethod, false);
src += runtimeMethod + `(${localeSrc},${provideArgumentVarName(node.argumentName)},${jsonStringify(node.name)}`;
compileFragment(node, next);
src += ')';
},
plural(node) {
enterChild();
src += compileSelect(node, RuntimeMethod.PLURAL, attributeMode, PluralCategory.OTHER, true, options);
},
selectOrdinal(node) {
enterChild();
src += compileSelect(node, RuntimeMethod.SELECT_ORDINAL, attributeMode, PluralCategory.OTHER, true, options);
},
select(node) {
enterChild();
src += compileSelect(node, RuntimeMethod.SELECT, attributeMode, otherSelectCaseKey, false, options);
},
octothorpe(node) {
let selectNode = node.parent;
while (selectNode != null && !isSelectNode(selectNode)) {
selectNode = selectNode.parent;
}
if (selectNode) {
enterChild();
onRuntimeMethodUsed?.(RuntimeMethod.ARGUMENT, false);
src += RuntimeMethod.ARGUMENT + `(${localeSrc},${provideArgumentVarName(selectNode.argumentName)})`;
} else {
die('Octothorpe must be nested in a select', node.start);
}
},
});
return src;
}
/**
* Compiles a select node to a source code.
*
* @param node The select node to compile.
* @param runtimeMethod The runtime method name that is used to resolve what case to use.
* @param attributeMode If `true` then attribute runtime methods are used.
* @param otherSelectCaseKey The case key that would be a default.
* @param plural If `true` then runtime method call is formatted like plural.
* @param options The node compiler options.
*
* @returns The compiled source.
*/
function compileSelect(
node: ISelectNode,
runtimeMethod: RuntimeMethod,
attributeMode: boolean,
otherSelectCaseKey: string,
plural: boolean,
options: Readonly<INodeCompilerOptions>,
): string {
const {
provideArgumentVarName,
indexVarName,
localeSrc,
onRuntimeMethodUsed,
onSelectUsed,
} = options;
let knownKeys: Array<string>;
if (plural) {
knownKeys = pluralCategories;
} else {
knownKeys = [];
for (const childNode of node.children) {
if (knownKeys.indexOf(childNode.key) === -1) {
knownKeys.push(childNode.key);
}
}
}
let otherNode: ISelectCaseNode | undefined;
let childrenSrc = '';
let childrenCount = 0;
for (let i = 0; i < knownKeys.length; ++i) {
for (const childNode of node.children) {
if (childNode.key !== knownKeys[i] || isBlankNode(childNode)) {
continue;
}
if (childNode.key === otherSelectCaseKey) {
otherNode = childNode;
continue;
}
if (childrenCount > 0) {
childrenSrc += ':' + indexVarName;
}
childrenSrc += `===${i}?` + compileNode(childNode, options, attributeMode);
++childrenCount;
}
}
onSelectUsed?.(node);
let src = '';
if (childrenCount === 0) {
return otherNode ? compileNode(otherNode, options, attributeMode) : src;
}
if (childrenCount > 1) {
src += `(${indexVarName}=`;
}
onRuntimeMethodUsed?.(runtimeMethod, childrenCount > 1);
src += runtimeMethod
+ '('
+ (plural ? localeSrc + ',' : '')
+ provideArgumentVarName(node.argumentName)
+ (plural ? '' : ',' + knownKeys.map(jsonStringify).join(','))
+ ')';
let otherSrc;
if (otherNode != null) {
otherSrc = compileNode(otherNode, options, attributeMode);
} else {
otherSrc = compileEmptyFragment(onRuntimeMethodUsed);
}
src += childrenCount > 1 ? `,${indexVarName}${childrenSrc}:${otherSrc})` : childrenSrc + ':' + otherSrc;
return src;
}
/**
* Compiles an empty fragment and notifies a listener.
*
* @param onRuntimeMethodUsed The listener to notify.
*/
export function compileEmptyFragment(onRuntimeMethodUsed: INodeCompilerOptions['onRuntimeMethodUsed']): string {
onRuntimeMethodUsed?.(RuntimeMethod.FRAGMENT, false);
return RuntimeMethod.FRAGMENT + '()';
}