summaryrefslogtreecommitdiff
path: root/autochomp
diff options
context:
space:
mode:
Diffstat (limited to 'autochomp')
-rw-r--r--autochomp/Cargo.toml2
-rw-r--r--autochomp/benches/parse.rs173
-rw-r--r--autochomp/benches/parse/data/12.nb1
-rw-r--r--autochomp/benches/parse/data/1548.nb40
-rw-r--r--autochomp/benches/parse/data/194.nb7
-rw-r--r--autochomp/benches/parse/data/24.nb1
-rw-r--r--autochomp/benches/parse/data/3096.nb82
-rw-r--r--autochomp/benches/parse/data/387.nb13
-rw-r--r--autochomp/benches/parse/data/48.nb2
-rw-r--r--autochomp/benches/parse/data/774.nb21
-rw-r--r--autochomp/benches/parse/data/97.nb3
-rw-r--r--autochomp/benches/parse/main.rs64
-rw-r--r--autochomp/src/lib.rs703
-rw-r--r--autochomp/src/main.rs18
-rw-r--r--autochomp/tests/compare/main.rs39
l---------autochomp/tests/compare/nibble1
-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
21 files changed, 562 insertions, 667 deletions
diff --git a/autochomp/Cargo.toml b/autochomp/Cargo.toml
index 5c323bc..6250332 100644
--- a/autochomp/Cargo.toml
+++ b/autochomp/Cargo.toml
@@ -6,13 +6,13 @@ edition = "2018"
[dev-dependencies]
criterion = "0.3.3"
-proc-macro2 = "1.0.24"
syn = "1.0.58"
[dependencies]
chewed = {path = "../chewed"}
chomp = {path = "../"}
chomp-macro = {path = "../chomp-macro"}
+proc-macro2 = "1.0.24"
[[bench]]
name = "parse"
diff --git a/autochomp/benches/parse.rs b/autochomp/benches/parse.rs
deleted file mode 100644
index a42543a..0000000
--- a/autochomp/benches/parse.rs
+++ /dev/null
@@ -1,173 +0,0 @@
-use std::error::Error;
-
-use chewed::{IterWrapper, Parser};
-use chomp::{
- chomp::ast::{Function, NamedExpression},
- nibble,
-};
-use criterion::{
- criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration,
- Throughput,
-};
-
-const INPUTS: &[&str] = &[
- r##"match _ : None | "a" : Some;"##,
- r##"let opt(x) = _ : None | "a" : Some;
- match [plus](("a" : First) . (opt(plus) : Next));"##,
- r##"let opt(x) = _ : None | x : Some;
- let plus(x) = [plus]((x : First) . (opt(plus) : Next));
- let star(x) = opt(plus(x));
- match [rec]("a" : Base | "b" . rec : Step);"##,
- r##"let opt(x) = _ : None | x : Some;
- let plus(x) = [plus]((x : First) . (opt(plus) : Next));
- let star(x) = opt(plus(x));
- let star_(base, step) = [rec](base : Base | step . rec : Step);
- let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}";
- let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" ;
- let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
- match "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";"##,
- r##"let opt(x) = _ : None | x : Some;
- let plus(x) = [plus]((x : First) . (opt(plus) : Next));
- let star(x) = opt(plus(x));
- let star_(base, step) = [rec](base : Base | step . rec : Step);
- let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}";
- let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" ;
- let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
- let hex_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
- "a" | "b" | "c" | "d" | "e" | "f" |
- "A" | "B" | "C" | "D" | "E" | "F" ;
- 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" ;
- match "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" ;"##,
- r##"let opt(x) = _ : None | x : Some;
- let plus(x) = [plus]((x : First) . (opt(plus) : Next));
- let star(x) = opt(plus(x));
- let star_(base, step) = [rec](base : Base | step . rec : Step);
- let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}";
- let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" ;
- let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
- let hex_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
- "a" | "b" | "c" | "d" | "e" | "f" |
- "A" | "B" | "C" | "D" | "E" | "F" ;
- 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 =
- (" " | "!" | "#" | "$" | "%" | "&" | "'" |
- "(" | ")" | "*" | "+" | "," | "-" | "." | "/" |
- "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
- "8" | "9" | ":" | ";" | "<" | "=" | ">" | "?" |
- "@" | "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" );
- match "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" ;"##,
- r##"let opt(x) = _ : None | x : Some;
- let plus(x) = [plus]((x : First) . (opt(plus) : Next));
- let star(x) = opt(plus(x));
- let star_(base, step) = [rec](base : Base | step . rec : Step);
- let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}";
- let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" ;
- let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
- let hex_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
- "a" | "b" | "c" | "d" | "e" | "f" |
- "A" | "B" | "C" | "D" | "E" | "F" ;
- 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 =
- (" " | "!" | "#" | "$" | "%" | "&" | "'" |
- "(" | ")" | "*" | "+" | "," | "-" | "." | "/" |
- "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
- "8" | "9" | ":" | ";" | "<" | "=" | ">" | "?" |
- "@" | "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" | "{" | "|" | "}" | "~") : Literal |
- "\\" . (
- ("\"" | "'" | "n" | "r" | "t" | "\\" | "0") : Ascii |
- "x" . oct_digit . hex_digit : Oct |
- "u{" .hex_digit
- .opt(hex_digit
- .opt(hex_digit
- .opt(hex_digit
- .opt(hex_digit . opt(hex_digit))))) . "}" : Unicode
- ) : Escape ;
- let ws = plus(Pattern_Whitespace);
- let punctuated(x, p) = [rec]((x : First) . (opt(p . opt(ws) . rec) : Next));
- let list(x) = "(" . opt(ws) . [rec]((x : First) . (opt("," . opt(ws) . opt(rec)) : Next)) . ")";
- let epsilon = "_";
- let ident = XID_Start . star(XID_Continue);
- let literal = "\"" . (plus(literal_char) : Contents) . "\"";
- let parens(expr) = "(" . opt(ws) . (expr : Inner) . ")";
- let fix(expr) = "[" . opt(ws) . (ident : Arg) . opt(ws) . "]" . opt(ws) . (parens(expr) : Inner);
- let term(expr) =
- epsilon . opt(ws) : Epsilon
- | literal . opt(ws) : Literal
- | parens(expr) . opt(ws) : Parens
- | fix(expr) . opt(ws) : Fix
- | ident . opt(ws) . opt(list(expr) . opt(ws)) : CallOrVariable
- ;
- let label = ":" . opt(ws) . (ident : Label) . opt(ws);
- let cat(expr) = punctuated(term(expr), ".");
- let alt(expr) = punctuated((cat(expr) : Cat) . (opt(label) : Name), "|");
- let expr = [expr](alt(expr));
- let let = "let" . ws . (ident : Name) . opt(ws) . (opt(list(ident . opt(ws)) . opt(ws)) : Args) . "=" . opt(ws) . (expr : Expr) . ";" . opt(ws);
- let goal = "match" . ws . (expr : Expr) . ";" . opt(ws);
- match star_(star_(goal : Goal, let : Let), Pattern_Whitespace);"##,
-];
-
-fn parse_autochomp(input: &str) -> Result<(Vec<Function>, NamedExpression), Box<dyn Error>> {
- IterWrapper::new(input.chars())
- .parse::<autochomp::Ast>()
- .map_err(|e| Box::new(e) as Box<dyn Error>)
- .and_then(|ast| ast.convert().map_err(|e| Box::new(e) as Box<dyn Error>))
-}
-
-fn parse_chomp(input: &str) -> Result<(Vec<Function>, NamedExpression), Box<dyn Error>> {
- syn::parse_str::<nibble::File>(input)
- .map_err(|e| Box::new(e) as Box<dyn Error>)
- .and_then(|file| file.convert().map_err(|e| Box::new(e) as Box<dyn Error>))
-}
-
-fn bench_parse(c: &mut Criterion) {
- let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
- let mut group = c.benchmark_group("Parse");
- group.plot_config(plot_config);
- for (i, input) in INPUTS.iter().enumerate() {
- group.throughput(Throughput::Bytes(input.len() as u64));
- group.bench_with_input(BenchmarkId::new("Chomp", i), *input, |b, i| {
- b.iter(|| parse_chomp(i))
- });
- group.bench_with_input(BenchmarkId::new("AutoChomp", i), *input, |b, i| {
- b.iter(|| parse_autochomp(i))
- });
- }
-}
-
-criterion_group!(benches, bench_parse);
-criterion_main!(benches);
diff --git a/autochomp/benches/parse/data/12.nb b/autochomp/benches/parse/data/12.nb
new file mode 100644
index 0000000..eaa4670
--- /dev/null
+++ b/autochomp/benches/parse/data/12.nb
@@ -0,0 +1 @@
+match "ab";
diff --git a/autochomp/benches/parse/data/1548.nb b/autochomp/benches/parse/data/1548.nb
new file mode 100644
index 0000000..cabdfe9
--- /dev/null
+++ b/autochomp/benches/parse/data/1548.nb
@@ -0,0 +1,40 @@
+let bot = !(/rec/ "a" . rec);
+let zero = /zero suc/ zero;
+let suc n = /zero suc/ suc (n zero suc);
+
+let opt some = _ : None | some;
+let plus iter = !(/plus/ iter . (opt plus));
+let star iter = opt (plus iter);
+
+let up_to x n = n bot (/rec/ x . opt rec);
+
+let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}";
+
+let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" ;
+let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
+let hex_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
+ "a" | "b" | "c" | "d" | "e" | "f" |
+ "A" | "B" | "C" | "D" | "E" | "F" ;
+
+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" ;
+
+match
+ (" " | "!" | "#" | "$" | "%" | "&" | "'" |
+ "(" | ")" | "*" | "+" | "," | "-" | "." | "/" |
+ "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
+ "8" | "9" | ":" | ";" | "<" | "=" | ">" | "?" |
+ "@" | "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"
+ );
diff --git a/autochomp/benches/parse/data/194.nb b/autochomp/benches/parse/data/194.nb
new file mode 100644
index 0000000..01730c6
--- /dev/null
+++ b/autochomp/benches/parse/data/194.nb
@@ -0,0 +1,7 @@
+let bot = !(/rec/ "a" . rec);
+let zero = /zero suc/ zero;
+let suc n = /zero suc/ suc (n zero suc);
+
+let opt some = _ : None | some;
+let plus iter = !(/plus/ iter . (opt plus));
+match opt "ab";
diff --git a/autochomp/benches/parse/data/24.nb b/autochomp/benches/parse/data/24.nb
new file mode 100644
index 0000000..f960c3b
--- /dev/null
+++ b/autochomp/benches/parse/data/24.nb
@@ -0,0 +1 @@
+match !(/rec/ "a".rec);
diff --git a/autochomp/benches/parse/data/3096.nb b/autochomp/benches/parse/data/3096.nb
new file mode 100644
index 0000000..f90100c
--- /dev/null
+++ b/autochomp/benches/parse/data/3096.nb
@@ -0,0 +1,82 @@
+let bot = !(/rec/ "a" . rec);
+let zero = /zero suc/ zero;
+let suc n = /zero suc/ suc (n zero suc);
+
+let opt some = _ : None | some;
+let plus iter = !(/plus/ iter . (opt plus));
+let star iter = opt (plus iter);
+
+let up_to x n = n bot (/rec/ x . opt rec);
+
+let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}";
+
+let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" ;
+let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
+let hex_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
+ "a" | "b" | "c" | "d" | "e" | "f" |
+ "A" | "B" | "C" | "D" | "E" | "F" ;
+
+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 =
+ (" " | "!" | "#" | "$" | "%" | "&" | "'" |
+ "(" | ")" | "*" | "+" | "," | "-" | "." | "/" |
+ "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
+ "8" | "9" | ":" | ";" | "<" | "=" | ">" | "?" |
+ "@" | "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" | "{" | "|" | "}" | "~") : Literal |
+ "\\" . (
+ ("\"" | "'" | "n" | "r" | "t" | "\\" | "0") : Ascii |
+ "x" . oct_digit . hex_digit : Oct |
+ "u{" . up_to hex_digit (suc (suc (suc (suc (suc (suc zero)))))) . "}" : Unicode
+ ) : Escape ;
+
+let ws = plus Pattern_Whitespace;
+let ows = opt ws;
+
+let list inner = !(/list/ inner . opt (ws . opt list));
+let separated inner sep = !(/separated/ inner . opt (sep . ows . separated));
+
+let epsilon = "_";
+let ident = XID_Start . star XID_Continue;
+let literal = "\"" . (plus literal_char : Contents) . "\"";
+let fix term = "!" . ows . term;
+let parens expr = "(" . ows . expr . ")";
+
+let names = list ident;
+
+let term expr = !(/term/
+ epsilon : Epsilon
+ | literal : Literal
+ | parens expr : Parens
+ | fix term : Fix
+ | ident : Variable
+ );
+
+let label = ":" . ows . ident . ows;
+
+let call expr = list (term expr);
+let cat expr = separated (call expr) ".";
+let alt expr = separated (cat expr . opt label : Labelled) "|";
+let lambda expr = "/" . ows . names . "/" . ows . alt expr;
+let expr = !(/expr/ alt expr | lambda expr);
+let goal = "match" . ws . expr . ";" . ows;
+let let stmt = "let" . ws . names . "=" . ows . expr . ";" . ows . stmt;
+let stmt = !(/stmt/ let stmt | goal);
+match !(/skip/ Pattern_Whitespace . skip | stmt);
diff --git a/autochomp/benches/parse/data/387.nb b/autochomp/benches/parse/data/387.nb
new file mode 100644
index 0000000..0f60b89
--- /dev/null
+++ b/autochomp/benches/parse/data/387.nb
@@ -0,0 +1,13 @@
+let bot = !(/rec/ "a" . rec);
+let zero = /zero suc/ zero;
+let suc n = /zero suc/ suc (n zero suc);
+
+let opt some = _ : None | some;
+let plus iter = !(/plus/ iter . (opt plus));
+let star iter = opt (plus iter);
+
+let up_to x n = n bot (/rec/ x . opt rec);
+
+let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}";
+
+match = "0" |"1";
diff --git a/autochomp/benches/parse/data/48.nb b/autochomp/benches/parse/data/48.nb
new file mode 100644
index 0000000..7059bb8
--- /dev/null
+++ b/autochomp/benches/parse/data/48.nb
@@ -0,0 +1,2 @@
+let bot = !(/rec/ "a" . rec);
+match /zero/zero;
diff --git a/autochomp/benches/parse/data/774.nb b/autochomp/benches/parse/data/774.nb
new file mode 100644
index 0000000..89c22ca
--- /dev/null
+++ b/autochomp/benches/parse/data/774.nb
@@ -0,0 +1,21 @@
+let bot = !(/rec/ "a" . rec);
+let zero = /zero suc/ zero;
+let suc n = /zero suc/ suc (n zero suc);
+
+let opt some = _ : None | some;
+let plus iter = !(/plus/ iter . (opt plus));
+let star iter = opt (plus iter);
+
+let up_to x n = n bot (/rec/ x . opt rec);
+
+let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}";
+
+let oct_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" ;
+let digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
+let hex_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
+ "a" | "b" | "c" | "d" | "e" | "f" |
+ "A" | "B" | "C" | "D" | "E" | "F" ;
+
+match
+ "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" |
+ "i" | "j" | "k" | "l";
diff --git a/autochomp/benches/parse/data/97.nb b/autochomp/benches/parse/data/97.nb
new file mode 100644
index 0000000..d0d00f9
--- /dev/null
+++ b/autochomp/benches/parse/data/97.nb
@@ -0,0 +1,3 @@
+let bot = !(/rec/ ("a" . rec));
+let zero = /zero suc/ zero;
+match (/zero suc/ suc (_ zero suc));
diff --git a/autochomp/benches/parse/main.rs b/autochomp/benches/parse/main.rs
new file mode 100644
index 0000000..64fd88a
--- /dev/null
+++ b/autochomp/benches/parse/main.rs
@@ -0,0 +1,64 @@
+use std::error::Error;
+
+use chewed::{IterWrapper, Parser};
+use chomp::{
+ chomp::ast::NamedExpression,
+ nibble::{
+ self,
+ convert::{Context, Convert},
+ },
+};
+use criterion::{
+ criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration,
+ Throughput,
+};
+
+const INPUTS: &[&str] = &[
+ include_str!("data/12.nb"),
+ include_str!("data/24.nb"),
+ include_str!("data/48.nb"),
+ include_str!("data/97.nb"),
+ include_str!("data/194.nb"),
+ include_str!("data/387.nb"),
+ include_str!("data/774.nb"),
+ include_str!("data/1548.nb"),
+ include_str!("data/3096.nb"),
+
+];
+
+fn parse_autochomp(input: &str) -> Result<NamedExpression, Box<dyn Error>> {
+ IterWrapper::new(input.chars())
+ .parse::<autochomp::Ast>()
+ .map_err(|e| Box::new(e) as Box<dyn Error>)
+ .and_then(|ast| {
+ ast.convert(&mut Context::default())
+ .map_err(|e| Box::new(e) as Box<dyn Error>)
+ })
+}
+
+fn parse_chomp(input: &str) -> Result<NamedExpression, Box<dyn Error>> {
+ syn::parse_str::<nibble::Statement>(input)
+ .map_err(|e| Box::new(e) as Box<dyn Error>)
+ .and_then(|stmt| {
+ stmt.convert(&mut Context::default())
+ .map_err(|e| Box::new(e) as Box<dyn Error>)
+ })
+}
+
+fn bench_parse(c: &mut Criterion) {
+ let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
+ let mut group = c.benchmark_group("Parse");
+ group.plot_config(plot_config);
+ for (i, input) in INPUTS.iter().enumerate() {
+ group.throughput(Throughput::Bytes(input.len() as u64));
+ group.bench_with_input(BenchmarkId::new("Chomp", i), *input, |b, i| {
+ b.iter(|| parse_chomp(i))
+ });
+ group.bench_with_input(BenchmarkId::new("AutoChomp", i), *input, |b, i| {
+ b.iter(|| parse_autochomp(i))
+ });
+ }
+}
+
+criterion_group!(benches, bench_parse);
+criterion_main!(benches);
diff --git a/autochomp/src/lib.rs b/autochomp/src/lib.rs
index c795954..2fed0f0 100644
--- a/autochomp/src/lib.rs
+++ b/autochomp/src/lib.rs
@@ -1,18 +1,25 @@
-use std::convert::TryInto;
+use std::{convert::TryInto, mem};
use chomp::{
chomp::{
- ast::{self, Alt, Call, Cat, Fix, Function, NamedExpression, Parameter, Variable},
- Name,
+ ast::{self, Alt, Call, Cat, Fix, Lambda, Let, Literal, NamedExpression, Variable},
+ name::{Content, Name},
},
- nibble::convert::{Binding, Context, Convert, ConvertError},
+ nibble::convert::{Context, Convert, ConvertError},
};
+use chomp_macro::nibble;
+use proc_macro2::Span;
-chomp_macro::nibble! {
- let opt(x) = _ : None | x : Some;
- let plus(x) = [plus]((x : First) . (opt(plus) : Next));
- let star(x) = opt(plus(x));
- let star_(base, step) = [rec](base : Base | step . rec : Step);
+nibble! {
+ let bot = !(/rec/ "a" . rec);
+ let zero = /zero suc/ zero;
+ let suc n = /zero suc/ suc (n zero suc);
+
+ let opt some = _ : None | some;
+ let plus iter = !(/plus/ iter . (opt plus));
+ let star iter = opt (plus iter);
+
+ let up_to x n = n bot (/rec/ x . opt rec);
let Pattern_Whitespace = "\t"|"\n"|"\x0B"|"\x0c"|"\r"|" "|"\u{85}"|"\u{200e}"|"\u{200f}"|"\u{2028}"|"\u{2029}";
@@ -25,9 +32,6 @@ chomp_macro::nibble! {
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" |
@@ -52,96 +56,146 @@ chomp_macro::nibble! {
"x" | "y" | "z" | "{" | "|" | "}" | "~") : Literal |
"\\" . (
("\"" | "'" | "n" | "r" | "t" | "\\" | "0") : Ascii |
- "x" . oct_digit . hex_digit : Oct |
- "u{" .hex_digit
- .opt(hex_digit
- .opt(hex_digit
- .opt(hex_digit
- .opt(hex_digit . opt(hex_digit))))) . "}" : Unicode
+ "x" . oct_digit . hex_digit : Oct |
+ "u{" . up_to hex_digit (suc (suc (suc (suc (suc (suc zero)))))) . "}" : Unicode
) : Escape ;
- let ws = plus(Pattern_Whitespace);
+ let ws = plus Pattern_Whitespace;
+ let ows = opt ws;
- let punctuated(x, p) = [rec]((x : First) . (opt(p . opt(ws) . rec) : Next));
- let list(x) = "(" . opt(ws) . [rec]((x : First) . (opt("," . opt(ws) . opt(rec)) : Next)) . ")";
+ let list inner = !(/list/ inner . opt (ws . opt list));
+ let separated inner sep = !(/separated/ inner . opt (sep . ows . separated));
let epsilon = "_";
- let ident = XID_Start . star(XID_Continue);
- let literal = "\"" . (plus(literal_char) : Contents) . "\"";
- let parens(expr) = "(" . opt(ws) . (expr : Inner) . ")";
- let fix(expr) = "[" . opt(ws) . (ident : Arg) . opt(ws) . "]" . opt(ws) . (parens(expr) : Inner);
-
- let term(expr) =
- epsilon . opt(ws) : Epsilon
- | literal . opt(ws) : Literal
- | parens(expr) . opt(ws) : Parens
- | fix(expr) . opt(ws) : Fix
- | ident . opt(ws) . opt(list(expr) . opt(ws)) : CallOrVariable
- ;
-
- let label = ":" . opt(ws) . (ident : Label) . opt(ws);
- let cat(expr) = punctuated(term(expr), ".");
- let alt(expr) = punctuated((cat(expr) : Cat) . (opt(label) : Name), "|");
- let expr = [expr](alt(expr));
- let let = "let" . ws . (ident : Name) . opt(ws) . (opt(list(ident . opt(ws)) . opt(ws)) : Args) . "=" . opt(ws) . (expr : Expr) . ";" . opt(ws);
- let goal = "match" . ws . (expr : Expr) . ";" . opt(ws);
-
- match star_(star_(goal : Goal, let : Let), Pattern_Whitespace);
-}
+ let ident = XID_Start . star XID_Continue;
+ let literal = "\"" . (plus literal_char : Contents) . "\"";
+ let fix term = "!" . ows . term;
+ let parens expr = "(" . ows . expr . ")";
+
+ let names = list ident;
+
+ let term expr = !(/term/
+ epsilon : Epsilon
+ | literal : Literal
+ | parens expr : Parens
+ | fix term : Fix
+ | ident : Variable
+ );
+
+ let label = ":" . ows . ident . ows;
+
+ let call expr = list (term expr);
+ let cat expr = separated (call expr) ".";
+ let alt expr = separated (cat expr . opt label : Labelled) "|";
+ let lambda expr = "/" . ows . names . "/" . ows . alt expr;
+ let expr = !(/expr/ alt expr | lambda expr);
+ let goal = "match" . ws . expr . ";" . ows;
+ let let stmt = "let" . ws . names . "=" . ows . expr . ";" . ows . stmt;
+ let stmt = !(/stmt/ let stmt | goal);
+ match !(/skip/ Pattern_Whitespace . skip | stmt);
+}
+
+impl Convert for Ast {
+ fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
+ let mut inner = self;
-impl Ast {
- pub fn convert(self) -> Result<(Vec<Function>, NamedExpression), ConvertError> {
- let content = Star2::from(self.0);
- let mut names = Vec::new();
- let mut map = Vec::new();
-
- let mut iter = content.into_iter();
-
- for stmt in &mut iter {
- let name: Name = stmt.name1.into();
- let params = Option::from(stmt.args1)
- .into_iter()
- .flat_map(List2::into_iter)
- .map(Name::from);
- let mut context = Context::new(&names, params.clone());
- let mut expr = stmt.expr1.convert(&mut context)?;
- names.push(name.clone());
- expr.name = Some(name.clone());
- map.push(Function {
- name,
- params: params.map(Some).collect(),
- expr,
- span: None,
- });
+ while let Ast::Branch1(cat) = inner {
+ inner = *cat.skip1;
}
- let mut context = Context::new(&names, Vec::new());
- let goal = iter.consume().expr1.convert(&mut context)?;
+ match inner {
+ Ast::Branch1(_) => unreachable!(),
+ Ast::Stmt1(stmt) => stmt.convert(context),
+ }
+ }
+}
- Ok((map, goal))
+impl Convert for Stmt1 {
+ fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
+ match self {
+ Stmt1::Goal1(goal) => goal.expr1.convert(context),
+ Stmt1::Let1(stmt) => {
+ let mut names = stmt.names1.into_iter().peekable();
+ let name = Name::new_let(names.next().unwrap());
+ let bound = if names.peek().is_none() {
+ stmt.expr1.convert(context)?
+ } else {
+ let args: Vec<Name> = names.map(Name::new_variable).collect();
+ let expr = stmt.expr1;
+ let inner = context.with_variables(args.clone(), |ctx| expr.convert(ctx))?;
+ NamedExpression {
+ name: None,
+ expr: Lambda {
+ args,
+ inner: Box::new(inner),
+ }
+ .into(),
+ span: Span::call_site(),
+ }
+ };
+ context.push_variable(name.clone());
+ let body = stmt.stmt1.convert(context)?;
+ Ok(NamedExpression {
+ name: None,
+ expr: Let {
+ name: name.clone(),
+ bound: Box::new(NamedExpression {
+ name: Some(name),
+ ..bound
+ }),
+ body: Box::new(body),
+ }
+ .into(),
+ span: Span::call_site(),
+ })
+ }
+ }
}
}
impl Convert for Expr1 {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
- let mut iter = self.0.into_iter();
- let first = iter.next().unwrap().convert(context)?;
- let rest = iter
- .map(|term| Ok((None, term.convert(context)?)))
- .collect::<Result<Vec<_>, _>>()?;
-
- let mut iter = rest.into_iter();
- if let Some((punct, second)) = iter.next() {
+ match self {
+ Expr1::Alt1(alt) => alt.convert(context),
+ Expr1::Lambda1(lambda) => lambda.convert(context),
+ }
+ }
+}
+
+impl Convert for Lambda1 {
+ fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
+ let args: Vec<Name> = self.names1.into_iter().map(Name::new_variable).collect();
+ let alt = self.alt1;
+ let inner = context.with_variables(args.clone(), |ctx| alt.convert(ctx))?;
+ Ok(NamedExpression {
+ name: None,
+ expr: Lambda {
+ args,
+ inner: Box::new(inner),
+ }
+ .into(),
+ span: Span::call_site(),
+ })
+ }
+}
+
+impl Convert for Alt1 {
+ fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
+ let first = self.labelled1.convert(context)?;
+ let mut rest = self
+ .opt1
+ .into_iter()
+ .map(|inner| inner.convert(context).map(|e| (Span::call_site(), e)))
+ .peekable();
+ if rest.peek().is_some() {
Ok(NamedExpression {
name: None,
expr: Alt {
first: Box::new(first),
- punct,
- second: Box::new(second),
- rest: iter.collect(),
+ rest: rest.collect::<Result<_, _>>()?,
}
.into(),
- span: None,
+ span: Span::call_site(),
})
} else {
Ok(first)
@@ -149,11 +203,14 @@ impl Convert for Expr1 {
}
}
-impl Convert for First1 {
+impl Convert for Labelled1 {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
let named = self.cat1.convert(context)?;
- let name = Option::from(self.name1).or(named.name);
-
+ let label = match self.opt1 {
+ Opt15::None1(_) => None,
+ Opt15::Label1(l) => Some(Name::new_label(l.ident1)),
+ };
+ let name = Name::merge(label, named.name);
Ok(NamedExpression {
name,
expr: named.expr,
@@ -164,24 +221,45 @@ impl Convert for First1 {
impl Convert for Cat1 {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
- let mut iter = self.into_iter();
- let first = iter.next().unwrap().convert(context)?;
- let rest = iter
- .map(|term| Ok((None, term.convert(context)?)))
- .collect::<Result<Vec<_>, _>>()?;
-
- let mut iter = rest.into_iter();
- if let Some((punct, second)) = iter.next() {
+ let first = self.call1.convert(context)?;
+ let mut rest = self
+ .opt1
+ .into_iter()
+ .map(|inner| inner.convert(context).map(|e| (Span::call_site(), e)))
+ .peekable();
+ if rest.peek().is_some() {
Ok(NamedExpression {
name: None,
expr: Cat {
first: Box::new(first),
- punct,
- second: Box::new(second),
- rest: iter.collect(),
+ rest: rest.collect::<Result<_, _>>()?,
+ }
+ .into(),
+ span: Span::call_site(),
+ })
+ } else {
+ Ok(first)
+ }
+ }
+}
+
+impl Convert for Call1 {
+ fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
+ let first = self.term1.convert(context)?;
+ let mut rest = self
+ .opt1
+ .into_iter()
+ .map(|inner| inner.convert(context))
+ .peekable();
+ if rest.peek().is_some() {
+ Ok(NamedExpression {
+ name: None,
+ expr: Call {
+ on: Box::new(first),
+ args: rest.collect::<Result<_, _>>()?,
}
.into(),
- span: None,
+ span: Span::call_site(),
})
} else {
Ok(first)
@@ -192,416 +270,215 @@ impl Convert for Cat1 {
impl Convert for Term1 {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
match self {
- Self::Epsilon1(_) => Ok(NamedExpression {
+ Term1::Epsilon1(_) => Ok(NamedExpression {
name: None,
expr: ast::Epsilon.into(),
- span: None,
+ span: Span::call_site(),
}),
- Self::Literal1(l) => Ok(NamedExpression {
+ Term1::Literal1(literal) => Ok(NamedExpression {
name: None,
- expr: l.value().into(),
- span: None,
+ expr: literal.contents1.into_iter().collect::<Literal>().into(),
+ span: Span::call_site(),
}),
- Self::Parens1(p) => p.parens1.expr1.convert(context),
- Self::Fix1(f) => f.fix1.convert(context),
- Self::CallOrVariable1(c) => c.convert(context),
+ Term1::Parens1(parens) => parens.expr1.convert(context),
+ Term1::Fix1(fix) => fix.convert(context),
+ Term1::Variable1(var) => var.convert(context),
}
}
}
impl Convert for Fix1 {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
- let arg = self.arg1.into();
- let expr = *self.inner1.expr1;
- let inner = context.with_variable(&arg, |context| expr.convert(context))?;
-
+ let inner = self.term1.convert(context)?;
Ok(NamedExpression {
name: None,
expr: Fix {
- arg: Some(arg),
inner: Box::new(inner),
}
.into(),
- span: None,
+ span: Span::call_site(),
})
}
}
-impl Convert for CallOrVariable1 {
+impl Convert for Variable1 {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
- let name = self.ident1.into();
-
- match self.opt2 {
- Opt20::None1(_) => {
- let binding = context
- .lookup(&name)
- .ok_or_else(|| ConvertError::UndeclaredName(Box::new(name.clone())))?;
-
- Ok(match binding {
- Binding::Variable(index) => NamedExpression {
- name: Some(name),
- expr: Variable { index }.into(),
- span: None,
- },
- Binding::Parameter(index) => NamedExpression {
- name: Some(name),
- expr: Parameter { index }.into(),
- span: None,
- },
- Binding::Global => NamedExpression {
- name: None,
- expr: Call {
- name,
- args: Vec::new(),
- }
- .into(),
- span: None,
- },
- })
- }
- Opt20::Some1(s) => {
- let args = s
- .list1
- .into_iter()
- .map(|arg| arg.convert(context))
- .collect::<Result<_, _>>()?;
- Ok(NamedExpression {
- name: None,
- expr: Call { name, args }.into(),
- span: None,
- })
- }
- }
- }
-}
-
-impl Literal3 {
- pub fn value(self) -> String {
- self.literal1
- .contents1
- .into_iter()
- .map(LiteralChar1::value)
- .collect()
- }
-}
-
-impl LiteralChar1 {
- pub fn value(self) -> char {
- match self {
- Self::Literal1(c) => c.into(),
- Self::Escape1(e) => e.1.value(),
- }
- }
-}
-
-impl Alt143 {
- pub fn value(self) -> char {
- match self {
- Self::Ascii1(a) => a.value(),
- Self::Oct1(o) => o.value(),
- Self::Unicode1(u) => u.value(),
- }
- }
-}
-
-impl Ascii1 {
- pub fn value(self) -> char {
- match self {
- Self::Branch1(_) => '\"',
- Self::Branch2(_) => '\'',
- Self::Branch3(_) => '\n',
- Self::Branch4(_) => '\r',
- Self::Branch5(_) => '\t',
- Self::Branch6(_) => '\\',
- Self::Branch7(_) => '\0',
- }
- }
-}
-
-impl Oct1 {
- pub fn value(self) -> char {
- let s: String = [char::from(self.oct_digit1), char::from(self.hex_digit1)]
- .iter()
- .collect();
- u32::from_str_radix(&s, 16).unwrap().try_into().unwrap()
- }
-}
-
-impl Unicode1 {
- pub fn value(self) -> char {
- let s: String = [self.hex_digit1.to_string(), self.opt1.to_string()]
- .iter()
- .map::<&str, _>(|s| s)
- .collect();
- u32::from_str_radix(&s, 16).unwrap().try_into().unwrap()
- }
-}
-
-impl IntoIterator for Cat1 {
- type Item = Term1;
+ let name = Name::new_variable(self);
+ let index = context
+ .lookup(&name)
+ .ok_or_else(|| ConvertError::UndeclaredName(Box::new(name.clone())))?;
- type IntoIter = Cat1Iter;
-
- fn into_iter(self) -> Self::IntoIter {
- Cat1Iter(Some(self))
- }
-}
-
-pub struct Cat1Iter(Option<Cat1>);
-
-impl Iterator for Cat1Iter {
- type Item = Term1;
-
- fn next(&mut self) -> Option<Self::Item> {
- let cat = self.0.take()?.0;
- let term = cat.term1;
- self.0 = cat.next1.into();
- Some(term)
+ Ok(NamedExpression {
+ name: Some(name),
+ expr: Variable { index }.into(),
+ span: Span::call_site(),
+ })
}
}
-impl IntoIterator for Contents1 {
- type Item = LiteralChar1;
+impl IntoIterator for Names1 {
+ type Item = Ident1;
- type IntoIter = Contents1Iter;
+ type IntoIter = Opt3;
fn into_iter(self) -> Self::IntoIter {
- Contents1Iter(Some(self))
+ Opt3::List1(Box::new(self))
}
}
-pub struct Contents1Iter(Option<Contents1>);
-
-impl Iterator for Contents1Iter {
- type Item = LiteralChar1;
+impl Iterator for Opt3 {
+ type Item = Ident1;
fn next(&mut self) -> Option<Self::Item> {
- let cat = self.0.take()?.0;
- let lit = cat.literal_char1;
- self.0 = cat.next1.into();
- Some(lit)
- }
-}
-
-impl IntoIterator for List1 {
- type Item = Expr1;
-
- type IntoIter = Fix192Iter;
-
- fn into_iter(self) -> Self::IntoIter {
- Fix192Iter(Some(self.part3))
+ let orig = mem::replace(self, Opt3::None1(Epsilon));
+ match orig {
+ Opt3::None1(_) => None,
+ Opt3::List1(names) => {
+ if let Opt4::Some1(some) = names.opt1 {
+ *self = some.opt1;
+ }
+ Some(names.ident1)
+ }
+ }
}
}
-pub struct Fix192Iter(Option<Fix192>);
-
-impl Iterator for Fix192Iter {
- type Item = Expr1;
+impl Iterator for Opt16 {
+ type Item = Labelled1;
fn next(&mut self) -> Option<Self::Item> {
- let cat = self.0.take()?.0;
- let expr = *cat.expr1;
- self.0 = cat.next1.into();
- Some(expr)
- }
-}
-
-impl IntoIterator for Star2 {
- type Item = Let1;
-
- type IntoIter = Star2Iter;
-
- fn into_iter(self) -> Self::IntoIter {
- Star2Iter(Some(self))
- }
-}
-
-pub struct Star2Iter(Option<Star2>);
-
-impl Star2Iter {
- pub fn consume(self) -> Goal1 {
- let mut star = self.0.unwrap();
-
- loop {
- match star.0 {
- Alt274::Step1(step) => star = *step.rec1,
- Alt274::Goal1(goal) => return goal,
+ let orig = mem::replace(self, Opt16::None1(Epsilon));
+ match orig {
+ Opt16::None1(_) => None,
+ Opt16::Some1(some) => {
+ *self = some.separated1.opt1;
+ Some(some.separated1.labelled1)
}
}
}
}
-impl Iterator for Star2Iter {
- type Item = Let1;
+impl Iterator for Opt14 {
+ type Item = Call1;
fn next(&mut self) -> Option<Self::Item> {
- let star = self.0.take().unwrap();
-
- // You can probably be safer about this and use `mem::swap` or similar.
- // I cannot think of a way how, so this will do.
- if let Alt274::Step1(step) = star.0 {
- let stmt = step.let1;
- self.0 = Some(*step.rec1);
- Some(stmt)
- } else {
- self.0 = Some(star);
- None
+ let orig = mem::replace(self, Opt14::None1(Epsilon));
+ match orig {
+ Opt14::None1(_) => None,
+ Opt14::Some1(some) => {
+ *self = some.separated1.opt1;
+ Some(some.separated1.call1)
+ }
}
}
}
-impl IntoIterator for Alt1 {
- type Item = First1;
-
- type IntoIter = Alt1Iter;
-
- fn into_iter(self) -> Self::IntoIter {
- Alt1Iter(Some(self))
- }
-}
-
-#[derive(Clone)]
-pub struct Alt1Iter(Option<Alt1>);
-
-impl Iterator for Alt1Iter {
- type Item = First1;
+impl Iterator for Opt13 {
+ type Item = Term1;
fn next(&mut self) -> Option<Self::Item> {
- let cat = self.0.take()?.0;
- let first = cat.first1;
- self.0 = cat.next1.into();
- Some(first)
+ let orig = mem::replace(self, Opt13::None1(Epsilon));
+ match orig {
+ Opt13::None1(_) => None,
+ Opt13::Some1(some) => match some.opt1 {
+ Opt12::None1(_) => None,
+ Opt12::List1(call) => {
+ *self = call.opt1;
+ Some(call.term1)
+ }
+ },
+ }
}
}
-impl IntoIterator for List2 {
- type Item = Ident2;
+impl IntoIterator for Contents1 {
+ type Item = char;
- type IntoIter = Fix246Iter;
+ type IntoIter = Opt11;
fn into_iter(self) -> Self::IntoIter {
- Fix246Iter(Some(self.part3))
+ Opt11::Plus1(Box::new(self))
}
}
-#[derive(Clone)]
-pub struct Fix246Iter(Option<Fix246>);
-
-impl Iterator for Fix246Iter {
- type Item = Ident2;
+impl Iterator for Opt11 {
+ type Item = char;
fn next(&mut self) -> Option<Self::Item> {
- let cat = self.0.take()?.0;
- let expr = cat.first1.ident1;
- self.0 = cat.next1.into();
- Some(expr)
- }
-}
-
-impl From<Next6> for Option<Alt1> {
- fn from(o: Next6) -> Self {
- match o {
- Next6::None1(_) => None,
- Next6::Some1(s) => Some(*s.rec1),
- }
- }
-}
-
-impl From<Args1> for Option<List2> {
- fn from(o: Args1) -> Self {
- match o {
- Args1::None1(_) => None,
- Args1::Some1(s) => Some(s.list1),
+ let orig = mem::replace(self, Opt11::None1(Epsilon));
+ match orig {
+ Opt11::None1(_) => None,
+ Opt11::Plus1(contents) => {
+ *self = contents.opt1;
+ Some(contents.literal_char1.into())
+ }
}
}
}
-impl From<Next2> for Option<Contents1> {
- fn from(o: Next2) -> Self {
- match o {
- Next2::None1(_) => None,
- Next2::Plus1(s) => Some(*s),
+impl From<LiteralChar1> for char {
+ fn from(c: LiteralChar1) -> Self {
+ match c {
+ LiteralChar1::Literal1(c) => c.into(),
+ LiteralChar1::Escape1(e) => e.into(),
}
}
}
-impl From<Next4> for Option<Fix192> {
- fn from(o: Next4) -> Self {
- match o {
- Next4::None1(_) => None,
- Next4::Some1(s) => match s.opt2 {
- Opt18::None1(_) => None,
- Opt18::Rec1(e) => Some(*e),
- },
+impl From<Escape1> for char {
+ fn from(e: Escape1) -> Self {
+ match e.1 {
+ Alt171::Ascii1(a) => a.escape(),
+ Alt171::Oct1(o) => o.into(),
+ Alt171::Unicode1(u) => u.into(),
}
}
}
-impl From<Next5> for Option<Cat1> {
- fn from(o: Next5) -> Self {
- match o {
- Next5::None1(_) => None,
- Next5::Some1(s) => Some(*s.rec1)
+impl Ascii1 {
+ fn escape(self) -> char {
+ match self {
+ Ascii1::Branch1(_) => '\"',
+ Ascii1::Branch2(_) => '\'',
+ Ascii1::Branch3(_) => '\n',
+ Ascii1::Branch4(_) => '\r',
+ Ascii1::Branch5(_) => '\t',
+ Ascii1::Branch6(_) => '\\',
+ Ascii1::Branch7(_) => '\0',
}
}
}
-impl From<Next7> for Option<Fix246> {
- fn from(o: Next7) -> Self {
- match o {
- Next7::None1(_) => None,
- Next7::Some1(s) => match s.opt2 {
- Opt30::None1(_) => None,
- Opt30::Rec1(e) => Some(*e),
- },
- }
+impl From<Oct1> for char {
+ fn from(o: Oct1) -> Self {
+ let s: String = [char::from(o.oct_digit1), char::from(o.hex_digit1)]
+ .iter()
+ .collect();
+ u32::from_str_radix(&s, 16).unwrap().try_into().unwrap()
}
}
-impl From<Name1> for Option<Name> {
- fn from(o: Name1) -> Self {
- match o {
- Name1::None1(_) => None,
- Name1::Label1(l) => Some(l.label1.to_string().into()),
- }
+impl From<Unicode1> for char {
+ fn from(u: Unicode1) -> Self {
+ let s = u.up_to1.to_string();
+ u32::from_str_radix(&s, 16).unwrap().try_into().unwrap()
}
}
-impl From<Name2> for Name {
- fn from(i: Name2) -> Self {
+impl From<Variable1> for Content {
+ fn from(i: Variable1) -> Self {
i.to_string().into()
}
}
-impl From<Arg1> for Name {
- fn from(i: Arg1) -> Self {
+impl From<Ident2> for Content {
+ fn from(i: Ident2) -> Self {
i.to_string().into()
}
}
-impl From<Ident1> for Name {
+impl From<Ident1> for Content {
fn from(i: Ident1) -> Self {
i.to_string().into()
}
}
-
-impl From<Ident2> for Name {
- fn from(i: Ident2) -> Self {
- i.to_string().into()
- }
-}
-
-
-impl From<Alt277> for Star2 {
- fn from(mut a: Alt277) -> Self {
- while let Alt277::Step1(step) = a {
- a = (*step.rec1).0;
- }
-
- if let Alt277::Star1(s) = a {
- s
- } else {
- unreachable!()
- }
- }
-}
diff --git a/autochomp/src/main.rs b/autochomp/src/main.rs
index cbc0646..d8a78e1 100644
--- a/autochomp/src/main.rs
+++ b/autochomp/src/main.rs
@@ -7,7 +7,7 @@ use std::{
use chewed::{IterWrapper, Parser};
use chomp::{
chomp::{
- ast::substitute::InlineCalls,
+ ast::substitute::Reduce,
typed::{
context::Context,
lower::{Backend, GenerateCode},
@@ -16,7 +16,9 @@ use chomp::{
visit::Visitable,
},
lower::RustBackend,
+ nibble::convert::{self, Convert},
};
+use proc_macro2::Span;
fn main() {
let mut input = String::new();
@@ -28,12 +30,12 @@ fn main() {
.parse::<autochomp::Ast>()
.map_err(|e| Box::new(e) as Box<dyn Error>)
})
- .and_then(|ast| ast.convert().map_err(|e| Box::new(e) as Box<dyn Error>))
- .and_then(|(funs, goal)| {
- funs.into_iter()
- .try_rfold(goal, |goal, function| {
- goal.fold(&mut InlineCalls { function })
- })
+ .and_then(|ast| {
+ ast.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| {
@@ -46,7 +48,7 @@ fn main() {
.map(|typed| {
let mut backend = RustBackend::default();
let id = typed.gen(&mut backend);
- backend.emit_code(None, None, id)
+ backend.emit_code(None, Span::call_site(), id)
})
.and_then(|code| {
write!(io::stdout(), "{:#}", code).map_err(|e| Box::new(e) as Box<dyn Error>)
diff --git a/autochomp/tests/compare/main.rs b/autochomp/tests/compare/main.rs
index 681014a..112f642 100644
--- a/autochomp/tests/compare/main.rs
+++ b/autochomp/tests/compare/main.rs
@@ -1,26 +1,39 @@
use chewed::{IterWrapper, Parser};
-use chomp::{chomp::ast::{Function, NamedExpression}, nibble};
+use chomp::{
+ chomp::ast::NamedExpression,
+ nibble::{
+ self,
+ convert::{Context, Convert},
+ },
+};
-fn chomp(input: &str) -> (Vec<Function>, NamedExpression) {
- syn::parse_str::<nibble::File>(&input).unwrap().convert().unwrap()
+fn chomp(input: &str) -> NamedExpression {
+ syn::parse_str::<nibble::Statement>(&input)
+ .unwrap()
+ .convert(&mut Context::default())
+ .unwrap()
}
-fn autonibble(input: &str) -> (Vec<Function>, NamedExpression) {
- IterWrapper::new(input.chars()).parse::<autochomp::Ast>().unwrap().convert().unwrap()
+fn autonibble(input: &str) -> NamedExpression {
+ IterWrapper::new(input.chars())
+ .parse::<autochomp::Ast>()
+ .unwrap()
+ .convert(&mut Context::default())
+ .unwrap()
}
macro_rules! compare {
- ($name:ident, $file:literal) => {
- #[test]
+ ($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");
+compare!(compare_sheep, "nibble/sheep.nb");
+compare!(compare_ratata, "nibble/ratata.nb");
+compare!(compare_regex, "nibble/regex.nb");
+compare!(compare_regex_fix, "nibble/regex_fix.nb");
+compare!(compare_nibble, "nibble/nibble_exp.nb");
diff --git a/autochomp/tests/compare/nibble b/autochomp/tests/compare/nibble
new file mode 120000
index 0000000..bfe7e8e
--- /dev/null
+++ b/autochomp/tests/compare/nibble
@@ -0,0 +1 @@
+../../../tests/full/nibble \ No newline at end of file
diff --git a/autochomp/tests/compare/nibble_exp.nb b/autochomp/tests/compare/nibble_exp.nb
deleted file mode 100644
index 6e6d8b5..0000000
--- a/autochomp/tests/compare/nibble_exp.nb
+++ /dev/null
@@ -1,44 +0,0 @@
-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
deleted file mode 100644
index 2cf7cd9..0000000
--- a/autochomp/tests/compare/ratata.nb
+++ /dev/null
@@ -1,3 +0,0 @@
-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
deleted file mode 100644
index ad37f3b..0000000
--- a/autochomp/tests/compare/regex.nb
+++ /dev/null
@@ -1,5 +0,0 @@
-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
deleted file mode 100644
index 5b7533c..0000000
--- a/autochomp/tests/compare/regex_fix.nb
+++ /dev/null
@@ -1,4 +0,0 @@
-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
deleted file mode 100644
index 1016fec..0000000
--- a/autochomp/tests/compare/sheep.nb
+++ /dev/null
@@ -1,3 +0,0 @@
-let opt(x) = _ | x;
-let plus(x) = [rec](x . opt(rec));
-match "ba" . plus("a");