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(+) 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