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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
//! AST node analysis — the `analyze_node` traversal and source-text helpers.
//!
//! This module provides the core recursive descent over the Perl AST that
//! produces semantic tokens and hover information. It is an `impl` block
//! extension of `SemanticAnalyzer`; Rust allows splitting `impl` blocks across
//! files within the same module, so the types defined in `mod.rs` are
//! directly accessible here.
use crate::SourceLocation;
use crate::ast::{Node, NodeKind};
use crate::symbol::{ScopeId, ScopeKind, SymbolKind};
use regex::Regex;
use std::sync::OnceLock;
use super::SemanticAnalyzer;
use super::builtins::{
get_builtin_documentation, is_builtin_function, is_control_keyword, is_file_test_operator,
};
use super::hover::HoverInfo;
use super::tokens::{SemanticToken, SemanticTokenModifier, SemanticTokenType};
impl SemanticAnalyzer {
/// Analyze a node and generate semantic information
pub(super) fn analyze_node(&mut self, node: &Node, scope_id: ScopeId) {
match &node.kind {
NodeKind::Program { statements } => {
for stmt in statements {
self.analyze_node(stmt, scope_id);
}
}
NodeKind::VariableDeclaration { declarator, variable, attributes, initializer } => {
// Add semantic token for declaration
if let NodeKind::Variable { sigil, name } = &variable.kind {
let token_type = match declarator.as_str() {
"my" | "state" => SemanticTokenType::VariableDeclaration,
"our" => SemanticTokenType::Variable,
"local" => SemanticTokenType::Variable,
_ => SemanticTokenType::Variable,
};
let mut modifiers = vec![SemanticTokenModifier::Declaration];
if declarator == "state" || attributes.iter().any(|a| a == ":shared") {
modifiers.push(SemanticTokenModifier::Static);
}
self.semantic_tokens.push(SemanticToken {
location: variable.location,
token_type,
modifiers,
});
// Add hover info
let hover = HoverInfo {
signature: format!("{} {}{}", declarator, sigil, name),
documentation: self.extract_documentation(node.location.start),
details: if attributes.is_empty() {
vec![]
} else {
vec![format!("Attributes: {}", attributes.join(", "))]
},
};
self.hover_info.insert(variable.location, hover);
}
if let Some(init) = initializer {
self.analyze_node(init, scope_id);
}
}
NodeKind::Variable { sigil, name } => {
let kind = match sigil.as_str() {
"$" => SymbolKind::scalar(),
"@" => SymbolKind::array(),
"%" => SymbolKind::hash(),
_ => return,
};
// Find the symbol definition
let symbols = self.symbol_table.find_symbol(name, scope_id, kind);
let token_type = if let Some(symbol) = symbols.first() {
match symbol.declaration.as_deref() {
Some("my") | Some("state") => SemanticTokenType::Variable,
Some("our") => SemanticTokenType::Variable,
_ => SemanticTokenType::Variable,
}
} else {
// Undefined variable
SemanticTokenType::Variable
};
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type,
modifiers: vec![],
});
// Add hover info if we found the symbol
if let Some(symbol) = symbols.first() {
let hover = HoverInfo {
signature: format!(
"{} {}{}",
symbol.declaration.as_deref().unwrap_or(""),
sigil,
name
)
.trim()
.to_string(),
documentation: symbol.documentation.clone(),
details: vec![format!(
"Defined at line {}",
self.line_number(symbol.location.start)
)],
};
self.hover_info.insert(node.location, hover);
}
}
NodeKind::Subroutine { name, prototype, signature, attributes, body, name_span: _ } => {
if let Some(sub_name) = name {
// Named subroutine
let token = SemanticToken {
location: node.location,
token_type: SemanticTokenType::FunctionDeclaration,
modifiers: vec![SemanticTokenModifier::Declaration],
};
self.semantic_tokens.push(token);
// Add hover info
let mut signature_str = format!("sub {}", sub_name);
if let Some(sig_node) = signature {
signature_str.push_str(&format_signature_params(sig_node));
}
let hover = HoverInfo {
signature: signature_str,
documentation: self.extract_sub_documentation(node.location.start, body),
details: if attributes.is_empty() {
vec![]
} else {
vec![format!("Attributes: {}", attributes.join(", "))]
},
};
self.hover_info.insert(node.location, hover);
} else {
// Anonymous subroutine (closure)
// Add semantic token for the 'sub' keyword
self.semantic_tokens.push(SemanticToken {
location: SourceLocation {
start: node.location.start,
end: node.location.start + 3, // "sub"
},
token_type: SemanticTokenType::Keyword,
modifiers: vec![],
});
// Add hover info for anonymous subs
let mut signature_str = "sub".to_string();
if let Some(sig_node) = signature {
signature_str.push_str(&format_signature_params(sig_node));
}
signature_str.push_str(" { ... }");
let mut details = vec!["Anonymous subroutine (closure)".to_string()];
if !attributes.is_empty() {
details.push(format!("Attributes: {}", attributes.join(", ")));
}
let hover = HoverInfo {
signature: signature_str,
documentation: self.extract_sub_documentation(node.location.start, body),
details,
};
self.hover_info.insert(node.location, hover);
}
{
// Get the subroutine scope from the symbol table
let sub_scope = self.get_scope_for(node, ScopeKind::Subroutine);
if let Some(proto) = prototype {
self.analyze_node(proto, sub_scope);
}
if let Some(sig) = signature {
self.analyze_node(sig, sub_scope);
}
self.analyze_node(body, sub_scope);
}
}
NodeKind::Method { name, signature, attributes, body } => {
self.semantic_tokens.push(SemanticToken {
location: node.location, // Approximate, ideally name span
token_type: SemanticTokenType::FunctionDeclaration,
modifiers: vec![SemanticTokenModifier::Declaration],
});
// Add hover info
let hover = HoverInfo {
signature: format!("method {}", name),
documentation: self.extract_sub_documentation(node.location.start, body),
details: if attributes.is_empty() {
vec![]
} else {
vec![format!("Attributes: {}", attributes.join(", "))]
},
};
self.hover_info.insert(node.location, hover);
// Analyze body in new scope (assumed same as Subroutine scope kind for now)
let sub_scope = self.get_scope_for(node, ScopeKind::Subroutine);
if let Some(sig) = signature {
self.analyze_node(sig, sub_scope);
}
self.analyze_node(body, sub_scope);
}
NodeKind::FunctionCall { name, args } => {
// Check if this is a built-in function
{
let token_type = if is_control_keyword(name) {
SemanticTokenType::KeywordControl
} else if is_builtin_function(name) {
SemanticTokenType::Function
} else {
// Check if it's a user-defined function
let symbols =
self.symbol_table.find_symbol(name, scope_id, SymbolKind::Subroutine);
if symbols.is_empty() {
SemanticTokenType::Function
} else {
SemanticTokenType::Function
}
};
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type,
modifiers: if is_builtin_function(name) && !is_control_keyword(name) {
vec![SemanticTokenModifier::DefaultLibrary]
} else {
vec![]
},
});
// Add hover for built-ins
if let Some(doc) = get_builtin_documentation(name) {
let hover = HoverInfo {
signature: doc.signature.to_string(),
documentation: Some(doc.description.to_string()),
details: vec![],
};
self.hover_info.insert(node.location, hover);
}
}
// Name is already a string, not a node
for arg in args {
self.analyze_node(arg, scope_id);
}
}
NodeKind::Package { name, block, name_span: _ } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Namespace,
modifiers: vec![SemanticTokenModifier::Declaration],
});
// Try POD docs first, then fall back to leading comments
let documentation = self
.extract_pod_name_section(name)
.or_else(|| self.extract_documentation(node.location.start));
let hover = HoverInfo {
signature: format!("package {}", name),
documentation,
details: vec![],
};
self.hover_info.insert(node.location, hover);
if let Some(block_node) = block {
let package_scope = self.get_scope_for(node, ScopeKind::Package);
self.analyze_node(block_node, package_scope);
}
}
NodeKind::String { value: _, interpolated: _ } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::String,
modifiers: vec![],
});
}
NodeKind::Number { value: _ } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Number,
modifiers: vec![],
});
}
NodeKind::Regex { .. } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Regex,
modifiers: vec![],
});
}
NodeKind::Match { expr, .. } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Regex,
modifiers: vec![],
});
self.analyze_node(expr, scope_id);
}
NodeKind::Substitution { expr, .. } => {
// Substitution operator: s/// - add semantic token for the operator
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Operator,
modifiers: vec![],
});
self.analyze_node(expr, scope_id);
}
NodeKind::Transliteration { expr, .. } => {
// Transliteration operator: tr/// or y/// - add semantic token for the operator
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Operator,
modifiers: vec![],
});
self.analyze_node(expr, scope_id);
}
NodeKind::LabeledStatement { label: _, statement } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Label,
modifiers: vec![],
});
{
self.analyze_node(statement, scope_id);
}
}
// Control flow keywords
NodeKind::If { condition, then_branch, elsif_branches, else_branch } => {
self.analyze_node(condition, scope_id);
self.analyze_node(then_branch, scope_id);
for (elsif_cond, elsif_branch) in elsif_branches {
self.analyze_node(elsif_cond, scope_id);
self.analyze_node(elsif_branch, scope_id);
}
if let Some(else_node) = else_branch {
self.analyze_node(else_node, scope_id);
}
}
NodeKind::While { condition, body, continue_block: _ } => {
self.analyze_node(condition, scope_id);
self.analyze_node(body, scope_id);
}
NodeKind::For { init, condition, update, body, .. } => {
if let Some(init_node) = init {
self.analyze_node(init_node, scope_id);
}
if let Some(cond_node) = condition {
self.analyze_node(cond_node, scope_id);
}
if let Some(update_node) = update {
self.analyze_node(update_node, scope_id);
}
self.analyze_node(body, scope_id);
}
NodeKind::Foreach { variable, list, body, continue_block } => {
self.analyze_node(variable, scope_id);
self.analyze_node(list, scope_id);
self.analyze_node(body, scope_id);
if let Some(cb) = continue_block {
self.analyze_node(cb, scope_id);
}
}
// Recursively analyze other nodes
NodeKind::Block { statements } => {
for stmt in statements {
self.analyze_node(stmt, scope_id);
}
}
NodeKind::Binary { left, right, .. } => {
self.analyze_node(left, scope_id);
self.analyze_node(right, scope_id);
}
NodeKind::Assignment { lhs, rhs, .. } => {
self.analyze_node(lhs, scope_id);
self.analyze_node(rhs, scope_id);
}
// Phase 1: Critical LSP Features (Issue #188)
NodeKind::VariableListDeclaration {
declarator,
variables,
attributes,
initializer,
} => {
// Handle multi-variable declarations like: my ($x, $y, $z) = (1, 2, 3);
for var in variables {
if let NodeKind::Variable { sigil, name } = &var.kind {
let token_type = match declarator.as_str() {
"my" | "state" => SemanticTokenType::VariableDeclaration,
"our" => SemanticTokenType::Variable,
"local" => SemanticTokenType::Variable,
_ => SemanticTokenType::Variable,
};
let mut modifiers = vec![SemanticTokenModifier::Declaration];
if declarator == "state" || attributes.iter().any(|a| a == ":shared") {
modifiers.push(SemanticTokenModifier::Static);
}
self.semantic_tokens.push(SemanticToken {
location: var.location,
token_type,
modifiers,
});
// Add hover info
let hover = HoverInfo {
signature: format!("{} {}{}", declarator, sigil, name),
documentation: self.extract_documentation(var.location.start),
details: if attributes.is_empty() {
vec![]
} else {
vec![format!("Attributes: {}", attributes.join(", "))]
},
};
self.hover_info.insert(var.location, hover);
}
}
if let Some(init) = initializer {
self.analyze_node(init, scope_id);
}
}
NodeKind::Ternary { condition, then_expr, else_expr } => {
// Handle conditional expressions: $x ? $y : $z
self.analyze_node(condition, scope_id);
self.analyze_node(then_expr, scope_id);
self.analyze_node(else_expr, scope_id);
}
NodeKind::ArrayLiteral { elements } => {
// Handle array constructors: [1, 2, 3, 4]
for elem in elements {
self.analyze_node(elem, scope_id);
}
}
NodeKind::HashLiteral { pairs } => {
// Handle hash constructors: { key1 => "value1", key2 => "value2" }
for (key, value) in pairs {
self.analyze_node(key, scope_id);
self.analyze_node(value, scope_id);
}
}
NodeKind::Try { body, catch_blocks, finally_block } => {
// Handle try/catch error handling
self.analyze_node(body, scope_id);
for (_var, catch_body) in catch_blocks {
// Note: var is just a String (variable name), not a Node
self.analyze_node(catch_body, scope_id);
}
if let Some(finally) = finally_block {
self.analyze_node(finally, scope_id);
}
}
NodeKind::PhaseBlock { phase: _, phase_span: _, block } => {
// Handle BEGIN/END/INIT/CHECK/UNITCHECK blocks
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Keyword,
modifiers: vec![],
});
self.analyze_node(block, scope_id);
}
NodeKind::ExpressionStatement { expression } => {
// Handle expression statements: $x + 10;
// Just delegate to the wrapped expression
self.analyze_node(expression, scope_id);
}
NodeKind::Do { block } => {
// Handle do blocks: do { ... }
// Do blocks create expression context but maintain scope
self.analyze_node(block, scope_id);
}
NodeKind::Eval { block } => {
// Handle eval blocks: eval { dangerous_operation(); }
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Keyword,
modifiers: vec![],
});
// Eval blocks should create a new scope for error isolation
self.analyze_node(block, scope_id);
}
NodeKind::Defer { block } => {
// defer { } blocks run on scope exit; analyze the block for symbol resolution
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Keyword,
modifiers: vec![],
});
self.analyze_node(block, scope_id);
}
NodeKind::VariableWithAttributes { variable, attributes } => {
// Handle attributed variables: my $x :shared = 42;
// Analyze the base variable node
self.analyze_node(variable, scope_id);
// Add modifier tokens for special attributes
if attributes.iter().any(|a| a == ":shared" || a == ":lvalue") {
// The variable node was already processed, so we just note the attributes
// in the hover info (if we need to enhance it later)
}
}
NodeKind::Unary { op, operand } => {
// Handle unary operators: -$x, !$x, ++$x, $x++
// Add token for the operator itself (if needed for highlighting)
if matches!(op.as_str(), "++" | "--" | "!" | "-" | "~" | "\\") {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Operator,
modifiers: vec![],
});
}
// Handle file test operators: -e, -d, -f, -r, -w, -x, -s, -z, -T, -B, etc.
if is_file_test_operator(op) {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Operator,
modifiers: vec![],
});
}
self.analyze_node(operand, scope_id);
}
NodeKind::Readline { filehandle } => {
// Handle readline/diamond operator: <STDIN>, <$fh>, <>
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Operator, // diamond operator is an I/O operator
modifiers: vec![],
});
// Add hover info for common filehandles
if let Some(fh) = filehandle {
let hover = HoverInfo {
signature: format!("<{}>", fh),
documentation: match fh.as_str() {
"STDIN" => Some("Standard input filehandle".to_string()),
"STDOUT" => Some("Standard output filehandle".to_string()),
"STDERR" => Some("Standard error filehandle".to_string()),
_ => Some(format!("Read from filehandle {}", fh)),
},
details: vec![],
};
self.hover_info.insert(node.location, hover);
} else {
// Bare <> reads from ARGV or STDIN
let hover = HoverInfo {
signature: "<>".to_string(),
documentation: Some("Read from command-line files or STDIN".to_string()),
details: vec![],
};
self.hover_info.insert(node.location, hover);
}
}
// Phase 2/3 Handlers
NodeKind::MethodCall { object, method, args } => {
self.analyze_node(object, scope_id);
if let Some(offset) =
self.find_substring_in_source_after(node, method, object.location.end)
{
self.semantic_tokens.push(SemanticToken {
location: SourceLocation { start: offset, end: offset + method.len() },
token_type: SemanticTokenType::Method,
modifiers: vec![],
});
}
for arg in args {
self.analyze_node(arg, scope_id);
}
}
NodeKind::IndirectCall { method, object, args } => {
if let Some(offset) = self.find_method_name_in_source(node, method) {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation { start: offset, end: offset + method.len() },
token_type: SemanticTokenType::Method,
modifiers: vec![],
});
}
self.analyze_node(object, scope_id);
for arg in args {
self.analyze_node(arg, scope_id);
}
}
NodeKind::Use { module, args, .. } => {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation {
start: node.location.start,
end: node.location.start + 3,
},
token_type: SemanticTokenType::Keyword,
modifiers: vec![],
});
let mut args_start = node.location.start + 3;
if let Some(offset) = self.find_substring_in_source(node, module) {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation { start: offset, end: offset + module.len() },
token_type: SemanticTokenType::Namespace,
modifiers: vec![],
});
args_start = offset + module.len();
}
self.analyze_string_args(node, args, args_start);
}
NodeKind::No { module, args, .. } => {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation {
start: node.location.start,
end: node.location.start + 2,
},
token_type: SemanticTokenType::Keyword,
modifiers: vec![],
});
let mut args_start = node.location.start + 2;
if let Some(offset) = self.find_substring_in_source(node, module) {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation { start: offset, end: offset + module.len() },
token_type: SemanticTokenType::Namespace,
modifiers: vec![],
});
args_start = offset + module.len();
}
self.analyze_string_args(node, args, args_start);
}
NodeKind::Given { expr, body } => {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation {
start: node.location.start,
end: node.location.start + 5,
}, // given
token_type: SemanticTokenType::KeywordControl,
modifiers: vec![],
});
self.analyze_node(expr, scope_id);
self.analyze_node(body, scope_id);
}
NodeKind::When { condition, body } => {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation {
start: node.location.start,
end: node.location.start + 4,
}, // when
token_type: SemanticTokenType::KeywordControl,
modifiers: vec![],
});
self.analyze_node(condition, scope_id);
self.analyze_node(body, scope_id);
}
NodeKind::Default { body } => {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation {
start: node.location.start,
end: node.location.start + 7,
}, // default
token_type: SemanticTokenType::KeywordControl,
modifiers: vec![],
});
self.analyze_node(body, scope_id);
}
NodeKind::Return { value } => {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation {
start: node.location.start,
end: node.location.start + 6,
}, // return
token_type: SemanticTokenType::KeywordControl,
modifiers: vec![],
});
if let Some(v) = value {
self.analyze_node(v, scope_id);
}
}
NodeKind::Class { name, body, .. } => {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation {
start: node.location.start,
end: node.location.start + 5,
}, // class
token_type: SemanticTokenType::Keyword,
modifiers: vec![],
});
if let Some(offset) = self.find_substring_in_source(node, name) {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation { start: offset, end: offset + name.len() },
token_type: SemanticTokenType::Class,
modifiers: vec![SemanticTokenModifier::Declaration],
});
}
let class_scope = self.get_scope_for(node, ScopeKind::Package);
self.analyze_node(body, class_scope);
}
NodeKind::Signature { parameters } => {
for param in parameters {
self.analyze_node(param, scope_id);
}
}
NodeKind::MandatoryParameter { variable }
| NodeKind::OptionalParameter { variable, .. }
| NodeKind::SlurpyParameter { variable }
| NodeKind::NamedParameter { variable } => {
self.analyze_node(variable, scope_id);
}
NodeKind::Diamond | NodeKind::Ellipsis => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Operator,
modifiers: vec![],
});
}
NodeKind::Undef => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Keyword,
modifiers: vec![],
});
}
NodeKind::Identifier { .. } => {
// Bareword identifiers, usually left to lexical highlighting
// but we handle them to avoid the default case.
}
NodeKind::Heredoc { .. } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::String,
modifiers: vec![],
});
}
NodeKind::Glob { .. } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Operator,
modifiers: vec![],
});
}
NodeKind::DataSection { .. } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Comment,
modifiers: vec![],
});
}
NodeKind::Prototype { .. } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Punctuation,
modifiers: vec![],
});
}
NodeKind::Typeglob { .. } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::Variable,
modifiers: vec![],
});
}
NodeKind::Untie { variable } => {
self.analyze_node(variable, scope_id);
}
NodeKind::LoopControl { .. } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::KeywordControl,
modifiers: vec![],
});
}
NodeKind::Goto { target } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::KeywordControl,
modifiers: vec![],
});
self.analyze_node(target, scope_id);
}
NodeKind::MissingExpression
| NodeKind::MissingStatement
| NodeKind::MissingIdentifier
| NodeKind::MissingBlock => {
// No tokens for missing constructs
}
NodeKind::Tie { variable, package, args } => {
self.analyze_node(variable, scope_id);
self.analyze_node(package, scope_id);
for arg in args {
self.analyze_node(arg, scope_id);
}
}
NodeKind::StatementModifier { statement, condition, modifier } => {
// Handle postfix loop modifiers: for, while, until, foreach
// e.g., print $_ for @list; or $x++ while $x < 10;
if matches!(modifier.as_str(), "for" | "foreach" | "while" | "until") {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::KeywordControl,
modifiers: vec![],
});
}
self.analyze_node(statement, scope_id);
self.analyze_node(condition, scope_id);
}
NodeKind::Format { name, .. } => {
self.semantic_tokens.push(SemanticToken {
location: node.location,
token_type: SemanticTokenType::FunctionDeclaration,
modifiers: vec![SemanticTokenModifier::Declaration],
});
let hover = HoverInfo {
signature: format!("format {} =", name),
documentation: None,
details: vec![],
};
self.hover_info.insert(node.location, hover);
}
NodeKind::Error { .. } | NodeKind::UnknownRest => {
// No semantic tokens for error nodes
}
}
}
/// Extract documentation (POD or comments) immediately preceding a
/// position.
///
/// The returned string is trimmed and corresponds to whichever of POD
/// or comments is found first at the very end of `source[..start]`. If
/// both kinds appear earlier in the source but neither is *immediately
/// before* `start`, this returns `None` — anchoring is intentional so
/// that documentation blocks belonging to one declaration do not bleed
/// into a later, unrelated declaration.
///
/// Anchoring uses `\z` (absolute end of string) rather than `$`. With
/// the `m` flag, `$` matches at the end of every line, which made the
/// previous regex match POD blocks anywhere in `before` and leak them
/// into hover docs for unrelated subs that followed.
pub(super) fn extract_documentation(&self, start: usize) -> Option<String> {
static POD_RE: OnceLock<Result<Regex, regex::Error>> = OnceLock::new();
static COMMENT_RE: OnceLock<Result<Regex, regex::Error>> = OnceLock::new();
if self.source.is_empty() || start > self.source.len() {
return None;
}
let before = &self.source[..start];
// Check for POD blocks ending with =cut, anchored at end of string.
let pod_re = POD_RE
.get_or_init(|| Regex::new(r"(?s)(=[a-zA-Z0-9].*?\r?\n=cut(?:\r?\n)?)\s*\z"))
.as_ref()
.ok()?;
if let Some(caps) = pod_re.captures(before) {
if let Some(pod_text) = caps.get(1) {
return Some(pod_text.as_str().trim().to_string());
}
}
// Check for consecutive comment lines, anchored at end of string.
let comment_re =
COMMENT_RE.get_or_init(|| Regex::new(r"(?m)(#.*\r?\n)+[\t ]*\z")).as_ref().ok()?;
if let Some(caps) = comment_re.captures(before) {
if let Some(comment_match) = caps.get(0) {
// Strip the # prefix from each comment line
let doc = comment_match
.as_str()
.lines()
.map(|line| line.trim_start_matches('#').trim())
.filter(|line| !line.is_empty())
.collect::<Vec<_>>()
.join(" ");
return Some(doc);
}
}
None
}
/// Extract documentation for a subroutine or method, falling back to
/// inline POD blocks inside the body when no leading docs are found.
///
/// Resolution order:
/// 1. Leading docs (POD or comments) immediately preceding `start` —
/// matches `extract_documentation` and preserves the existing
/// "explicit author intent wins" precedence.
/// 2. The first POD block inside `body` (between `body.location.start`
/// and `body.location.end`). This handles the inline-POD style of
/// documenting a sub from within its body, e.g.:
///
/// ```perl
/// sub process_data {
/// =pod
/// Internal documentation for this sub
/// =cut
/// ...
/// }
/// ```
pub(super) fn extract_sub_documentation(&self, start: usize, body: &Node) -> Option<String> {
self.extract_documentation(start).or_else(|| self.find_pod_in_node_body(body))
}
/// Find the first POD block inside the source range covered by `body`.
///
/// Matches any POD block (`=pod`, `=head1`, `=item`, etc.) that ends
/// with a `=cut` directive. The returned string is trimmed of
/// surrounding whitespace and includes the opening directive through
/// the closing `=cut`, mirroring the format produced by
/// `extract_documentation` for leading POD blocks.
///
/// **Deliberate divergence from perlpod:** the regex allows optional
/// leading whitespace (`^\s*`) before the opening POD directive. Per
/// [perlpod](https://perldoc.perl.org/perlpod), POD directives must
/// begin at column 0 — `perl` itself silently ignores lines like
/// ` =pod`. The LSP intentionally relaxes this rule so that authors
/// who indent `=pod` inside a sub body (a common editor style) still
/// get hover documentation. This is a UX choice: we surface what the
/// author *wrote* as documentation rather than enforcing the strict
/// perlpod column-0 rule. See issue #4599 for the decision record.
pub(super) fn find_pod_in_node_body(&self, body: &Node) -> Option<String> {
static BODY_POD_RE: OnceLock<Result<Regex, regex::Error>> = OnceLock::new();
let start = body.location.start;
let end = body.location.end;
if self.source.is_empty() || end <= start || end > self.source.len() {
return None;
}
let body_src = &self.source[start..end];
let pod_re = BODY_POD_RE
.get_or_init(|| Regex::new(r"(?ms)^\s*(=[a-zA-Z0-9].*?\r?\n=cut)\b"))
.as_ref()
.ok()?;
let caps = pod_re.captures(body_src)?;
let pod_text = caps.get(1)?.as_str().trim().to_string();
Some(pod_text)
}
/// Extract the POD `=head1 NAME` section for a package.
///
/// Scans the entire source for a `=head1 NAME` POD section and returns
/// its content if it mentions the given package name.
pub(super) fn extract_pod_name_section(&self, package_name: &str) -> Option<String> {
if self.source.is_empty() {
return None;
}
let mut in_name_section = false;
let mut name_lines: Vec<&str> = Vec::new();
for line in self.source.lines() {
let trimmed: &str = line.trim();
if trimmed.starts_with("=head1") {
if in_name_section {
break;
}
let heading = trimmed.strip_prefix("=head1").map(|s: &str| s.trim());
if heading == Some("NAME") {
in_name_section = true;
continue;
}
} else if trimmed.starts_with("=cut") && in_name_section {
break;
} else if trimmed.starts_with('=') && in_name_section {
break;
} else if in_name_section && !trimmed.is_empty() {
name_lines.push(trimmed);
}
}
if !name_lines.is_empty() {
let name_doc = name_lines.join(" ");
if name_doc.contains(package_name)
|| name_doc.contains(&package_name.replace("::", "-"))
{
return Some(name_doc);
}
}
None
}
/// Get scope id for a node by consulting the symbol table
pub(super) fn get_scope_for(&self, node: &Node, kind: ScopeKind) -> ScopeId {
for scope in self.symbol_table.scopes.values() {
if scope.kind == kind
&& scope.location.start == node.location.start
&& scope.location.end == node.location.end
{
return scope.id;
}
}
0
}
/// Get line number from byte offset (simplified version)
pub(super) fn line_number(&self, offset: usize) -> usize {
if self.source.is_empty() { 1 } else { self.source[..offset].lines().count() + 1 }
}
/// Find substring in source within node's range
pub(super) fn find_substring_in_source(&self, node: &Node, substring: &str) -> Option<usize> {
if self.source.len() < node.location.end {
return None;
}
let node_text = &self.source[node.location.start..node.location.end];
if let Some(pos) = node_text.find(substring) {
return Some(node.location.start + pos);
}
None
}
/// Find method name in source within node's range
pub(super) fn find_method_name_in_source(
&self,
node: &Node,
method_name: &str,
) -> Option<usize> {
self.find_substring_in_source(node, method_name)
}
/// Find substring in source within node's range, starting search after a specific absolute offset
pub(super) fn find_substring_in_source_after(
&self,
node: &Node,
substring: &str,
after: usize,
) -> Option<usize> {
if self.source.len() < node.location.end || after >= node.location.end {
return None;
}
let start_rel = after.saturating_sub(node.location.start);
let node_text = &self.source[node.location.start..node.location.end];
if start_rel >= node_text.len() {
return None;
}
let text_to_search = &node_text[start_rel..];
if let Some(pos) = text_to_search.find(substring) {
return Some(node.location.start + start_rel + pos);
}
None
}
/// Analyze string arguments for highlighting (e.g. in use/no statements)
pub(super) fn analyze_string_args(
&mut self,
node: &Node,
args: &[String],
start_offset: usize,
) {
let mut current_offset = start_offset;
for arg in args {
if let Some(offset) = self.find_substring_in_source_after(node, arg, current_offset) {
self.semantic_tokens.push(SemanticToken {
location: SourceLocation { start: offset, end: offset + arg.len() },
token_type: SemanticTokenType::String,
modifiers: vec![],
});
current_offset = offset + arg.len();
}
}
}
/// Infer the type of a node based on its context and initialization.
///
/// Provides basic type inference for Perl expressions to enhance hover
/// information with derived type information. Supports common patterns:
/// - Literal values (numbers, strings, arrays, hashes)
/// - Variable references (looks up declaration)
/// - Function calls (basic return type hints)
///
/// In the semantic workflow (Parse -> Index -> Analyze), this method runs
/// during the Analyze stage and consumes symbols produced during Index.
///
/// # Arguments
///
/// * `node` - The AST node to infer type for
///
/// # Returns
///
/// A string describing the inferred type, or None if type cannot be determined
pub fn infer_type(&self, node: &Node) -> Option<String> {
match &node.kind {
NodeKind::Number { .. } => Some("number".to_string()),
NodeKind::String { .. } => Some("string".to_string()),
NodeKind::ArrayLiteral { .. } => Some("array".to_string()),
NodeKind::HashLiteral { .. } => Some("hash".to_string()),
NodeKind::Variable { sigil, name } => {
// Look up the variable in the symbol table
let kind = match sigil.as_str() {
"$" => SymbolKind::scalar(),
"@" => SymbolKind::array(),
"%" => SymbolKind::hash(),
_ => return None,
};
let symbols = self.symbol_table.find_symbol(name, 0, kind);
symbols.first()?;
// Return the basic type based on sigil
match sigil.as_str() {
"$" => Some("scalar".to_string()),
"@" => Some("array".to_string()),
"%" => Some("hash".to_string()),
_ => None,
}
}
NodeKind::FunctionCall { name, .. } => {
// Basic return type inference for built-in functions
match name.as_str() {
"scalar" => Some("scalar".to_string()),
"ref" => Some("string".to_string()),
"length" | "index" | "rindex" => Some("number".to_string()),
"split" => Some("array".to_string()),
"keys" | "values" => Some("array".to_string()),
_ => None,
}
}
NodeKind::Binary { op, .. } => {
// Infer based on operator
match op.as_str() {
"+" | "-" | "*" | "/" | "%" | "**" => Some("number".to_string()),
"." | "x" => Some("string".to_string()),
"==" | "!=" | "<" | ">" | "<=" | ">=" | "eq" | "ne" | "lt" | "gt" | "le"
| "ge" => Some("boolean".to_string()),
_ => None,
}
}
_ => None,
}
}
}
/// Build a parenthesised parameter list string from a `Signature` AST node.
///
/// Extracts each parameter variable's sigil and name and joins them with
/// ", ". Returns `"(...)"` as a safe fallback for any unrecognised structure.
fn format_signature_params(sig_node: &Node) -> String {
let NodeKind::Signature { parameters } = &sig_node.kind else {
return "(...)".to_string();
};
let labels: Vec<String> = parameters
.iter()
.filter_map(|param| {
let var = match ¶m.kind {
NodeKind::MandatoryParameter { variable }
| NodeKind::OptionalParameter { variable, .. }
| NodeKind::SlurpyParameter { variable }
| NodeKind::NamedParameter { variable } => variable.as_ref(),
NodeKind::Variable { .. } => param,
_ => return None,
};
if let NodeKind::Variable { sigil, name } = &var.kind {
Some(format!("{}{}", sigil, name))
} else {
None
}
})
.collect();
format!("({})", labels.join(", "))
}