1#![doc(
14 html_logo_url = "https://raw.githubusercontent.com/pest-parser/pest/master/pest-logo.svg",
15 html_favicon_url = "https://raw.githubusercontent.com/pest-parser/pest/master/pest-logo.svg"
16)]
17#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
18
19#[cfg(test)]
20#[macro_use]
21extern crate pest;
22
23use once_cell::sync::Lazy;
24use std::fmt::Display;
25
26use pest::error::Error;
27use pest::unicode::unicode_property_names;
28
29pub mod ast;
30pub mod optimizer;
31pub mod parser;
32pub mod validator;
33
34pub fn unwrap_or_report<T, E>(result: Result<T, E>) -> T
37where
38 E: IntoIterator,
39 E::Item: Display,
40{
41 result.unwrap_or_else(|e| {
42 panic!(
43 "{}{}",
44 "grammar error\n\n".to_owned(),
45 &e.into_iter()
46 .map(|error| format!("{}", error))
47 .collect::<Vec<_>>()
48 .join("\n\n")
49 )
50 })
51}
52
53type UsedBuiltinAndOptimized<'i> = (Vec<&'i str>, Vec<optimizer::OptimizedRule>);
57
58pub fn parse_and_optimize(
60 grammar: &str,
61) -> Result<UsedBuiltinAndOptimized<'_>, Vec<Error<parser::Rule>>> {
62 let pairs = match parser::parse(parser::Rule::grammar_rules, grammar) {
63 Ok(pairs) => Ok(pairs),
64 Err(error) => Err(vec![error]),
65 }?;
66
67 let defaults = validator::validate_pairs(pairs.clone())?;
68 let ast = parser::consume_rules(pairs)?;
69
70 Ok((defaults, optimizer::optimize(ast)))
71}
72
73#[doc(hidden)]
74#[deprecated(note = "use `pest::unicode::unicode_property_names` instead")]
75pub static UNICODE_PROPERTY_NAMES: Lazy<Vec<&str>> =
76 Lazy::new(|| unicode_property_names().collect::<Vec<_>>());