summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/full/main.rs61
-rw-r--r--tests/full/nibble/nibble_exp.nb42
-rw-r--r--tests/full/nibble/ratata.nb3
-rw-r--r--tests/full/nibble/regex.nb5
-rw-r--r--tests/full/nibble/regex_fix.nb4
-rw-r--r--tests/full/nibble/sheep.nb3
6 files changed, 118 insertions, 0 deletions
diff --git a/tests/full/main.rs b/tests/full/main.rs
new file mode 100644
index 0000000..95a1f60
--- /dev/null
+++ b/tests/full/main.rs
@@ -0,0 +1,61 @@
+use std::error::Error;
+
+use chomp::{
+ chomp::{
+ ast::substitute::Reduce,
+ typed::{
+ context::Context,
+ lower::{Backend, GenerateCode},
+ TypeInfer,
+ },
+ visit::Visitable,
+ },
+ lower::RustBackend,
+ nibble::{
+ convert::{self, Convert},
+ Statement,
+ },
+};
+use proc_macro2::{Span, TokenStream};
+
+fn chomp(input: &str) -> Result<TokenStream, Box<dyn Error>> {
+ syn::parse_str::<Statement>(&input)
+ .map_err(|e| Box::new(e) as Box<dyn Error>)
+ .and_then(|nibble: Statement| {
+ nibble
+ .convert(&mut convert::Context::default())
+ .map_err(|e| Box::new(e) as Box<dyn Error>)
+ })
+ .and_then(|expr| {
+ expr.fold(&mut Reduce)
+ .map_err(|e| Box::new(e) as Box<dyn Error>)
+ })
+ .and_then(|term| {
+ let mut context = Context::default();
+ term.fold(&mut TypeInfer {
+ context: &mut context,
+ })
+ .map_err(|e| Box::new(e) as Box<dyn Error>)
+ })
+ .map(|typed| {
+ let mut backend = RustBackend::default();
+ let id = typed.gen(&mut backend);
+ backend.emit_code(None, Span::call_site(), id)
+ })
+}
+
+macro_rules! compile {
+ ($name:ident, $file:literal) => {
+ #[test]
+ fn $name() {
+ let input = include_str!($file);
+ chomp(input).unwrap();
+ }
+ };
+}
+
+compile!(compile_sheep, "nibble/sheep.nb");
+compile!(compile_ratata, "nibble/ratata.nb");
+compile!(compile_regex, "nibble/regex.nb");
+compile!(compile_regex_fix, "nibble/regex_fix.nb");
+compile!(compile_nibble, "nibble/nibble_exp.nb");
diff --git a/tests/full/nibble/nibble_exp.nb b/tests/full/nibble/nibble_exp.nb
new file mode 100644
index 0000000..67f2be1
--- /dev/null
+++ b/tests/full/nibble/nibble_exp.nb
@@ -0,0 +1,42 @@
+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 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) "|";
+match !alt; \ No newline at end of file
diff --git a/tests/full/nibble/ratata.nb b/tests/full/nibble/ratata.nb
new file mode 100644
index 0000000..feeccca
--- /dev/null
+++ b/tests/full/nibble/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/tests/full/nibble/regex.nb b/tests/full/nibble/regex.nb
new file mode 100644
index 0000000..4aa98a7
--- /dev/null
+++ b/tests/full/nibble/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/tests/full/nibble/regex_fix.nb b/tests/full/nibble/regex_fix.nb
new file mode 100644
index 0000000..54daf61
--- /dev/null
+++ b/tests/full/nibble/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/tests/full/nibble/sheep.nb b/tests/full/nibble/sheep.nb
new file mode 100644
index 0000000..922ae84
--- /dev/null
+++ b/tests/full/nibble/sheep.nb
@@ -0,0 +1,3 @@
+let opt x = _ | x;
+let plus x = !(/rec/ x . opt rec);
+match "ba" . plus "a";