Skip to main content

meta_language/
link_flags.rs

1/// Tree-sitter-compatible parse status flags modeled as link metadata.
2#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
3pub struct LinkFlags {
4    bits: u8,
5}
6
7impl LinkFlags {
8    const IS_ERROR: u8 = 0b0001;
9    const HAS_ERROR: u8 = 0b0010;
10    const IS_MISSING: u8 = 0b0100;
11    const IS_EXTRA: u8 = 0b1000;
12
13    /// Clean link flags.
14    #[must_use]
15    pub const fn clean() -> Self {
16        Self { bits: 0 }
17    }
18
19    /// Flags for an error link. Like tree-sitter's `has_error`, an error
20    /// link also reports that it has an error.
21    #[must_use]
22    pub const fn error() -> Self {
23        Self {
24            bits: Self::IS_ERROR | Self::HAS_ERROR,
25        }
26    }
27
28    /// Flags for a link that contains an error below it.
29    #[must_use]
30    pub const fn containing_error() -> Self {
31        Self {
32            bits: Self::HAS_ERROR,
33        }
34    }
35
36    /// Flags for a missing link, which also reports that it has an error.
37    #[must_use]
38    pub const fn missing() -> Self {
39        Self {
40            bits: Self::IS_MISSING | Self::HAS_ERROR,
41        }
42    }
43
44    /// Flags for an extra/trivia link.
45    #[must_use]
46    pub const fn extra() -> Self {
47        Self {
48            bits: Self::IS_EXTRA,
49        }
50    }
51
52    /// Returns flags with the error and has-error bits enabled.
53    #[must_use]
54    pub const fn with_error(mut self) -> Self {
55        self.bits |= Self::IS_ERROR | Self::HAS_ERROR;
56        self
57    }
58
59    /// Returns flags with the containing-error bit enabled.
60    #[must_use]
61    pub const fn with_containing_error(mut self) -> Self {
62        self.bits |= Self::HAS_ERROR;
63        self
64    }
65
66    /// Returns flags with the missing and has-error bits enabled.
67    #[must_use]
68    pub const fn with_missing(mut self) -> Self {
69        self.bits |= Self::IS_MISSING | Self::HAS_ERROR;
70        self
71    }
72
73    /// Returns flags with the extra/trivia bit enabled.
74    #[must_use]
75    pub const fn with_extra(mut self) -> Self {
76        self.bits |= Self::IS_EXTRA;
77        self
78    }
79
80    /// Whether this link is an error link.
81    #[must_use]
82    pub const fn is_error(self) -> bool {
83        self.bits & Self::IS_ERROR != 0
84    }
85
86    /// Whether this link is, or contains, an error or missing link
87    /// (tree-sitter `has_error` semantics).
88    #[must_use]
89    pub const fn has_error(self) -> bool {
90        self.bits & Self::HAS_ERROR != 0
91    }
92
93    /// Whether this link is missing from the source text.
94    #[must_use]
95    pub const fn is_missing(self) -> bool {
96        self.bits & Self::IS_MISSING != 0
97    }
98
99    /// Whether this link is extra source trivia.
100    #[must_use]
101    pub const fn is_extra(self) -> bool {
102        self.bits & Self::IS_EXTRA != 0
103    }
104}