1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! Contains pomsky features that can be individually enabled and disabled.

use std::fmt;

use pomsky_syntax::Span;

use crate::diagnose::{CompileError, CompileErrorKind, UnsupportedError};

/// A set of enabled pomsky features. By default, all features are enabled.
/// You can disabled specific features with
///
/// ```
/// use pomsky::features::PomskyFeatures;
///
/// let allowed_features = PomskyFeatures::default()
///     .grapheme(false)
///     .variables(false);
/// ```
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct PomskyFeatures {
    bits: u16,
}

impl fmt::Debug for PomskyFeatures {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PomskyFeatures")
            .field("grapheme", &self.supports(Self::GRAPHEME))
            .field("numbered_groups", &self.supports(Self::NUMBERED_GROUPS))
            .field("named_groups", &self.supports(Self::NAMED_GROUPS))
            .field("atomic_groups", &self.supports(Self::ATOMIC_GROUPS))
            .field("references", &self.supports(Self::REFERENCES))
            .field("lazy_mode", &self.supports(Self::LAZY_MODE))
            .field("ranges", &self.supports(Self::RANGES))
            .field("variables", &self.supports(Self::VARIABLES))
            .field("lookahead", &self.supports(Self::LOOKAHEAD))
            .field("lookbehind", &self.supports(Self::LOOKBEHIND))
            .field("boundaries", &self.supports(Self::BOUNDARIES))
            .field("regexes", &self.supports(Self::REGEXES))
            .field("dot", &self.supports(Self::DOT))
            .finish()
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for PomskyFeatures {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        let mut feat = PomskyFeatures::default();
        feat.grapheme(bool::arbitrary(u)?);
        feat.numbered_groups(bool::arbitrary(u)?);
        feat.named_groups(bool::arbitrary(u)?);
        feat.atomic_groups(bool::arbitrary(u)?);
        feat.references(bool::arbitrary(u)?);
        feat.lazy_mode(bool::arbitrary(u)?);
        feat.ranges(bool::arbitrary(u)?);
        feat.variables(bool::arbitrary(u)?);
        feat.lookahead(bool::arbitrary(u)?);
        feat.lookbehind(bool::arbitrary(u)?);
        feat.boundaries(bool::arbitrary(u)?);
        feat.regexes(bool::arbitrary(u)?);
        feat.dot(bool::arbitrary(u)?);
        Ok(feat)
    }
}

impl Default for PomskyFeatures {
    fn default() -> Self {
        Self {
            bits: Self::GRAPHEME
                | Self::NUMBERED_GROUPS
                | Self::NAMED_GROUPS
                | Self::REFERENCES
                | Self::LAZY_MODE
                | Self::RANGES
                | Self::VARIABLES
                | Self::LOOKAHEAD
                | Self::LOOKBEHIND
                | Self::BOUNDARIES
                | Self::ATOMIC_GROUPS
                | Self::REGEXES
                | Self::DOT,
        }
    }
}

impl PomskyFeatures {
    pub(crate) const GRAPHEME: u16 = 1 << 0;
    pub(crate) const NUMBERED_GROUPS: u16 = 1 << 1;
    pub(crate) const NAMED_GROUPS: u16 = 1 << 2;
    pub(crate) const REFERENCES: u16 = 1 << 3;
    pub(crate) const LAZY_MODE: u16 = 1 << 4;
    pub(crate) const RANGES: u16 = 1 << 5;
    pub(crate) const VARIABLES: u16 = 1 << 6;
    pub(crate) const LOOKAHEAD: u16 = 1 << 7;
    pub(crate) const LOOKBEHIND: u16 = 1 << 8;
    pub(crate) const BOUNDARIES: u16 = 1 << 9;
    pub(crate) const ATOMIC_GROUPS: u16 = 1 << 10;
    pub(crate) const REGEXES: u16 = 1 << 11;
    pub(crate) const DOT: u16 = 1 << 12;

    /// Creates an empty set of features. With this set, all optional features
    /// are disabled.
    ///
    /// Use `PomskyFeatures::default()` instead if you want features to be
    /// enabled by default.
    #[must_use]
    pub fn new() -> Self {
        PomskyFeatures { bits: 0 }
    }

    fn set_bit(&mut self, bit: u16, support: bool) {
        if support {
            self.bits |= bit;
        } else {
            self.bits &= bit ^ 0xFF_FF_u16;
        }
    }

    fn supports(self, bit: u16) -> bool {
        (self.bits & bit) != 0
    }

