diff options
-rw-r--r-- | Cargo.lock | 9 | ||||
-rw-r--r-- | abnf_to_pest/Cargo.toml | 2 | ||||
-rw-r--r-- | abnf_to_pest/src/lib.rs | 59 |
3 files changed, 34 insertions, 36 deletions
@@ -1031,10 +1031,11 @@ checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" [[package]] name = "pretty" -version = "0.5.2" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f60c0d9f6fc88ecdd245d90c1920ff76a430ab34303fc778d33b1d0a4c3bf6d3" +checksum = "ad9940b913ee56ddd94aec2d3cd179dd47068236f42a1a6415ccf9d880ce2a61" dependencies = [ + "arrayvec", "typed-arena", ] @@ -1616,9 +1617,9 @@ checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" [[package]] name = "typed-arena" -version = "1.7.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9b2228007eba4120145f785df0f6c92ea538f5a3635a612ecf4e334c8c1446d" +checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae" [[package]] name = "typenum" diff --git a/abnf_to_pest/Cargo.toml b/abnf_to_pest/Cargo.toml index 6e4cfe7..6a4abc4 100644 --- a/abnf_to_pest/Cargo.toml +++ b/abnf_to_pest/Cargo.toml @@ -16,4 +16,4 @@ doctest = false abnf = "0.6.0" indexmap = "1.0.2" itertools = "0.9.0" -pretty = "0.5.2" +pretty = "0.10.0" diff --git a/abnf_to_pest/src/lib.rs b/abnf_to_pest/src/lib.rs index a579493..64672cc 100644 --- a/abnf_to_pest/src/lib.rs +++ b/abnf_to_pest/src/lib.rs @@ -24,35 +24,35 @@ use abnf::types::{Node, NumVal, Repeat, Rule}; use indexmap::map::IndexMap; use itertools::Itertools; -use pretty::{BoxDoc, Doc}; +use pretty::BoxDoc; trait Pretty { - fn pretty(&self) -> Doc<'static, BoxDoc<'static, ()>>; + fn pretty(&self) -> BoxDoc<'static>; } impl Pretty for Node { - fn pretty(&self) -> Doc<'static, BoxDoc<'static, ()>> { + fn pretty(&self) -> BoxDoc<'static> { use Node::*; match self { - Alternation(nodes) => Doc::intersperse( + Alternation(nodes) => BoxDoc::intersperse( nodes.iter().map(|x| x.pretty().nest(2).group()), - Doc::space().append(Doc::text("| ")), + BoxDoc::space().append(BoxDoc::text("| ")), ), - Concatenation(nodes) => Doc::intersperse( + Concatenation(nodes) => BoxDoc::intersperse( nodes.iter().map(|x| x.pretty()), - Doc::space().append(Doc::text("~ ")), + BoxDoc::space().append(BoxDoc::text("~ ")), ), Repetition(rep) => { rep.get_node().pretty().append(rep.get_repeat().pretty()) } - Rulename(s) => Doc::text(escape_rulename(s)), - Group(n) => Doc::text("(") + Rulename(s) => BoxDoc::text(escape_rulename(s)), + Group(n) => BoxDoc::text("(") .append(n.pretty().nest(4).group()) - .append(Doc::text(")")), - Optional(n) => Doc::text("(") + .append(BoxDoc::text(")")), + Optional(n) => BoxDoc::text("(") .append(n.pretty().nest(4).group()) - .append(Doc::text(")?")), - CharVal(s) => Doc::text(format!( + .append(BoxDoc::text(")?")), + CharVal(s) => BoxDoc::text(format!( "^\"{}\"", s.replace("\"", "\\\"").replace("\\", "\\\\") )), @@ -63,8 +63,8 @@ impl Pretty for Node { } impl Pretty for Repeat { - fn pretty(&self) -> Doc<'static, BoxDoc<'static, ()>> { - Doc::text(match (self.get_min().unwrap_or(0), self.get_max()) { + fn pretty(&self) -> BoxDoc<'static> { + BoxDoc::text(match (self.get_min().unwrap_or(0), self.get_max()) { (0, None) => "*".into(), (1, None) => "+".into(), (0, Some(1)) => "?".into(), @@ -76,9 +76,9 @@ impl Pretty for Repeat { } impl Pretty for NumVal { - fn pretty(&self) -> Doc<'static, BoxDoc<'static, ()>> { + fn pretty(&self) -> BoxDoc<'static> { use NumVal::*; - Doc::text(match self { + BoxDoc::text(match self { Range(x, y) => { format!("'{}'..'{}'", format_char(*x), format_char(*y)) } @@ -135,16 +135,16 @@ pub struct PestyRule { } impl Pretty for (String, PestyRule) { - fn pretty(&self) -> Doc<'static, BoxDoc<'static, ()>> { + fn pretty(&self) -> BoxDoc<'static> { let (name, rule) = self; - Doc::nil() - .append(Doc::text(name.clone())) - .append(Doc::text(" = ")) - .append(Doc::text(if rule.silent { "_" } else { "" })) - .append(Doc::text("{")) - .append(Doc::space().append(rule.node.pretty()).nest(2)) - .append(Doc::space()) - .append(Doc::text("}")) + BoxDoc::nil() + .append(BoxDoc::text(name.clone())) + .append(BoxDoc::text(" = ")) + .append(BoxDoc::text(if rule.silent { "_" } else { "" })) + .append(BoxDoc::text("{")) + .append(BoxDoc::space().append(rule.node.pretty()).nest(2)) + .append(BoxDoc::space()) + .append(BoxDoc::text("}")) .group() } } @@ -170,13 +170,10 @@ pub fn parse_abnf( .collect()) } -pub fn render_rules_to_pest<I>( - rules: I, -) -> Doc<'static, BoxDoc<'static, ()>, ()> +pub fn render_rules_to_pest<I>(rules: I) -> BoxDoc<'static> where I: IntoIterator<Item = (String, PestyRule)>, { let pretty_rules = rules.into_iter().map(|x| x.pretty()); - let doc: Doc<_> = Doc::intersperse(pretty_rules, Doc::newline()); - doc + BoxDoc::intersperse(pretty_rules, BoxDoc::hardline()) } |