-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpack.config.ts
384 lines (316 loc) · 13.4 KB
/
tpack.config.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/// <reference path="typings/node/node.d.ts" />
/// <reference path="typings/tpack/tpack.d.ts" />
import * as tpack from "tpack";
import * as ts from "typescript";
// nodes.ts => nodes.ts & nodeVisitor.ts
tpack.task("gen-nodes", function () {
tpack.allowOverwriting = true;
tpack.src("src/ast/nodes.ts").pipe((file, options) => {
// 第一步:语法解析。
const program = ts.createProgram([file.path], options);
const sourceFile = program.getSourceFiles()[program.getSourceFiles().length - 1];
// 第二步:提取类型信息。
const nodes = {};
for (const statement of sourceFile.statements) {
if (statement.kind !== ts.SyntaxKind.ClassDeclaration || !(statement.flags & ts.NodeFlags.Export)) {
continue;
}
const clazz = <ts.ClassDeclaration>statement;
const baseClazzType = clazz.heritageClauses && clazz.heritageClauses.length && clazz.heritageClauses[0].types[0];
let members = {};
let optional = {};
for (const member of clazz.members) {
if (member.kind !== ts.SyntaxKind.PropertyDeclaration) {
continue;
}
const prop = <ts.PropertyDeclaration>member;
if (!prop.type) {
continue;
}
members = members || {};
const type = sourceFile.text.substring(prop.type.pos, prop.type.end).trim();
members[(<ts.Identifier>prop.name).text] = type;
if (getDocComment(prop, false).indexOf("undefined") >= 0 || getDocComment(prop, false).indexOf("可能不存在") >= 0/* || isArrayType(type)*/) {
optional[(<ts.Identifier>prop.name).text] = true;
}
}
nodes[clazz.name.text] = {
summary: getDocComment(clazz),
isAbstract: clazz.modifiers && clazz.modifiers.some(t => t.kind === ts.SyntaxKind.AbstractKeyword),
name: clazz.name.text,
extends: baseClazzType && sourceFile.text.substring(baseClazzType.pos, baseClazzType.end).trim() || null,
members,
optional,
node: clazz,
};
}
// 第三步:删除非节点类。
for (const name in nodes) {
const type = nodes[name];
if (!isNodeType(name)) {
delete nodes[name];
continue;
}
for (const member in type.members) {
if (!isNodeType(type.members[member])) {
delete type.members[member];
}
}
}
// 第四步:继承父节点信息。
for (const name in nodes) {
const type = nodes[name];
if (type.isAbstract) continue;
let p = type;
while (p = nodes[p.extends]) {
var r = {};
for (const m in p.members) {
r[m] = p.members[m];
}
for (const m in type.members) {
r[m] = type.members[m];
}
type.members = r;
for (const m in p.optional) {
if (!type.optional[m]) {
type.optional[m] = p.optional[m];
}
}
}
}
// 第五步:修复类型信息。
let acceptSummary = getDocComment(getMember(nodes["Node"].node, "accept"), false);
let eachSummary = getDocComment(getMember(nodes["Node"].node, "each"), false);
let changes = [];
for (const name in nodes) {
const type = nodes[name];
if (type.isAbstract) {
continue;
}
const each = getMember(type.node, "each");
if (each) {
changes.push({
remove: true,
pos: each.pos,
end: each.end,
});
}
const eachContentItems = [];
for (const member in type.members) {
const memberType = type.members[member];
let tpl = isArrayType(memberType) ? `this.${member}.each(callback, scope)` : `callback.call(scope, this.${member}, "${member}", this) !== false`;
if (type.optional[member]) {
tpl = `(!this.${member} || ${tpl})`;
}
eachContentItems.push(tpl);
}
if (eachContentItems.length) {
const eachContent = `
${eachSummary}
each(callback: EachCallback, scope?: any) {
return ${eachContentItems.join(" &&\n ")};
}`;
changes.push({
insert: true,
pos: each ? each.pos : (<ts.ClassDeclaration>type.node).members.end,
content: eachContent
});
}
const accept = getMember(type.node, "accept");
if (accept) {
changes.push({
remove: true,
pos: accept.pos,
end: accept.end,
});
}
const content = `
${acceptSummary}
accept(vistior: NodeVisitor) {
return vistior.visit${type.name}(this);
}`;
changes.push({
insert: true,
pos: accept ? accept.pos : (<ts.ClassDeclaration>type.node).members.end,
content: content
});
}
// 第六步:应用修复。
let source = sourceFile.text;
changes.sort((x, y) => y.pos > x.pos ? 1 : y.pos < x.pos ? -1 : y.remove ? 1 : -1);
for (const change of changes) {
if (change.remove) {
source = source.substr(0, change.pos) + source.substr(change.end);
} else {
source = source.substr(0, change.pos) + change.content + source.substr(change.pos);
}
}
file.content = source;
// 第七步:生成 NodeVistior。
var result = `/**
* @fileOverview 节点访问器
* @generated 此文件可使用 \`$ tpack gen-nodes\` 命令生成。
*/
import * as nodes from './nodes';
/**
* 表示一个节点访问器。
*/
export abstract class NodeVisitor {
/**
* 访问一个逗号隔开的节点列表(<..., ...>。
* @param nodes 要访问的节点列表。
*/
visitNodeList<T extends nodes.Node>(nodes: nodes.NodeList<T>) {
for(const node of nodes) {
node.accept(this);
}
}
`;
for (const name in nodes) {
const type = nodes[name];
if (type.isAbstract) {
continue;
}
let memberList = [];
for (const member in type.members) {
memberList.push(` ${type.optional[member] ? "node." + member + " && " : ""}node.${member}.accept(this);`);
}
result += `
/**
* ${type.summary.replace("表示", "访问")}
* @param node 要访问的节点。
*/
visit${type.name}(node: nodes.${type.name}) {
${memberList.join("\n")}
}
`
function getNodeMembers(type) {
let r = [];
return r;
}
}
result += `
}`;
require("fs").writeFileSync("src/ast/nodeVisitor.ts", result);
function getDocComment(node: ts.Node, removeSpace = true) {
const comments: ts.CommentRange[] = (ts as any).getJsDocComments(node, sourceFile);
if (!comments || !comments.length) return;
const comment = comments[comments.length - 1];
const commentText = sourceFile.text.substring(comment.pos, comment.end);
return removeSpace ? commentText.substring(3, commentText.length - 2).replace(/^\s*\*\s*/gm, "").trim() : commentText;
}
function getMember(node: ts.ClassDeclaration, name: string) {
return (<ts.ClassDeclaration>node).members.filter(x => (<ts.Identifier>x.name).text == name)[0];
}
function isNodeType(type: string) {
if (/^NodeList</.test(type)) return true;
let p = nodes[type.replace(/\s*\|.*$/, "")];
while (p) {
if (p.name === "Node") return true;
p = nodes[p.extends];
}
return false;
}
function isArrayType(type: string) {
return /<.*>|\[\]/.test(type);
}
});
tpack.src("src/ast/tokenType.ts").pipe((file, options) => {
// 第一步:语法解析。
const program = ts.createProgram([file.path], options);
const sourceFile = program.getSourceFiles()[program.getSourceFiles().length - 1];
// 第二步:提取类型信息。
const data = {};
const tokenType = <ts.EnumDeclaration>sourceFile.statements.filter(t => t.kind === ts.SyntaxKind.EnumDeclaration && (<ts.EnumDeclaration>t).name.text === "TokenType")[0];
const val = 0;
for (const member of tokenType.members) {
const summary = getDocComment(member);
const string = (/关键字\s*(\w+)|\((.+?)\)/.exec(summary) || []).slice(1).join("")
const info = {
summary,
string,
keyword: /关键字/.test(summary) || /0x0|EOF|\}\.|xx|.\.\.\./.test(string) || !string,
value: val++
};
data[(<ts.Identifier>member.name).text] = info;
}
// generateKeywordLexer(data, 0);
// 第三步:生成优先级数组。
// 第四步:生成优先级数组。
function getDocComment(node: ts.Node, removeSpace = true) {
const comments: ts.CommentRange[] = (ts as any).getJsDocComments(node, sourceFile);
if (!comments || !comments.length) return;
const comment = comments[comments.length - 1];
const commentText = sourceFile.text.substring(comment.pos, comment.end);
return removeSpace ? commentText.substring(3, commentText.length - 2).replace(/^\s*\*\s*/gm, "").trim() : commentText;
}
function generateKeywordLexer(data, indent) {
const names = {};
const items = [];
for (const name in data) {
const info = data[name];
if (info.keyword) {
continue;
}
names[info.string] = name;
items.push(info.string);
}
items.sort();
let result = '';
for (let i = 0; i < items.length;) {
const c = items[i];
let hasSameCount = 0;
for (let j = i + 1; j < items.length; j++) {
if (items[j].charAt(0) === c.charAt(0)) {
hasSameCount++;
}
}
result += genIndents(indent) + "// " + c;
for (var j = 0; j < hasSameCount; j++) {
result += ", " + items[i + j + 1];
}
result += "\n";
result += genIndents(indent) + 'case CharCode.' + names[c] + ':\n';
if (hasSameCount === 0) {
result += 'result.type = TokenType.' + names[c] + ';\n';
result += 'break;\n';
i++;
}
if (hasSameCount === 1) {
result += genIndents(indent) + 'if(this.sourceText.charCodeAt(this.sourceStart) === TokenType.' + names[items[i + 1]] + ') {';
result += genIndents(indent + 1) + 'this.sourceStart++;';
result += genIndents(indent + 1) + 'result.type = TokenType.' + names[items[i + 1]] + ';\n';
result += genIndents(indent + 1) + 'break;\n';
result += genIndents(indent) + '}';
result += genIndents(indent) + 'result.type = TokenType.' + names[c] + ';\n';
result += genIndents(indent) + 'break;\n';
i += 2;
}
if (hasSameCount >= 2) {
result += genIndents(indent) + ' switch (this.sourceText.charCodeAt(this.sourceStart)) {\n';
for (let j = 0; j < hasSameCount; j++) {
result += genIndents(indent + 1) + 'case CharCode.' + names[items[i + j + 1]] + ':\n';
result += genIndents(indent + 2) + 'this.sourceStart++;\n';
result += genIndents(indent + 2) + 'result.type = TokenType.' + names[items[i + j + 1]] + ';\n';
result += genIndents(indent + 2) + 'break;\n';
}
result += genIndents(indent + 1) + 'default:\n';
result += genIndents(indent + 2) + 'result.type = TokenType.' + names[c] + ';\n';
result += genIndents(indent + 2) + 'break;\n';
result += genIndents(indent) + '}\n';
i += hasSameCount + 1;
result += genIndents(indent) + 'break;\n';
}
result += genIndents(indent) + '\n';
}
console.log(result)
function genIndents(indent) {
var result = '';
while (indent-- > 0)
result += '\t';
return result;
}
return result;
}
});
});