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
mod ast;
mod error;
#[allow(
clippy::unused_unit,
clippy::unit_arg,
clippy::cyclomatic_complexity,
clippy::len_zero,
clippy::single_match,
clippy::naive_bytecount,
clippy::suspicious_else_formatting
)]
mod grammar;
#[cfg(test)]
mod tests;
mod traversion;
mod util;
pub use self::ast::*;
pub use self::error::*;
pub use self::traversion::Traversion;
pub mod transformations;
mod default_transformations;
use self::default_transformations::*;
pub fn parse(input: &str) -> Result<Element, MWError> {
let source_lines = util::get_source_lines(input);
#[cfg(feature = "ptime")]
let starttime = time::precise_time_ns();
let result = match grammar::document(input, &source_lines) {
Err(e) => Err(error::MWError::ParseError(error::ParseError::from(
&e, input,
))),
Ok(r) => Ok(r),
}?;
#[cfg(feature = "ptime")]
let parsedtime = time::precise_time_ns();
let settings = GeneralSettings {};
let trans_result = apply_transformations(result, &settings);
#[cfg(feature = "ptime")]
{
eprintln!(
"Parse Timer: Parsing took {} ms.",
((parsedtime - starttime) as f64) / 1.0e6
);
eprintln!(
"Parse Timer: Transformation took {} ms.",
((time::precise_time_ns() - parsedtime) as f64) / 1.0e6
);
}
trans_result.map_err(error::MWError::TransformationError)
}
fn apply_transformations(
mut root: Element,
settings: &GeneralSettings,
) -> transformations::TResult {
root = validate_external_refs(root, settings)?;
root = fold_headings_transformation(root, settings)?;
root = fold_lists_transformation(root, settings)?;
root = whitespace_paragraphs_to_empty(root, settings)?;
root = collapse_paragraphs(root, settings)?;
root = collapse_consecutive_text(root, settings)?;
root = enumerate_anon_args(root, settings)?;
Ok(root)
}