pomsky_syntax/exprs/
boundary.rs

1//! Implements _boundaries_. The analogues in the regex world are
2//! [word boundaries](https://www.regular-expressions.info/wordboundaries.html) and
3//! [anchors](https://www.regular-expressions.info/anchors.html).
4
5use crate::Span;
6
7/// A [word boundary](https://www.regular-expressions.info/wordboundaries.html) or
8/// [anchor](https://www.regular-expressions.info/anchors.html), which we combine under the term
9/// _boundary_.
10///
11/// All boundaries use a variation of the `%` sigil, so they are easy to
12/// remember.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
15pub struct Boundary {
16    pub kind: BoundaryKind,
17    pub unicode_aware: bool,
18    pub span: Span,
19}
20
21impl Boundary {
22    pub fn new(kind: BoundaryKind, unicode_aware: bool, span: Span) -> Self {
23        Boundary { kind, unicode_aware, span }
24    }
25
26    pub fn kind(&self) -> BoundaryKind {
27        self.kind
28    }
29
30    #[cfg(feature = "dbg")]
31    pub(super) fn pretty_print(&self, buf: &mut crate::PrettyPrinter) {
32        match self.kind {
33            BoundaryKind::Start => buf.push('^'),
34            BoundaryKind::End => buf.push('$'),
35            BoundaryKind::Word => buf.push('%'),
36            BoundaryKind::NotWord => buf.push_str("!%"),
37            BoundaryKind::WordStart => buf.push_str("<"),
38            BoundaryKind::WordEnd => buf.push_str(">"),
39        }
40    }
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
45pub enum BoundaryKind {
46    /// `Start`, the start of the string (or start of line in single-line mode)
47    Start,
48    /// `End`, the end of the string (or end of line in single-line mode)
49    End,
50    /// `%`, a word boundary
51    Word,
52    /// `!%`, not a word boundary
53    NotWord,
54    /// `<` the beginning of a word
55    WordStart,
56    /// `>` the end of a word
57    WordEnd,
58}