diff options
Diffstat (limited to '')
-rw-r--r-- | abnf_to_pest/Cargo.toml | 10 | ||||
-rw-r--r-- | abnf_to_pest/src/lib.rs (renamed from build.rs) | 48 |
2 files changed, 17 insertions, 41 deletions
diff --git a/abnf_to_pest/Cargo.toml b/abnf_to_pest/Cargo.toml new file mode 100644 index 0000000..bfa6b6d --- /dev/null +++ b/abnf_to_pest/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "abnf_to_pest" +version = "0.1.0" +authors = ["Nadrieril <nadrieril@users.noreply.github.com>"] +edition = "2018" + +[dependencies] +abnf = { git = "https://github.com/Nadrieril/abnf" } +itertools = "0.8.0" +pretty = "0.5.2" diff --git a/build.rs b/abnf_to_pest/src/lib.rs index a418c67..cb7ba99 100644 --- a/build.rs +++ b/abnf_to_pest/src/lib.rs @@ -1,20 +1,18 @@ -use std::fs::File; -use std::io::{Read,Write,BufReader,BufRead}; use std::collections::HashMap; use itertools::Itertools; -use lalrpop; - -struct PestRuleSettings { - visible: bool, - replace: Option<String>, +pub struct PestRuleSettings { + pub visible: bool, + pub replace: Option<String>, } impl Default for PestRuleSettings { - fn default() -> Self { PestRuleSettings { visible: true, replace: None } } + fn default() -> Self { + PestRuleSettings { visible: true, replace: None } + } } -fn abnf_to_pest(data: &Vec<u8>, rule_settings: &HashMap<String, PestRuleSettings>) -> std::io::Result<String> { +pub fn abnf_to_pest(data: &Vec<u8>, rule_settings: &HashMap<String, PestRuleSettings>) -> std::io::Result<String> { use abnf::abnf::*; use pretty::{Doc, BoxDoc}; fn format_rule(x: Rule, rule_settings: &HashMap<String, PestRuleSettings>) -> Doc<BoxDoc<()>> { @@ -114,35 +112,3 @@ fn abnf_to_pest(data: &Vec<u8>, rule_settings: &HashMap<String, PestRuleSettings Ok(format!("{}", doc.pretty(80))) } -fn main() -> std::io::Result<()> { - lalrpop::process_root().unwrap(); - println!("cargo:rerun-if-changed=src/grammar.lalrpop"); - - - let abnf_path = "dhall-lang/standard/dhall.abnf"; - let visibility_path = "src/dhall.pest.visibility"; - let pest_path = "src/dhall.pest"; - println!("cargo:rerun-if-changed={}", abnf_path); - println!("cargo:rerun-if-changed={}", visibility_path); - - 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 rule_settings: HashMap<String, PestRuleSettings> = HashMap::new(); - for line in BufReader::new(File::open(visibility_path)?).lines() { - let line = line?; - if line.len() >= 2 && &line[0..2] == "# " { - rule_settings.insert(line[2..].into(), PestRuleSettings { visible: false, ..Default::default() }); - } else { - rule_settings.insert(line, PestRuleSettings { visible: true, ..Default::default() }); - } - } - - let mut file = File::create(pest_path)?; - writeln!(&mut file, "{}", abnf_to_pest(&data, &rule_settings)?)?; - writeln!(&mut file, "final_expression = _{{ SOI ~ complete_expression ~ EOI }}")?; - - Ok(()) -} |