use crate::context::ParsingContext;
use dsntk_feel::Name;
use std::cell::RefCell;
use std::collections::HashSet;
use std::fmt;
pub struct ParsingScope {
stack: RefCell<Vec<ParsingContext>>,
}
impl From<&dsntk_feel::FeelScope> for ParsingScope {
fn from(scope: &dsntk_feel::FeelScope) -> Self {
let stack = RefCell::new(vec![]);
for feel_context in scope.contexts() {
stack.borrow_mut().push(feel_context.into());
}
Self { stack }
}
}
impl Default for ParsingScope {
fn default() -> Self {
Self {
stack: RefCell::new(vec![ParsingContext::default()]),
}
}
}
impl fmt::Display for ParsingScope {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}]", self.stack.borrow_mut().iter().map(|ctx| ctx.to_string()).collect::<Vec<String>>().join(", "))
}
}
impl ParsingScope {
pub fn pop(&self) -> Option<ParsingContext> {
self.stack.borrow_mut().pop()
}
pub fn push(&self, ctx: ParsingContext) {
self.stack.borrow_mut().push(ctx);
}
pub fn push_default(&self) {
self.stack.borrow_mut().push(ParsingContext::default());
}
pub fn set_name(&self, name: Name) {
if let Some(last_ctx) = self.stack.borrow_mut().last_mut() {
last_ctx.set_name(name);
}
}
pub fn set_context(&self, name: Name, ctx: ParsingContext) {
if let Some(last_ctx) = self.stack.borrow_mut().last_mut() {
last_ctx.set_context(name, ctx);
}
}
pub fn flattened_keys(&self) -> HashSet<String> {
self.stack.borrow().iter().flat_map(|ctx| ctx.flattened_keys()).collect::<HashSet<String>>()
}
}