use crate::{
lexer::Span,
utils::{Expected, SimpleSpan},
};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct UnexpectedKeyword<'a, F, S = SimpleSpan> {
span: S,
found: F,
expected: Expected<'a, &'a str>,
}
impl<'a, F, S> UnexpectedKeyword<'a, F, S> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(span: S, found: F, expected: Expected<'a, &'a str>) -> Self {
Self {
span,
found,
expected,
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn expected_one(span: S, found: F, expected: &'a str) -> Self {
Self::new(span, found, Expected::one(expected))
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn expected_one_of(span: S, found: F, expected: &'a [&'a str]) -> Self {
Self::new(span, found, Expected::one_of(expected))
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn span(&self) -> S
where
S: Copy,
{
self.span
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn span_ref(&self) -> &S {
&self.span
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn found(&self) -> &F {
&self.found
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn expected(&self) -> Expected<'a, &'a str> {
self.expected
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn bump(&mut self, offset: &S::Offset) -> &mut Self
where
S: Span,
{
self.span.bump(offset);
self
}
}
impl<S: core::fmt::Display> core::fmt::Display for UnexpectedKeyword<'_, S> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self.expected {
Expected::One(expected) => {
write!(
f,
"unexpected '{}', expected '{}' keyword",
self.found, expected
)
}
Expected::OneOf(expected) => {
write!(f, "unexpected '{}', expected one of: ", self.found)?;
for (i, kw) in expected.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "'{}'", kw)?;
}
write!(f, " keyword")
}
}
}
}
impl<S: core::fmt::Debug + core::fmt::Display> core::error::Error for UnexpectedKeyword<'_, S> {}