Repository navigation
Expand file tree
/
Copy pathprimary-parser.ts
More file actions
221 lines (199 loc) · 5.22 KB
/
Copy pathprimary-parser.ts
File metadata and controls
221 lines (199 loc) · 5.22 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
/**
* 基本类型解析器 - 负责解析字面量、标识符、数组、对象等基本类型
*/
import type {
ASTNode,
ArrayExpr,
Identifier,
NumberLiteral,
ObjectExpr,
ObjectProperty,
StringLiteral,
} from "../../types/ast-types";
import { ESCAPE_CHARS } from "./constants";
import { Tokenizer } from "./tokenizer";
import { isDigit, isHexDigit, isIdentifierPart } from "./utils";
/**
* PrimaryParser 类 - 继承 Tokenizer,提供基本类型的解析功能
*/
export class PrimaryParser extends Tokenizer {
/**
* 解析数字字面量
*/
protected parseNumber(): NumberLiteral {
const start = this.pos;
// 处理十六进制、八进制、二进制
if (this.peek() === "0") {
const next = this.peekAt(1)?.toLowerCase();
if (next === "x" || next === "o" || next === "b") {
this.advance();
this.advance();
while (isHexDigit(this.peek())) {
this.advance();
}
const raw = this.source.slice(start, this.pos);
return {
type: "NumberLiteral",
value: Number(raw),
raw,
};
}
}
// 整数部分
while (isDigit(this.peek())) {
this.advance();
}
// 小数部分
if (this.peek() === "." && isDigit(this.peekAt(1))) {
this.advance();
while (isDigit(this.peek())) {
this.advance();
}
}
// 指数部分
if (this.peek()?.toLowerCase() === "e") {
this.advance();
if (this.peek() === "+" || this.peek() === "-") {
this.advance();
}
while (isDigit(this.peek())) {
this.advance();
}
}
const raw = this.source.slice(start, this.pos);
return {
type: "NumberLiteral",
value: Number(raw),
raw,
};
}
/**
* 解析字符串字面量
*/
protected parseString(): StringLiteral {
const quote = this.peek() as "'" | '"' | "`";
this.advance();
let value = "";
while (this.pos < this.source.length && this.peek() !== quote) {
if (this.peek() === "\\") {
this.advance();
const escaped = this.peek();
if (escaped === "u") {
const unicodeStart = this.pos;
this.advance();
let hex = "";
for (let i = 0; i < 4; i++) {
const digit = this.peek();
if (!isHexDigit(digit)) {
throw new Error(`Invalid unicode escape at position ${unicodeStart}`);
}
hex += digit;
this.advance();
}
value += String.fromCharCode(Number.parseInt(hex, 16));
} else {
value += ESCAPE_CHARS[escaped] ?? escaped;
this.advance();
}
} else {
value += this.peek();
this.advance();
}
}
this.expect(quote);
return { type: "StringLiteral", value, quote };
}
/**
* 解析数组字面量
*/
protected parseArray(): ArrayExpr {
this.expect("[");
const elements: ASTNode[] = [];
this.skipWhitespace();
while (this.peek() !== "]") {
elements.push(this.parseExpression());
this.skipWhitespace();
if (this.peek() === ",") {
this.advance();
this.skipWhitespace();
} else {
break;
}
}
this.expect("]");
return { type: "ArrayExpr", elements };
}
/**
* 解析对象字面量
*/
protected parseObject(): ObjectExpr {
this.expect("{");
const properties: ObjectProperty[] = [];
this.skipWhitespace();
while (this.peek() !== "}") {
const prop = this.parseObjectProperty();
properties.push(prop);
this.skipWhitespace();
if (this.peek() === ",") {
this.advance();
this.skipWhitespace();
} else {
break;
}
}
this.expect("}");
return { type: "ObjectExpr", properties };
}
/**
* 解析对象属性
*/
protected parseObjectProperty(): ObjectProperty {
this.skipWhitespace();
let key: ASTNode;
let computed = false;
if (this.peek() === "[") {
this.advance();
this.skipWhitespace();
key = this.parseExpression();
this.skipWhitespace();
this.expect("]");
computed = true;
} else if (this.peek() === '"' || this.peek() === "'") {
key = this.parseString();
} else {
key = this.parseIdentifier();
}
this.skipWhitespace();
if (this.peek() === ":") {
this.advance();
this.skipWhitespace();
const value = this.parseExpression();
return { key, value, computed, shorthand: false };
}
// Shorthand property
if (key.type !== "Identifier") {
throw new Error("Shorthand property must be an identifier");
}
return { key, value: key, computed: false, shorthand: true };
}
/**
* 解析标识符
*/
protected parseIdentifier(): Identifier {
const start = this.pos;
while (isIdentifierPart(this.peek())) {
this.advance();
}
const name = this.source.slice(start, this.pos);
if (!name) {
throw new Error(`Expected identifier at position ${this.pos}`);
}
return { type: "Identifier", name };
}
/**
* 解析表达式 - 需要在子类中实现
*/
protected parseExpression(): ASTNode {
throw new Error("parseExpression must be implemented in subclass");
}
}