#[cfg(doc)]
pub mod reference;
pub mod ast;
mod execution;
pub mod functions;
pub mod graph;
mod parser;
pub use execution::ExecutionError;
pub use execution::Variables;
pub use parser::Location;
pub use parser::ParseError;
use string_interner::symbol::SymbolU32;
use string_interner::StringInterner;
use std::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Identifier(SymbolU32);
impl DisplayWithContext for Identifier {
fn fmt(&self, f: &mut fmt::Formatter, ctx: &Context) -> fmt::Result {
write!(f, "{}", ctx.identifiers.resolve(self.0).unwrap())
}
}
#[derive(Default)]
pub struct Context {
identifiers: StringInterner,
}
impl Context {
pub fn new() -> Context {
Context::default()
}
#[inline(always)]
pub fn add_identifier<T: AsRef<str>>(&mut self, identifier: T) -> Identifier {
Identifier(self.identifiers.get_or_intern(identifier))
}
#[inline(always)]
pub fn get_identifier<T: AsRef<str>>(&self, identifier: T) -> Option<Identifier> {
self.identifiers.get(identifier).map(Identifier)
}
pub fn resolve(&self, identifier: Identifier) -> &str {
self.identifiers.resolve(identifier.0).unwrap()
}
}
pub trait DisplayWithContext
where
Self: Sized,
{
fn fmt(&self, f: &mut fmt::Formatter, ctx: &Context) -> fmt::Result;
fn display_with<'a>(&'a self, ctx: &'a Context) -> Box<dyn fmt::Display + 'a> {
struct Impl<'a, T: DisplayWithContext>(&'a T, &'a Context);
impl<'a, T: DisplayWithContext> fmt::Display for Impl<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f, self.1)
}
}
Box::new(Impl(self, ctx))
}
}
impl<T: DisplayWithContext> DisplayWithContext for Box<T> {
fn fmt(&self, f: &mut fmt::Formatter, ctx: &Context) -> fmt::Result {
self.as_ref().fmt(f, ctx)
}
}