2022-02-12 10:50:37 +01:00
|
|
|
use crate::{AliasId, BlockId, DeclId, Span};
|
2021-11-16 00:16:06 +01:00
|
|
|
|
2022-01-14 22:06:32 +01:00
|
|
|
use indexmap::IndexMap;
|
2021-11-16 00:16:06 +01:00
|
|
|
|
|
|
|
// TODO: Move the import pattern matching logic here from use/hide commands and
|
|
|
|
// parse_use/parse_hide
|
|
|
|
|
|
|
|
/// Collection of definitions that can be exported from a module
|
|
|
|
#[derive(Debug, Clone)]
|
2022-05-07 21:39:22 +02:00
|
|
|
pub struct Module {
|
2022-01-14 22:06:32 +01:00
|
|
|
pub decls: IndexMap<Vec<u8>, DeclId>,
|
2022-02-12 10:50:37 +01:00
|
|
|
pub aliases: IndexMap<Vec<u8>, AliasId>,
|
2022-08-27 00:32:19 +02:00
|
|
|
pub env_block: Option<BlockId>,
|
2022-01-30 23:05:25 +01:00
|
|
|
pub span: Option<Span>,
|
2021-11-16 00:16:06 +01:00
|
|
|
}
|
|
|
|
|
2022-05-07 21:39:22 +02:00
|
|
|
impl Module {
|
2021-11-16 00:16:06 +01:00
|
|
|
pub fn new() -> Self {
|
2022-05-07 21:39:22 +02:00
|
|
|
Module {
|
2022-01-14 22:06:32 +01:00
|
|
|
decls: IndexMap::new(),
|
2022-02-12 10:50:37 +01:00
|
|
|
aliases: IndexMap::new(),
|
2022-08-27 00:32:19 +02:00
|
|
|
env_block: None,
|
2022-01-30 23:05:25 +01:00
|
|
|
span: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_span(span: Span) -> Self {
|
2022-05-07 21:39:22 +02:00
|
|
|
Module {
|
2022-01-30 23:05:25 +01:00
|
|
|
decls: IndexMap::new(),
|
2022-02-12 10:50:37 +01:00
|
|
|
aliases: IndexMap::new(),
|
2022-08-27 00:32:19 +02:00
|
|
|
env_block: None,
|
2022-01-30 23:05:25 +01:00
|
|
|
span: Some(span),
|
2021-11-16 00:16:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 10:57:10 +02:00
|
|
|
pub fn add_decl(&mut self, name: Vec<u8>, decl_id: DeclId) -> Option<DeclId> {
|
|
|
|
self.decls.insert(name, decl_id)
|
2021-11-16 00:16:06 +01:00
|
|
|
}
|
|
|
|
|
2022-07-29 10:57:10 +02:00
|
|
|
pub fn add_alias(&mut self, name: Vec<u8>, alias_id: AliasId) -> Option<AliasId> {
|
|
|
|
self.aliases.insert(name, alias_id)
|
2022-02-12 10:50:37 +01:00
|
|
|
}
|
|
|
|
|
2022-08-27 00:32:19 +02:00
|
|
|
pub fn add_env_block(&mut self, block_id: BlockId) {
|
|
|
|
self.env_block = Some(block_id);
|
|
|
|
}
|
|
|
|
|
2022-05-07 21:39:22 +02:00
|
|
|
pub fn extend(&mut self, other: &Module) {
|
2021-11-16 00:16:06 +01:00
|
|
|
self.decls.extend(other.decls.clone());
|
2022-09-25 18:52:43 +02:00
|
|
|
self.aliases.extend(other.aliases.clone());
|
2021-11-16 00:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
2022-09-25 18:52:43 +02:00
|
|
|
self.decls.is_empty() && self.aliases.is_empty()
|
2021-11-16 00:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_decl_id(&self, name: &[u8]) -> Option<DeclId> {
|
|
|
|
self.decls.get(name).copied()
|
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn get_alias_id(&self, name: &[u8]) -> Option<AliasId> {
|
|
|
|
self.aliases.get(name).copied()
|
|
|
|
}
|
|
|
|
|
2021-11-16 00:16:06 +01:00
|
|
|
pub fn has_decl(&self, name: &[u8]) -> bool {
|
|
|
|
self.decls.contains_key(name)
|
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn has_alias(&self, name: &[u8]) -> bool {
|
|
|
|
self.aliases.contains_key(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decl_name_with_head(&self, name: &[u8], head: &[u8]) -> Option<Vec<u8>> {
|
|
|
|
if self.has_decl(name) {
|
2021-11-16 00:16:06 +01:00
|
|
|
let mut new_name = head.to_vec();
|
|
|
|
new_name.push(b' ');
|
|
|
|
new_name.extend(name);
|
2022-02-12 10:50:37 +01:00
|
|
|
Some(new_name)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn alias_name_with_head(&self, name: &[u8], head: &[u8]) -> Option<Vec<u8>> {
|
|
|
|
if self.has_alias(name) {
|
|
|
|
let mut new_name = head.to_vec();
|
|
|
|
new_name.push(b' ');
|
|
|
|
new_name.extend(name);
|
|
|
|
Some(new_name)
|
2021-11-16 00:16:06 +01:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decls_with_head(&self, head: &[u8]) -> Vec<(Vec<u8>, DeclId)> {
|
|
|
|
self.decls
|
|
|
|
.iter()
|
|
|
|
.map(|(name, id)| {
|
|
|
|
let mut new_name = head.to_vec();
|
|
|
|
new_name.push(b' ');
|
|
|
|
new_name.extend(name);
|
|
|
|
(new_name, *id)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn decl_names_with_head(&self, head: &[u8]) -> Vec<Vec<u8>> {
|
|
|
|
self.decls
|
|
|
|
.keys()
|
|
|
|
.map(|name| {
|
|
|
|
let mut new_name = head.to_vec();
|
|
|
|
new_name.push(b' ');
|
|
|
|
new_name.extend(name);
|
|
|
|
new_name
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2022-03-19 19:58:01 +01:00
|
|
|
pub fn aliases_with_head(&self, head: &[u8]) -> Vec<(Vec<u8>, AliasId)> {
|
|
|
|
self.aliases
|
|
|
|
.iter()
|
|
|
|
.map(|(name, id)| {
|
|
|
|
let mut new_name = head.to_vec();
|
|
|
|
new_name.push(b' ');
|
|
|
|
new_name.extend(name);
|
|
|
|
(new_name, *id)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn alias_names_with_head(&self, head: &[u8]) -> Vec<Vec<u8>> {
|
|
|
|
self.aliases
|
|
|
|
.keys()
|
|
|
|
.map(|name| {
|
|
|
|
let mut new_name = head.to_vec();
|
|
|
|
new_name.push(b' ');
|
|
|
|
new_name.extend(name);
|
|
|
|
new_name
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2021-11-16 00:16:06 +01:00
|
|
|
pub fn decls(&self) -> Vec<(Vec<u8>, DeclId)> {
|
|
|
|
self.decls
|
|
|
|
.iter()
|
|
|
|
.map(|(name, id)| (name.clone(), *id))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2022-02-12 10:50:37 +01:00
|
|
|
pub fn decl_names(&self) -> Vec<Vec<u8>> {
|
|
|
|
self.decls.keys().cloned().collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn alias_names(&self) -> Vec<Vec<u8>> {
|
|
|
|
self.aliases.keys().cloned().collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn aliases(&self) -> Vec<(Vec<u8>, AliasId)> {
|
|
|
|
self.aliases
|
|
|
|
.iter()
|
|
|
|
.map(|(name, id)| (name.clone(), *id))
|
|
|
|
.collect()
|
|
|
|
}
|
2021-11-16 00:16:06 +01:00
|
|
|
}
|
|
|
|
|
2022-05-07 21:39:22 +02:00
|
|
|
impl Default for Module {
|
2021-11-16 00:16:06 +01:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|