| 1 | // pest. The Elegant Parser |
| 2 | // Copyright (c) 2018 DragoČ™ Tiselice |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 |
| 5 | // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT |
| 6 | // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 7 | // option. All files in the project carrying such notice may not be copied, |
| 8 | // modified, or distributed except according to those terms. |
| 9 | //! # pest meta |
| 10 | //! |
| 11 | //! This crate parses, validates, optimizes, and converts pest's own grammars to ASTs. |
| 12 | |
| 13 | #![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 ] |
| 21 | extern crate pest; |
| 22 | |
| 23 | use once_cell::sync::Lazy; |
| 24 | use std::fmt::Display; |
| 25 | |
| 26 | use pest::error::Error; |
| 27 | use pest::unicode::unicode_property_names; |
| 28 | |
| 29 | pub mod ast; |
| 30 | pub mod optimizer; |
| 31 | pub mod parser; |
| 32 | pub mod validator; |
| 33 | |
| 34 | /// A helper that will unwrap the result or panic |
| 35 | /// with the nicely formatted error message. |
| 36 | pub fn unwrap_or_report<T, E>(result: Result<T, E>) -> T |
| 37 | where |
| 38 | E: IntoIterator, |
| 39 | E::Item: Display, |
| 40 | { |
| 41 | result.unwrap_or_else(|e: 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 | |
| 53 | /// A tuple returned by the validation and processing of the parsed grammar. |
| 54 | /// The first element is the vector of used builtin rule names, |
| 55 | /// the second element is the vector of optimized rules. |
| 56 | type UsedBuiltinAndOptimized<'i> = (Vec<&'i str>, Vec<optimizer::OptimizedRule>); |
| 57 | |
| 58 | /// Parses, validates, processes and optimizes the provided grammar. |
| 59 | pub fn parse_and_optimize( |
| 60 | grammar: &str, |
| 61 | ) -> Result<UsedBuiltinAndOptimized<'_>, Vec<Error<parser::Rule>>> { |
| 62 | let pairs: Pairs<'_, Rule> = match parser::parse(parser::Rule::grammar_rules, data:grammar) { |
| 63 | Ok(pairs: Pairs<'_, Rule>) => Ok(pairs), |
| 64 | Err(error: Error) => Err(vec![error]), |
| 65 | }?; |
| 66 | |
| 67 | let defaults: Vec<&str> = validator::validate_pairs(pairs.clone())?; |
| 68 | let ast: Vec = parser::consume_rules(pairs)?; |
| 69 | |
| 70 | Ok((defaults, optimizer::optimize(rules:ast))) |
| 71 | } |
| 72 | |
| 73 | #[doc (hidden)] |
| 74 | #[deprecated (note = "use `pest::unicode::unicode_property_names` instead" )] |
| 75 | pub static UNICODE_PROPERTY_NAMES: Lazy<Vec<&str>> = |
| 76 | Lazy::new(|| unicode_property_names().collect::<Vec<_>>()); |
| 77 | |