intuicio_parser/
predict.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::{ParseResult, Parser, ParserExt, ParserHandle, ParserRegistry};

pub mod shorthand {
    use super::*;

    pub fn pred(parser: ParserHandle) -> ParserHandle {
        PredictParser::new(parser).into_handle()
    }
}

#[derive(Clone)]
pub struct PredictParser(ParserHandle);

impl PredictParser {
    pub fn new(parser: ParserHandle) -> Self {
        Self(parser)
    }
}

impl Parser for PredictParser {
    fn parse<'a>(&self, registry: &ParserRegistry, input: &'a str) -> ParseResult<'a> {
        let (_, result) = self.0.parse(registry, input)?;
        Ok((input, result))
    }

    fn extend(&self, parser: ParserHandle) {
        self.0.extend(parser);
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        predict::PredictParser,
        shorthand::{lit, pred, seq},
        ParserRegistry,
    };

    fn is_async<T: Send + Sync>() {}

    #[test]
    fn test_predict() {
        is_async::<PredictParser>();

        let registry = ParserRegistry::default();
        let sentence = seq([lit("foo"), pred(lit("bar"))]);
        let (rest, _) = sentence.parse(&registry, "foobar").unwrap();
        assert_eq!(rest, "bar");
    }
}