wrote tests for parser and checks
This commit is contained in:
parent
d49e0d38e6
commit
91adcd40f5
5 changed files with 52 additions and 119 deletions
|
@ -1,6 +1,6 @@
|
|||
use super::{
|
||||
error::{FileId, SyntaxError},
|
||||
PipelineElement, PipelineElementKind,
|
||||
parse_syntax, PipelineElement, PipelineElementKind,
|
||||
};
|
||||
|
||||
pub fn check(
|
||||
|
@ -11,17 +11,15 @@ pub fn check(
|
|||
let mut errs = Vec::new();
|
||||
|
||||
if let Err(e_span) = check_missing_streamer(&syntax) {
|
||||
errs.push(SyntaxError::MissingStreamer(vec![(file_id, e_span)]));
|
||||
errs.push(SyntaxError::MissingStreamer(vec![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 let Err(err_locs) = check_missing_filters(&syntax) {
|
||||
errs.push(SyntaxError::MissingFilter(err_locs))
|
||||
}
|
||||
|
||||
if let Err(e_span) = check_missing_sink(&syntax) {
|
||||
errs.push(SyntaxError::MissingSink(vec![(file_id, e_span)]));
|
||||
errs.push(SyntaxError::MissingSink(vec![e_span]));
|
||||
}
|
||||
|
||||
if errs.is_empty() {
|
||||
|
@ -43,6 +41,14 @@ fn check_missing_streamer(syntax: &Vec<PipelineElement>) -> Result<(), logos::Sp
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_check_missing_streamer() {
|
||||
let test_data = "| invert | save \"./image_processed.jpg\"";
|
||||
let syntax = parse_syntax(test_data).unwrap();
|
||||
|
||||
assert_eq!(check_missing_streamer(&syntax), Err(0..1))
|
||||
}
|
||||
|
||||
fn check_missing_filters(syntax: &Vec<PipelineElement>) -> Result<(), Vec<logos::Span>> {
|
||||
let mut missing_filter_locs = Vec::new();
|
||||
|
||||
|
@ -69,6 +75,14 @@ fn check_missing_filters(syntax: &Vec<PipelineElement>) -> Result<(), Vec<logos:
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_check_missing_filters() {
|
||||
let test_data = "meow | | test | awa | | nya";
|
||||
let syntax = parse_syntax(test_data).unwrap();
|
||||
|
||||
assert_eq!(check_missing_filters(&syntax), Err(vec![5..8, 20..25]))
|
||||
}
|
||||
|
||||
fn check_missing_sink(syntax: &Vec<PipelineElement>) -> Result<(), logos::Span> {
|
||||
if let Some(&PipelineElement {
|
||||
kind: PipelineElementKind::Pipe,
|
||||
|
@ -80,3 +94,11 @@ fn check_missing_sink(syntax: &Vec<PipelineElement>) -> Result<(), logos::Span>
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_check_missing_sink() {
|
||||
let test_data = "meow | invert | ";
|
||||
let syntax = parse_syntax(test_data).unwrap();
|
||||
|
||||
assert_eq!(check_missing_sink(&syntax), Err(14..15))
|
||||
}
|
||||
|
|
|
@ -6,13 +6,13 @@ pub type FileId = usize;
|
|||
#[derive(Debug, Clone)]
|
||||
pub enum SyntaxError {
|
||||
/// This variant indicates a token that the Lexer didn't recognize
|
||||
InvalidToken(Vec<(FileId, logos::Span)>),
|
||||
InvalidToken(Vec<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)>),
|
||||
MissingStreamer(Vec<logos::Span>),
|
||||
/// `MissingSink` means, that the pipeline ends with a Pipe (`|`), meaning that the output can't go anywhere
|
||||
MissingSink(Vec<(FileId, logos::Span)>),
|
||||
MissingSink(Vec<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)>),
|
||||
MissingFilter(Vec<logos::Span>),
|
||||
/// A literal cannot be a sink, TODO
|
||||
LiteralAsSink,
|
||||
/// A literal can't be a filter either, TODO
|
||||
|
@ -22,14 +22,14 @@ pub enum SyntaxError {
|
|||
}
|
||||
|
||||
impl SyntaxError {
|
||||
pub fn to_diagnostic(&self) -> Diagnostic<usize> {
|
||||
pub fn to_diagnostic(&self, file_id: usize) -> 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")
|
||||
.map(|span| {
|
||||
Label::primary(file_id, span.clone()).with_message("invalid token")
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
|
@ -37,8 +37,8 @@ impl SyntaxError {
|
|||
.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")
|
||||
.map(|span| {
|
||||
Label::primary(file_id, span.clone()).with_message("missing streamer")
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
|
@ -46,8 +46,8 @@ impl SyntaxError {
|
|||
.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")
|
||||
.map(|span| {
|
||||
Label::primary(file_id, span.clone()).with_message("no filter here")
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
|
@ -55,9 +55,7 @@ impl SyntaxError {
|
|||
.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")
|
||||
})
|
||||
.map(|span| Label::primary(file_id, span.clone()).with_message("no sink"))
|
||||
.collect(),
|
||||
),
|
||||
_ => unimplemented!(),
|
||||
|
|
|
@ -5,7 +5,6 @@ use logos::Span;
|
|||
|
||||
use crate::lexer::Token;
|
||||
|
||||
use self::error::FileId;
|
||||
use self::error::SyntaxError;
|
||||
|
||||
pub mod check;
|
||||
|
@ -37,7 +36,7 @@ pub enum CommandPartKind {
|
|||
String(String),
|
||||
}
|
||||
|
||||
pub fn parse_syntax(input: &str, file_id: FileId) -> Result<Vec<PipelineElement>, SyntaxError> {
|
||||
pub fn parse_syntax(input: &str) -> Result<Vec<PipelineElement>, Vec<logos::Span>> {
|
||||
let lexer = Token::lexer(input);
|
||||
let mut errs = Vec::new();
|
||||
|
||||
|
@ -83,7 +82,7 @@ pub fn parse_syntax(input: &str, file_id: FileId) -> Result<Vec<PipelineElement>
|
|||
_ => {}
|
||||
}
|
||||
} else {
|
||||
errs.push((file_id, span))
|
||||
errs.push(span)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -91,7 +90,7 @@ pub fn parse_syntax(input: &str, file_id: FileId) -> Result<Vec<PipelineElement>
|
|||
let span =
|
||||
partial_command.first().unwrap().span.start..partial_command.last().unwrap().span.end;
|
||||
r.push(PipelineElement {
|
||||
kind: PipelineElementKind::Command(mem::replace(&mut partial_command, Vec::new())),
|
||||
kind: PipelineElementKind::Command(std::mem::take(&mut partial_command)),
|
||||
span,
|
||||
});
|
||||
}
|
||||
|
@ -99,6 +98,12 @@ pub fn parse_syntax(input: &str, file_id: FileId) -> Result<Vec<PipelineElement>
|
|||
if errs.is_empty() {
|
||||
Ok(r)
|
||||
} else {
|
||||
Err(SyntaxError::InvalidToken(errs))
|
||||
Err(errs)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_toks() {
|
||||
let test_data = "meow | gay $ error!\\";
|
||||
assert_eq!(parse_syntax(test_data), Err(vec![11..12, 19..20]))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue