swamp_analyzer/
shared.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5
6use source_map_cache::{FileId, SourceMap};
7use swamp_modules::prelude::ModuleRef;
8use swamp_modules::{
9    prelude::Modules,
10    symtbl::{SymbolTable, SymbolTableRef},
11};
12use swamp_semantic::ProgramState;
13
14pub struct SharedState<'a> {
15    pub state: &'a mut ProgramState,
16    pub lookup_table: SymbolTable,
17    pub definition_table: SymbolTable,
18    pub modules: &'a Modules,
19    pub source_map: &'a SourceMap,
20    pub file_id: FileId,
21    pub core_symbol_table: SymbolTableRef,
22}
23
24impl<'a> SharedState<'a> {
25    #[must_use]
26    pub fn get_symbol_table(&'a self, path: &[String]) -> Option<&'a SymbolTable> {
27        if path.is_empty() {
28            return Some(&self.lookup_table);
29        }
30        self.get_module(path).map(|module| &module.symbol_table)
31    }
32
33    #[must_use]
34    pub fn get_module(&'a self, path: &[String]) -> Option<&'a ModuleRef> {
35        let resolved_path = {
36            self.lookup_table.get_package_version(&path[0]).map_or_else(
37                || path.to_vec(),
38                |found_version| {
39                    let mut new_path = path.to_vec();
40                    let complete_name = format!("{}-{found_version}", path[0]);
41                    new_path[0] = complete_name;
42                    new_path
43                    //path.to_vec()
44                },
45            )
46        };
47
48        if path.len() == 1 {
49            if let Some(module_ref) = self.lookup_table.get_module_link(&path[0]) {
50                return Some(module_ref);
51            }
52        }
53
54        if let Some(x) = self.modules.get(&resolved_path) {
55            return Some(x);
56        }
57
58        None
59    }
60}