iowo/src/syntax/error.rs

66 lines
2.8 KiB
Rust

use codespan_reporting::diagnostic::{Diagnostic, Label};
pub type FileId = usize;
/// The enum representing a syntax error, used for error reporting
#[derive(Debug, Clone)]
pub enum SyntaxError {
/// This variant indicates a token that the Lexer didn't recognize
InvalidToken(Vec<(FileId, logos::Span)>),
/// `MissingStreamer` means, that the pipeline starts with a Pipe (`|`), so it has no streamer as input in front of it.
MissingStreamer(Vec<(FileId, logos::Span)>),
/// `MissingSink` means, that the pipeline ends with a Pipe (`|`), meaning that the output can't go anywhere
MissingSink(Vec<(FileId, logos::Span)>),
/// This indicates a missing filter somewhere in the pipeline, meaning that there's 2 pipes after one another
MissingFilter(Vec<(FileId, logos::Span)>),
/// A literal cannot be a sink, TODO
LiteralAsSink,
/// A literal can't be a filter either, TODO
LiteralAsFilter,
/// A literal acting as streamer cannot take arguments, TODO
LiteralWithArgs,
}
impl SyntaxError {
pub fn to_diagnostic(&self) -> Diagnostic<usize> {
match self {
Self::InvalidToken(errs) => Diagnostic::error()
.with_message("failed to parse invalid tokens")
.with_labels(
errs.into_iter()
.map(|(file_id, span)| {
Label::primary(*file_id, span.clone()).with_message("invalid token")
})
.collect(),
),
Self::MissingStreamer(locs) => Diagnostic::error()
.with_message("pipelines must always start with a streamer")
.with_labels(
locs.into_iter()
.map(|(file_id, span)| {
Label::primary(*file_id, span.clone()).with_message("missing streamer")
})
.collect(),
),
Self::MissingFilter(locs) => Diagnostic::error()
.with_message("missing filters in pipelines")
.with_labels(
locs.into_iter()
.map(|(file_id, span)| {
Label::primary(*file_id, span.clone()).with_message("no filter here")
})
.collect(),
),
Self::MissingSink(locs) => Diagnostic::error()
.with_message("pipelines cannot end on a pipe")
.with_labels(
locs.into_iter()
.map(|(file_id, span)| {
Label::primary(*file_id, span.clone()).with_message("no sink")
})
.collect(),
),
_ => unimplemented!(),
}
}
}