css_lexer/
kindset.rs

1use crate::Kind;
2
3/// Match a token against one or more [Kinds][Kind].
4///
5/// Each [Kind] represents the token "type". [KindSet] is a bitmask of all possible [Kinds][Kind]. This is useful for
6/// efficiently comparing a token to see if it matches N token [Kinds][Kind].
7///
8/// # Example
9///
10/// ```
11/// use css_lexer::*;
12/// let mut lexer = Lexer::new(&EmptyAtomSet::ATOMS, "width: 1px");
13/// // The first token is either an AtKeyword, Ident or Function:
14/// assert_eq!(lexer.advance(), KindSet::new(&[Kind::AtKeyword, Kind::Ident, Kind::Function]));
15/// ```
16#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct KindSet(u32);
18
19impl KindSet {
20	/// A [KindSet] that matches no [Kinds][Kind].
21	pub const NONE: KindSet = KindSet::new(&[]);
22
23	/// A [KindSet] that matches all trivia; [Kind::Whitespace] and [Kind::Comment].
24	pub const TRIVIA: KindSet = KindSet::new(&[Kind::Whitespace, Kind::Comment]);
25
26	/// A [KindSet] that matches just Whitespace. This is the same as [Kind::Whitespace] but can be useful to apply to
27	/// functions that expect a [KindSet] rather than [Kind].
28	pub const WHITESPACE: KindSet = KindSet::new(&[Kind::Whitespace]);
29
30	/// A [KindSet] that matches just Whitespace. This is the same as [Kind::Comment] but can be useful to apply to
31	/// functions that expect a [KindSet] rather than [Kind].
32	pub const COMMENTS: KindSet = KindSet::new(&[Kind::Comment]);
33
34	/// A [KindSet] that matches either [Kind::RightCurly] or [Kind::Semicolon]. This is useful for matching
35	/// stop-tokens, for example checking the end of a declaration.
36	pub const RIGHT_CURLY_OR_SEMICOLON: KindSet = KindSet::new(&[Kind::RightCurly, Kind::Semicolon]);
37
38	/// A [KindSet] that matches either [Kind::LeftCurly] or [Kind::Semicolon]. This is useful for matching
39	/// stop-tokens, for example checking the end of an at-rule prelude.
40	pub const LEFT_CURLY_OR_SEMICOLON: KindSet = KindSet::new(&[Kind::LeftCurly, Kind::Semicolon]);
41
42	/// A [KindSet] that matches either [Kind::LeftCurly] or [Kind::RightParen] or [Kind::Semicolon]. This is useful for
43	/// matching stop-tokens, for example checking the end of a function.
44	pub const LEFT_CURLY_RIGHT_PAREN_OR_SEMICOLON: KindSet =
45		KindSet::new(&[Kind::LeftCurly, Kind::RightParen, Kind::Semicolon]);
46
47	/// A [KindSet] that matches either [Kind::LeftCurly] or [Kind::RightParen] or [Kind::Comma] or [Kind::Semicolon].
48	/// This is useful for matching stop-tokens, for example checking the end of a function or Selector.
49	pub const LEFT_CURLY_RIGHT_PAREN_COMMA_OR_SEMICOLON: KindSet =
50		KindSet::new(&[Kind::LeftCurly, Kind::RightParen, Kind::Comma, Kind::Semicolon]);
51
52	/// A [KindSet] that matches either [Kind::Ident], [Kind::AtKeyword], [Kind::Function], [Kind::Hash].
53	pub const IDENT_LIKE: KindSet = KindSet::new(&[Kind::Ident, Kind::AtKeyword, Kind::Function, Kind::Hash]);
54
55	/// A [KindSet] that matches any single character token, such as [Kind::Delim] or [Kind::Colon] - [Kind::RightCurly].
56	pub const DELIM_LIKE: KindSet = KindSet::new(&[
57		Kind::Delim,
58		Kind::Colon,
59		Kind::Semicolon,
60		Kind::Comma,
61		Kind::LeftSquare,
62		Kind::RightSquare,
63		Kind::LeftParen,
64		Kind::RightParen,
65		Kind::LeftCurly,
66		Kind::RightCurly,
67	]);
68
69	/// A [KindSet] that matches _any_ token.
70	pub const ANY: KindSet = KindSet(u32::MAX);
71
72	/// Creates a new [KindSet] with the combination of all given [Kinds][Kind].
73	///
74	/// This function is marked `const` to allow creation of const [KindSets][KindSet].
75	pub const fn new(kinds: &[Kind]) -> Self {
76		let mut u = 0;
77		let mut i = 0;
78		let len = kinds.len();
79		while i < len {
80			u |= 1 << (kinds[i] as u8 % 32);
81			i += 1;
82		}
83		Self(u)
84	}
85
86	/// Returns a new [KindSet] with the addition of the supplied [Kind].
87	///
88	/// This function is marked `const` to allow creation of const [KindSets][KindSet].
89	pub const fn add(&self, kind: Kind) -> Self {
90		Self(self.0 | (1 << (kind as u8 % 32)))
91	}
92
93	/// Check if a [KindSet] contains the subpplied [Kind].
94	pub fn contains(&self, kind: Kind) -> bool {
95		self.0 & (1 << (kind as u8 % 32)) != 0
96	}
97
98	pub(crate) const fn contains_bits(&self, kind_bits: u8) -> bool {
99		self.0 & (1 << (kind_bits % 32)) != 0
100	}
101}
102
103#[test]
104fn test_kindset_contains() {
105	let set = KindSet::new(&[Kind::Eof, Kind::Whitespace, Kind::Comment]);
106	assert!(set.contains(Kind::Eof));
107	assert!(set.contains(Kind::Whitespace));
108	assert!(set.contains(Kind::Comment));
109	assert!(!set.contains(Kind::String));
110	assert!(!set.contains(Kind::Url));
111}