use crate::{
Lexer,
error::{
Unclosed, Undelimited, Unopened,
syntax::{FullContainer, MissingSyntaxOf, TooFew, TooMany},
token::{
MissingLeadingOf, MissingSeparatorOf, MissingTrailingOf, UnexpectedLeadingOf,
UnexpectedRepeatedOf, UnexpectedToken, UnexpectedTrailingOf,
},
},
lexer::Cursor,
utils::{Message, Spanned},
};
use super::Token;
pub use fatal::Fatal;
pub use silent::Silent;
mod fatal;
mod ignored;
mod silent;
pub trait Emitter<'a, L, Lang: ?Sized = ()> {
type Error;
fn emit_lexer_error(
&mut self,
err: Spanned<<L::Token as Token<'a>>::Error, L::Span>,
) -> Result<(), Self::Error>
where
L: Lexer<'a>;
fn emit_unexpected_token(
&mut self,
err: UnexpectedToken<'a, L::Token, <L::Token as Token<'a>>::Kind, L::Span, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'a>;
fn emit_error(&mut self, err: Spanned<Self::Error, L::Span>) -> Result<(), Self::Error>
where
L: Lexer<'a>;
fn rewind(&mut self, cursor: &Cursor<'a, '_, L>)
where
L: Lexer<'a>;
}
impl<'a, L, U, Lang: ?Sized> Emitter<'a, L, Lang> for &mut U
where
U: Emitter<'a, L, Lang>,
{
type Error = U::Error;
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_lexer_error(
&mut self,
err: Spanned<<L::Token as Token<'a>>::Error, L::Span>,
) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).emit_lexer_error(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_unexpected_token(
&mut self,
err: UnexpectedToken<'a, L::Token, <L::Token as Token<'a>>::Kind, L::Span, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).emit_unexpected_token(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_error(&mut self, err: Spanned<Self::Error, L::Span>) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).emit_error(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn rewind(&mut self, cursor: &Cursor<'a, '_, L>)
where
L: Lexer<'a>,
{
(**self).rewind(cursor)
}
}
pub trait FromEmitterError<'a, L, Lang: ?Sized = ()> {
fn from_lexer_error(err: Spanned<<L::Token as Token<'a>>::Error, L::Span>) -> Self
where
L: Lexer<'a>;
fn from_unexpected_token(
err: UnexpectedToken<'a, L::Token, <L::Token as Token<'a>>::Kind, L::Span, Lang>,
) -> Self
where
L: Lexer<'a>;
}
impl<'a, T, L, Lang: ?Sized> FromEmitterError<'a, L, Lang> for T
where
L: Lexer<'a>,
T: From<<L::Token as Token<'a>>::Error>
+ From<UnexpectedToken<'a, L::Token, <L::Token as Token<'a>>::Kind, L::Span, Lang>>,
{
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_lexer_error(err: Spanned<<L::Token as Token<'a>>::Error, L::Span>) -> Self
where
L: Lexer<'a>,
{
err.into_data().into()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_unexpected_token(
err: UnexpectedToken<'a, L::Token, <L::Token as Token<'a>>::Kind, L::Span, Lang>,
) -> Self
where
L: Lexer<'a>,
{
err.into()
}
}
pub trait BatchEmitter<'a, L, Error, Lang: ?Sized = ()>: Emitter<'a, L, Lang> {
fn create_batch(&mut self, span: L::Span, description: Message)
where
L: Lexer<'a>;
fn create_batch_with_error(
&mut self,
description: Message,
err: Spanned<Error, L::Span>,
) -> Result<(), Self::Error>
where
L: Lexer<'a>;
fn emit_to_batch(
&mut self,
id: &L::Span,
err: Spanned<Error, L::Span>,
) -> Result<(), Self::Error>
where
L: Lexer<'a>;
fn emit_batch(&mut self, id: &L::Span) -> Result<(), Self::Error>
where
L: Lexer<'a>;
fn drop_batch(&mut self, id: &L::Span)
where
L: Lexer<'a>;
}
impl<'a, L, Error, U, Lang: ?Sized> BatchEmitter<'a, L, Error, Lang> for &mut U
where
U: BatchEmitter<'a, L, Error, Lang>,
{
#[cfg_attr(not(tarpaulin), inline(always))]
fn create_batch(&mut self, span: L::Span, description: Message)
where
L: Lexer<'a>,
{
(**self).create_batch(span, description)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn create_batch_with_error(
&mut self,
description: Message,
err: Spanned<Error, L::Span>,
) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).create_batch_with_error(description, err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_to_batch(&mut self, id: &L::Span, err: Spanned<Error, L::Span>) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).emit_to_batch(id, err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_batch(&mut self, id: &L::Span) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).emit_batch(id)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn drop_batch(&mut self, id: &L::Span)
where
L: Lexer<'a>,
{
(**self).drop_batch(id)
}
}
pub trait DelimiterEmitter<'a, Delim, L, Lang: ?Sized = ()>: Emitter<'a, L, Lang> {
fn emit_unclosed(&mut self, err: Unclosed<Delim, L::Span, Lang>) -> Result<(), Self::Error>
where
L: Lexer<'a>;
fn emit_unopened(&mut self, err: Unopened<Delim, L::Span, Lang>) -> Result<(), Self::Error>
where
L: Lexer<'a>;
fn emit_undelimited(&mut self, err: Undelimited<Delim, L::Span, Lang>) -> Result<(), Self::Error>
where
L: Lexer<'a>;
}
impl<'a, Delim, L, U, Lang: ?Sized> DelimiterEmitter<'a, Delim, L, Lang> for &mut U
where
U: DelimiterEmitter<'a, Delim, L, Lang>,
{
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_unclosed(&mut self, err: Unclosed<Delim, L::Span, Lang>) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).emit_unclosed(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_unopened(&mut self, err: Unopened<Delim, L::Span, Lang>) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).emit_unopened(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_undelimited(&mut self, err: Undelimited<Delim, L::Span, Lang>) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).emit_undelimited(err)
}
}
pub trait FromDelimiterEmitterError<'a, Delim, L, Lang: ?Sized = ()> {
fn from_unclosed(err: Unclosed<Delim, L::Span, Lang>) -> Self
where
L: Lexer<'a>;
fn from_unopened(err: Unopened<Delim, L::Span, Lang>) -> Self
where
L: Lexer<'a>;
fn from_undelimited(err: Undelimited<Delim, L::Span, Lang>) -> Self
where
L: Lexer<'a>;
}
impl<'a, T, Delim, L, Lang: ?Sized> FromDelimiterEmitterError<'a, Delim, L, Lang> for T
where
L: Lexer<'a>,
T: From<Unclosed<Delim, L::Span, Lang>>
+ From<Unopened<Delim, L::Span, Lang>>
+ From<Undelimited<Delim, L::Span, Lang>>,
{
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_unclosed(err: Unclosed<Delim, L::Span, Lang>) -> Self
where
L: Lexer<'a>,
{
err.into()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_unopened(err: Unopened<Delim, L::Span, Lang>) -> Self
where
L: Lexer<'a>,
{
err.into()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_undelimited(err: Undelimited<Delim, L::Span, Lang>) -> Self
where
L: Lexer<'a>,
{
err.into()
}
}
pub trait RepeatedEmitter<'a, O: ?Sized, L, Lang: ?Sized = ()>: Emitter<'a, L, Lang> {
fn emit_too_few(&mut self, err: TooFew<O, L::Span, Lang>) -> Result<(), Self::Error>
where
L: Lexer<'a>;
fn emit_too_many(&mut self, err: TooMany<O, L::Span, Lang>) -> Result<(), Self::Error>
where
L: Lexer<'a>;
fn emit_full_container(
&mut self,
err: FullContainer<O, L::Span, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'a>;
}
impl<'a, O, L, U, Lang: ?Sized> RepeatedEmitter<'a, O, L, Lang> for &mut U
where
U: RepeatedEmitter<'a, O, L, Lang>,
{
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_too_few(&mut self, err: TooFew<O, L::Span, Lang>) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).emit_too_few(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_too_many(&mut self, err: TooMany<O, L::Span, Lang>) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).emit_too_many(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_full_container(&mut self, err: FullContainer<O, L::Span, Lang>) -> Result<(), Self::Error>
where
L: Lexer<'a>,
{
(**self).emit_full_container(err)
}
}
pub trait FromRepeatedEmitterError<'a, O: ?Sized, L, Lang: ?Sized = ()> {
fn from_too_few(err: TooFew<O, L::Span, Lang>) -> Self
where
L: Lexer<'a>;
fn from_too_many(err: TooMany<O, L::Span, Lang>) -> Self
where
L: Lexer<'a>;
fn from_full_container(err: FullContainer<O, L::Span, Lang>) -> Self
where
L: Lexer<'a>;
}
impl<'a, T, O: ?Sized, L, Lang: ?Sized> FromRepeatedEmitterError<'a, O, L, Lang> for T
where
L: Lexer<'a>,
T: From<TooFew<O, L::Span, Lang>>
+ From<TooMany<O, L::Span, Lang>>
+ From<FullContainer<O, L::Span, Lang>>,
{
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_too_few(err: TooFew<O, L::Span, Lang>) -> Self
where
L: Lexer<'a>,
{
err.into()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_too_many(err: TooMany<O, L::Span, Lang>) -> Self
where
L: Lexer<'a>,
{
err.into()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_full_container(err: FullContainer<O, L::Span, Lang>) -> Self
where
L: Lexer<'a>,
{
err.into()
}
}
pub trait SeparatedByEmitter<'inp, O, Sep, L, Lang: ?Sized = ()>:
RepeatedEmitter<'inp, O, L, Lang>
+ BatchEmitter<'inp, L, UnexpectedLeadingOf<'inp, Sep, L, Lang>, Lang>
+ BatchEmitter<'inp, L, UnexpectedTrailingOf<'inp, Sep, L, Lang>, Lang>
+ BatchEmitter<'inp, L, UnexpectedRepeatedOf<'inp, Sep, L, Lang>, Lang>
where
L: Lexer<'inp>,
{
fn emit_missing_separator(
&mut self,
err: MissingSeparatorOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>;
fn emit_missing_element(
&mut self,
err: MissingSyntaxOf<'inp, O, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>;
fn emit_missing_leading_separator(
&mut self,
err: MissingLeadingOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>;
fn emit_missing_trailing_separator(
&mut self,
err: MissingTrailingOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>;
fn emit_unexpected_repeated_separator(
&mut self,
err: UnexpectedRepeatedOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>;
fn emit_unexpected_leading_separator(
&mut self,
err: UnexpectedLeadingOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>;
fn emit_unexpected_trailing_separator(
&mut self,
err: UnexpectedTrailingOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>;
}
impl<'inp, O, L, Sep, U, Lang: ?Sized> SeparatedByEmitter<'inp, O, Sep, L, Lang> for &mut U
where
L: Lexer<'inp>,
U: SeparatedByEmitter<'inp, O, Sep, L, Lang>,
{
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_missing_separator(
&mut self,
err: MissingSeparatorOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>,
{
(**self).emit_missing_separator(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_missing_element(
&mut self,
err: MissingSyntaxOf<'inp, O, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>,
{
(**self).emit_missing_element(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_missing_leading_separator(
&mut self,
err: MissingLeadingOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>,
{
(**self).emit_missing_leading_separator(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_missing_trailing_separator(
&mut self,
err: MissingTrailingOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>,
{
(**self).emit_missing_trailing_separator(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_unexpected_repeated_separator(
&mut self,
err: UnexpectedRepeatedOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>,
{
(**self).emit_unexpected_repeated_separator(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_unexpected_leading_separator(
&mut self,
err: UnexpectedLeadingOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>,
{
(**self).emit_unexpected_leading_separator(err)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn emit_unexpected_trailing_separator(
&mut self,
err: UnexpectedTrailingOf<'inp, Sep, L, Lang>,
) -> Result<(), Self::Error>
where
L: Lexer<'inp>,
{
(**self).emit_unexpected_trailing_separator(err)
}
}
pub trait FromSeparatedByEmitterError<'inp, O, Sep, L, Lang: ?Sized = ()>:
FromRepeatedEmitterError<'inp, O, L, Lang>
+ FromEmitterError<'inp, L, Lang>
+ From<UnexpectedLeadingOf<'inp, Sep, L, Lang>>
+ From<UnexpectedTrailingOf<'inp, Sep, L, Lang>>
+ From<UnexpectedRepeatedOf<'inp, Sep, L, Lang>>
where
L: Lexer<'inp>,
{
fn from_missing_separator(err: MissingSeparatorOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>;
fn from_missing_element(err: MissingSyntaxOf<'inp, O, L, Lang>) -> Self
where
L: Lexer<'inp>;
fn from_missing_leading_separator(err: MissingLeadingOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>;
fn from_missing_trailing_separator(err: MissingTrailingOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>;
fn from_unexpected_repeated_separator(err: UnexpectedRepeatedOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>;
fn from_unexpected_leading_separator(err: UnexpectedLeadingOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>;
fn from_unexpected_trailing_separator(err: UnexpectedTrailingOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>;
}
impl<'inp, T, O, Sep, L, Lang: ?Sized> FromSeparatedByEmitterError<'inp, O, Sep, L, Lang> for T
where
L: Lexer<'inp>,
T: From<MissingSeparatorOf<'inp, Sep, L, Lang>>
+ From<MissingSyntaxOf<'inp, O, L, Lang>>
+ From<MissingLeadingOf<'inp, Sep, L, Lang>>
+ From<MissingTrailingOf<'inp, Sep, L, Lang>>
+ From<UnexpectedRepeatedOf<'inp, Sep, L, Lang>>
+ From<UnexpectedLeadingOf<'inp, Sep, L, Lang>>
+ From<UnexpectedTrailingOf<'inp, Sep, L, Lang>>
+ FromEmitterError<'inp, L, Lang>
+ FromRepeatedEmitterError<'inp, O, L, Lang>,
{
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_missing_separator(err: MissingSeparatorOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>,
{
err.into()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_missing_element(err: MissingSyntaxOf<'inp, O, L, Lang>) -> Self
where
L: Lexer<'inp>,
{
err.into()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_missing_leading_separator(err: MissingLeadingOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>,
{
err.into()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_missing_trailing_separator(err: MissingTrailingOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>,
{
err.into()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_unexpected_repeated_separator(err: UnexpectedRepeatedOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>,
{
err.into()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_unexpected_leading_separator(err: UnexpectedLeadingOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>,
{
err.into()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_unexpected_trailing_separator(err: UnexpectedTrailingOf<'inp, Sep, L, Lang>) -> Self
where
L: Lexer<'inp>,
{
err.into()
}
}