pomsky_syntax/exprs/
lookaround.rs

1use crate::Span;
2
3use super::Rule;
4
5#[derive(Debug, Clone)]
6#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
7pub struct Lookaround {
8    pub kind: LookaroundKind,
9    pub rule: Rule,
10    pub span: Span,
11}
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
15pub enum LookaroundKind {
16    Ahead,
17    Behind,
18    AheadNegative,
19    BehindNegative,
20}
21
22impl Lookaround {
23    pub(crate) fn new(rule: Rule, kind: LookaroundKind, span: Span) -> Self {
24        Lookaround { kind, rule, span }
25    }
26
27    #[cfg(feature = "dbg")]
28    pub(super) fn pretty_print(&self, buf: &mut crate::PrettyPrinter, needs_parens: bool) {
29        let s = match self.kind {
30            LookaroundKind::Ahead => ">>",
31            LookaroundKind::Behind => "<<",
32            LookaroundKind::AheadNegative => "!>>",
33            LookaroundKind::BehindNegative => "!<<",
34        };
35        if needs_parens {
36            buf.push('(');
37            buf.start_indentation(s);
38        } else {
39            buf.push_str(s);
40            buf.push(' ');
41        }
42
43        self.rule.pretty_print(buf, false);
44
45        if needs_parens {
46            buf.end_indentation(")");
47        }
48    }
49}