implement super basic check

This commit is contained in:
Schrottkatze 2023-11-15 11:18:45 +01:00
parent b71b7f309b
commit 3f4846744b
2 changed files with 44 additions and 4 deletions

View file

@ -1,16 +1,23 @@
use lexer::Token; use lexer::Token;
use logos::Lexer; use logos::Lexer;
use logos::Logos; use logos::Logos;
use syntax::parse; use syntax::parse_syntax;
use utils::ws; use utils::ws;
use winnow::prelude::*; use winnow::prelude::*;
use winnow::Parser; use winnow::Parser;
use crate::syntax::check_syntax;
mod lexer; mod lexer;
mod syntax; mod syntax;
mod utils; mod utils;
fn main() { fn main() {
// valid
let input = "load \"./image.png\" | invert | save \"./image_processed.jpg\""; 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)
} }

View file

@ -1,5 +1,14 @@
use core::panic;
use std::mem; 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::Logos;
use logos::Span; use logos::Span;
@ -31,7 +40,7 @@ pub enum CommandPartKind {
String(String), String(String),
} }
pub fn parse(input: &str) -> Vec<PipelineElement> { pub fn parse_syntax(input: &str) -> Vec<PipelineElement> {
let lexer = Token::lexer(input); let lexer = Token::lexer(input);
let mut r = Vec::new(); let mut r = Vec::new();
@ -76,6 +85,7 @@ pub fn parse(input: &str) -> Vec<PipelineElement> {
_ => {} _ => {}
} }
} else { } else {
panic!("Error at {span:?}")
} }
} }
@ -91,8 +101,31 @@ pub fn parse(input: &str) -> Vec<PipelineElement> {
r r
} }
pub fn check_syntax(syntax: Vec<PipelineElement>, 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] #[test]
fn test_simple_parse_pipeline() { fn test_simple_parse_pipeline() {
let test_pipeline = "load ./image.png | invert | save ./image_processed.jpg"; let test_pipeline = "load ./image.png | invert | save ./image_processed.jpg";
parse(test_pipeline); parse_syntax(test_pipeline);
} }