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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use crate::parser::Parser;
use perl_ast::ast::{Node, NodeKind};
/// Helper: parse code and return the full AST.
fn parse_program(code: &str) -> Node {
let mut parser = Parser::new(code);
match parser.parse() {
Ok(ast) => ast,
Err(e) => panic!("Parse failed for `{code}`: {e:?}"),
}
}
/// Helper: parse code and return the first statement node.
fn parse_first_stmt(code: &str) -> Node {
let ast = parse_program(code);
match ast.kind {
NodeKind::Program { mut statements } if !statements.is_empty() => {
statements.swap_remove(0)
}
_ => panic!("Expected Program with statements, got: {}", ast.to_sexp()),
}
}
/// Helper: check that the AST sexp contains no ERROR nodes.
fn assert_no_errors(code: &str) {
let ast = parse_program(code);
let sexp = ast.to_sexp();
assert!(!sexp.contains("ERROR"), "Parse of `{}` produced ERROR nodes: {}", code, sexp,);
}
/// Helper: extract expression from an ExpressionStatement.
fn unwrap_expr_stmt(stmt: Node) -> Node {
match stmt.kind {
NodeKind::ExpressionStatement { expression } => *expression,
_ => panic!(
"Expected ExpressionStatement, got {} (sexp: {})",
stmt.kind.kind_name(),
stmt.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Basic coderef invocation: $code->()
// ---------------------------------------------------------------
#[test]
fn coderef_invocation_no_args() {
let code = "$code->();";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
match &expr.kind {
NodeKind::FunctionCall { name, args } => {
assert_eq!(name, "->()", "Expected ->() coderef call, got name={}", name);
// First arg is the callee ($code)
assert!(!args.is_empty(), "Expected at least 1 arg (the callee expression)",);
assert_eq!(
args[0].kind.kind_name(),
"Variable",
"First arg should be the Variable node for $code",
);
// No additional args beyond the callee
assert_eq!(args.len(), 1, "Expected exactly 1 arg (callee only) for empty arglist");
}
_ => panic!(
"Expected FunctionCall for $code->(), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Coderef invocation with arguments: $code->($x, $y)
// ---------------------------------------------------------------
#[test]
fn coderef_invocation_with_args() {
let code = "$code->($x, $y);";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
match &expr.kind {
NodeKind::FunctionCall { name, args } => {
assert_eq!(name, "->()");
// First arg is callee, then $x and $y
assert!(args.len() >= 3, "Expected callee + 2 args, got {} args", args.len());
}
_ => panic!(
"Expected FunctionCall for $code->($x, $y), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Hash element coderef: $hash{callback}->()
// ---------------------------------------------------------------
#[test]
fn hash_element_coderef_invocation() {
let code = "$hash{callback}->();";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
match &expr.kind {
NodeKind::FunctionCall { name, args } => {
assert_eq!(name, "->()");
// First arg should be the hash access expression $hash{callback}
assert!(!args.is_empty(), "Expected callee arg");
assert_eq!(
args[0].kind.kind_name(),
"Binary",
"First arg should be Binary (hash access), got {}",
args[0].kind.kind_name(),
);
}
_ => panic!(
"Expected FunctionCall for $hash{{callback}}->(), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Array element coderef: $arr[0]->()
// ---------------------------------------------------------------
#[test]
fn array_element_coderef_invocation() {
let code = "$arr[0]->();";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
match &expr.kind {
NodeKind::FunctionCall { name, args } => {
assert_eq!(name, "->()");
assert!(!args.is_empty(), "Expected callee arg");
assert_eq!(
args[0].kind.kind_name(),
"Binary",
"First arg should be Binary (array access), got {}",
args[0].kind.kind_name(),
);
}
_ => panic!(
"Expected FunctionCall for $arr[0]->(), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Chained method then coderef: $obj->method->()
// ---------------------------------------------------------------
#[test]
fn chained_method_then_coderef() {
let code = "$obj->method->();";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
match &expr.kind {
NodeKind::FunctionCall { name, args } => {
assert_eq!(name, "->()");
// First arg should be the MethodCall node for $obj->method
assert!(!args.is_empty(), "Expected callee arg");
assert_eq!(
args[0].kind.kind_name(),
"MethodCall",
"First arg should be MethodCall for $obj->method, got {}",
args[0].kind.kind_name(),
);
}
_ => panic!(
"Expected FunctionCall for $obj->method->(), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// $self->can('method')->() chained
// ---------------------------------------------------------------
#[test]
fn can_method_coderef_invocation() {
let code = "$self->can('method')->();";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
match &expr.kind {
NodeKind::FunctionCall { name, args } => {
assert_eq!(name, "->()");
assert!(!args.is_empty(), "Expected callee arg");
// The callee should be a MethodCall ($self->can('method'))
assert_eq!(
args[0].kind.kind_name(),
"MethodCall",
"Expected MethodCall as callee, got {}",
args[0].kind.kind_name(),
);
}
_ => panic!(
"Expected FunctionCall for $self->can('method')->(), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Ampersand coderef call: &$code()
// ---------------------------------------------------------------
#[test]
fn ampersand_coderef_call() {
// &$code() - the & sigil with a scalar variable and parens
// This is handled in parse_variable_from_sigil, not in postfix.
// Just verify it parses without errors.
let code = "&$coderef();";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
let sexp = expr.to_sexp();
assert!(
sexp.contains("call") || sexp.contains("Call"),
"Expected some call form for &$coderef(), got: {}",
sexp,
);
}
// ---------------------------------------------------------------
// Coderef invocation with complex expression args
// ---------------------------------------------------------------
#[test]
fn coderef_invocation_with_string_arg() {
let code = r#"$handler->("hello");"#;
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
match &expr.kind {
NodeKind::FunctionCall { name, args } => {
assert_eq!(name, "->()");
// callee + one string arg
assert_eq!(args.len(), 2, "Expected callee + 1 arg, got {} args", args.len());
}
_ => panic!(
"Expected FunctionCall for $handler->(\"hello\"), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Double coderef chain: $a->()->()
// ---------------------------------------------------------------
#[test]
fn double_coderef_chain() {
let code = "$factory->()->();";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
// The outer call should be a ->() wrapping another ->()
match &expr.kind {
NodeKind::FunctionCall { name, args } => {
assert_eq!(name, "->()");
assert!(!args.is_empty(), "Expected callee arg");
// The inner callee should also be a ->() FunctionCall
match &args[0].kind {
NodeKind::FunctionCall { name: inner_name, .. } => {
assert_eq!(inner_name, "->()");
}
_ => panic!(
"Expected inner FunctionCall for $factory->()->(), got {} (sexp: {})",
args[0].kind.kind_name(),
args[0].to_sexp()
),
}
}
_ => panic!(
"Expected FunctionCall for $factory->()->(), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Coderef in assignment context
// ---------------------------------------------------------------
#[test]
fn coderef_invocation_in_assignment() {
let code = "my $result = $callback->(42);";
assert_no_errors(code);
// Drill into the AST to find the FunctionCall with name "->()"
let ast = parse_program(code);
let mut found = false;
if let NodeKind::Program { ref statements } = ast.kind {
if let Some(stmt) = statements.first() {
if let NodeKind::VariableDeclaration { initializer: Some(ref init), .. } = stmt.kind
{
if let NodeKind::FunctionCall { ref name, ref args } = init.kind {
assert_eq!(name, "->()");
// callee ($callback) + arg (42)
assert_eq!(
args.len(),
2,
"Expected callee + 1 arg, got {} args",
args.len()
);
found = true;
}
}
}
}
assert!(
found,
"Did not find ->() FunctionCall in assignment RHS (sexp: {})",
ast.to_sexp()
);
}
}