summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGreg Brown <gmb60@cam.ac.uk>2021-04-21 10:19:00 +0100
committerGreg Brown <gmb60@cam.ac.uk>2021-04-21 10:19:00 +0100
commit449695dcf87d26b0d06a51ca27bbc8214338f954 (patch)
tree32edf289a1d8287149f160a9b54cecb339be321c
parentbb3c8d1455f7a102a0c0abffd757ccace94f77d5 (diff)
Add some comparison tests for Autochomp.
-rw-r--r--autochomp/Cargo.toml1
-rw-r--r--autochomp/tests/compare/main.rs26
-rw-r--r--autochomp/tests/compare/nibble_exp.nb44
-rw-r--r--autochomp/tests/compare/ratata.nb3
-rw-r--r--autochomp/tests/compare/regex.nb5
-rw-r--r--autochomp/tests/compare/regex_fix.nb4
-rw-r--r--autochomp/tests/compare/sheep.nb3
-rw-r--r--src/chomp/ast/mod.rs18
8 files changed, 99 insertions, 5 deletions
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<Function>, NamedExpression) {
+ syn::parse_str::<nibble::cst::File>(&input).unwrap().convert().unwrap()
+}
+
+fn autonibble(input: &str) -> (Vec<Function>, NamedExpression) {
+ IterWrapper::new(input.chars()).parse::<autochomp::Ast>().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<NamedExpression>,
pub punct: Option<Span>,
pub second: Box<NamedExpression>,
- pub rest: Vec<(Option<Span>, NamedExpression)>
+ pub rest: Vec<(Option<Span>, 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<Span>,
}
+
+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 {}