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
use super::super::expr::errors::{ParseError, ParseResult};
use super::super::*;
impl Parser {
pub(in super::super) fn parse_ts_ident_or_placeholder(&mut self) -> Option<IrNode> {
if self.at(SyntaxKind::At) {
// It's a placeholder - parse it
let first_placeholder = self.parse_interpolation().ok()?;
let mut parts = vec![first_placeholder];
// Check for more placeholders or identifier suffix (e.g., @{prefix}@{suffix}Name)
loop {
if self.at(SyntaxKind::At) {
// Another placeholder follows
let placeholder = self.parse_interpolation().ok()?;
parts.push(placeholder);
} else if let Some(token) = self.current() {
if token.kind == SyntaxKind::Ident {
// Identifier suffix follows
let suffix_token = self.consume().unwrap();
parts.push(IrNode::ident(&suffix_token));
break;
} else {
// End of identifier parts
break;
}
} else {
break;
}
}
// Return single placeholder or IdentBlock for multiple parts
if parts.len() == 1 {
Some(parts.into_iter().next().unwrap())
} else {
Some(IrNode::IdentBlock {
span: IrSpan::empty(),
parts,
})
}
} else if let Some(token) = self.current() {
if token.kind == SyntaxKind::Ident || token.kind.is_ts_keyword() {
let t = self.consume().unwrap();
Some(IrNode::ident(&t))
} else {
None
}
} else {
None
}
}
// parse_optional_type_params is now in expr/mod.rs with proper error handling
pub(in super::super) fn parse_param_list(&mut self) -> ParseResult<Vec<IrNode>> {
if !self.at(SyntaxKind::LParen) {
return Ok(vec![]);
}
self.consume(); // (
let mut params = Vec::new();
while !self.at_eof() && !self.at(SyntaxKind::RParen) {
self.skip_whitespace();
if self.at(SyntaxKind::RParen) {
break;
}
match self.parse_param() {
Ok(param) => params.push(param),
Err(e) => {
// If parse_param returns an error, check if we should recover
// This can happen with unexpected tokens like string literals in param position
if !self.at(SyntaxKind::RParen) && !self.at(SyntaxKind::Comma) {
// Return the error with context for better debugging
return Err(e.with_context("parsing function parameter list"));
}
// Otherwise we can recover by continuing to the next parameter
}
}
self.skip_whitespace();
if self.at(SyntaxKind::Comma) {
self.consume();
}
}
self.expect(SyntaxKind::RParen);
Ok(params)
}
pub(in super::super) fn parse_param(&mut self) -> ParseResult<IrNode> {
self.skip_whitespace();
// Parse optional modifiers (for constructor parameter properties)
// These are stored but currently we use simplified Param structure
let mut _accessibility = None;
let mut _readonly = false;
loop {
match self.current_kind() {
Some(SyntaxKind::PublicKw) => {
_accessibility = Some(Accessibility::Public);
self.consume();
self.skip_whitespace();
}
Some(SyntaxKind::PrivateKw) => {
_accessibility = Some(Accessibility::Private);
self.consume();
self.skip_whitespace();
}
Some(SyntaxKind::ProtectedKw) => {
_accessibility = Some(Accessibility::Protected);
self.consume();
self.skip_whitespace();
}
Some(SyntaxKind::ReadonlyKw) => {
_readonly = true;
self.consume();
self.skip_whitespace();
}
_ => break,
}
}
// Rest parameter? (check for `...` text)
let rest = if self.current_text() == Some("...") {
self.consume();
true
} else {
false
};
let name = self.parse_ts_ident_or_placeholder().ok_or_else(|| {
ParseError::unexpected_eof(self.current_byte_offset(), "parameter name")
})?;
self.skip_whitespace();
let optional = if self.at(SyntaxKind::Question) {
self.consume();
self.skip_whitespace();
true
} else {
false
};
let type_ann = if self.at(SyntaxKind::Colon) {
self.consume();
self.skip_whitespace();
self.push_context(Context::type_annotation([
SyntaxKind::Comma,
SyntaxKind::RParen,
SyntaxKind::Eq,
]));
let ty = self
.parse_type_until(&[SyntaxKind::Comma, SyntaxKind::RParen, SyntaxKind::Eq])?
.ok_or_else(|| {
ParseError::unexpected_eof(
self.current_byte_offset(),
"parameter type annotation",
)
})?;
self.pop_context();
Some(Box::new(ty))
} else {
None
};
let default_value = if self.at(SyntaxKind::Eq) {
self.consume();
self.skip_whitespace();
Some(Box::new(
self.parse_ts_expr_until(&[SyntaxKind::Comma, SyntaxKind::RParen])
.map_err(|e| e.with_context("parsing parameter default value"))?,
))
} else {
None
};
let name_span = name.span();
// Build the pattern
let binding = IrNode::BindingIdent {
span: name_span,
name: Box::new(name),
type_ann,
optional,
};
// Wrap with rest or assign pattern if needed
let pat = if rest {
Box::new(IrNode::RestPat {
span: name_span,
arg: Box::new(binding),
type_ann: None, // type_ann is already on BindingIdent
})
} else if let Some(right) = default_value {
Box::new(IrNode::AssignPat {
span: name_span,
left: Box::new(binding),
right,
})
} else {
Box::new(binding)
};
Ok(IrNode::Param {
span: name_span,
decorators: vec![],
pat,
})
}
/// Parses a type annotation until one of the terminator tokens is reached.
/// Uses the structured parse_type function instead of collecting raw tokens.
/// Also handles control flow blocks ({#for}, {#if}, etc.) that may appear in type contexts.
pub(in super::super) fn parse_type_until(
&mut self,
terminators: &[SyntaxKind],
) -> ParseResult<Option<IrNode>> {
self.skip_whitespace();
// Check if we're already at a terminator or EOF
if self.at_eof() {
return Ok(None);
}
if let Some(k) = self.current_kind() {
if terminators.contains(&k) {
return Ok(None);
}
// Handle control flow blocks in type contexts
if k.is_brace_hash_open() {
let control = self.parse_type_control_block(k)?;
return Ok(Some(control));
}
}
// Use the structured parse_type function which returns proper IR nodes
let ty = self.parse_type()?;
Ok(Some(ty))
}
pub(in super::super) fn parse_type_list_until(
&mut self,
terminator: SyntaxKind,
) -> ParseResult<Vec<IrNode>> {
let mut types = Vec::new();
while !self.at_eof() && !self.at(terminator) {
self.skip_whitespace();
if let Some(ty) = self.parse_type_until(&[SyntaxKind::Comma, terminator])? {
types.push(ty);
}
if self.at(SyntaxKind::Comma) {
self.consume();
}
}
Ok(types)
}
pub(in super::super) fn parse_ts_expr_until(
&mut self,
terminators: &[SyntaxKind],
) -> super::expr::errors::ParseResult<IrNode> {
#[cfg(debug_assertions)]
let debug_parser = std::env::var("MF_DEBUG_PARSER").is_ok();
#[cfg(debug_assertions)]
if debug_parser {
eprintln!(
"[MF_DEBUG_PARSER] parse_ts_expr_until: terminators={:?}",
terminators
);
}
// Use the structured Pratt parser with termination-aware context
let result = self.parse_expression_until(terminators);
#[cfg(debug_assertions)]
if debug_parser {
match &result {
Ok(node) => {
eprintln!(
"[MF_DEBUG_PARSER] parse_ts_expr_until: success, node={:?}",
node
);
}
Err(err) => {
eprintln!("[MF_DEBUG_PARSER] parse_ts_expr_until: error={:?}", err);
}
}
}
result
}
}