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
/// Test-only symbol allocator that starts at 1 to avoid EOF collision
#[cfg(test)]
pub mod test {
use adze_ir::SymbolId;
/// Symbol allocator for tests that guarantees no collision with EOF (SymbolId(0))
pub struct SymbolAllocator(u16);
impl SymbolAllocator {
/// Create a new allocator starting at SymbolId(1)
pub fn new() -> Self {
Self(1)
}
/// Allocate the next symbol ID
pub fn next_id(&mut self) -> SymbolId {
let id = self.0;
self.0 = self.0.checked_add(1).expect("Symbol ID overflow");
SymbolId(id)
}
/// Allocate N consecutive symbol IDs
pub fn next_n(&mut self, n: usize) -> Vec<SymbolId> {
(0..n).map(|_| self.next_id()).collect()
}
/// Get current symbol without advancing
pub fn current(&self) -> SymbolId {
SymbolId(self.0)
}
}
impl Default for SymbolAllocator {
fn default() -> Self {
Self::new()
}
}
}