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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

// ABNF PARSING RULES
//
// Header:
// ```abnf
// ; Introduction
// ; -------------
// ```
//
// Code block in docs (note double whitespace after colon):
// ```abnf
// ;  code
// ;  code
//```
//
// Rule:
// ```abnf
// address = "address"
// ```
//
// Line:
// ``` abnf
// ;;;;;;;;;
// ```
//

#![forbid(unsafe_code)]

use abnf::types::{Node, Rule};
use anyhow::{anyhow, Result};
use std::collections::{HashMap, HashSet};

/// Processor's scope. Used when code block or definition starts or ends.
#[derive(Debug, Clone)]
enum Scope {
    Free,
    Code,
    Definition(Rule),
}

/// Transforms abnf file into Markdown.
#[derive(Debug, Clone)]
struct Processor<'a> {
    rules: HashMap<String, Rule>,
    grammar: &'a str,
    scope: Scope,
    line: u32,
    out: String,
}

impl<'a> Processor<'a> {
    fn new(grammar: &'a str, abnf: Vec<Rule>) -> Processor<'a> {
        // we need a hashmap to pull rules easily
        let rules: HashMap<String, Rule> = abnf.into_iter().map(|rule| (rule.name().to_string(), rule)).collect();

        Processor { grammar, line: 0, out: String::new(), rules, scope: Scope::Free }
    }

    /// Main function for this struct.
    /// Goes through each line and transforms it into proper markdown.
    fn process(&mut self) {
        let lines = self.grammar.lines();
        let mut prev = "";

        for line in lines {
            self.line += 1;

            // code block in comment (not highlighted as abnf)
            if let Some(code) = line.strip_prefix(";  ") {
                self.enter_scope(Scope::Code);
                self.append_str(code);

            // just comment. end of code block
            } else if let Some(code) = line.strip_prefix("; ") {
                self.enter_scope(Scope::Free);
                self.append_str(code);

            // horizontal rule - section separator
            } else if line.starts_with(";;;;;;;;;;") {
                self.enter_scope(Scope::Free);
                self.append_str("\n--------\n");

            // empty line in comment. end of code block
            } else if line.starts_with(';') {
                self.enter_scope(Scope::Free);
                self.append_str("\n\n");

            // just empty line. end of doc, start of definition
            } else if line.is_empty() {
                self.enter_scope(Scope::Free);
                self.append_str("");

            // definition (may be multiline)
            } else {
                // if there's an equality sign and previous line was empty
                if line.contains('=') && prev.is_empty() {
                    let (def, _) = line.split_at(line.find('=').unwrap());
                    let def = def.trim();

                    // try to find rule matching definition or fail
                    let rule = self.rules.get(&def.to_string()).cloned().unwrap();

                    self.enter_scope(Scope::Definition(rule));
                }

                self.append_str(line);
            }

            prev = line;
        }
    }

    /// Append new line into output, add newline character.
    fn append_str(&mut self, line: &str) {
        self.out.push_str(line);
        self.out.push('\n');
    }

    /// Enter new scope (definition or code block). Allows customizing
    /// pre and post lines for each scope entered or exited.
    fn enter_scope(&mut self, new_scope: Scope) {
        match (&self.scope, &new_scope) {
            // exchange scopes between Free and Code
            (Scope::Free, Scope::Code) => self.append_str("```"),
            (Scope::Code, Scope::Free) => self.append_str("```"),
            // exchange scopes between Free and Definition
            (Scope::Free, Scope::Definition(rule)) => {
                self.append_str(&format!("<a name=\"{}\"></a>", rule.name()));
                self.append_str("```abnf");
            }
            (Scope::Definition(rule), Scope::Free) => {
                let mut rules: Vec<String> = Vec::new();
                parse_abnf_node(rule.node(), &mut rules);

                // 1. leave only unique keys
                // 2. map each rule into a link
                // 3. sort the links so they don't keep changing order
                // 4. join results as a list
                // Note: GitHub only allows custom tags with 'user-content-' prefix
                let mut keyvec = rules
                    .into_iter()
                    .collect::<HashSet<_>>()
                    .into_iter()
                    .map(|tag| format!("[{}](#user-content-{tag})", &tag))
                    .collect::<Vec<String>>();
                keyvec.sort();
                let keys = keyvec.join(", ");

                self.append_str("```");
                if !keys.is_empty() {
                    self.append_str(&format!("\nGo to: _{keys}_;\n"));
                }
            }
            (_, _) => (),
        };

        self.scope = new_scope;
    }
}

/// Recursively parse ABNF Node and fill sum vec with found rule names.
fn parse_abnf_node(node: &Node, sum: &mut Vec<String>) {
    match node {
        // these two are just vectors of rules
        Node::Alternatives(vec) | Node::Concatenation(vec) => {
            for node in vec {
                parse_abnf_node(node, sum);
            }
        }
        Node::Group(node) | Node::Optional(node) => parse_abnf_node(node.as_ref(), sum),

        // push rulename if it is known
        Node::Rulename(name) => sum.push(name.clone()),

        // do nothing for other nodes
        _ => (),
    }
}

fn main() -> Result<()> {
    // Take Leo ABNF grammar file.
    let args: Vec<String> = std::env::args().collect();
    let abnf_path = if let Some(path) = args.get(1) {
        std::path::Path::new(path)
    } else {
        return Err(anyhow!("Usage Error: expects one argument to abnf file to convert."));
    };
    let grammar = std::fs::read_to_string(abnf_path)?;

    // Parse ABNF to get list of all definitions.
    // Rust ABNF does not provide support for `%s` (case sensitive strings, part of
    // the standard); so we need to remove all occurrences before parsing.
    let parsed = abnf::rulelist(&str::replace(&grammar, "%s", "")).map_err(|e| {
        eprintln!("{}", &e);
        anyhow::anyhow!(e)
    })?;

    // Init parser and run it. That's it.
    let mut parser = Processor::new(&grammar, parsed);
    parser.process();

    // Print result of conversion to STDOUT.
    println!("{}", parser.out);

    Ok(())
}