#![doc(
html_logo_url = "https://raw.githubusercontent.com/pest-parser/pest/master/pest-logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/pest-parser/pest/master/pest-logo.svg"
)]
#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
#[cfg(test)]
#[macro_use]
extern crate pest;
use once_cell::sync::Lazy;
use std::fmt::Display;
use pest::error::Error;
use pest::unicode::unicode_property_names;
pub mod ast;
pub mod optimizer;
pub mod parser;
pub mod validator;
pub fn unwrap_or_report<T, E>(result: Result<T, E>) -> T
where
E: IntoIterator,
E::Item: Display,
{
result.unwrap_or_else(|e| {
panic!(
"{}{}",
"grammar error\n\n".to_owned(),
&e.into_iter()
.map(|error| format!("{}", error))
.collect::<Vec<_>>()
.join("\n\n")
)
})
}
type UsedBuiltinAndOptimized<'i> = (Vec<&'i str>, Vec<optimizer::OptimizedRule>);
pub fn parse_and_optimize(
grammar: &str,
) -> Result<UsedBuiltinAndOptimized<'_>, Vec<Error<parser::Rule>>> {
let pairs = match parser::parse(parser::Rule::grammar_rules, grammar) {
Ok(pairs) => Ok(pairs),
Err(error) => Err(vec![error]),
}?;
let defaults = validator::validate_pairs(pairs.clone())?;
let ast = parser::consume_rules(pairs)?;
Ok((defaults, optimizer::optimize(ast)))
}
#[doc(hidden)]
#[deprecated(note = "use `pest::unicode::unicode_property_names` instead")]
pub static UNICODE_PROPERTY_NAMES: Lazy<Vec<&str>> =
Lazy::new(|| unicode_property_names().collect::<Vec<_>>());