summaryrefslogtreecommitdiff
path: root/abnf_to_pest/src/lib.rs
blob: 72746c07359bd991fca26351bef7420d2450f05e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#![allow(clippy::implicit_hasher, clippy::or_fun_call)]
use itertools::Itertools;
use std::collections::HashMap;

pub struct PestRuleSettings {
    pub visible: bool,
    pub replace: Option<String>,
}

impl Default for PestRuleSettings {
    fn default() -> Self {
        PestRuleSettings {
            visible: true,
            replace: None,
        }
    }
}

pub fn abnf_to_pest(
    data: &[u8],
    rule_settings: &HashMap<String, PestRuleSettings>,
) -> std::io::Result<String> {
    use abnf::abnf::*;
    use pretty::{BoxDoc, Doc};
    fn format_rule(
        x: Rule,
        rule_settings: &HashMap<String, PestRuleSettings>,
    ) -> Doc<BoxDoc<()>> {
        let rulename = format_rulename(x.name);
        let default = Default::default();
        let setting = rule_settings.get(&rulename).unwrap_or(&default);
        let visible = if setting.visible { "" } else { "_" };
        let contents = match setting.replace {
            None => format_alternation(x.elements),
            Some(ref x) => Doc::text(x.clone()),
        };
        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("-", "_");
        if x == "if"
            || x == "else"
            || x == "as"
            || x == "let"
            || x == "in"
            || x == "fn"
            // TODO: remove when https://github.com/pest-parser/pest/pull/375 gets into a release
            || x == "whitespace"
        {
            x + "_"
        } else {
            x
        }
    }
    fn format_alternation(x: Alternation) -> Doc<'static, BoxDoc<'static, ()>> {
        Doc::intersperse(
            x.concatenations
                .into_iter()
                .map(|x| format_concatenation(x).nest(2).group()),
            Doc::space().append(Doc::text("| ")),
        )
    }
    fn format_concatenation(
        x: Concatenation,
    ) -> Doc<'static, BoxDoc<'static, ()>> {
        Doc::intersperse(
            x.repetitions.into_iter().map(format_repetition),
            Doc::space().append(Doc::text("~ ")),
        )
    }
    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) {
            (0, None) => "*".into(),
            (1, None) => "+".into(),
            (0, Some(1)) => "?".into(),
            (min, None) => format!("{{{},}}", min),
            (min, Some(max)) if min == max => format!("{{{}}}", min),
            (min, Some(max)) => format!("{{{},{}}}", min, max),
        }
    }
    fn format_element(x: Element) -> Doc<'static, BoxDoc<'static, ()>> {
        use abnf::abnf::Element::*;
        match x {
            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!(),
        }
    }
    fn format_range(x: Range) -> String {
        use abnf::abnf::Range::*;
        match x {
            Range(x, y) => {
                format!("'{}'..'{}'", format_char(x), format_char(y))
            }
            OneOf(v) => {
                format!("\"{}\"", v.into_iter().map(format_char).join(""))
            }
        }
    }
    fn format_char(x: u64) -> String {
        if x <= u64::from(u8::max_value()) {
            let x: u8 = x as u8;
            if x.is_ascii_graphic() {
                let x: char = x as char;
                if x != '"' && x != '\'' && x != '\\' {
                    return x.to_string();
                }
            }
        }
        format!("\\u{{{:02X}}}", x)
    }
    let make_err =
        |e| std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e));

    let rules = rulelist_comp(&data).map_err(make_err)?.1;
    let formatted_rules =
        rules.into_iter().map(|x| format_rule(x, rule_settings));
    let doc: Doc<_> = Doc::intersperse(formatted_rules, Doc::newline());
    Ok(format!("{}", doc.pretty(80)))
}