diff options
author | Nadrieril | 2020-10-30 20:41:42 +0000 |
---|---|---|
committer | Nadrieril | 2020-10-30 20:41:42 +0000 |
commit | 90aea87a068625c09783c67ad5e879f3ee4deb2a (patch) | |
tree | c02ef1783c6a032b85dd05bc02541106d37c2542 /abnf_to_pest | |
parent | 2fe834f3acf3acc8a0225ec80ed8619a6cbc8e85 (diff) |
Update pretty dependency
Diffstat (limited to 'abnf_to_pest')
-rw-r--r-- | abnf_to_pest/Cargo.toml | 2 | ||||
-rw-r--r-- | abnf_to_pest/src/lib.rs | 59 |
2 files changed, 29 insertions, 32 deletions
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()) } |