harper_core/expr/
first_match_of.rs

1use super::Expr;
2use crate::{Span, Token};
3
4/// A naive expr collection that naively iterates through a list of patterns,
5/// returning the first one that matches.
6///
7/// Compare to [`LongestMatchOf`](super::LongestMatchOf), which returns the longest match.
8#[derive(Default)]
9pub struct FirstMatchOf {
10    exprs: Vec<Box<dyn Expr>>,
11}
12
13impl FirstMatchOf {
14    pub fn new(exprs: Vec<Box<dyn Expr>>) -> Self {
15        Self { exprs }
16    }
17
18    pub fn add(&mut self, expr: impl Expr + 'static) {
19        self.exprs.push(Box::new(expr));
20    }
21}
22
23impl Expr for FirstMatchOf {
24    fn run(&self, cursor: usize, tokens: &[Token], source: &[char]) -> Option<Span<Token>> {
25        self.exprs
26            .iter()
27            .find_map(|p| p.run(cursor, tokens, source))
28    }
29}