    pub(super) fn require(self, feature: u16, span: Span) -> Result<(), CompileError> {
        if self.supports(feature) {
            Ok(())
        } else {
            Err(CompileErrorKind::UnsupportedPomskySyntax(match feature {
                Self::GRAPHEME => UnsupportedError::Grapheme,
                Self::NUMBERED_GROUPS => UnsupportedError::NumberedGroups,
                Self::NAMED_GROUPS => UnsupportedError::NamedGroups,
                Self::REFERENCES => UnsupportedError::References,
                Self::LAZY_MODE => UnsupportedError::LazyMode,
                Self::RANGES => UnsupportedError::Ranges,
                Self::VARIABLES => UnsupportedError::Variables,
                Self::LOOKAHEAD => UnsupportedError::Lookahead,
                Self::LOOKBEHIND => UnsupportedError::Lookbehind,
                Self::BOUNDARIES => UnsupportedError::Boundaries,
                Self::ATOMIC_GROUPS => UnsupportedError::AtomicGroups,
                Self::REGEXES => UnsupportedError::Regexes,
                Self::DOT => UnsupportedError::Dot,
                _ => panic!("Unknown feature `0x{feature:0x}`"),
            })
            .at(span))
        }
    }

    /// Set support for `Grapheme`
    pub fn grapheme(&mut self, support: bool) -> Self {
        self.set_bit(Self::GRAPHEME, support);
        *self
    }

    /// Set support for numbered groups, e.g. `:('test')`
    pub fn numbered_groups(&mut self, support: bool) -> Self {
        self.set_bit(Self::NUMBERED_GROUPS, support);
        *self
    }

    /// Set support for named groups, e.g. `:test('!')`
    pub fn named_groups(&mut self, support: bool) -> Self {
        self.set_bit(Self::NAMED_GROUPS, support);
        *self
    }

    /// Set support for atomic groups, e.g. `atomic ('if' | 'else' | 'while' |
    /// 'for')`
    pub fn atomic_groups(&mut self, support: bool) -> Self {
        self.set_bit(Self::ATOMIC_GROUPS, support);
        *self
    }

    /// Set support for references, e.g. `::-1` or `:foo() ::foo`
    pub fn references(&mut self, support: bool) -> Self {
        self.set_bit(Self::REFERENCES, support);
        *self
    }

    /// Set support for lazy mode, i.e. `enable lazy;`
    pub fn lazy_mode(&mut self, support: bool) -> Self {
        self.set_bit(Self::LAZY_MODE, support);
        *self
    }

    /// Set support for ranges, e.g. `range '1'-'255'`
    pub fn ranges(&mut self, support: bool) -> Self {
        self.set_bit(Self::RANGES, support);
        *self
    }

    /// Set support for variables, e.g. `let x = 'hello' 'world'?;`
    pub fn variables(&mut self, support: bool) -> Self {
        self.set_bit(Self::VARIABLES, support);
        *self
    }

    /// Set support for lookahead, e.g. `>> 'test'`
    pub fn lookahead(&mut self, support: bool) -> Self {
        self.set_bit(Self::LOOKAHEAD, support);
        *self
    }

    /// Set support for lookbehind, e.g. `<< 'test'`
    pub fn lookbehind(&mut self, support: bool) -> Self {
        self.set_bit(Self::LOOKBEHIND, support);
        *self
    }

    /// Set support for boundaries, i.e. `%` and `!%`
    pub fn boundaries(&mut self, support: bool) -> Self {
        self.set_bit(Self::BOUNDARIES, support);
        *self
    }

    /// Set support for raw regular expressions, e.g. `regex
    /// '.[\p{Alpha}&&[^test]]'`
    pub fn regexes(&mut self, support: bool) -> Self {
        self.set_bit(Self::REGEXES, support);
        *self
    }

    /// Set support for the dot, i.e. `.`
    pub fn dot(&mut self, support: bool) -> Self {
        self.set_bit(Self::DOT, support);
        *self
    }
}

#[test]
fn test_toggles() {
    let features = PomskyFeatures::new()
        .grapheme(true)
        .numbered_groups(true)
        .named_groups(true)
        .atomic_groups(true)
        .references(true)
        .lazy_mode(true)
        .ranges(true)
        .variables(true)
        .lookahead(true)
        .lookbehind(true)
        .boundaries(true)
        .regexes(true)
        .dot(true);

    assert_eq!(features.bits, PomskyFeatures::default().bits);
}