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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Tests for chained method calls after dereference constructs.
//!
//! Perl allows chaining method calls after hash/array dereferences:
//! $obj->{key}->method()
//! $obj->[0]->method()
//! $self->{db}->resultset('Foo')->search({})
//! $hash{key}->method->another
//! $ref->method->{key}->[0]->final_method
//!
//! The parser must correctly continue the postfix chain after subscript
//! operations following an arrow.
#[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:\n{}", 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()
),
}
}
// ---------------------------------------------------------------
// Method call after arrow-hash deref: $obj->{key}->method()
// ---------------------------------------------------------------
#[test]
fn method_after_arrow_hash_deref() {
let code = "$obj->{key}->method();";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
match &expr.kind {
NodeKind::MethodCall { object, method, .. } => {
assert_eq!(method, "method", "Expected method name 'method', got '{}'", method);
// The object should be a Binary{} (hash access) node wrapping $obj
assert_eq!(
object.kind.kind_name(),
"Binary",
"Object of method call should be Binary (hash deref), got {}",
object.kind.kind_name(),
);
}
_ => panic!(
"Expected MethodCall for $obj->{{key}}->method(), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Method call after arrow-array deref: $obj->[0]->method()
// ---------------------------------------------------------------
#[test]
fn method_after_arrow_array_deref() {
let code = "$obj->[0]->method();";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
match &expr.kind {
NodeKind::MethodCall { object, method, .. } => {
assert_eq!(method, "method", "Expected method name 'method', got '{}'", method);
// The object should be a Binary[] (array access) node wrapping $obj
assert_eq!(
object.kind.kind_name(),
"Binary",
"Object of method call should be Binary (array deref), got {}",
object.kind.kind_name(),
);
}
_ => panic!(
"Expected MethodCall for $obj->[0]->method(), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Deep chain: $self->{db}->resultset('Foo')->search({})
// ---------------------------------------------------------------
#[test]
fn deep_chain_hash_deref_then_methods() {
let code = "$self->{db}->resultset('Foo')->search({});";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
// Outermost should be ->search({})
match &expr.kind {
NodeKind::MethodCall { object, method, args } => {
assert_eq!(method, "search");
assert!(!args.is_empty(), "search() should have at least one argument");
// The object of search() should be ->resultset('Foo')
match &object.kind {
NodeKind::MethodCall { method: inner_method, .. } => {
assert_eq!(inner_method, "resultset");
}
_ => panic!(
"Expected inner MethodCall (resultset), got {} (sexp: {})",
object.kind.kind_name(),
object.to_sexp()
),
}
}
_ => panic!(
"Expected MethodCall for deep chain, got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Chain after bare hash access: $hash{key}->method->another
// ---------------------------------------------------------------
#[test]
fn chain_after_bare_hash_access() {
let code = "$hash{key}->method->another;";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
// Outermost: ->another
match &expr.kind {
NodeKind::MethodCall { object, method, .. } => {
assert_eq!(method, "another");
// Inner: ->method
match &object.kind {
NodeKind::MethodCall { object: inner_obj, method: inner_method, .. } => {
assert_eq!(inner_method, "method");
// Innermost: $hash{key} (Binary hash access)
assert_eq!(
inner_obj.kind.kind_name(),
"Binary",
"Innermost should be Binary (hash access), got {}",
inner_obj.kind.kind_name(),
);
}
_ => panic!(
"Expected inner MethodCall, got {} (sexp: {})",
object.kind.kind_name(),
object.to_sexp()
),
}
}
_ => panic!(
"Expected MethodCall for chain after bare hash, got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Mixed chain: $ref->method->{key}->[0]->final_method
// ---------------------------------------------------------------
#[test]
fn mixed_chain_method_hash_array_method() {
let code = "$ref->method->{key}->[0]->final_method;";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
// Outermost should be ->final_method
match &expr.kind {
NodeKind::MethodCall { method, .. } => {
assert_eq!(method, "final_method");
}
_ => panic!(
"Expected MethodCall (final_method), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Arrow-hash deref without method (baseline): $obj->{key}
// ---------------------------------------------------------------
#[test]
fn arrow_hash_deref_alone() {
let code = "$obj->{key};";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
assert_eq!(
expr.kind.kind_name(),
"Binary",
"Expected Binary (hash access), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp(),
);
}
// ---------------------------------------------------------------
// Arrow-array deref without method (baseline): $obj->[0]
// ---------------------------------------------------------------
#[test]
fn arrow_array_deref_alone() {
let code = "$obj->[0];";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
assert_eq!(
expr.kind.kind_name(),
"Binary",
"Expected Binary (array access), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp(),
);
}
// ---------------------------------------------------------------
// Hash deref then array deref: $obj->{key}->[0]
// ---------------------------------------------------------------
#[test]
fn hash_deref_then_array_deref() {
let code = "$obj->{key}->[0];";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
// Outermost should be ->[] (arrow array deref)
match &expr.kind {
NodeKind::Binary { op, left, .. } => {
assert_eq!(op, "->[]", "Outer should be arrow array deref, got op={}", op);
// Inner should be ->{} (arrow hash deref)
match &left.kind {
NodeKind::Binary { op: inner_op, .. } => {
assert_eq!(
inner_op, "->{}",
"Inner should be arrow hash deref, got op={}",
inner_op
);
}
_ => panic!(
"Expected inner Binary (hash), got {} (sexp: {})",
left.kind.kind_name(),
left.to_sexp()
),
}
}
_ => panic!(
"Expected Binary (array subscript), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Array deref then hash deref: $obj->[0]->{key}
// ---------------------------------------------------------------
#[test]
fn array_deref_then_hash_deref() {
let code = "$obj->[0]->{key};";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
// Outermost should be ->{} (arrow hash deref)
match &expr.kind {
NodeKind::Binary { op, left, .. } => {
assert_eq!(op, "->{}", "Outer should be arrow hash deref, got op={}", op);
// Inner should be ->[] (arrow array deref)
match &left.kind {
NodeKind::Binary { op: inner_op, .. } => {
assert_eq!(
inner_op, "->[]",
"Inner should be arrow array deref, got op={}",
inner_op
);
}
_ => panic!(
"Expected inner Binary (array), got {} (sexp: {})",
left.kind.kind_name(),
left.to_sexp()
),
}
}
_ => panic!(
"Expected Binary (hash subscript), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Method returning hashref chained: $obj->get_config->{timeout}
// ---------------------------------------------------------------
#[test]
fn method_then_hash_deref() {
let code = "$obj->get_config->{timeout};";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
// Outermost should be ->{} (arrow hash deref on method result)
match &expr.kind {
NodeKind::Binary { op, left, .. } => {
assert_eq!(op, "->{}", "Outer should be arrow hash deref, got op={}", op);
// Inner should be the MethodCall
match &left.kind {
NodeKind::MethodCall { method, .. } => {
assert_eq!(method, "get_config");
}
_ => panic!(
"Expected inner MethodCall, got {} (sexp: {})",
left.kind.kind_name(),
left.to_sexp()
),
}
}
_ => panic!(
"Expected Binary (hash subscript), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Method returning arrayref chained: $obj->get_items->[0]
// ---------------------------------------------------------------
#[test]
fn method_then_array_deref() {
let code = "$obj->get_items->[0];";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
// Outermost should be ->[] (arrow array deref on method result)
match &expr.kind {
NodeKind::Binary { op, left, .. } => {
assert_eq!(op, "->[]", "Outer should be arrow array deref, got op={}", op);
// Inner should be the MethodCall
match &left.kind {
NodeKind::MethodCall { method, .. } => {
assert_eq!(method, "get_items");
}
_ => panic!(
"Expected inner MethodCall, got {} (sexp: {})",
left.kind.kind_name(),
left.to_sexp()
),
}
}
_ => panic!(
"Expected Binary (array subscript), got {} (sexp: {})",
expr.kind.kind_name(),
expr.to_sexp()
),
}
}
// ---------------------------------------------------------------
// Arrow dereference should produce distinct op from bare access:
// $obj->{key} should produce op "->{}" (arrow hash deref)
// $obj{key} should produce op "{}" (bare hash access)
// $obj->[0] should produce op "->[]" (arrow array deref)
// $obj[0] should produce op "[]" (bare array access)
// ---------------------------------------------------------------
#[test]
fn arrow_hash_deref_distinct_from_bare_hash() {
// Arrow dereference: $obj->{key}
let code_arrow = "$obj->{key};";
let expr_arrow = unwrap_expr_stmt(parse_first_stmt(code_arrow));
let arrow_op = match &expr_arrow.kind {
NodeKind::Binary { op, .. } => op.clone(),
_ => panic!("Expected Binary for arrow deref, got {}", expr_arrow.to_sexp()),
};
// Bare hash access: $obj{key}
let code_bare = "$obj{key};";
let expr_bare = unwrap_expr_stmt(parse_first_stmt(code_bare));
let bare_op = match &expr_bare.kind {
NodeKind::Binary { op, .. } => op.clone(),
_ => panic!("Expected Binary for bare hash, got {}", expr_bare.to_sexp()),
};
assert_eq!(
arrow_op, "->{}",
"Arrow hash deref should use op '->{{}}' but got '{}'",
arrow_op
);
assert_eq!(bare_op, "{}", "Bare hash access should use op '{{}}' but got '{}'", bare_op);
}
#[test]
fn arrow_array_deref_distinct_from_bare_array() {
// Arrow dereference: $obj->[0]
let code_arrow = "$obj->[0];";
let expr_arrow = unwrap_expr_stmt(parse_first_stmt(code_arrow));
let arrow_op = match &expr_arrow.kind {
NodeKind::Binary { op, .. } => op.clone(),
_ => panic!("Expected Binary for arrow deref, got {}", expr_arrow.to_sexp()),
};
// Bare array access: $obj[0]
let code_bare = "$obj[0];";
let expr_bare = unwrap_expr_stmt(parse_first_stmt(code_bare));
let bare_op = match &expr_bare.kind {
NodeKind::Binary { op, .. } => op.clone(),
_ => panic!("Expected Binary for bare array, got {}", expr_bare.to_sexp()),
};
assert_eq!(
arrow_op, "->[]",
"Arrow array deref should use op '->[]' but got '{}'",
arrow_op
);
assert_eq!(bare_op, "[]", "Bare array access should use op '[]' but got '{}'", bare_op);
}
// ---------------------------------------------------------------
// Arrow dereference in chains should use the distinct operators
// ---------------------------------------------------------------
#[test]
fn mixed_chain_uses_arrow_deref_ops() {
// $ref->method->{key}->[0]->final_method
// Should produce: MethodCall(final_method,
// ->[](->{}(MethodCall(method, $ref), key), 0))
let code = "$ref->method->{key}->[0]->final_method;";
assert_no_errors(code);
let expr = unwrap_expr_stmt(parse_first_stmt(code));
// Walk the chain from outside in:
// 1. Outermost: ->final_method (MethodCall)
let method_call = match &expr.kind {
NodeKind::MethodCall { object, method, .. } => {
assert_eq!(method, "final_method");
object
}
_ => panic!("Expected MethodCall, got {}", expr.to_sexp()),
};
// 2. Next: ->[0] (Binary with op "->[]")
let array_deref = match &method_call.kind {
NodeKind::Binary { op, left, .. } => {
assert_eq!(op, "->[]", "Expected arrow array deref '->[]', got '{}'", op);
left
}
_ => panic!("Expected Binary (->[] array deref), got {}", method_call.to_sexp()),
};
// 3. Next: ->{key} (Binary with op "->{}")
match &array_deref.kind {
NodeKind::Binary { op, left, .. } => {
assert_eq!(op, "->{}", "Expected arrow hash deref '->{{}}', got '{}'", op);
// 4. Innermost: ->method (MethodCall)
match &left.kind {
NodeKind::MethodCall { method, .. } => {
assert_eq!(method, "method");
}
_ => panic!(
"Expected inner MethodCall, got {} (sexp: {})",
left.kind.kind_name(),
left.to_sexp()
),
}
}
_ => panic!("Expected Binary (arrow hash deref), got {}", array_deref.to_sexp()),
}
}
}