From 449695dcf87d26b0d06a51ca27bbc8214338f954 Mon Sep 17 00:00:00 2001 From: Greg Brown Date: Wed, 21 Apr 2021 10:19:00 +0100 Subject: Add some comparison tests for Autochomp. --- autochomp/Cargo.toml | 1 + autochomp/tests/compare/main.rs | 26 +++++++++++++++++++++ autochomp/tests/compare/nibble_exp.nb | 44 +++++++++++++++++++++++++++++++++++ autochomp/tests/compare/ratata.nb | 3 +++ autochomp/tests/compare/regex.nb | 5 ++++ autochomp/tests/compare/regex_fix.nb | 4 ++++ autochomp/tests/compare/sheep.nb | 3 +++ src/chomp/ast/mod.rs | 18 ++++++++++---- 8 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 autochomp/tests/compare/main.rs create mode 100644 autochomp/tests/compare/nibble_exp.nb create mode 100644 autochomp/tests/compare/ratata.nb create mode 100644 autochomp/tests/compare/regex.nb create mode 100644 autochomp/tests/compare/regex_fix.nb create mode 100644 autochomp/tests/compare/sheep.nb diff --git a/autochomp/Cargo.toml b/autochomp/Cargo.toml index 036641c..5c323bc 100644 --- a/autochomp/Cargo.toml +++ b/autochomp/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [dev-dependencies] criterion = "0.3.3" +proc-macro2 = "1.0.24" syn = "1.0.58" [dependencies] diff --git a/autochomp/tests/compare/main.rs b/autochomp/tests/compare/main.rs new file mode 100644 index 0000000..5149881 --- /dev/null +++ b/autochomp/tests/compare/main.rs @@ -0,0 +1,26 @@ +use chewed::{IterWrapper, Parser}; +use chomp::{chomp::ast::{Function, NamedExpression}, nibble}; + +fn chomp(input: &str) -> (Vec, NamedExpression) { + syn::parse_str::(&input).unwrap().convert().unwrap() +} + +fn autonibble(input: &str) -> (Vec, NamedExpression) { + IterWrapper::new(input.chars()).parse::().unwrap().convert().unwrap() +} + +macro_rules! compare { + ($name:ident, $file:literal) => { + #[test] + fn $name() { + let input = include_str!($file); + assert_eq!(chomp(input), autonibble(input)) + } + }; +} + +compare!(compare_sheep, "sheep.nb"); +compare!(compare_ratata, "ratata.nb"); +compare!(compare_regex, "regex.nb"); +compare!(compare_regex_fix, "regex_fix.nb"); +compare!(compare_nibble, "nibble_exp.nb"); diff --git a/autochomp/tests/compare/nibble_exp.nb b/autochomp/tests/compare/nibble_exp.nb new file mode 100644 index 0000000..6e6d8b5 --- /dev/null +++ b/autochomp/tests/compare/nibble_exp.nb @@ -0,0 +1,44 @@ +let opt(x) = _ | x; +let plus(x) = [plus](x . opt(plus)); +let star(x) = [star](opt(x . star)); + +let Pattern_Whitespace = "\n"|" "; + +let XID_Start = + "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | + "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | + "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | + "y" | "z" | + "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | + "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | + "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | + "Y" | "Z" ; +let XID_Continue = + XID_Start | "_" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; + +let literal_char = XID_Continue; + +let ws = star(Pattern_Whitespace); +let must_ws = plus(Pattern_Whitespace); + +let punctuated(x, p) = [rec](x . opt(p . ws . rec)); +let list(x) = "(" . ws . [rec](x . opt("," . ws . opt(rec))) . ")"; + +let epsilon = "_"; +let ident = XID_Start . star(XID_Continue); +let literal = "\"" . plus(literal_char) . "\""; +let parens(expr) = "(" . ws . expr . ")"; +let fix(expr) = "[" . ws . ident . ws . "]" . ws . parens(expr); + +let term(expr) = + epsilon . ws + | literal . ws + | parens(expr) . ws + | fix(expr) . ws + | ident . ws . opt(list(expr) . ws) + ; + +let cat(expr) = punctuated(term(expr), "."); +let alt(expr) = punctuated(cat(expr), "|"); +let expr = [expr](alt(expr)); +match expr; \ No newline at end of file diff --git a/autochomp/tests/compare/ratata.nb b/autochomp/tests/compare/ratata.nb new file mode 100644 index 0000000..2cf7cd9 --- /dev/null +++ b/autochomp/tests/compare/ratata.nb @@ -0,0 +1,3 @@ +let opt(x) = _ | x; +let plus(x) = [rec](x . opt(rec)); +match plus(("r" | "t") . "a"); diff --git a/autochomp/tests/compare/regex.nb b/autochomp/tests/compare/regex.nb new file mode 100644 index 0000000..ad37f3b --- /dev/null +++ b/autochomp/tests/compare/regex.nb @@ -0,0 +1,5 @@ +let opt(x) = _ | x; +let plus(x) = [plus](x . opt(plus)); +let star(x) = [star](opt(x . star)); + +match plus("a") . star("b"); diff --git a/autochomp/tests/compare/regex_fix.nb b/autochomp/tests/compare/regex_fix.nb new file mode 100644 index 0000000..5b7533c --- /dev/null +++ b/autochomp/tests/compare/regex_fix.nb @@ -0,0 +1,4 @@ +let opt(x) = _ | x; +let ws = [star](opt(" " . star)); + +match [rec]("a" . opt("." . ws . rec)); diff --git a/autochomp/tests/compare/sheep.nb b/autochomp/tests/compare/sheep.nb new file mode 100644 index 0000000..1016fec --- /dev/null +++ b/autochomp/tests/compare/sheep.nb @@ -0,0 +1,3 @@ +let opt(x) = _ | x; +let plus(x) = [rec](x . opt(rec)); +match "ba" . plus("a"); diff --git a/src/chomp/ast/mod.rs b/src/chomp/ast/mod.rs index e4ed2fc..6d547a3 100644 --- a/src/chomp/ast/mod.rs +++ b/src/chomp/ast/mod.rs @@ -50,7 +50,7 @@ pub struct Alt { pub first: Box, pub punct: Option, pub second: Box, - pub rest: Vec<(Option, NamedExpression)> + pub rest: Vec<(Option, NamedExpression)>, } impl Display for Alt { @@ -69,10 +69,10 @@ impl PartialEq for Alt { && self.second == other.second && self.rest.len() == other.rest.len() && self - .rest - .iter() - .zip(other.rest.iter()) - .all(|((_, me), (_, them))| me == them) + .rest + .iter() + .zip(other.rest.iter()) + .all(|((_, me), (_, them))| me == them) } } @@ -323,3 +323,11 @@ pub struct Function { pub expr: NamedExpression, pub span: Option, } + +impl PartialEq for Function { + fn eq(&self, other: &Self) -> bool { + self.name == other.name && self.params == other.params && self.expr == other.expr + } +} + +impl Eq for Function {} -- cgit v1.2.3