use crate::analysis::k_tuple::TerminalMappings;
use crate::grammar::cfg::TerminalIndexFn;
use crate::{Symbol, Terminal};
use parol_runtime::lexer::EOI;
use parol_runtime::TerminalIndex;
use std::fmt::{Debug, Display, Error, Formatter};
pub const EPS: TerminalIndex = TerminalIndex::MAX;
pub(crate) const INVALID: TerminalIndex = TerminalIndex::MAX - 1;
#[derive(Default, Debug, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct CompiledTerminal(pub TerminalIndex);
impl CompiledTerminal {
pub fn create<F, R>(s: &Symbol, terminal_index_resolver: R) -> Self
where
R: AsRef<F>,
F: TerminalIndexFn,
{
match s {
Symbol::T(Terminal::Trm(t, k, ..)) => {
Self(terminal_index_resolver.as_ref().terminal_index(t, *k))
}
Symbol::T(Terminal::End) => Self(EOI),
_ => panic!("Unexpected symbol type: {:?}", s),
}
}
}
impl AsRef<TerminalIndex> for CompiledTerminal {
fn as_ref(&self) -> &TerminalIndex {
&self.0
}
}
impl Display for CompiledTerminal {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(f, "{}", self.0)
}
}
impl From<TerminalIndex> for CompiledTerminal {
fn from(t: TerminalIndex) -> Self {
Self(t)
}
}
impl TerminalMappings<CompiledTerminal> for CompiledTerminal {
#[inline]
fn eps() -> CompiledTerminal {
Self(EPS)
}
#[inline]
fn end() -> CompiledTerminal {
Self(EOI)
}
#[inline]
fn is_eps(&self) -> bool {
self.0 == EPS
}
#[inline]
fn is_end(&self) -> bool {
self.0 == EOI
}
#[inline]
fn is_inv(&self) -> bool {
self.0 == INVALID
}
}