Skip to main content

peggen_core/
parser.rs

1use core::marker::PhantomData;
2
3use crate::*;
4
5/// A simple wrapper to make ParseImpl easier to use. 
6pub struct Parser<T>(PhantomData<T>);
7
8impl<T> Parser<T> {
9    /// Parse without extra value
10    pub fn parse(input: &str) -> Result<T, ()> 
11        where T: ParseImpl<0, false> + AstImpl<()>
12    {
13        // Parse input input a tag stack
14        let mut trace = Vec::new();
15        let mut stack = Vec::new();
16        let end = 0;
17        <T as ParseImpl<0, false>>::parse_impl(input, end, 0, false, &mut trace, &mut stack)?;
18        // Analyze the tag stack into this value
19        Ok(T::peggen_ast(input, &stack, ()).1)
20    }
21    /// Parse with extra value provided
22    pub fn parse_with<Extra>(input: &str, with: Extra) -> Result<T, ()> 
23        where T: ParseImpl<0, false> + AstImpl<Extra>,
24              Extra: Copy
25    {
26        // Parse input input a tag stack
27        let mut trace = Vec::new();
28        let mut stack = Vec::new();
29        let end = 0;
30        <T as ParseImpl<0, false>>::parse_impl(input, end, 0, false, &mut trace, &mut stack)?;
31        // Analyze the tag stack into this value, with extra value attached
32        Ok(T::peggen_ast(input, &stack, with).1)
33    }
34    /// Only parse into a tag stack
35    pub fn sequence(input: &str) -> Result<Vec<Tag>, ()> 
36        where T: ParseImpl<0, false> + AstImpl<()>
37    {
38        let mut trace = Vec::new();
39        let mut stack = Vec::new();
40        let end = 0;
41        <T as ParseImpl<0, false>>::parse_impl(input, end, 0, false, &mut trace, &mut stack)?;
42        Ok(stack)
43    }
44}