/
parser.ts
194 lines (158 loc) · 3.97 KB
/
parser.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
namespace Parser {
let sourceText: string;
let pos: number;
let end: number;
let token;
/**
* syntax
* html = '<' identifier props '>' html '</' identifier '>' | identifier
* props = '' | identifier '=' '"' identifier '"'
*
* identifier = [a-z]+
* whitespace = ' ' | '\n'
*
* example
* <div class="test"><span>abc</span></div>
*/
export function parse(code) {
sourceText = code;
pos = 0;
end = sourceText.length;
nextToken(); // <
assert(SyntaxKind.LeftBracket);
const html = parseHtml();
nextToken(); // eof
assert(SyntaxKind.EndOfFile);
return html;
}
function parseHtml() {
nextToken(); // identifier
assert(SyntaxKind.Identifier);
const tagName = token;
nextToken(); // identifier or '>'
const props = parseProps();
nextToken(); // identifier or '<'
let child;
if (token.kind === SyntaxKind.Identifier) {
child = token;
} else {
assert(SyntaxKind.LeftBracket);
child = parseHtml();
}
nextToken(); // </
assert(SyntaxKind.LeftBracketSlash);
nextToken(); // identifier
assert(SyntaxKind.Identifier);
const rightTagName = token;
nextToken(); // >
assert(SyntaxKind.RightBracket);
return {
tagName,
props,
child,
rightTagName,
};
}
function parseProps() {
const props = [];
while (true) {
if (token.kind === SyntaxKind.RightBracket) {
break;
}
assert(SyntaxKind.Identifier);
const propName = token;
nextToken(); // =
assert(SyntaxKind.Equal);
nextToken(); // "
assert(SyntaxKind.Quote);
nextToken(); // identifier
assert(SyntaxKind.Identifier);
const propValue = token;
props.push({
name: propName,
value: propValue,
});
nextToken(); // "
nextToken(); // identifier or '>'
}
return props;
}
function nextToken() {
while (true) {
if (pos >= end) {
return token = createNode(SyntaxKind.EndOfFile, pos, pos, null);
}
let ch = sourceText.charAt(pos);
switch (ch) {
case '<':
if (sourceText.charAt(pos + 1) === '/') {
return token = createNode(SyntaxKind.LeftBracketSlash, pos, pos += 2, '</');
}
return token = createNode(SyntaxKind.LeftBracket, pos, ++pos, '<');
case '>':
return token = createNode(SyntaxKind.RightBracket, pos, ++pos, '>');
case '=':
return token = createNode(SyntaxKind.Equal, pos, ++pos, '=');
case '"':
return token = createNode(SyntaxKind.Quote, pos, ++pos, '"');
case ' ':
case '\n':
++pos;
continue;
default:
if (isIdentifierStart(ch)) {
return token = scanIdentifier();
}
return token = createNode(SyntaxKind.RightBracket, pos, ++pos, ch);
}
}
}
function isIdentifierStart(ch) {
return ch >= 'a' && ch <= 'z';
}
function isIdentifierPart(ch) {
return isIdentifierStart(ch);
}
function scanIdentifier() {
const identifierStart = pos;
++pos;
while (true) {
if (pos >= end) {
break;
}
const ch = sourceText.charAt(pos);
if (!isIdentifierPart(ch)) {
break;
}
++pos;
}
const value = sourceText.slice(identifierStart, pos);
return createNode(SyntaxKind.Identifier, identifierStart, pos, value);
}
function createNode(kind: SyntaxKind, pos, end, value) {
return {
kind,
kindName: Object.values(SyntaxKind)[kind],
pos,
end,
value,
};
}
function assert(kind) {
if (token.kind === kind) {
return;
}
const message = `unexpected token: ${JSON.stringify(token)}`;
throw new Error(message);
}
enum SyntaxKind {
LeftBracket,
RightBracket,
Identifier,
Unknown,
EndOfFile,
LeftBracketSlash,
Equal,
Quote,
}
}