oom/match/
match.rs

1use crate::{Matcher, Position, Production, Span, StackRange, State};
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4pub struct Match {
5    pub matcher: Production,
6    pub span: Span,
7    pub inner: Vec<Match>,
8}
9impl Matcher for Match {
10    fn name(&self) -> &str {
11        self.matcher.name()
12    }
13
14    fn to_str(&self) -> String {
15        self.matcher.to_str()
16    }
17
18    fn as_production(&self) -> Production {
19        self.matcher.as_production()
20    }
21
22    fn is_match(&self, state: &mut State, input: &str, start: &Position) -> Option<Match> {
23        self.matcher.is_match(state, input, start)
24    }
25}
26impl Match {
27    pub fn to_tuple(&self) -> (Production, Span) {
28        (self.matcher.clone(), self.span.clone())
29    }
30
31    pub fn matcher(&self) -> Production {
32        self.matcher.clone()
33    }
34
35    pub fn inner(&self) -> Vec<Match> {
36        self.inner.clone()
37    }
38
39    pub fn span(&self) -> Span {
40        self.span.clone()
41    }
42
43    pub fn with_inner(&self, matches: Vec<Match>) -> Match {
44        let mut r#match = self.clone();
45        r#match.inner.extend(matches);
46        r#match
47    }
48}
49impl<T: Matcher> From<(T, Span)> for Match {
50    fn from(ms: (T, Span)) -> Match {
51        let (matcher, span) = ms;
52        Match {
53            matcher: matcher.as_production(),
54            span,
55            inner: Vec::new(),
56        }
57    }
58}
59
60impl<T: Matcher> From<&(T, Span)> for Match {
61    fn from(ms: &(T, Span)) -> Match {
62        let (matcher, span) = ms;
63        Match {
64            matcher: (*matcher).as_production(),
65            span: (*span).clone(),
66            inner: Vec::new(),
67        }
68    }
69}
70
71impl Into<(Production, Span)> for Match {
72    fn into(self) -> (Production, Span) {
73        self.to_tuple()
74    }
75}
76
77impl Into<Option<(Production, Span)>> for Match {
78    fn into(self) -> Option<(Production, Span)> {
79        Some((self.matcher.as_production(), self.span.clone()))
80    }
81}
82
83impl Into<Production> for Match {
84    fn into(self) -> Production {
85        self.matcher.clone()
86    }
87}
88
89impl Into<Span> for Match {
90    fn into(self) -> Span {
91        self.span.clone()
92    }
93}
94
95impl Into<((usize, usize), (usize, usize))> for Match {
96    fn into(self) -> ((usize, usize), (usize, usize)) {
97        (self.span.start.to_tuple(), self.span.end.to_tuple())
98    }
99}
100
101impl Into<String> for Match {
102    fn into(self) -> String {
103        self.span.to_string()
104    }
105}