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
//! Parser context for tracking scope during AST traversal
//!
//! This module provides scope tracking utilities that all language parsers
//! can use to communicate proper scope information to resolvers.
use crate::symbol::ScopeContext;
use crate::types::SymbolKind;
/// Scope types that parsers track during AST traversal
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScopeType {
/// Global scope (project-wide)
Global,
/// Module/file scope
Module,
/// Function or method scope
Function {
/// For JS/TS: whether this function's declarations are hoisted
hoisting: bool,
},
/// Class, struct, or similar type scope
Class,
/// Block scope (if/for/while/etc)
Block,
/// Package or namespace scope
Package,
/// Namespace scope (for languages that support it)
Namespace,
}
impl ScopeType {
/// Create a non-hoisting function scope (default for most languages)
pub fn function() -> Self {
ScopeType::Function { hoisting: false }
}
/// Create a hoisting function scope (for JavaScript/TypeScript)
pub fn hoisting_function() -> Self {
ScopeType::Function { hoisting: true }
}
}
/// Parser context for tracking current scope during parsing
#[derive(Debug, Clone)]
pub struct ParserContext {
/// Stack of current scopes (innermost last)
scope_stack: Vec<ScopeType>,
/// Current class name (if inside a class)
current_class: Option<String>,
/// Current function name (if inside a function)
current_function: Option<String>,
}
impl Default for ParserContext {
fn default() -> Self {
Self::new()
}
}
impl ParserContext {
/// Create a new parser context starting at module scope
pub fn new() -> Self {
Self {
scope_stack: vec![ScopeType::Module],
current_class: None,
current_function: None,
}
}
/// Enter a new scope
pub fn enter_scope(&mut self, scope_type: ScopeType) {
// Update tracking based on scope type
match scope_type {
ScopeType::Class => {
// Class name should be set separately via set_current_class
}
ScopeType::Function { .. } => {
// Function name should be set separately via set_current_function
}
_ => {}
}
self.scope_stack.push(scope_type);
}
/// Exit the current scope
pub fn exit_scope(&mut self) {
if self.scope_stack.len() > 1 {
// Never pop the module scope
let exited = self.scope_stack.pop();
// Clear context when exiting certain scopes
if let Some(scope) = exited {
match scope {
ScopeType::Class => self.current_class = None,
ScopeType::Function { .. } => self.current_function = None,
_ => {}
}
}
}
}
/// Get the current scope context for symbol creation
pub fn current_scope_context(&self) -> ScopeContext {
// Find the most relevant scope
for scope in self.scope_stack.iter().rev() {
match scope {
ScopeType::Function { hoisting } => {
// Determine parent info
let (parent_name, parent_kind) = if let Some(func_name) = &self.current_function
{
(Some(func_name.clone().into()), Some(SymbolKind::Function))
} else if let Some(class_name) = &self.current_class {
(Some(class_name.clone().into()), Some(SymbolKind::Class))
} else {
(None, None)
};
return ScopeContext::Local {
hoisted: *hoisting,
parent_name,
parent_kind,
};
}
ScopeType::Block => {
// Block scope is still local
// Determine parent info
let (parent_name, parent_kind) = if let Some(func_name) = &self.current_function
{
(Some(func_name.clone().into()), Some(SymbolKind::Function))
} else if let Some(class_name) = &self.current_class {
(Some(class_name.clone().into()), Some(SymbolKind::Class))
} else {
(None, None)
};
return ScopeContext::Local {
hoisted: false,
parent_name,
parent_kind,
};
}
ScopeType::Class => {
return ScopeContext::ClassMember {
class_name: None, // Parsers can populate this if they track class names
};
}
ScopeType::Package | ScopeType::Namespace => {
return ScopeContext::Package;
}
ScopeType::Global => {
return ScopeContext::Global;
}
ScopeType::Module => {
// Keep looking for more specific scope
continue;
}
}
}
// Default to module scope
ScopeContext::Module
}
/// Check if currently inside a class
pub fn is_in_class(&self) -> bool {
self.scope_stack
.iter()
.any(|s| matches!(s, ScopeType::Class))
}
/// Check if currently inside a function
pub fn is_in_function(&self) -> bool {
self.scope_stack
.iter()
.any(|s| matches!(s, ScopeType::Function { .. }))
}
/// Check if at module level (not inside class or function)
pub fn is_module_level(&self) -> bool {
!self.is_in_class() && !self.is_in_function()
}
/// Set the current class name
pub fn set_current_class(&mut self, name: Option<String>) {
self.current_class = name;
}
/// Set the current function name
pub fn set_current_function(&mut self, name: Option<String>) {
self.current_function = name;
}
/// Get the current class name
pub fn current_class(&self) -> Option<&str> {
self.current_class.as_deref()
}
/// Get the current function name
pub fn current_function(&self) -> Option<&str> {
self.current_function.as_deref()
}
/// Create a scope context for a parameter
pub fn parameter_scope_context() -> ScopeContext {
ScopeContext::Parameter
}
/// Create a scope context for a global symbol
pub fn global_scope_context() -> ScopeContext {
ScopeContext::Global
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_context() {
let ctx = ParserContext::new();
assert_eq!(ctx.current_scope_context(), ScopeContext::Module);
assert!(ctx.is_module_level());
assert!(!ctx.is_in_class());
assert!(!ctx.is_in_function());
}
#[test]
fn test_class_scope() {
let mut ctx = ParserContext::new();
ctx.enter_scope(ScopeType::Class);
ctx.set_current_class(Some("MyClass".to_string()));
assert!(matches!(
ctx.current_scope_context(),
ScopeContext::ClassMember { .. }
));
assert!(ctx.is_in_class());
assert!(!ctx.is_module_level());
assert_eq!(ctx.current_class(), Some("MyClass"));
ctx.exit_scope();
assert_eq!(ctx.current_scope_context(), ScopeContext::Module);
assert!(ctx.is_module_level());
assert_eq!(ctx.current_class(), None);
}
#[test]
fn test_function_scope() {
let mut ctx = ParserContext::new();
ctx.enter_scope(ScopeType::Function { hoisting: false });
ctx.set_current_function(Some("my_func".to_string()));
assert_eq!(
ctx.current_scope_context(),
ScopeContext::Local {
hoisted: false,
parent_name: Some("my_func".to_string().into()),
parent_kind: Some(SymbolKind::Function),
}
);
assert!(ctx.is_in_function());
assert!(!ctx.is_module_level());
ctx.exit_scope();
assert_eq!(ctx.current_scope_context(), ScopeContext::Module);
}
#[test]
fn test_nested_scopes() {
let mut ctx = ParserContext::new();
// Enter class
ctx.enter_scope(ScopeType::Class);
assert!(matches!(
ctx.current_scope_context(),
ScopeContext::ClassMember { .. }
));
// Enter method within class
ctx.enter_scope(ScopeType::Function { hoisting: false });
// Since we didn't set current_function, parent info will be None
assert_eq!(
ctx.current_scope_context(),
ScopeContext::Local {
hoisted: false,
parent_name: None,
parent_kind: None,
}
);
assert!(ctx.is_in_class());
assert!(ctx.is_in_function());
// Exit method
ctx.exit_scope();
assert!(matches!(
ctx.current_scope_context(),
ScopeContext::ClassMember { .. }
));
// Exit class
ctx.exit_scope();
assert_eq!(ctx.current_scope_context(), ScopeContext::Module);
}
#[test]
fn test_hoisted_function() {
let mut ctx = ParserContext::new();
ctx.enter_scope(ScopeType::Function { hoisting: true });
assert_eq!(
ctx.current_scope_context(),
ScopeContext::Local {
hoisted: true,
parent_name: None,
parent_kind: None,
}
);
}
}