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
use super::super::*;
use super::ParseResult;
use crate::compiler::ir::TsCatchClause;
impl Parser {
/// Parse a TypeScript try-catch-finally statement with structured IR.
///
/// Syntax:
/// - `try { } catch (e) { }`
/// - `try { } catch { }` (parameter-less catch)
/// - `try { } finally { }`
/// - `try { } catch (e) { } finally { }`
/// - `try { } catch (e: Error) { }` (typed catch parameter)
pub(in super::super) fn parse_ts_try_stmt(&mut self) -> ParseResult<IrNode> {
let start_byte = self.current_byte_offset();
// Consume "try"
self.consume()
.ok_or_else(|| ParseError::unexpected_eof(self.current_byte_offset(), "try keyword"))?;
self.skip_whitespace();
// Parse the try block
if !self.at(SyntaxKind::LBrace) {
return Err(ParseError::new(
ParseErrorKind::UnexpectedToken,
self.current_byte_offset(),
)
.with_context("expected '{' after 'try'"));
}
let block = Box::new(
self.parse_block_stmt()
.map_err(|e| e.with_context("try block"))?,
);
self.skip_whitespace();
// Parse optional catch clause
let handler = if self.at(SyntaxKind::CatchKw) {
Some(self.parse_catch_clause()?)
} else {
None
};
self.skip_whitespace();
// Parse optional finally clause
let finalizer = if self.at(SyntaxKind::FinallyKw) {
self.consume(); // consume "finally"
self.skip_whitespace();
if !self.at(SyntaxKind::LBrace) {
return Err(ParseError::new(
ParseErrorKind::UnexpectedToken,
self.current_byte_offset(),
)
.with_context("expected '{' after 'finally'"));
}
Some(Box::new(
self.parse_block_stmt()
.map_err(|e| e.with_context("finally block"))?,
))
} else {
None
};
// Must have either catch or finally
if handler.is_none() && finalizer.is_none() {
return Err(ParseError::new(
ParseErrorKind::UnexpectedToken,
self.current_byte_offset(),
)
.with_context("try statement requires catch or finally clause"));
}
Ok(IrNode::TsTryStmt {
span: IrSpan::new(start_byte, self.current_byte_offset()),
block,
handler,
finalizer,
})
}
/// Parse a catch clause: `catch (e) { }` or `catch { }`
fn parse_catch_clause(&mut self) -> ParseResult<TsCatchClause> {
let start_byte = self.current_byte_offset();
// Consume "catch"
self.consume().ok_or_else(|| {
ParseError::unexpected_eof(self.current_byte_offset(), "catch keyword")
})?;
self.skip_whitespace();
// Parse optional catch parameter
let param = if self.at(SyntaxKind::LParen) {
self.consume(); // consume '('
self.skip_whitespace();
// Parse the catch parameter (identifier with optional type annotation)
let param_start = self.current_byte_offset();
// Parse the parameter name (could be a placeholder or identifier)
let name = self.parse_ts_ident_or_placeholder().ok_or_else(|| {
ParseError::new(
ParseErrorKind::ExpectedIdentifier,
self.current_byte_offset(),
)
.with_context("catch parameter name")
})?;
self.skip_whitespace();
// Check for optional type annotation
let param_node = if self.at(SyntaxKind::Colon) {
self.consume(); // consume ':'
self.skip_whitespace();
let type_ann = self
.parse_type_until(&[SyntaxKind::RParen])?
.ok_or_else(|| {
ParseError::new(
ParseErrorKind::ExpectedTypeAnnotation,
self.current_byte_offset(),
)
.with_context("catch parameter type")
})?;
IrNode::BindingIdent {
span: IrSpan::new(param_start, self.current_byte_offset()),
name: Box::new(name),
type_ann: Some(Box::new(type_ann)),
optional: false,
}
} else {
name
};
self.skip_whitespace();
// Expect ')'
if !self.at(SyntaxKind::RParen) {
return Err(ParseError::new(
ParseErrorKind::MissingClosingParen,
self.current_byte_offset(),
)
.with_context("catch parameter"));
}
self.consume(); // consume ')'
self.skip_whitespace();
Some(Box::new(param_node))
} else {
None
};
// Parse the catch body
if !self.at(SyntaxKind::LBrace) {
return Err(ParseError::new(
ParseErrorKind::UnexpectedToken,
self.current_byte_offset(),
)
.with_context("expected '{' after catch"));
}
let body = Box::new(
self.parse_block_stmt()
.map_err(|e| e.with_context("catch body"))?,
);
Ok(TsCatchClause {
span: IrSpan::new(start_byte, self.current_byte_offset()),
param,
body,
})
}
}