pomsky_syntax/exprs/
rule.rs1use crate::Span;
2
3use super::{
4 Alternation, Boundary, CharClass, Group, Literal, Lookaround, Range, Recursion, Reference,
5 Regex, Repetition, StmtExpr, Variable, intersection::Intersection, negation::Negation,
6};
7
8#[derive(Debug, Clone)]
10#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
11pub enum Rule {
12 Literal(Literal),
14 CharClass(CharClass),
16 Group(Group),
18 Alternation(Alternation),
21 Intersection(Intersection),
23 Repetition(Box<Repetition>),
27 Boundary(Boundary),
29 Lookaround(Box<Lookaround>),
31 Variable(Variable),
33 Reference(Reference),
35 Range(Range),
37 StmtExpr(Box<StmtExpr>),
39 Negation(Box<Negation>),
41 Regex(Regex),
43 Recursion(Recursion),
45
46 Grapheme,
48 Codepoint,
50 Dot,
52}
53
54impl Rule {
55 pub fn span(&self) -> Span {
57 match self {
58 Rule::Literal(l) => l.span,
59 Rule::CharClass(c) => c.span,
60 Rule::Group(g) => g.span,
61 Rule::Alternation(a) => a.span,
62 Rule::Intersection(i) => i.span,
63 Rule::Repetition(r) => r.span,
64 Rule::Boundary(b) => b.span,
65 Rule::Lookaround(l) => l.span,
66 Rule::Variable(v) => v.span,
67 Rule::Reference(r) => r.span,
68 Rule::Range(r) => r.span,
69 Rule::StmtExpr(m) => m.span,
70 Rule::Negation(n) => n.not_span.join(n.rule.span()),
71 Rule::Regex(r) => r.span,
72 Rule::Recursion(r) => r.span,
73 Rule::Grapheme | Rule::Codepoint | Rule::Dot => Span::empty(),
74 }
75 }
76
77 #[cfg(feature = "dbg")]
78 pub(crate) fn pretty_print(&self, buf: &mut crate::PrettyPrinter, needs_parens: bool) {
79 match self {
80 Rule::Literal(l) => l.pretty_print(buf),
81 Rule::CharClass(c) => c.pretty_print(buf),
82 Rule::Group(g) => g.pretty_print(buf, needs_parens),
83 Rule::Alternation(a) => a.pretty_print(buf, needs_parens),
84 Rule::Intersection(i) => i.pretty_print(buf, needs_parens),
85 Rule::Repetition(r) => r.pretty_print(buf),
86 Rule::Boundary(b) => b.pretty_print(buf),
87 Rule::Lookaround(l) => l.pretty_print(buf, needs_parens),
88 Rule::Variable(v) => v.pretty_print(buf),
89 Rule::Reference(r) => r.pretty_print(buf),
90 Rule::Range(r) => r.pretty_print(buf),
91 Rule::StmtExpr(s) => s.pretty_print(buf),
92 Rule::Negation(n) => n.pretty_print(buf, needs_parens),
93 Rule::Regex(r) => r.pretty_print(buf),
94 Rule::Recursion(_) => buf.push_str("recursion"),
95 Rule::Grapheme => buf.push_str("Grapheme"),
96 Rule::Codepoint => buf.push_str("Codepoint"),
97 Rule::Dot => buf.push_str("."),
98 }
99 }
100}
101
102#[cfg(feature = "dbg")]
103impl core::fmt::Display for Rule {
104 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105 let mut buf = crate::PrettyPrinter::new();
106 self.pretty_print(&mut buf, false);
107 f.write_str(&buf.finish())
108 }
109}