Repository navigation
Expand file tree
/
Copy pathexpression-parser.ts
More file actions
310 lines (275 loc) · 7.58 KB
/
Copy pathexpression-parser.ts
File metadata and controls
310 lines (275 loc) · 7.58 KB
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
/**
* 表达式解析器 - 负责解析各种表达式(二元运算、一元运算、条件表达式等)
*/
import { BUILTIN_CONSTRUCTORS, PRECEDENCE, RIGHT_ASSOCIATIVE } from "../../types/ast-types";
import type { ASTNode } from "../../types/ast-types";
import { ArrowFunctionParser } from "./arrow-function-parser";
import { isDigit, isIdentifierStart } from "./utils";
/**
* ExpressionParser 类 - 继承 ArrowFunctionParser,实现完整的表达式解析功能
*/
export class ExpressionParser extends ArrowFunctionParser {
/**
* 解析表达式的入口
*/
protected override parseExpression(): ASTNode {
return this.parseConditional();
}
/**
* 解析条件(三元)表达式: test ? consequent : alternate
*/
protected parseConditional(): ASTNode {
let node = this.parseBinary(0);
this.skipWhitespace();
if (this.peek() === "?") {
this.advance();
this.skipWhitespace();
const consequent = this.parseExpression();
this.skipWhitespace();
this.expect(":");
this.skipWhitespace();
const alternate = this.parseExpression();
node = {
type: "ConditionalExpr",
test: node,
consequent,
alternate,
};
}
return node;
}
/**
* 解析二元表达式,使用优先级爬升算法
*/
protected parseBinary(minPrec: number): ASTNode {
let left = this.parseUnary();
while (true) {
this.skipWhitespace();
const op = this.peekOperator();
if (!op || PRECEDENCE[op] === undefined || PRECEDENCE[op] < minPrec) {
break;
}
this.pos += op.length;
this.skipWhitespace();
const nextMinPrec = RIGHT_ASSOCIATIVE.has(op) ? PRECEDENCE[op] : PRECEDENCE[op] + 1;
const right = this.parseBinary(nextMinPrec);
left = {
type: "BinaryExpr",
operator: op,
left,
right,
};
}
return left;
}
/**
* 解析一元表达式
*/
protected parseUnary(): ASTNode {
this.skipWhitespace();
// generate() 对内置构造函数写出 `new Date(...)`。new 不进入 AST,
// 构造与否在求值时按同一名单判断。
if (this.matchKeyword("new")) {
this.skipWhitespace();
const callee = this.parseIdentifier();
if (!BUILTIN_CONSTRUCTORS.has(callee.name)) {
throw new Error(`Unsupported constructor: ${callee.name}`);
}
this.skipWhitespace();
this.expect("(");
const args = this.parseArguments();
this.expect(")");
return this.continuePostfix({
type: "CallExpr",
callee,
arguments: args,
optional: false,
});
}
const ch = this.peek();
// Single-character unary operators
if (ch === "!" || ch === "~" || ch === "+" || ch === "-") {
this.advance();
this.skipWhitespace();
return {
type: "UnaryExpr",
operator: ch,
argument: this.parseUnary(),
prefix: true,
};
}
// Keyword unary operators
for (const keyword of ["typeof", "void"] as const) {
if (this.matchKeyword(keyword)) {
this.skipWhitespace();
return {
type: "UnaryExpr",
operator: keyword,
argument: this.parseUnary(),
prefix: true,
};
}
}
return this.parsePostfix();
}
/**
* 解析后缀表达式(成员访问、函数调用、可选链等)
*/
protected parsePostfix(): ASTNode {
return this.continuePostfix(this.parsePrimary());
}
protected continuePostfix(start: ASTNode): ASTNode {
let node = start;
while (true) {
this.skipWhitespace();
const ch = this.peek();
if (ch === ".") {
this.advance();
this.skipWhitespace();
const property = this.parseIdentifier();
node = {
type: "MemberExpr",
object: node,
property,
computed: false,
optional: false,
};
} else if (ch === "[") {
this.advance();
this.skipWhitespace();
const property = this.parseExpression();
this.skipWhitespace();
this.expect("]");
node = {
type: "MemberExpr",
object: node,
property,
computed: true,
optional: false,
};
} else if (ch === "(") {
this.advance();
const args = this.parseArguments();
this.expect(")");
node = {
type: "CallExpr",
callee: node,
arguments: args,
optional: false,
};
} else if (ch === "?" && this.peekAt(1) === ".") {
this.advance();
this.advance();
this.skipWhitespace();
if (this.peek() === "[") {
this.advance();
this.skipWhitespace();
const property = this.parseExpression();
this.skipWhitespace();
this.expect("]");
node = {
type: "MemberExpr",
object: node,
property,
computed: true,
optional: true,
};
} else if (this.peek() === "(") {
this.advance();
const args = this.parseArguments();
this.expect(")");
node = {
type: "CallExpr",
callee: node,
arguments: args,
optional: true,
};
} else {
const property = this.parseIdentifier();
node = {
type: "MemberExpr",
object: node,
property,
computed: false,
optional: true,
};
}
} else {
break;
}
}
return node;
}
/**
* 解析基本表达式(字面量、标识符、括号表达式、箭头函数等)
*/
protected parsePrimary(): ASTNode {
this.skipWhitespace();
const ch = this.peek();
// 数字
if (isDigit(ch) || (ch === "." && isDigit(this.peekAt(1)))) {
return this.parseNumber();
}
// 字符串
if (ch === '"' || ch === "'" || ch === "`") {
return this.parseString();
}
// 数组
if (ch === "[") {
return this.parseArray();
}
// 对象
if (ch === "{") {
return this.parseObject();
}
// 括号表达式或箭头函数参数列表
if (ch === "(") {
const arrowFunc = this.tryParseArrowFunction();
if (arrowFunc) return arrowFunc;
this.advance();
this.skipWhitespace();
const expr = this.parseExpression();
this.skipWhitespace();
this.expect(")");
return expr;
}
// 关键字字面量
if (this.matchKeyword("true")) {
return { type: "BooleanLiteral", value: true };
}
if (this.matchKeyword("false")) {
return { type: "BooleanLiteral", value: false };
}
if (this.matchKeyword("null")) {
return { type: "NullLiteral" };
}
if (this.matchKeyword("undefined")) {
return { type: "Identifier", name: "undefined" };
}
// 标识符(可能是单参数箭头函数)
if (isIdentifierStart(ch)) {
const arrowFunc = this.tryParseSingleParamArrowFunction();
if (arrowFunc) return arrowFunc;
return this.parseIdentifier();
}
throw new Error(`Unexpected character at position ${this.pos}: ${ch}`);
}
/**
* 解析函数调用参数列表
*/
protected parseArguments(): ASTNode[] {
const args: ASTNode[] = [];
this.skipWhitespace();
while (this.peek() !== ")") {
args.push(this.parseExpression());
this.skipWhitespace();
if (this.peek() === ",") {
this.advance();
this.skipWhitespace();
} else {
break;
}
}
return args;
}
}