summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNadrieril2019-03-01 00:44:55 +0100
committerNadrieril2019-03-01 00:44:55 +0100
commit7ec8c19e31bc88a6863ba2af40802ede31b18306 (patch)
tree7d009506a4cedfab65bfc25b9e812100b15f1cad
parent7b30677613bb8687649473361ce1a826509742a0 (diff)
Improve pest file layout
Diffstat (limited to '')
-rw-r--r--Cargo.lock16
-rw-r--r--Cargo.toml1
-rw-r--r--build.rs56
3 files changed, 57 insertions, 16 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 4f91cf0..ae09890 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -133,6 +133,7 @@ dependencies = [
"nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"pest 2.1.0 (git+https://github.com/pest-parser/pest)",
"pest_derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pretty 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"term-painter 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -406,6 +407,14 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "pretty"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "typed-arena 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "proc-macro2"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -682,6 +691,11 @@ dependencies = [
]
[[package]]
+name = "typed-arena"
+version = "1.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "typenum"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -794,6 +808,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum phf_generator 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662"
"checksum phf_shared 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0"
"checksum precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
+"checksum pretty 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f60c0d9f6fc88ecdd245d90c1920ff76a430ab34303fc778d33b1d0a4c3bf6d3"
"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
"checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1"
"checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca"
@@ -825,6 +840,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum term-painter 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "dcaa948f0e3e38470cd8dc8dcfe561a75c9e43f28075bb183845be2b9b3c08cf"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
+"checksum typed-arena 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c6c06a92aef38bb4dc5b0df00d68496fc31307c5344c867bb61678c6e1671ec5"
"checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169"
"checksum ucd-trie 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "71a9c5b1fe77426cf144cc30e49e955270f5086e31a6441dfa8b32efc09b9d77"
"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
diff --git a/Cargo.toml b/Cargo.toml
index 48c5488..9775e6a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,6 +9,7 @@ build = "build.rs"
lalrpop = "0.16.3"
abnf = { git = "https://github.com/Nadrieril/abnf" }
itertools = "0.8.0"
+pretty = "0.5.2"
[dependencies]
bytecount = "0.5.1"
diff --git a/build.rs b/build.rs
index c50e955..c2cc11d 100644
--- a/build.rs
+++ b/build.rs
@@ -7,12 +7,21 @@ use lalrpop;
fn abnf_to_pest(data: &Vec<u8>, visibility_map: &HashMap<String, bool>) -> std::io::Result<String> {
use abnf::abnf::*;
- fn format_rule(x: Rule, visibility_map: &HashMap<String, bool>) -> String {
+ use pretty::{Doc, BoxDoc};
+ fn format_rule(x: Rule, visibility_map: &HashMap<String, bool>) -> Doc<BoxDoc<()>> {
let rulename = format_rulename(x.name);
let contents = format_alternation(x.elements);
let visible = visibility_map.get(&rulename).unwrap_or(&true);
- let visible = if *visible {""} else {"_"};
- format!("{} = {}{{ {} }}", rulename, visible, contents)
+ let visible = if *visible { "" } else { "_" };
+ Doc::nil()
+ .append(Doc::text(rulename))
+ .append(Doc::text(" = "))
+ .append(Doc::text(visible))
+ .append(Doc::text("{"))
+ .append(Doc::space().append(contents).nest(2))
+ .append(Doc::space())
+ .append(Doc::text("}"))
+ .group()
}
fn format_rulename(x: String) -> String {
let x = x.replace("-", "_");
@@ -22,14 +31,21 @@ fn abnf_to_pest(data: &Vec<u8>, visibility_map: &HashMap<String, bool>) -> std::
x
}
}
- fn format_alternation(x: Alternation) -> String {
- x.concatenations.into_iter().map(format_concatenation).join("\n | ")
+ fn format_alternation(x: Alternation) -> Doc<'static, BoxDoc<'static, ()>> {
+ Doc::intersperse(
+ x.concatenations.into_iter().map(format_concatenation),
+ Doc::space().append(Doc::text("| "))
+ )
}
- fn format_concatenation(x: Concatenation) -> String {
- x.repetitions.into_iter().map(format_repetition).join(" ~ ")
+ fn format_concatenation(x: Concatenation) -> Doc<'static, BoxDoc<'static, ()>> {
+ Doc::intersperse(
+ x.repetitions.into_iter().map(format_repetition),
+ Doc::text(" ~ ")
+ )
}
- fn format_repetition(x: Repetition) -> String {
- format!("{}{}", format_element(x.element), x.repeat.map(format_repeat).unwrap_or("".into()))
+ fn format_repetition(x: Repetition) -> Doc<'static, BoxDoc<'static, ()>> {
+ format_element(x.element)
+ .append(x.repeat.map(format_repeat).map(Doc::text).unwrap_or(Doc::nil()))
}
fn format_repeat(x: Repeat) -> String {
match (x.min.unwrap_or(0), x.max) {
@@ -41,14 +57,20 @@ fn abnf_to_pest(data: &Vec<u8>, visibility_map: &HashMap<String, bool>) -> std::
(min, Some(max)) => format!("{{{},{}}}", min, max),
}
}
- fn format_element(x: Element) -> String {
+ fn format_element(x: Element) -> Doc<'static, BoxDoc<'static, ()>> {
use abnf::abnf::Element::*;
match x {
- Rulename(s) => format_rulename(s),
- Group(g) => format!("({})", format_alternation(g.alternation)),
- Option(o) => format!("({})?", format_alternation(o.alternation)),
- CharVal(s) => format!("^\"{}\"", s.replace("\"", "\\\"").replace("\\", "\\\\")),
- NumVal(r) => format_range(r),
+ Rulename(s) => Doc::text(format_rulename(s)),
+ Group(g) =>
+ Doc::text("(")
+ .append(format_alternation(g.alternation).nest(4).group())
+ .append(Doc::text(")")),
+ Option(o) =>
+ Doc::text("(")
+ .append(format_alternation(o.alternation).nest(4).group())
+ .append(Doc::text(")?")),
+ CharVal(s) => Doc::text(format!("^\"{}\"", s.replace("\"", "\\\"").replace("\\", "\\\\"))),
+ NumVal(r) => Doc::text(format_range(r)),
ProseVal(_) => unimplemented!(),
}
}
@@ -74,7 +96,9 @@ fn abnf_to_pest(data: &Vec<u8>, visibility_map: &HashMap<String, bool>) -> std::
let make_err = |e| std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e));
let rules = rulelist_comp(&data).map_err(make_err)?.1;
- Ok(rules.into_iter().map(|x| format_rule(x, visibility_map)).join("\n"))
+ let formatted_rules = rules.into_iter().map(|x| format_rule(x, visibility_map));
+ let doc: Doc<_> = Doc::intersperse(formatted_rules, Doc::newline());
+ Ok(format!("{}", doc.pretty(80)))
}
fn main() -> std::io::Result<()> {