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
use crate::graph::NodeId;
use std::collections::HashMap;
use std::path::PathBuf;
/// A global symbol table for resolving cross-file references.
///
/// Maps Fully Qualified Names (FQNs) to Node IDs.
/// Example FQN: "arbor::graph::SymbolTable" -> NodeId(42)
#[derive(Debug, Default, Clone)]
pub struct SymbolTable {
/// Map of FQN to NodeId
by_fqn: HashMap<String, NodeId>,
/// Map of File Path to list of exported symbols (FQNs)
/// Used to resolve wildcard imports or find all symbols in a file.
exports_by_file: HashMap<PathBuf, Vec<String>>,
}
impl SymbolTable {
/// Creates a new empty symbol table.
pub fn new() -> Self {
Self::default()
}
/// Registers a symbol in the table.
///
/// * `fqn` - Fully Qualified Name (e.g., "pkg.module.function")
/// * `id` - The Node ID in the graph
/// * `file` - The file path defining this symbol
pub fn insert(&mut self, fqn: String, id: NodeId, file: PathBuf) {
self.by_fqn.insert(fqn.clone(), id);
self.exports_by_file.entry(file).or_default().push(fqn);
}
/// Resolves a Fully Qualified Name to a Node ID.
pub fn resolve(&self, fqn: &str) -> Option<NodeId> {
self.by_fqn.get(fqn).copied()
}
/// Returns all symbols exported by a file.
pub fn get_file_exports(&self, file: &PathBuf) -> Option<&Vec<String>> {
self.exports_by_file.get(file)
}
/// Clears the symbol table.
pub fn clear(&mut self) {
self.by_fqn.clear();
self.exports_by_file.clear();
}
/// Resolves a symbol name with context-aware matching.
///
/// Resolution order:
/// 1. Exact FQN match
/// 2. Suffix match (e.g., "helper" matches "pkg.Utils.helper")
/// - Only matches if unambiguous OR in same directory as `context_file`
///
/// Returns None if:
/// - No match found
/// - Multiple matches exist and none are in the same directory (ambiguous)
pub fn resolve_with_context(
&self,
name: &str,
context_file: &std::path::Path,
) -> Option<NodeId> {
// 1. Try exact match first
if let Some(id) = self.by_fqn.get(name) {
return Some(*id);
}
// 2. Suffix match
let context_dir = context_file.parent();
let mut candidates: Vec<(&String, NodeId, bool)> = Vec::new();
for (fqn, &id) in &self.by_fqn {
// Check if FQN ends with the name (with separator)
if fqn.ends_with(name) {
// Ensure it's a proper suffix (preceded by separator or start)
let prefix_len = fqn.len() - name.len();
if prefix_len == 0
|| fqn.chars().nth(prefix_len - 1) == Some('.')
|| fqn.chars().nth(prefix_len - 1) == Some(':')
{
// Check if in same directory
let same_dir = self
.exports_by_file
.iter()
.find(|(_, exports)| exports.contains(fqn))
.map(|(file, _)| file.parent() == context_dir)
.unwrap_or(false);
candidates.push((fqn, id, same_dir));
}
}
}
match candidates.len() {
0 => None,
1 => Some(candidates[0].1),
_ => {
// Multiple candidates: only resolve if exactly one is in same directory
let same_dir_candidates: Vec<_> =
candidates.iter().filter(|(_, _, same)| *same).collect();
if same_dir_candidates.len() == 1 {
Some(same_dir_candidates[0].1)
} else {
// Ambiguous: don't auto-link
None
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_insert_resolve() {
let mut table = SymbolTable::new();
let path = PathBuf::from("main.rs");
let id = NodeId::new(1);
table.insert("main::foo".to_string(), id, path.clone());
assert_eq!(table.resolve("main::foo"), Some(id));
assert_eq!(table.resolve("main::bar"), None);
let exports = table.get_file_exports(&path).unwrap();
assert_eq!(exports.len(), 1);
assert_eq!(exports[0], "main::foo");
}
#[test]
fn test_resolve_with_context_exact_match() {
let mut table = SymbolTable::new();
let path = PathBuf::from("src/utils.rs");
let id = NodeId::new(1);
table.insert("pkg.utils.helper".to_string(), id, path.clone());
// Exact match works from any context
let result =
table.resolve_with_context("pkg.utils.helper", &PathBuf::from("other/file.rs"));
assert_eq!(result, Some(id));
}
#[test]
fn test_resolve_with_context_suffix_match() {
let mut table = SymbolTable::new();
let path = PathBuf::from("src/utils.rs");
let id = NodeId::new(1);
table.insert("pkg.utils.helper".to_string(), id, path.clone());
// Suffix match works when unambiguous
let result = table.resolve_with_context("helper", &PathBuf::from("other/file.rs"));
assert_eq!(result, Some(id));
}
#[test]
fn test_resolve_with_context_ambiguous_returns_none() {
let mut table = SymbolTable::new();
let id1 = NodeId::new(1);
let id2 = NodeId::new(2);
// Two helpers in different directories
table.insert(
"pkg.a.helper".to_string(),
id1,
PathBuf::from("src/a/mod.rs"),
);
table.insert(
"pkg.b.helper".to_string(),
id2,
PathBuf::from("src/b/mod.rs"),
);
// Ambiguous: from unrelated directory, should return None
let result = table.resolve_with_context("helper", &PathBuf::from("src/c/caller.rs"));
assert_eq!(result, None);
}
#[test]
fn test_resolve_with_context_locality_preference() {
let mut table = SymbolTable::new();
let id1 = NodeId::new(1);
let id2 = NodeId::new(2);
// Two helpers in different directories
table.insert(
"pkg.a.helper".to_string(),
id1,
PathBuf::from("src/a/mod.rs"),
);
table.insert(
"pkg.b.helper".to_string(),
id2,
PathBuf::from("src/b/mod.rs"),
);
// From src/a/, should resolve to id1 (same directory)
let result = table.resolve_with_context("helper", &PathBuf::from("src/a/caller.rs"));
assert_eq!(result, Some(id1));
// From src/b/, should resolve to id2 (same directory)
let result = table.resolve_with_context("helper", &PathBuf::from("src/b/caller.rs"));
assert_eq!(result, Some(id2));
}
}