logical_expression_pest_parser/
parser.rs

1use pest::Parser;
2use pest::iterators::Pairs;
3use pest_derive::Parser;
4use thiserror::Error;
5
6/// The Pest parser struct.
7///
8/// The grammar is included directly from the `grammar.pest` file.
9///
10/// # Grammar
11#[derive(Parser)]
12#[doc = include_str!("grammar.pest")]
13#[grammar = "./grammar.pest"]
14pub struct Grammar;
15
16/// Custom parsing error created using the [thiserror] crate.
17#[derive(Debug, Error)]
18pub enum ParserError {
19    /// Wrapper for a [pest] crate error.
20    #[error("ParsingError: {0}")]
21    PestError(Box<pest::error::Error<Rule>>),
22
23    /// Error for empty input.
24    #[error("Empty input")]
25    EmptyInputError,
26}
27
28/// Parses an input string into `Pairs<Rule>`.
29///
30/// It checks for empty error and then uses [Grammar] to parse the input with the `file` rule.
31///
32/// # Arguments
33/// * `input` - The string to parse.
34///
35/// # Returns
36/// A [Result] containing successful `Pairs<'_, Rule>` or a [ParserError].
37///
38/// # Errors
39/// Returns [ParserError::PestError] if Pest fails to parse the input string.
40///
41/// Returns [ParserError::EmptyInputError] if `input` is empty.
42pub fn parse(input: &str) -> Result<Pairs<'_, Rule>, ParserError> {
43    if input.is_empty() {
44        return Err(ParserError::EmptyInputError);
45    }
46
47    let pairs =
48        Grammar::parse(Rule::file, input).map_err(|e| ParserError::PestError(Box::new(e)))?;
49    Ok(pairs)
50}