use crate::syntax::ast::Span;
use crate::syntax::ast::{position::Position, Node};
use crate::syntax::lexer::Error as LexError;
use std::fmt;
pub type ParseResult = Result<Node, ParseError>;
pub(crate) trait ErrorContext {
fn context(self, context: &'static str) -> Self;
}
impl<T> ErrorContext for Result<T, ParseError> {
fn context(self, context: &'static str) -> Self {
self.map_err(|e| e.context(context))
}
}
impl From<LexError> for ParseError {
fn from(e: LexError) -> Self {
Self::lex(e)
}
}
#[derive(Debug)]
pub enum ParseError {
Expected {
expected: Box<[String]>,
found: Box<str>,
span: Span,
context: &'static str,
},
Unexpected {
found: Box<str>,
span: Span,
message: Option<&'static str>,
},
AbruptEnd,
Lex { err: LexError },
General {
message: &'static str,
position: Position,
},
Unimplemented {
message: &'static str,
position: Position,
},
}
impl ParseError {
fn context(self, new_context: &'static str) -> Self {
match self {
Self::Expected {
expected,
found,
span,
..
} => Self::expected(expected, found, span, new_context),
e => e,
}
}
pub(super) fn expected<E, F>(expected: E, found: F, span: Span, context: &'static str) -> Self
where
E: Into<Box<[String]>>,
F: Into<Box<str>>,
{
Self::Expected {
expected: expected.into(),
found: found.into(),
span,
context,
}
}
pub(super) fn unexpected<F, C>(found: F, span: Span, message: C) -> Self
where
F: Into<Box<str>>,
C: Into<Option<&'static str>>,
{
Self::Unexpected {
found: found.into(),
span,
message: message.into(),
}
}
pub(crate) fn general(message: &'static str, position: Position) -> Self {
Self::General { message, position }
}
pub(super) fn wrong_function_declaration_non_strict(position: Position) -> Self {
Self::General {
message: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
position
}
}
pub(super) fn lex(e: LexError) -> Self {
Self::Lex { err: e }
}
#[allow(dead_code)]
pub(super) fn unimplemented(message: &'static str, position: Position) -> Self {
Self::Unimplemented { message, position }
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Expected {
expected,
found,
span,
context,
} => write!(
f,
"expected {}, got '{found}' in {context} at line {}, col {}",
if expected.len() == 1 {
format!(
"token '{}'",
expected.first().expect("already checked that length is 1")
)
} else {
format!(
"one of {}",
expected
.iter()
.enumerate()
.map(|(i, t)| {
format!(
"{}'{t}'",
if i == 0 {
""
} else if i == expected.len() - 1 {
" or "
} else {
", "
},
)
})
.collect::<String>()
)
},
span.start().line_number(),
span.start().column_number()
),
Self::Unexpected {
found,
span,
message,
} => write!(
f,
"unexpected token '{found}'{} at line {}, col {}",
if let Some(m) = message {
format!(", {m}")
} else {
String::new()
},
span.start().line_number(),
span.start().column_number()
),
Self::AbruptEnd => f.write_str("abrupt end"),
Self::General { message, position } => write!(
f,
"{message} at line {}, col {}",
position.line_number(),
position.column_number()
),
Self::Lex { err } => fmt::Display::fmt(err, f),
Self::Unimplemented { message, position } => write!(
f,
"{message} not yet implemented at line {}, col {}",
position.line_number(),
position.column_number()
),
}
}
}