-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.ts
167 lines (141 loc) · 3.56 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
import { NodeTypes } from './ast'
const enum TagType {
Start,
End,
}
export function baseParse(content: string) {
const context = createParserContext(content)
return createRoot(parseChildren(context, []))
}
function parseChildren(context, ancestors) {
const nodes: any = []
while (!isEnd(context, ancestors)) {
let node
const s = context.source
if (s.startsWith('{{')) {
// 处理 {{message}}
node = parseInterpolation(context)
} else if (s[0] === '<') {
// 处理element标签
if (/[a-z]/i.test(s[1])) {
node = parseElement(context, ancestors)
}
}
// 处理Text类型
if (!node) {
node = parseText(context)
}
nodes.push(node)
}
return nodes
}
function isEnd(context, ancestors) {
// 当遇到结束标签的时候
const s = context.source
// </div>
if (s.startsWith('</')) {
for (let i = ancestors.length - 1; i >= 0; i--) {
const tag = ancestors[i].tag
if (startsWithEndTagOpen(s, tag)) {
return true
}
}
}
// 当source没有值的时候
return !s
}
// 处理Text类型
function parseText(context) {
// 处理Text节点时,遇到'{{'停止截取
let endIndex = context.source.length
let endTokens = ['<', '{{']
for (let i = 0; i < endTokens.length; i++) {
const index = context.source.indexOf(endTokens[i])
if (index !== -1 && endIndex > index) {
endIndex = index
}
}
const content = paseTextData(context, endIndex)
return {
type: NodeTypes.TEXT,
content,
}
}
// 处理element标签
function parseElement(context: any, ancestors) {
const element: any = parseTag(context, TagType.Start)
ancestors.push(element)
element.children = parseChildren(context, ancestors)
ancestors.pop()
if (startsWithEndTagOpen(context.source, element.tag)) {
parseTag(context, TagType.End)
} else {
throw new Error(`缺少结束标签:${element.tag}`)
}
return element
}
function startsWithEndTagOpen(source, tag) {
return (
source.startsWith('</') &&
source.slice(2, 2 + tag.length).toLowerCase() === tag.toLowerCase()
)
}
function parseTag(context, type: TagType) {
// 1. 解析tag
const match: any = /^<\/?([a-z]*)/i.exec(context.source)
const tag = match[1]
// 2. 删除处理完成的代码
advanceBy(context, match[0].length)
advanceBy(context, 1)
if (type === TagType.End) return
return {
type: NodeTypes.ELEMENT,
tag,
}
}
// 处理插值
function parseInterpolation(context) {
const openDelimiter = '{{'
const closeDelimiter = '}}'
const closeIndex = context.source.indexOf(
closeDelimiter,
openDelimiter.length
)
advanceBy(context, openDelimiter.length)
const rawContentLength = closeIndex - openDelimiter.length
const rawContent = paseTextData(context, rawContentLength)
const content = rawContent.trim()
advanceBy(context, closeDelimiter.length)
return {
type: NodeTypes.INTERPOLATION,
content: {
type: NodeTypes.SIMPLE_EXPERSSION,
content,
},
}
}
// 删除处理完成的代码
function advanceBy(context: any, length: number) {
context.source = context.source.slice(length)
}
// 截取文本
function paseTextData(context: any, length) {
// 1. 获取content
const content = context.source.slice(0, length)
// 2. 推进 删除处理完成的代码
advanceBy(context, length)
return content
}
// 创建根节点
function createRoot(children) {
return {
children,
type: NodeTypes.ROOT,
}
}
// 创建上下文
function createParserContext(content: string): any {
return {
source: content,
}
}