pull latest abnf grammar

This commit is contained in:
gluax 2021-03-31 13:41:12 -04:00
commit 7b4542ae54
2 changed files with 1839 additions and 26 deletions

1817
grammar/abnf-grammar.md Normal file

File diff suppressed because it is too large Load Diff

View File

@ -76,49 +76,49 @@ impl<'a> Processor<'a> {
/// Main function for this struct.
/// Goes through each line and transforms it into proper markdown.
fn process(&mut self) -> Result<()> {
let mut lines = self.grammar.lines();
fn process(&mut self) {
let lines = self.grammar.lines();
let mut prev = "";
while let Some(line) = lines.next() {
for line in lines {
self.line += 1;
// code block in comment (not highlighted as abnf)
if line.starts_with("; ") {
self.enter_scope(Scope::Code)?;
self.append_str(&line[3..]);
if let Some(code) = line.strip_prefix("; ") {
self.enter_scope(Scope::Code);
self.append_str(code);
// just comment. end of code block
} else if line.starts_with("; ") {
self.enter_scope(Scope::Free)?;
self.append_str(&line[2..]);
} 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.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)?;
} 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.len() == 0 {
self.enter_scope(Scope::Free)?;
} 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.len() == 0 {
let (def, _) = line.split_at(line.find("=").unwrap());
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()).map(|rule| rule.clone()).unwrap();
let rule = self.rules.get(&def.to_string()).cloned().unwrap();
self.enter_scope(Scope::Definition(rule))?;
self.enter_scope(Scope::Definition(rule));
}
self.append_str(line);
@ -126,19 +126,17 @@ impl<'a> Processor<'a> {
prev = line;
}
Ok(())
}
/// Append new line into output, add newline character.
fn append_str(&mut self, line: &str) {
self.out.push_str(line);
self.out.push_str("\n");
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) -> Result<()> {
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("```"),
@ -166,7 +164,7 @@ impl<'a> Processor<'a> {
self.append_str("```");
if keys.len() > 0 {
if !keys.is_empty() {
self.append_str(&format!("\nGo to: _{}_;\n", keys));
}
}
@ -174,8 +172,6 @@ impl<'a> Processor<'a> {
};
self.scope = new_scope;
Ok(())
}
}
@ -215,7 +211,7 @@ fn main() -> Result<()> {
// Init parser and run it. That's it.
let mut parser = Processor::new(grammar, parsed);
parser.process()?;
parser.process();
// Print result of conversion to STDOUT.
println!("{}", parser.out);