mod fmt;
use super::{Symbol, SourcePos};
pub use fmt::ErrorsDisplayContext;
#[derive(Debug)]
pub enum ErrorKind {
UndeclaredVariable(Symbol),
DuplicateVariable(Symbol),
DuplicateKey(Symbol),
ReturnOutsideFunction,
SelfOutsideFunction,
TryOutsideFunction,
BreakOutsideLoop,
InvalidAssignment,
AsyncBuiltin,
}
#[derive(Debug)]
pub struct Error {
pub kind: ErrorKind,
pub pos: SourcePos,
}
impl Error {
pub fn undeclared_variable(symbol: Symbol, pos: SourcePos) -> Self {
Self {
kind: ErrorKind::UndeclaredVariable(symbol),
pos
}
}
pub fn duplicate_variable(symbol: Symbol, pos: SourcePos) -> Self {
Self {
kind: ErrorKind::DuplicateVariable(symbol),
pos
}
}
pub fn duplicate_key(symbol: Symbol, pos: SourcePos) -> Self {
Self {
kind: ErrorKind::DuplicateKey(symbol),
pos
}
}
pub fn return_outside_function(pos: SourcePos) -> Self {
Self {
kind: ErrorKind::ReturnOutsideFunction,
pos
}
}
pub fn self_outside_function(pos: SourcePos) -> Self {
Self {
kind: ErrorKind::SelfOutsideFunction,
pos
}
}
pub fn try_outside_function(pos: SourcePos) -> Self {
Self {
kind: ErrorKind::TryOutsideFunction,
pos
}
}
pub fn break_outside_loop(pos: SourcePos) -> Self {
Self {
kind: ErrorKind::BreakOutsideLoop,
pos
}
}
pub fn invalid_assignment(pos: SourcePos) -> Self {
Self {
kind: ErrorKind::InvalidAssignment,
pos
}
}
pub fn async_builtin(pos: SourcePos) -> Self {
Self {
kind: ErrorKind::AsyncBuiltin,
pos
}
}
}
#[derive(Debug, Default)]
pub struct Errors(pub Vec<Error>);
impl IntoIterator for Errors {
type Item = Error;
type IntoIter = std::vec::IntoIter<Error>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl Extend<Error> for Errors {
fn extend<T>(&mut self, iter: T)
where
T : IntoIterator<Item = Error>,
{
self.0.extend(iter)
}
}
impl std::error::Error for Errors { }