harper_core/expr/
all.rs

1use crate::{Span, Token, expr::Expr};
2
3/// An [`Expr`] that matches against tokens if and only if all of its children do.
4/// This can be useful for situations where you have multiple expressions that represent a grammatical
5/// error, but you need _all_ of them to match to be certain.
6///
7/// It will return the position of the farthest window.
8#[derive(Default)]
9pub struct All {
10    children: Vec<Box<dyn Expr>>,
11}
12
13impl All {
14    pub fn new(children: Vec<Box<dyn Expr>>) -> Self {
15        Self { children }
16    }
17
18    pub fn add(&mut self, e: impl Expr + 'static) {
19        self.children.push(Box::new(e));
20    }
21}
22
23impl Expr for All {
24    fn run(&self, cursor: usize, tokens: &[Token], source: &[char]) -> Option<Span<Token>> {
25        let mut longest: Option<Span<Token>> = None;
26
27        for expr in self.children.iter() {
28            let window = expr.run(cursor, tokens, source)?;
29
30            if let Some(longest_window) = longest {
31                if window.len() > longest_window.len() {
32                    longest = Some(window);
33                }
34            } else {
35                longest = Some(window);
36            }
37        }
38
39        longest
40    }
41}