From 1b6d2a9b6289755459d96608cd7bfdaa9eebda82 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Thu, 16 Nov 2023 08:58:39 +0100 Subject: [PATCH] implement check for missing filters in pipeline --- src/main.rs | 10 ++++++++++ src/syntax/check.rs | 32 ++++++++++++++++++++++++++++++++ src/syntax/error.rs | 13 +++++++++++-- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index ffb618c..484dc5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,16 @@ fn main() { out_errs.append(&mut errs) } + let invalid_missing_filters = "meow | | test | awa | | nya"; + let invalid_missing_filters_id = files.add("invalid_missing_filters", invalid_missing_filters); + if let Err(mut errs) = check( + parse_syntax(invalid_missing_filters, invalid_missing_filters_id).unwrap(), + invalid_missing_filters, + invalid_missing_filters_id, + ) { + out_errs.append(&mut errs) + } + // invalid let writer = StandardStream::stderr(ColorChoice::Always); let config = term::Config::default(); diff --git a/src/syntax/check.rs b/src/syntax/check.rs index 483655a..48c1a10 100644 --- a/src/syntax/check.rs +++ b/src/syntax/check.rs @@ -14,6 +14,12 @@ pub fn check( errs.push(SyntaxError::MissingStreamer(vec![(file_id, e_span)])); } + if let Err(mut err_locs) = check_missing_filters(&syntax) { + errs.push(SyntaxError::MissingFilter( + err_locs.into_iter().map(|loc| (file_id, loc)).collect(), + )) + } + if errs.is_empty() { Ok(syntax) } else { @@ -32,3 +38,29 @@ fn check_missing_streamer(syntax: &Vec) -> Result<(), logos::Sp Ok(()) } } + +fn check_missing_filters(syntax: &Vec) -> Result<(), Vec> { + let mut missing_filter_locs = Vec::new(); + + for i in 0..syntax.len() { + if let ( + Some(PipelineElement { + kind: PipelineElementKind::Pipe, + ref span, + }), + Some(PipelineElement { + kind: PipelineElementKind::Pipe, + span: ref span1, + }), + ) = (syntax.get(i), syntax.get(i + 1)) + { + missing_filter_locs.push(span.start..span1.end) + } + } + + if missing_filter_locs.is_empty() { + Ok(()) + } else { + Err(missing_filter_locs) + } +} diff --git a/src/syntax/error.rs b/src/syntax/error.rs index 9e572d0..d507d0a 100644 --- a/src/syntax/error.rs +++ b/src/syntax/error.rs @@ -10,9 +10,9 @@ pub enum SyntaxError { /// `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, + MissingSink(Vec<(FileId, logos::Span)>), /// This indicates a missing filter somewhere in the pipeline, meaning that there's 2 pipes after one another - MissingFilter, + MissingFilter(Vec<(FileId, logos::Span)>), /// A literal cannot be a sink LiteralAsSink, /// A literal can't be a filter either @@ -42,6 +42,15 @@ impl SyntaxError { }) .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(), + ), _ => todo!(), } }