-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile3.html
404 lines (364 loc) · 10.8 KB
/
compile3.html
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
// 构造JavaScript ast
const State = {
initial: 1, // 初始状态
tagOpen: 2, // 标签开始状态
tagName: 3, // 标签名称状态
text: 4, // 文本状态
tagEnd: 5, // 结束标签状态
tagEndName: 6 // 结束标签名称状态
}
const isAlpha = (char) => { // 用于判断是否是字母
return char >= 'a' && char <= 'z' || char >= 'A' && char <= 'Z'
}
const tokenize = (str) => { // 用于切割模板,返回token数组
let currentState = State.initial
const chars = [] // 用于缓存字符
const tokens = [] // 存储生成的token,并作为函数返回值进行返回
while (str) { // 使用while循环开启自动机,只要str没有被消费尽,自动机就会一直运行
const char = str[0]
switch (currentState) {
case State.initial:
if (char === '<') {
currentState = State.tagOpen
str = str.slice(1)
} else if (isAlpha(char)) {
currentState = State.text
chars.push(char)
str = str.slice(1)
}
break;
case State.tagOpen:
if (isAlpha(char)) {
currentState = State.tagName
chars.push(char)
str = str.slice(1)
} else if (char === '/') {
currentState = State.tagEnd
str = str.slice(1)
}
break;
case State.tagName:
if (isAlpha(char)) {
chars.push(char)
str = str.slice(1)
} else if (char === '>') {
currentState = State.initial
tokens.push({
type: 'tag',
name: chars.join('')
})
chars.length = 0
str = str.slice(1)
}
break;
case State.text:
if (isAlpha(char)) {
chars.push(char)
str = str.slice(1)
} else if (char === '<') {
currentState = State.tagOpen
tokens.push({
type: 'text',
content: chars.join('')
})
chars.length = 0
str = str.slice(1)
}
break;
case State.tagEnd:
if (isAlpha(char)) {
currentState = State.tagEndName
chars.push(char)
str = str.slice(1)
}
break;
case State.tagEndName:
if (isAlpha(char)) {
chars.push(char)
str = str.slice(1)
} else if (char === '>') {
currentState = State.initial
tokens.push({
type: 'tagEnd',
name: chars.join('')
})
chars.length = 0
str = str.slice(1)
}
break;
}
}
return tokens
}
const parse = (str) => { // 解析器,返回模板ast
const tokens = tokenize(str)
const root = {
type: 'Root',
children: []
}
const elementStack = [root] // 及其重要,构造父子关系的关键角色
while (tokens.length) {
const parent = elementStack.at(-1)
const t = tokens[0]
switch (t.type) {
case 'tag':
const elementNode = {
type: 'Element',
tag: t.name,
children: []
}
parent.children.push(elementNode)
elementStack.push(elementNode)
break;
case 'text':
const textNode = {
type: 'Text',
content: t.content
}
parent.children.push(textNode)
break;
case 'tagEnd':
elementStack.pop()
break;
}
tokens.shift()
}
return root
}
const dump = (node, indent = 0) => {
const type = node.type
const desc = node.type === 'Root' ? '' : node.type === 'Element' ? node.tag : node.content
console.log(`${'-'.repeat(indent)}${type}:${desc}`);
if (node.children) {
node.children.forEach(n => dump(n, indent + 2))
}
}
const traverseNode = (ast, context) => { // 遍历节点
context.currentNode = ast
const exitFns = [] // 退出阶段的回调函数数组
const transforms = context.nodeTransforms
for (let i = 0; i < transforms.length; i++) {
const onExit = transforms[i](context.currentNode, context)
if (onExit) { // 通过转换函数返回一个函数,将该函数作为退出阶段的回调函数添加到exitFns数组中
exitFns.push(onExit)
}
if (!context.currentNode) return // 当前节点被移除后,停止执行后续转换函数
}
const children = context.currentNode.children
if (children) {
for (let i = 0; i < children.length; i++) {
context.parent = context.currentNode
context.childIndex = i
traverseNode(children[i], context)
}
}
// 在节点处理的最后阶段执行缓存到exitFns中的回调函数(回到上一个traverseNode函数中)
// 注意这里需要从末尾函数开始往前执行
let i = exitFns.length
while (i--) {
exitFns[i]()
}
}
const transformElement = (node) => { // 转换函数
// if (node.type === 'Element' && node.tag === 'p') {
// node.tag = 'h1'
// }
return () => {
// 返回一个在退出阶段执行的回调函数,当这里代码执行时当前转换节点的子节点一定都处理完毕了
if (node.type !== 'Element') return;
const callExp = createCallExpression('h', [
createStringLiteral(node.tag)
])
node.children.length === 1 ? callExp.arguments.push(node.children[0].jsNode) : callExp.arguments.push(createArrayExpression(node.children.map(c => c.jsNode)))
node.jsNode = callExp
}
}
const transformText = (node, context) => { // 转换函数
// if (node.type === 'Text') {
// // node.content = node.content.repeat(2)
// context.replaceNode({
// type: 'Element',
// tag: 'span'
// })
// }
if (node.type !== 'Text') return;
node.jsNode = createStringLiteral(node.content)
}
const transformRoot = (node) => {
return () => {
if (node.type !== 'Root') return;
const vnodeJSast = node.children[0].jsNode
node.jsNode = {
type: 'FunctionDecl',
id: {type: 'Identifier', name: 'render'},
params: [],
body: [
{
type: 'ReturnStatement',
return: vnodeJSast
}
]
}
}
}
// const removeText = (node, context) => { // 转换函数
// if (node.type === 'Text') {
// context.removeNode()
// }
// }
const transform = (ast) => { // 转换函数,返回JavaScript ast
const context = { // 上下文数据
currentNode: null,
childIndex: 0,
parent: null,
nodeTransforms: [
transformElement,
transformText,
transformRoot
// removeText
],
replaceNode (node) { // 替换ast中的节点
context.parent.children[context.childIndex] = node
context.currentNode = node
},
removeNode () { // 移除ast中的节点
context.parent.children.splice(context.childIndex, 1)
context.currentNode = null
}
}
traverseNode(ast, context)
dump(ast)
}
const createStringLiteral = (value) => { // 用来创建StringLiteral节点
return {
type: 'StringLiteral',
value
}
}
const createIdentifier = (name) => { // 用来创建Identifier节点
return {
type: 'Identifier',
name
}
}
const createArrayExpression = (elements) => { // 用来创建ArrayExpression节点
return {
type: 'ArrayExpression',
elements
}
}
const createCallExpression = (callee, arguments) => { // 用来创建CallExpression节点
return {
type: 'CallExpression',
callee: createIdentifier(callee),
arguments
}
}
const genNodeList = (nodes, context) => {
const {push} = context
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i]
genNode(node, context)
if (i < nodes.length - 1) {
push(', ')
}
}
}
const genFunctionDecl = (node, context) => {
const {push, indent, deIndent} = context
push(`function ${node.id.name}`)
push('(')
genNodeList(node.params, context)
push(') ')
push('{')
indent()
node.body.forEach(n => genNode(n, context))
deIndent(),
push('}')
}
const genReturnStatement = (node, context) => {
const {push} = context
push('return ')
genNode(node.return, context)
}
const genStringLiteral = (node, context) => {
const {push} = context
push(`${node.value}`)
}
const genCallExpression = (node, context) => {
const {push} = context
const {callee, arguments: args} = node
push(`${callee.name}(`)
genNodeList(args, context)
push(')')
}
const genArrayExpression = (node, context) => {
const {push} = context
push('[')
genNodeList(node.elements, context)
push(']')
}
// console.log(ast.jsNode); // 模板ast所对应的JavaScript ast就存储在ast根节点的jsNode中
const genNode = (node, context) => { // 匹配不同类型节点,并调用与之对应的生成器函数
switch (node.type) {
case 'FunctionDecl':
genFunctionDecl(node, context)
break;
case 'ReturnStatement':
genReturnStatement(node, context)
break;
case 'CallExpression':
genCallExpression(node, context)
break;
case 'StringLiteral':
genStringLiteral(node, context)
break;
case 'ArrayExpression':
genArrayExpression(node, context)
break;
}
}
const generate = (node) => { // node: JavaScript ast
console.log(node);
const context = { // 上下文
code: '', // 存储最终生成的渲染函数代码
push(code) {
context.code += code
},
currentIndent: 0,
newline() { // 换行函数
context.code += '\n' + ` `.repeat(context.currentIndent)
},
indent() { // 用来进行缩进
context.currentIndent++
context.newline()
},
deIndent() { // 用来取消缩进
context.currentIndent--
context.newline()
}
}
genNode(node, context) // 代码生成本质上是字符串拼接的艺术
return context.code
}
const compile = (template) => { // 编译函数
const ast = parse(template) // 获取模板ast
transform(ast)
const code = generate(ast.jsNode)
return code
}
const code = compile('<div><p>vue</p><p>template</p></div>')
console.log(code, 'code');
</script>
</html>