From 52615794bfe69a3ad14bff0044ade776028c08cd Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 15 Apr 2019 17:33:32 +0200 Subject: Use upstream abnf crate --- abnf_to_pest/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'abnf_to_pest/src/lib.rs') diff --git a/abnf_to_pest/src/lib.rs b/abnf_to_pest/src/lib.rs index 1b8fc9c..46bd79d 100644 --- a/abnf_to_pest/src/lib.rs +++ b/abnf_to_pest/src/lib.rs @@ -110,8 +110,8 @@ pub fn escape_rulename(x: &str) -> String { } } -fn format_char(x: usize) -> String { - if x <= usize::from(u8::max_value()) { +fn format_char(x: u32) -> String { + if x <= u32::from(u8::max_value()) { let x: u8 = x as u8; if x.is_ascii_graphic() { let x: char = x as char; -- cgit v1.2.3 From 6ef11130281a16e5ca23250ac78bcd27c777a14e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 15 Apr 2019 17:43:39 +0200 Subject: Keep rule order in `abnf_to_pest` Closes #79 --- abnf_to_pest/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'abnf_to_pest/src/lib.rs') diff --git a/abnf_to_pest/src/lib.rs b/abnf_to_pest/src/lib.rs index 46bd79d..f1a167f 100644 --- a/abnf_to_pest/src/lib.rs +++ b/abnf_to_pest/src/lib.rs @@ -8,7 +8,7 @@ pub use abnf::abnf::{ }; use itertools::Itertools; use pretty::{BoxDoc, Doc}; -use std::collections::HashMap; +use indexmap::map::IndexMap; trait Pretty { fn pretty(&self) -> Doc<'static, BoxDoc<'static, ()>>; @@ -146,7 +146,7 @@ impl Pretty for (String, PestyRule) { /// Parse an abnf file. Returns a map of rules. pub fn parse_abnf( data: &[u8], -) -> Result, std::io::Error> { +) -> Result, std::io::Error> { let make_err = |e| std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)); let rules: Vec = -- cgit v1.2.3 From e75edcb22d43789de96ec1427a4668e21b537883 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 15 Apr 2019 17:51:47 +0200 Subject: Document abnf_to_pest somewhat --- abnf_to_pest/src/lib.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'abnf_to_pest/src/lib.rs') diff --git a/abnf_to_pest/src/lib.rs b/abnf_to_pest/src/lib.rs index f1a167f..c828570 100644 --- a/abnf_to_pest/src/lib.rs +++ b/abnf_to_pest/src/lib.rs @@ -1,6 +1,25 @@ #![allow(clippy::implicit_hasher, clippy::or_fun_call)] //! A tiny crate that helps convert ABNF grammars to [pest][pest]. +//! +//! Example usage: +//! ``` +//! let abnf_path = "src/grammar.abnf"; +//! let pest_path = "src/grammar.pest"; +//! +//! let mut file = File::open(abnf_path)?; +//! let mut data = Vec::new(); +//! file.read_to_end(&mut data)?; +//! data.push('\n' as u8); +//! +//! let mut rules = abnf_to_pest::parse_abnf(&data)?; +//! rules.remove("some_inconvenient_rule"); +//! +//! let mut file = File::create(pest_path)?; +//! writeln!(&mut file, "{}", render_rules_to_pest(rules).pretty(80))?; +//! ``` +//! +//! [pest]: https://pest.rs use abnf::abnf::Rule; pub use abnf::abnf::{ @@ -93,6 +112,10 @@ impl Pretty for Range { } } +/// Escape the rule name to be a valid Rust identifier. +/// +/// Replaces e.g. `if` with `if_`, and `rule-name` with `rule_name`. +/// Also changes `whitespace` to `whitespace_` because of https://github.com/pest-parser/pest/pull/374 pub fn escape_rulename(x: &str) -> String { let x = x.replace("-", "_"); if x == "if" -- cgit v1.2.3 From 7488297db17375bd586653820d1d1dc29f87f93a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 15 Apr 2019 17:56:24 +0200 Subject: Prepare for publication --- abnf_to_pest/src/lib.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'abnf_to_pest/src/lib.rs') diff --git a/abnf_to_pest/src/lib.rs b/abnf_to_pest/src/lib.rs index c828570..cff81ef 100644 --- a/abnf_to_pest/src/lib.rs +++ b/abnf_to_pest/src/lib.rs @@ -1,4 +1,5 @@ #![allow(clippy::implicit_hasher, clippy::or_fun_call)] +#![doc(html_root_url = "https://docs.rs/abnf_to_pest/0.1.0")] //! A tiny crate that helps convert ABNF grammars to [pest][pest]. //! -- cgit v1.2.3 From 7f0d5d7b697505d23afa4955b64daf6af52d2e1a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 15 Apr 2019 17:57:16 +0200 Subject: clippy --- abnf_to_pest/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'abnf_to_pest/src/lib.rs') diff --git a/abnf_to_pest/src/lib.rs b/abnf_to_pest/src/lib.rs index cff81ef..a44caf3 100644 --- a/abnf_to_pest/src/lib.rs +++ b/abnf_to_pest/src/lib.rs @@ -1,4 +1,3 @@ -#![allow(clippy::implicit_hasher, clippy::or_fun_call)] #![doc(html_root_url = "https://docs.rs/abnf_to_pest/0.1.0")] //! A tiny crate that helps convert ABNF grammars to [pest][pest]. @@ -60,7 +59,7 @@ impl Pretty for Repetition { self.repeat .as_ref() .map(Repeat::pretty) - .unwrap_or(Doc::nil()), + .unwrap_or_else(Doc::nil), ) } } -- cgit v1.2.3 From 2e6a2955e45e33aed285224eec291500375e5eb5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 15 Apr 2019 18:00:43 +0200 Subject: Seriously, trailing slash? --- abnf_to_pest/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'abnf_to_pest/src/lib.rs') diff --git a/abnf_to_pest/src/lib.rs b/abnf_to_pest/src/lib.rs index a44caf3..625798b 100644 --- a/abnf_to_pest/src/lib.rs +++ b/abnf_to_pest/src/lib.rs @@ -1,4 +1,4 @@ -#![doc(html_root_url = "https://docs.rs/abnf_to_pest/0.1.0")] +#![doc(html_root_url = "https://docs.rs/abnf_to_pest/0.1.1")] //! A tiny crate that helps convert ABNF grammars to [pest][pest]. //! -- cgit v1.2.3