use alloc::boxed::Box;
use core::fmt;
use crate::error::{DiagnosticData, DiagnosticInfo};
use crate::source::SourceSpan;
use crate::state::Lang;
use super::tokenization::{StreamPosition, Token};
pub type TokenResult<L, T> = core::result::Result<T, TokenError<L>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, DiagnosticInfo)]
#[non_exhaustive]
#[diagnostic(
id = "core.token.end-of-stream-after-escape",
message = "expected command name after escape character ‘{escape_char}’ but reached \
end of input"
)]
pub struct EndOfStreamAfterEscape {
pub escape_char: char,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, DiagnosticInfo)]
#[non_exhaustive]
#[diagnostic(id = "core.token.forbidden-char")]
pub struct ForbiddenChar {
#[diagnostic(key = "char")]
pub ch: char,
}
impl fmt::Display for ForbiddenChar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "character is forbidden here: ‘{}’ (U+{:04X})", self.ch, self.ch as u32)
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum TokenErrorKind {
EndOfStreamAfterEscape(EndOfStreamAfterEscape),
ForbiddenChar(ForbiddenChar),
Custom(Box<dyn DiagnosticData>),
}
impl TokenErrorKind {
pub(crate) fn into_condition(self) -> Box<dyn DiagnosticData> {
match self {
TokenErrorKind::EndOfStreamAfterEscape(condition) => Box::new(condition),
TokenErrorKind::ForbiddenChar(condition) => Box::new(condition),
TokenErrorKind::Custom(data) => data,
}
}
}
pub struct TokenError<L: Lang> {
kind: TokenErrorKind,
span: SourceSpan<L::SourceOrigin>,
recovery: Option<Box<TokenRecovery<L>>>,
}
pub struct TokenRecovery<L: Lang> {
pub token: Token<L>,
pub resume: StreamPosition<L>,
}
impl<L: Lang> TokenError<L> {
pub fn new(
kind: TokenErrorKind,
span: SourceSpan<L::SourceOrigin>,
recovery: Option<TokenRecovery<L>>,
) -> Self {
TokenError { kind, span, recovery: recovery.map(Box::new) }
}
pub fn kind(&self) -> &TokenErrorKind {
&self.kind
}
pub fn span(&self) -> &SourceSpan<L::SourceOrigin> {
&self.span
}
pub fn recovery(&self) -> Option<&TokenRecovery<L>> {
self.recovery.as_deref()
}
pub fn into_recovery(self) -> Option<TokenRecovery<L>> {
self.recovery.map(|boxed| *boxed)
}
}
impl<L: Lang> Clone for TokenRecovery<L> {
fn clone(&self) -> Self {
TokenRecovery { token: self.token.clone(), resume: self.resume.clone() }
}
}
impl<L: Lang> Clone for TokenError<L> {
fn clone(&self) -> Self {
TokenError {
kind: self.kind.clone(),
span: self.span.clone(),
recovery: self.recovery.clone(),
}
}
}
impl<L: Lang> PartialEq for TokenRecovery<L> {
fn eq(&self, other: &Self) -> bool {
self.token == other.token && self.resume == other.resume
}
}
impl<L: Lang> Eq for TokenRecovery<L> {}
impl<L: Lang> fmt::Debug for TokenRecovery<L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TokenRecovery")
.field("token", &self.token)
.field("resume", &self.resume)
.finish()
}
}
impl<L: Lang> fmt::Debug for TokenError<L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TokenError")
.field("kind", &self.kind)
.field("span", &self.span)
.field("recovery", &self.recovery)
.finish()
}
}
impl fmt::Display for TokenErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TokenErrorKind::EndOfStreamAfterEscape(condition) => {
fmt::Display::fmt(condition, f)
}
TokenErrorKind::ForbiddenChar(condition) => fmt::Display::fmt(condition, f),
TokenErrorKind::Custom(data) => fmt::Display::fmt(data, f),
}
}
}
impl<L: Lang> fmt::Display for TokenError<L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.kind, f)
}
}
impl<L: Lang> core::error::Error for TokenError<L> {}