plotnik_lib/ir/
matcher.rs1use super::{NodeFieldId, NodeTypeId, Slice};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum MatcherKind {
10 Epsilon,
11 Node,
12 Anonymous,
13 Wildcard,
14}
15
16#[repr(C, u32)]
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum Matcher {
22 Epsilon,
24
25 Node {
27 kind: NodeTypeId,
28 field: Option<NodeFieldId>,
29 negated_fields: Slice<NodeFieldId>,
30 },
31
32 Anonymous {
34 kind: NodeTypeId,
35 field: Option<NodeFieldId>,
36 negated_fields: Slice<NodeFieldId>,
37 },
38
39 Wildcard,
41}
42
43impl Matcher {
44 #[inline]
46 pub fn consumes_node(&self) -> bool {
47 !matches!(self, Matcher::Epsilon)
48 }
49
50 #[inline]
52 pub fn kind(&self) -> MatcherKind {
53 match self {
54 Matcher::Epsilon => MatcherKind::Epsilon,
55 Matcher::Node { .. } => MatcherKind::Node,
56 Matcher::Anonymous { .. } => MatcherKind::Anonymous,
57 Matcher::Wildcard => MatcherKind::Wildcard,
58 }
59 }
60
61 #[inline]
63 pub fn node_kind(&self) -> Option<NodeTypeId> {
64 match self {
65 Matcher::Node { kind, .. } | Matcher::Anonymous { kind, .. } => Some(*kind),
66 _ => None,
67 }
68 }
69
70 #[inline]
72 pub fn field(&self) -> Option<NodeFieldId> {
73 match self {
74 Matcher::Node { field, .. } | Matcher::Anonymous { field, .. } => *field,
75 _ => None,
76 }
77 }
78
79 #[inline]
81 pub fn negated_fields(&self) -> Slice<NodeFieldId> {
82 match self {
83 Matcher::Node { negated_fields, .. } | Matcher::Anonymous { negated_fields, .. } => {
84 *negated_fields
85 }
86 _ => Slice::empty(),
87 }
88 }
89}