Skip to main content

oak_css/lexer/
token_type.rs

1use oak_core::{TokenType, UniversalTokenRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5/// CSS token type
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8pub enum CssTokenType {
9    /// Whitespace
10    Whitespace,
11    /// Newline
12    Newline,
13    /// Comment
14    Comment,
15
16    /// String literal
17    StringLiteral,
18    /// Number literal
19    NumberLiteral,
20    /// Color literal
21    ColorLiteral,
22    /// Url literal
23    UrlLiteral,
24
25    /// Identifier
26    Identifier,
27    /// Class name
28    ClassName,
29    /// Id selector
30    IdSelector,
31    /// Tag name
32    TagName,
33    /// Pseudo class
34    PseudoClass,
35    /// Pseudo element
36    PseudoElement,
37    /// Attribute name
38    AttributeName,
39    /// Property name
40    PropertyName,
41    /// Function name
42    FunctionName,
43
44    /// !important
45    Important,
46    /// inherit
47    Inherit,
48    /// initial
49    Initial,
50    /// unset
51    Unset,
52    /// auto
53    Auto,
54    /// none
55    None,
56    /// normal
57    Normal,
58
59    /// px
60    Px,
61    /// em
62    Em,
63    /// rem
64    Rem,
65    /// %
66    Percent,
67    /// vh
68    Vh,
69    /// vw
70    Vw,
71    /// pt
72    Pt,
73    /// cm
74    Cm,
75    /// mm
76    Mm,
77    /// in
78    In,
79    /// pc
80    Pc,
81    /// ex
82    Ex,
83    /// ch
84    Ch,
85    /// vmin
86    Vmin,
87    /// vmax
88    Vmax,
89
90    /// :
91    Colon,
92    /// ;
93    Semicolon,
94    /// ,
95    Comma,
96    /// .
97    Dot,
98    /// #
99    Hash,
100    /// +
101    Plus,
102    /// -
103    Minus,
104    /// *
105    Star,
106    /// /
107    Slash,
108    /// =
109    Equals,
110    /// ~
111    Tilde,
112    /// |
113    Pipe,
114    /// ^
115    Caret,
116    /// $
117    Dollar,
118    /// >
119    GreaterThan,
120
121    /// (
122    LeftParen,
123    /// )
124    RightParen,
125    /// {
126    LeftBrace,
127    /// }
128    RightBrace,
129    /// [
130    LeftBracket,
131    /// ]
132    RightBracket,
133
134    /// @import
135    AtImport,
136    /// @media
137    AtMedia,
138    /// @keyframes
139    AtKeyframes,
140    /// @font-face
141    AtFontFace,
142    /// @charset
143    AtCharset,
144    /// @namespace
145    AtNamespace,
146    /// @supports
147    AtSupports,
148    /// @page
149    AtPage,
150    /// @document
151    AtDocument,
152    /// Generic @-rule
153    AtRule,
154
155    /// End of file
156    Eof,
157    /// Error
158    Error,
159}
160
161impl TokenType for CssTokenType {
162    const END_OF_STREAM: Self = Self::Eof;
163    type Role = UniversalTokenRole;
164
165    fn is_ignored(&self) -> bool {
166        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
167    }
168
169    fn is_comment(&self) -> bool {
170        matches!(self, Self::Comment)
171    }
172
173    fn is_whitespace(&self) -> bool {
174        matches!(self, Self::Whitespace | Self::Newline)
175    }
176
177    fn role(&self) -> Self::Role {
178        match self {
179            Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
180            Self::Comment => UniversalTokenRole::Comment,
181            Self::StringLiteral | Self::NumberLiteral | Self::ColorLiteral | Self::UrlLiteral => UniversalTokenRole::Literal,
182            Self::Identifier | Self::ClassName | Self::IdSelector | Self::TagName | Self::PseudoClass | Self::PseudoElement | Self::AttributeName | Self::PropertyName | Self::FunctionName => UniversalTokenRole::Name,
183            Self::Important
184            | Self::Inherit
185            | Self::Initial
186            | Self::Unset
187            | Self::Auto
188            | Self::None
189            | Self::Normal
190            | Self::AtImport
191            | Self::AtMedia
192            | Self::AtKeyframes
193            | Self::AtFontFace
194            | Self::AtCharset
195            | Self::AtNamespace
196            | Self::AtSupports
197            | Self::AtPage
198            | Self::AtDocument => UniversalTokenRole::Keyword,
199            Self::Colon | Self::Semicolon | Self::Comma | Self::Dot | Self::Hash | Self::Plus | Self::Minus | Self::Star | Self::Slash | Self::Equals | Self::Tilde | Self::Pipe | Self::Caret | Self::Dollar | Self::GreaterThan => {
200                UniversalTokenRole::Operator
201            }
202            Self::LeftParen | Self::RightParen | Self::LeftBrace | Self::RightBrace | Self::LeftBracket | Self::RightBracket => UniversalTokenRole::Punctuation,
203            Self::Eof => UniversalTokenRole::Eof,
204            Self::Error => UniversalTokenRole::Error,
205            _ => UniversalTokenRole::None,
206        }
207    }
208}