From 387959675cd53b3c75ad9b6215b07843f8c8f1d8 Mon Sep 17 00:00:00 2001 From: Greg Brown Date: Tue, 11 May 2021 13:53:56 +0100 Subject: Rename autochomp to autonibble. --- Cargo.toml | 2 +- autochomp/Cargo.toml | 19 -- autochomp/benches/parse/data/12.nb | 1 - autochomp/benches/parse/data/1548.nb | 40 --- autochomp/benches/parse/data/194.nb | 7 - autochomp/benches/parse/data/24.nb | 1 - autochomp/benches/parse/data/3096.nb | 82 ------ autochomp/benches/parse/data/387.nb | 13 - autochomp/benches/parse/data/48.nb | 2 - autochomp/benches/parse/data/774.nb | 21 -- autochomp/benches/parse/data/97.nb | 3 - autochomp/benches/parse/main.rs | 64 ----- autochomp/src/lib.rs | 484 ---------------------------------- autochomp/src/main.rs | 61 ----- autochomp/tests/compare/main.rs | 39 --- autochomp/tests/compare/nibble | 1 - autonibble/Cargo.toml | 19 ++ autonibble/benches/parse/data/12.nb | 1 + autonibble/benches/parse/data/1548.nb | 40 +++ autonibble/benches/parse/data/194.nb | 7 + autonibble/benches/parse/data/24.nb | 1 + autonibble/benches/parse/data/3096.nb | 82 ++++++ autonibble/benches/parse/data/387.nb | 13 + autonibble/benches/parse/data/48.nb | 2 + autonibble/benches/parse/data/774.nb | 21 ++ autonibble/benches/parse/data/97.nb | 3 + autonibble/benches/parse/main.rs | 64 +++++ autonibble/src/lib.rs | 484 ++++++++++++++++++++++++++++++++++ autonibble/src/main.rs | 61 +++++ autonibble/tests/compare/main.rs | 39 +++ autonibble/tests/compare/nibble | 1 + 31 files changed, 839 insertions(+), 839 deletions(-) delete mode 100644 autochomp/Cargo.toml delete mode 100644 autochomp/benches/parse/data/12.nb delete mode 100644 autochomp/benches/parse/data/1548.nb delete mode 100644 autochomp/benches/parse/data/194.nb delete mode 100644 autochomp/benches/parse/data/24.nb delete mode 100644 autochomp/benches/parse/data/3096.nb delete mode 100644 autochomp/benches/parse/data/387.nb delete mode 100644 autochomp/benches/parse/data/48.nb delete mode 100644 autochomp/benches/parse/data/774.nb delete mode 100644 autochomp/benches/parse/data/97.nb delete mode 100644 autochomp/benches/parse/main.rs delete mode 100644 autochomp/src/lib.rs delete mode 100644 autochomp/src/main.rs delete mode 100644 autochomp/tests/compare/main.rs delete mode 120000 autochomp/tests/compare/nibble create mode 100644 autonibble/Cargo.toml create mode 100644 autonibble/benches/parse/data/12.nb create mode 100644 autonibble/benches/parse/data/1548.nb create mode 100644 autonibble/benches/parse/data/194.nb create mode 100644 autonibble/benches/parse/data/24.nb create mode 100644 autonibble/benches/parse/data/3096.nb create mode 100644 autonibble/benches/parse/data/387.nb create mode 100644 autonibble/benches/parse/data/48.nb create mode 100644 autonibble/benches/parse/data/774.nb create mode 100644 autonibble/benches/parse/data/97.nb create mode 100644 autonibble/benches/parse/main.rs create mode 100644 autonibble/src/lib.rs create mode 100644 autonibble/src/main.rs create mode 100644 autonibble/tests/compare/main.rs create mode 120000 autonibble/tests/compare/nibble diff --git a/Cargo.toml b/Cargo.toml index 0d48eb1..5db76d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Greg Brown "] edition = "2018" [workspace] -members = ["autochomp", "chewed", "chomp-bench", "chomp-macro"] +members = ["autonibble", "chewed", "chomp-bench", "chomp-macro"] [dependencies] heck = "0.3.2" diff --git a/autochomp/Cargo.toml b/autochomp/Cargo.toml deleted file mode 100644 index 6250332..0000000 --- a/autochomp/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "autochomp" -version = "0.1.0" -authors = ["Greg Brown "] -edition = "2018" - -[dev-dependencies] -criterion = "0.3.3" -syn = "1.0.58" - -[dependencies] -chewed = {path = "../chewed"} -chomp = {path = "../"} -chomp-macro = {path = "../chomp-macro"} -proc-macro2 = "1.0.24" - -[[bench]] -name = "parse" -harness = false diff --git a/autochomp/benches/parse/data/12.nb b/autochomp/benches/parse/data/12.nb deleted file mode 100644 index eaa4670..0000000 --- a/autochomp/benches/parse/data/12.nb +++ /dev/null @@ -1 +0,0 @@ -match "ab"; diff --git a/autochomp/benches/parse/data/1548.nb b/autochomp/benches/parse/data/1548.nb deleted file mode 100644 index cabdfe9..0000000 --- a/autochomp/benches/parse/data/1548.nb +++ /dev/null @@ -1,40 +0,0 @@ -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 deleted file mode 100644 index 01730c6..0000000 --- a/autochomp/benches/parse/data/194.nb +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index f960c3b..0000000 --- a/autochomp/benches/parse/data/24.nb +++ /dev/null @@ -1 +0,0 @@ -match !(/rec/ "a".rec); diff --git a/autochomp/benches/parse/data/3096.nb b/autochomp/benches/parse/data/3096.nb deleted file mode 100644 index f90100c..0000000 --- a/autochomp/benches/parse/data/3096.nb +++ /dev/null @@ -1,82 +0,0 @@ -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 deleted file mode 100644 index 0f60b89..0000000 --- a/autochomp/benches/parse/data/387.nb +++ /dev/null @@ -1,13 +0,0 @@ -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 deleted file mode 100644 index 7059bb8..0000000 --- a/autochomp/benches/parse/data/48.nb +++ /dev/null @@ -1,2 +0,0 @@ -let bot = !(/rec/ "a" . rec); -match /zero/zero; diff --git a/autochomp/benches/parse/data/774.nb b/autochomp/benches/parse/data/774.nb deleted file mode 100644 index 89c22ca..0000000 --- a/autochomp/benches/parse/data/774.nb +++ /dev/null @@ -1,21 +0,0 @@ -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 deleted file mode 100644 index d0d00f9..0000000 --- a/autochomp/benches/parse/data/97.nb +++ /dev/null @@ -1,3 +0,0 @@ -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 deleted file mode 100644 index 64fd88a..0000000 --- a/autochomp/benches/parse/main.rs +++ /dev/null @@ -1,64 +0,0 @@ -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> { - IterWrapper::new(input.chars()) - .parse::() - .map_err(|e| Box::new(e) as Box) - .and_then(|ast| { - ast.convert(&mut Context::default()) - .map_err(|e| Box::new(e) as Box) - }) -} - -fn parse_chomp(input: &str) -> Result> { - syn::parse_str::(input) - .map_err(|e| Box::new(e) as Box) - .and_then(|stmt| { - stmt.convert(&mut Context::default()) - .map_err(|e| Box::new(e) as Box) - }) -} - -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 deleted file mode 100644 index 2fed0f0..0000000 --- a/autochomp/src/lib.rs +++ /dev/null @@ -1,484 +0,0 @@ -use std::{convert::TryInto, mem}; - -use chomp::{ - chomp::{ - ast::{self, Alt, Call, Cat, Fix, Lambda, Let, Literal, NamedExpression, Variable}, - name::{Content, Name}, - }, - nibble::convert::{Context, Convert, ConvertError}, -}; -use chomp_macro::nibble; -use proc_macro2::Span; - -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}"; - - 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); -} - -impl Convert for Ast { - fn convert(self, context: &mut Context) -> Result { - let mut inner = self; - - while let Ast::Branch1(cat) = inner { - inner = *cat.skip1; - } - - match inner { - Ast::Branch1(_) => unreachable!(), - Ast::Stmt1(stmt) => stmt.convert(context), - } - } -} - -impl Convert for Stmt1 { - fn convert(self, context: &mut Context) -> Result { - 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 = 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 { - 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 { - let args: Vec = 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 { - 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), - rest: rest.collect::>()?, - } - .into(), - span: Span::call_site(), - }) - } else { - Ok(first) - } - } -} - -impl Convert for Labelled1 { - fn convert(self, context: &mut Context) -> Result { - let named = self.cat1.convert(context)?; - 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, - span: named.span, - }) - } -} - -impl Convert for Cat1 { - fn convert(self, context: &mut Context) -> Result { - 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), - rest: rest.collect::>()?, - } - .into(), - span: Span::call_site(), - }) - } else { - Ok(first) - } - } -} - -impl Convert for Call1 { - fn convert(self, context: &mut Context) -> Result { - 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::>()?, - } - .into(), - span: Span::call_site(), - }) - } else { - Ok(first) - } - } -} - -impl Convert for Term1 { - fn convert(self, context: &mut Context) -> Result { - match self { - Term1::Epsilon1(_) => Ok(NamedExpression { - name: None, - expr: ast::Epsilon.into(), - span: Span::call_site(), - }), - Term1::Literal1(literal) => Ok(NamedExpression { - name: None, - expr: literal.contents1.into_iter().collect::().into(), - span: Span::call_site(), - }), - 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 { - let inner = self.term1.convert(context)?; - Ok(NamedExpression { - name: None, - expr: Fix { - inner: Box::new(inner), - } - .into(), - span: Span::call_site(), - }) - } -} - -impl Convert for Variable1 { - fn convert(self, context: &mut Context) -> Result { - let name = Name::new_variable(self); - let index = context - .lookup(&name) - .ok_or_else(|| ConvertError::UndeclaredName(Box::new(name.clone())))?; - - Ok(NamedExpression { - name: Some(name), - expr: Variable { index }.into(), - span: Span::call_site(), - }) - } -} - -impl IntoIterator for Names1 { - type Item = Ident1; - - type IntoIter = Opt3; - - fn into_iter(self) -> Self::IntoIter { - Opt3::List1(Box::new(self)) - } -} - -impl Iterator for Opt3 { - type Item = Ident1; - - fn next(&mut self) -> Option { - 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) - } - } - } -} - -impl Iterator for Opt16 { - type Item = Labelled1; - - fn next(&mut self) -> Option { - 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 Opt14 { - type Item = Call1; - - fn next(&mut self) -> Option { - 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 Iterator for Opt13 { - type Item = Term1; - - fn next(&mut self) -> Option { - 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 Contents1 { - type Item = char; - - type IntoIter = Opt11; - - fn into_iter(self) -> Self::IntoIter { - Opt11::Plus1(Box::new(self)) - } -} - -impl Iterator for Opt11 { - type Item = char; - - fn next(&mut self) -> Option { - 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 for char { - fn from(c: LiteralChar1) -> Self { - match c { - LiteralChar1::Literal1(c) => c.into(), - LiteralChar1::Escape1(e) => e.into(), - } - } -} - -impl From 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 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 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 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 for Content { - fn from(i: Variable1) -> Self { - i.to_string().into() - } -} - -impl From for Content { - fn from(i: Ident2) -> Self { - i.to_string().into() - } -} - -impl From for Content { - fn from(i: Ident1) -> Self { - i.to_string().into() - } -} diff --git a/autochomp/src/main.rs b/autochomp/src/main.rs deleted file mode 100644 index d8a78e1..0000000 --- a/autochomp/src/main.rs +++ /dev/null @@ -1,61 +0,0 @@ -use std::{ - error::Error, - io::{self, Read, Write}, - process::exit, -}; - -use chewed::{IterWrapper, Parser}; -use chomp::{ - chomp::{ - ast::substitute::Reduce, - typed::{ - context::Context, - lower::{Backend, GenerateCode}, - TypeInfer, - }, - visit::Visitable, - }, - lower::RustBackend, - nibble::convert::{self, Convert}, -}; -use proc_macro2::Span; - -fn main() { - let mut input = String::new(); - let res = io::stdin() - .read_to_string(&mut input) - .map_err(|e| Box::new(e) as Box) - .and_then(|_| { - IterWrapper::new(input.chars()) - .parse::() - .map_err(|e| Box::new(e) as Box) - }) - .and_then(|ast| { - ast.convert(&mut convert::Context::default()) - .map_err(|e| Box::new(e) as Box) - }) - .and_then(|expr| { - expr.fold(&mut Reduce) - .map_err(|e| Box::new(e) as Box) - }) - .and_then(|term| { - let mut context = Context::default(); - term.fold(&mut TypeInfer { - context: &mut context, - }) - .map_err(|e| Box::new(e) as Box) - }) - .map(|typed| { - let mut backend = RustBackend::default(); - let id = typed.gen(&mut backend); - backend.emit_code(None, Span::call_site(), id) - }) - .and_then(|code| { - write!(io::stdout(), "{:#}", code).map_err(|e| Box::new(e) as Box) - }); - - if let Err(e) = res { - eprintln!("{}", e); - exit(1) - } -} diff --git a/autochomp/tests/compare/main.rs b/autochomp/tests/compare/main.rs deleted file mode 100644 index 112f642..0000000 --- a/autochomp/tests/compare/main.rs +++ /dev/null @@ -1,39 +0,0 @@ -use chewed::{IterWrapper, Parser}; -use chomp::{ - chomp::ast::NamedExpression, - nibble::{ - self, - convert::{Context, Convert}, - }, -}; - -fn chomp(input: &str) -> NamedExpression { - syn::parse_str::(&input) - .unwrap() - .convert(&mut Context::default()) - .unwrap() -} - -fn autonibble(input: &str) -> NamedExpression { - IterWrapper::new(input.chars()) - .parse::() - .unwrap() - .convert(&mut Context::default()) - .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, "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 deleted file mode 120000 index bfe7e8e..0000000 --- a/autochomp/tests/compare/nibble +++ /dev/null @@ -1 +0,0 @@ -../../../tests/full/nibble \ No newline at end of file diff --git a/autonibble/Cargo.toml b/autonibble/Cargo.toml new file mode 100644 index 0000000..46fc075 --- /dev/null +++ b/autonibble/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "autonibble" +version = "0.1.0" +authors = ["Greg Brown "] +edition = "2018" + +[dev-dependencies] +criterion = "0.3.3" +syn = "1.0.58" + +[dependencies] +chewed = {path = "../chewed"} +chomp = {path = "../"} +chomp-macro = {path = "../chomp-macro"} +proc-macro2 = "1.0.24" + +[[bench]] +name = "parse" +harness = false diff --git a/autonibble/benches/parse/data/12.nb b/autonibble/benches/parse/data/12.nb new file mode 100644 index 0000000..eaa4670 --- /dev/null +++ b/autonibble/benches/parse/data/12.nb @@ -0,0 +1 @@ +match "ab"; diff --git a/autonibble/benches/parse/data/1548.nb b/autonibble/benches/parse/data/1548.nb new file mode 100644 index 0000000..cabdfe9 --- /dev/null +++ b/autonibble/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/autonibble/benches/parse/data/194.nb b/autonibble/benches/parse/data/194.nb new file mode 100644 index 0000000..01730c6 --- /dev/null +++ b/autonibble/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/autonibble/benches/parse/data/24.nb b/autonibble/benches/parse/data/24.nb new file mode 100644 index 0000000..f960c3b --- /dev/null +++ b/autonibble/benches/parse/data/24.nb @@ -0,0 +1 @@ +match !(/rec/ "a".rec); diff --git a/autonibble/benches/parse/data/3096.nb b/autonibble/benches/parse/data/3096.nb new file mode 100644 index 0000000..f90100c --- /dev/null +++ b/autonibble/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/autonibble/benches/parse/data/387.nb b/autonibble/benches/parse/data/387.nb new file mode 100644 index 0000000..0f60b89 --- /dev/null +++ b/autonibble/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/autonibble/benches/parse/data/48.nb b/autonibble/benches/parse/data/48.nb new file mode 100644 index 0000000..7059bb8 --- /dev/null +++ b/autonibble/benches/parse/data/48.nb @@ -0,0 +1,2 @@ +let bot = !(/rec/ "a" . rec); +match /zero/zero; diff --git a/autonibble/benches/parse/data/774.nb b/autonibble/benches/parse/data/774.nb new file mode 100644 index 0000000..89c22ca --- /dev/null +++ b/autonibble/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/autonibble/benches/parse/data/97.nb b/autonibble/benches/parse/data/97.nb new file mode 100644 index 0000000..d0d00f9 --- /dev/null +++ b/autonibble/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/autonibble/benches/parse/main.rs b/autonibble/benches/parse/main.rs new file mode 100644 index 0000000..98ad0f3 --- /dev/null +++ b/autonibble/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_autonibble(input: &str) -> Result> { + IterWrapper::new(input.chars()) + .parse::() + .map_err(|e| Box::new(e) as Box) + .and_then(|ast| { + ast.convert(&mut Context::default()) + .map_err(|e| Box::new(e) as Box) + }) +} + +fn parse_chomp(input: &str) -> Result> { + syn::parse_str::(input) + .map_err(|e| Box::new(e) as Box) + .and_then(|stmt| { + stmt.convert(&mut Context::default()) + .map_err(|e| Box::new(e) as Box) + }) +} + +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_autonibble(i)) + }); + } +} + +criterion_group!(benches, bench_parse); +criterion_main!(benches); diff --git a/autonibble/src/lib.rs b/autonibble/src/lib.rs new file mode 100644 index 0000000..2fed0f0 --- /dev/null +++ b/autonibble/src/lib.rs @@ -0,0 +1,484 @@ +use std::{convert::TryInto, mem}; + +use chomp::{ + chomp::{ + ast::{self, Alt, Call, Cat, Fix, Lambda, Let, Literal, NamedExpression, Variable}, + name::{Content, Name}, + }, + nibble::convert::{Context, Convert, ConvertError}, +}; +use chomp_macro::nibble; +use proc_macro2::Span; + +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}"; + + 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); +} + +impl Convert for Ast { + fn convert(self, context: &mut Context) -> Result { + let mut inner = self; + + while let Ast::Branch1(cat) = inner { + inner = *cat.skip1; + } + + match inner { + Ast::Branch1(_) => unreachable!(), + Ast::Stmt1(stmt) => stmt.convert(context), + } + } +} + +impl Convert for Stmt1 { + fn convert(self, context: &mut Context) -> Result { + 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 = 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 { + 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 { + let args: Vec = 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 { + 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), + rest: rest.collect::>()?, + } + .into(), + span: Span::call_site(), + }) + } else { + Ok(first) + } + } +} + +impl Convert for Labelled1 { + fn convert(self, context: &mut Context) -> Result { + let named = self.cat1.convert(context)?; + 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, + span: named.span, + }) + } +} + +impl Convert for Cat1 { + fn convert(self, context: &mut Context) -> Result { + 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), + rest: rest.collect::>()?, + } + .into(), + span: Span::call_site(), + }) + } else { + Ok(first) + } + } +} + +impl Convert for Call1 { + fn convert(self, context: &mut Context) -> Result { + 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::>()?, + } + .into(), + span: Span::call_site(), + }) + } else { + Ok(first) + } + } +} + +impl Convert for Term1 { + fn convert(self, context: &mut Context) -> Result { + match self { + Term1::Epsilon1(_) => Ok(NamedExpression { + name: None, + expr: ast::Epsilon.into(), + span: Span::call_site(), + }), + Term1::Literal1(literal) => Ok(NamedExpression { + name: None, + expr: literal.contents1.into_iter().collect::().into(), + span: Span::call_site(), + }), + 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 { + let inner = self.term1.convert(context)?; + Ok(NamedExpression { + name: None, + expr: Fix { + inner: Box::new(inner), + } + .into(), + span: Span::call_site(), + }) + } +} + +impl Convert for Variable1 { + fn convert(self, context: &mut Context) -> Result { + let name = Name::new_variable(self); + let index = context + .lookup(&name) + .ok_or_else(|| ConvertError::UndeclaredName(Box::new(name.clone())))?; + + Ok(NamedExpression { + name: Some(name), + expr: Variable { index }.into(), + span: Span::call_site(), + }) + } +} + +impl IntoIterator for Names1 { + type Item = Ident1; + + type IntoIter = Opt3; + + fn into_iter(self) -> Self::IntoIter { + Opt3::List1(Box::new(self)) + } +} + +impl Iterator for Opt3 { + type Item = Ident1; + + fn next(&mut self) -> Option { + 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) + } + } + } +} + +impl Iterator for Opt16 { + type Item = Labelled1; + + fn next(&mut self) -> Option { + 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 Opt14 { + type Item = Call1; + + fn next(&mut self) -> Option { + 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 Iterator for Opt13 { + type Item = Term1; + + fn next(&mut self) -> Option { + 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 Contents1 { + type Item = char; + + type IntoIter = Opt11; + + fn into_iter(self) -> Self::IntoIter { + Opt11::Plus1(Box::new(self)) + } +} + +impl Iterator for Opt11 { + type Item = char; + + fn next(&mut self) -> Option { + 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 for char { + fn from(c: LiteralChar1) -> Self { + match c { + LiteralChar1::Literal1(c) => c.into(), + LiteralChar1::Escape1(e) => e.into(), + } + } +} + +impl From 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 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 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 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 for Content { + fn from(i: Variable1) -> Self { + i.to_string().into() + } +} + +impl From for Content { + fn from(i: Ident2) -> Self { + i.to_string().into() + } +} + +impl From for Content { + fn from(i: Ident1) -> Self { + i.to_string().into() + } +} diff --git a/autonibble/src/main.rs b/autonibble/src/main.rs new file mode 100644 index 0000000..56c757d --- /dev/null +++ b/autonibble/src/main.rs @@ -0,0 +1,61 @@ +use std::{ + error::Error, + io::{self, Read, Write}, + process::exit, +}; + +use chewed::{IterWrapper, Parser}; +use chomp::{ + chomp::{ + ast::substitute::Reduce, + typed::{ + context::Context, + lower::{Backend, GenerateCode}, + TypeInfer, + }, + visit::Visitable, + }, + lower::RustBackend, + nibble::convert::{self, Convert}, +}; +use proc_macro2::Span; + +fn main() { + let mut input = String::new(); + let res = io::stdin() + .read_to_string(&mut input) + .map_err(|e| Box::new(e) as Box) + .and_then(|_| { + IterWrapper::new(input.chars()) + .parse::() + .map_err(|e| Box::new(e) as Box) + }) + .and_then(|ast| { + ast.convert(&mut convert::Context::default()) + .map_err(|e| Box::new(e) as Box) + }) + .and_then(|expr| { + expr.fold(&mut Reduce) + .map_err(|e| Box::new(e) as Box) + }) + .and_then(|term| { + let mut context = Context::default(); + term.fold(&mut TypeInfer { + context: &mut context, + }) + .map_err(|e| Box::new(e) as Box) + }) + .map(|typed| { + let mut backend = RustBackend::default(); + let id = typed.gen(&mut backend); + backend.emit_code(None, Span::call_site(), id) + }) + .and_then(|code| { + write!(io::stdout(), "{:#}", code).map_err(|e| Box::new(e) as Box) + }); + + if let Err(e) = res { + eprintln!("{}", e); + exit(1) + } +} diff --git a/autonibble/tests/compare/main.rs b/autonibble/tests/compare/main.rs new file mode 100644 index 0000000..014cfec --- /dev/null +++ b/autonibble/tests/compare/main.rs @@ -0,0 +1,39 @@ +use chewed::{IterWrapper, Parser}; +use chomp::{ + chomp::ast::NamedExpression, + nibble::{ + self, + convert::{Context, Convert}, + }, +}; + +fn chomp(input: &str) -> NamedExpression { + syn::parse_str::(&input) + .unwrap() + .convert(&mut Context::default()) + .unwrap() +} + +fn autonibble(input: &str) -> NamedExpression { + IterWrapper::new(input.chars()) + .parse::() + .unwrap() + .convert(&mut Context::default()) + .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, "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/autonibble/tests/compare/nibble b/autonibble/tests/compare/nibble new file mode 120000 index 0000000..bfe7e8e --- /dev/null +++ b/autonibble/tests/compare/nibble @@ -0,0 +1 @@ +../../../tests/full/nibble \ No newline at end of file -- cgit v1.2.3