use super::Module;
use crate::utils::ptr_util::WeakPtr;
#[derive(Clone, Debug, Default)]
pub struct Scope {
pub parser_scope: String,
pub module: Option<WeakPtr<Module>>,
}
impl Scope {
pub fn push_scope(&mut self, scope: &str) {
if !self.parser_scope.is_empty() {
self.parser_scope.push_str("::");
}
self.parser_scope.push_str(scope);
}
pub fn pop_scope(&mut self) {
if let Some(last_scope_index) = self.parser_scope.rfind("::") {
#[cfg(debug_assertions)]
{
let module_scope = self.module.as_ref().map(|m| m.borrow().nested_module_identifier());
debug_assert!(self.parser_scope.len() > module_scope.map_or(0, str::len))
}
self.parser_scope.truncate(last_scope_index);
} else {
debug_assert!(self.module.is_none());
self.parser_scope.clear();
}
}
}
pub fn get_scoped_identifier(identifier: &str, scope: &str) -> String {
if scope.is_empty() {
identifier.to_owned()
} else {
scope.to_owned() + "::" + identifier
}
}