Skip to main content

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