css_parse/feature.rs
1use bitmask_enum::bitmask;
2
3/// A set of runtime feature flags which can be enabled individually or in combination, which will change the way
4/// [Parser][crate::Parser] works.
5///
6/// To build multiple features, use the bitwise OR operator.
7///
8/// # Example
9///
10/// ```
11/// use css_lexer::Lexer;
12/// use css_parse::*;
13/// use bumpalo::Bump;
14/// let bump = Bump::default();
15/// let features = Feature::SingleLineComments | Feature::SeparateWhitespace;
16/// let source_text = "// foo";
17/// let lexer = Lexer::new_with_features(&EmptyAtomSet::ATOMS, &source_text, features.into());
18/// let mut parser = Parser::new(&bump, &source_text, lexer).with_features(features);
19/// ```
20#[bitmask(u8)]
21pub enum Feature {
22 /// This flag is forwarded to the [Lexer][css_lexer::Lexer] which, when enabled, will treat single line comments as valid
23 /// Comment tokens. If it encounters two consecutative SOLIDUS characters (`//`), it will return a
24 /// [Token][crate::Token] with [Kind::Comment][crate::Kind::Comment]. For more information about exactly what
25 /// happens here at the lexer level, consult the [crate::Feature::SingleLineComments] feature.
26 ///
27 /// This flag doesn't cause any changes in logic on the [Parser][crate::Parser]; comments will be collected in the
28 /// trivia tokens Vec as normal.
29 SingleLineComments,
30
31 /// This flag is forwarded to the [Lexer][css_lexer::Lexer] which, when enabled, will treat diffetent whitespace kinds as
32 /// descrete. For more information about exactly what happens here at the lexer level, consult the
33 /// [crate::Feature::SeparateWhitespace] feature.
34 ///
35 /// This flag doesn't cause any changes in logic on the [Parser][crate::Parser]; whitespace is typically collected in
36 /// the trivia Vec. AST nodes which call [Parser::set_skip()][crate::Parser::set_skip] to parse whitespace sensitive nodes
37 /// should be cognizant that this feature could be enabled, meaning that adjacent whitespace tokens are possible. To
38 /// counter adjacent tokens, simply parse any whitespace in a loop.
39 SeparateWhitespace,
40}
41
42impl From<Feature> for css_lexer::Feature {
43 fn from(value: Feature) -> Self {
44 let mut f = Self::none();
45 if value.contains(Feature::SingleLineComments) {
46 f |= Self::SingleLineComments
47 }
48 if value.contains(Feature::SeparateWhitespace) {
49 f |= Self::SeparateWhitespace
50 }
51 f
52 }
53}
54
55impl Default for Feature {
56 fn default() -> Self {
57 Self::none()
58 }
59}