intuicio_parser/
alternation.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use crate::{ParseResult, Parser, ParserExt, ParserHandle, ParserRegistry};
use std::fmt::Write;

pub mod shorthand {
    use super::*;

    pub fn alt(values: impl IntoIterator<Item = ParserHandle>) -> ParserHandle {
        AlternationParser::from_iter(values).into_handle()
    }
}

#[derive(Default, Clone)]
pub struct AlternationParser {
    parsers: Vec<ParserHandle>,
}

impl AlternationParser {
    pub fn with(mut self, parser: ParserHandle) -> Self {
        self.append(parser);
        self
    }

    pub fn append(&mut self, parser: ParserHandle) {
        self.parsers.push(parser);
    }

    pub fn prepend(&mut self, parser: ParserHandle) {
        self.parsers.insert(0, parser);
    }
}

impl Parser for AlternationParser {
    fn parse<'a>(&self, registry: &ParserRegistry, input: &'a str) -> ParseResult<'a> {
        let mut errors = String::new();
        for parser in &self.parsers {
            match parser.parse(registry, input) {
                Ok(result) => {
                    return Ok(result);
                }
                Err(error) => {
                    if errors.is_empty() {
                        write!(&mut errors, "{}", error)?;
                    } else {
                        write!(&mut errors, "\nOr: {}", error)?;
                    }
                }
            }
        }
        Err(errors.into())
    }
}

impl FromIterator<ParserHandle> for AlternationParser {
    fn from_iter<T: IntoIterator<Item = ParserHandle>>(iter: T) -> Self {
        Self {
            parsers: iter.into_iter().collect(),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        alternation::AlternationParser,
        shorthand::{alt, lit},
        ParserRegistry,
    };

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

    #[test]
    fn test_alternation() {
        is_async::<AlternationParser>();

        let registry = ParserRegistry::default();
        let sentence = alt([lit("foo"), lit("bar")]);
        let (rest, result) = sentence.parse(&registry, "foo").unwrap();
        assert_eq!(rest, "");
        result.consume::<String>().ok().unwrap();
        let (rest, result) = sentence.parse(&registry, "bar").unwrap();
        assert_eq!(rest, "");
        result.consume::<String>().ok().unwrap();
        assert_eq!(
            format!("{}", sentence.parse(&registry, "zee").err().unwrap()),
            "Expected 'foo'\nOr: Expected 'bar'"
        );
    }
}