From 3f4846744b306b4102e3ac9bdc9cc7ebef5d49a8 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Wed, 15 Nov 2023 11:18:45 +0100 Subject: [PATCH] implement super basic check --- src/main.rs | 11 +++++++++-- src/syntax.rs | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index ad6846e..99bd744 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,23 @@ use lexer::Token; use logos::Lexer; use logos::Logos; -use syntax::parse; +use syntax::parse_syntax; use utils::ws; use winnow::prelude::*; use winnow::Parser; +use crate::syntax::check_syntax; + mod lexer; mod syntax; mod utils; fn main() { + // valid let input = "load \"./image.png\" | invert | save \"./image_processed.jpg\""; - dbg!(parse(input)); + dbg!(parse_syntax(input)); + + // invalid + let invalid_no_streamer = "| invert | save \"./image_processed.jpg\""; + check_syntax(parse_syntax(invalid_no_streamer), invalid_no_streamer) } diff --git a/src/syntax.rs b/src/syntax.rs index b4ed5c6..1ea4c8b 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -1,5 +1,14 @@ +use core::panic; use std::mem; +use codespan_reporting::diagnostic::Diagnostic; +use codespan_reporting::diagnostic::Label; +use codespan_reporting::files::Files; +use codespan_reporting::files::SimpleFile; +use codespan_reporting::files::SimpleFiles; +use codespan_reporting::term; +use codespan_reporting::term::termcolor::ColorChoice; +use codespan_reporting::term::termcolor::StandardStream; use logos::Logos; use logos::Span; @@ -31,7 +40,7 @@ pub enum CommandPartKind { String(String), } -pub fn parse(input: &str) -> Vec { +pub fn parse_syntax(input: &str) -> Vec { let lexer = Token::lexer(input); let mut r = Vec::new(); @@ -76,6 +85,7 @@ pub fn parse(input: &str) -> Vec { _ => {} } } else { + panic!("Error at {span:?}") } } @@ -91,8 +101,31 @@ pub fn parse(input: &str) -> Vec { r } +pub fn check_syntax(syntax: Vec, raw_source: &str) { + let mut files = SimpleFiles::new(); + let file_id = files.add("input", raw_source); + + if let Some(PipelineElement { + kind: PipelineElementKind::Pipe, + span, + }) = syntax.first() + { + let err = Diagnostic::error() + .with_message("Missing streamer for pipeline") + .with_labels(vec![ + Label::primary(file_id, 0..span.end).with_message("expected streamer") + ]) + .with_notes(vec!["a pipeline can't start with a pipe".to_string()]); + + let writer = StandardStream::stderr(ColorChoice::Always); + let config = codespan_reporting::term::Config::default(); + + term::emit(&mut writer.lock(), &config, &files, &err).unwrap(); + } +} + #[test] fn test_simple_parse_pipeline() { let test_pipeline = "load ./image.png | invert | save ./image_processed.jpg"; - parse(test_pipeline); + parse_syntax(test_pipeline); }