typst_analyzer/typst_lang/
symbol_table.rs1use std::collections::HashMap;
5
6use oxc_index::IndexVec;
7
8use super::span::Span;
9
10oxc_index::define_index_type! {
11 pub struct SymbolId = u32;
13 IMPL_RAW_CONVERSIONS = true;
14}
15
16oxc_index::define_index_type! {
17 pub struct ReferenceId = u32;
18 IMPL_RAW_CONVERSIONS = true;
19}
20pub type SymbolIdToSpan = IndexVec<SymbolId, Span>;
22
23pub type ReferenceIdToReference = IndexVec<ReferenceId, Reference>;
24
25#[derive(Default, Debug)]
29pub struct SymbolTable {
30 pub span_to_symbol_id: HashMap<Span, SymbolId>,
32 pub symbol_id_to_span: SymbolIdToSpan,
34 pub reference_id_to_reference: ReferenceIdToReference,
35 pub span_to_reference_id: HashMap<Span, ReferenceId>,
36 pub symbol_id_to_references: HashMap<SymbolId, Vec<ReferenceId>>,
37}
38
39#[derive(Debug)]
40pub struct Reference {
41 pub span: Span,
42 pub symbol_id: Option<SymbolId>,
43}
44
45impl SymbolTable {
46 pub fn add_symbol(&mut self, span: Span) -> SymbolId {
47 let symbol_id = self.symbol_id_to_span.push(span.clone());
48 self.span_to_symbol_id.insert(span.clone(), symbol_id);
49 symbol_id
50 }
51
52 pub fn add_reference(&mut self, span: Span, symbol_id: Option<SymbolId>) {
53 let reference_id = self.reference_id_to_reference.push(Reference {
54 span: span.clone(),
55 symbol_id,
56 });
57 self.span_to_reference_id.insert(span, reference_id);
58 if let Some(symbol_id) = symbol_id {
59 self.symbol_id_to_references
60 .entry(symbol_id)
61 .or_default()
62 .push(reference_id);
63 }
64 }
65}