use oak_core::{Token, TokenType, UniversalTokenRole};
pub type SassToken = Token<SassTokenType>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum SassTokenType {
Whitespace,
Newline,
LineComment,
BlockComment,
Eof,
Error,
StringLiteral,
CharLiteral,
NumberLiteral,
FloatLiteral,
ColorLiteral,
Variable,
Identifier,
Import,
Include,
Extend,
Mixin,
Function,
Return,
If,
Else,
ElseIf,
For,
Each,
While,
Default,
Important,
Optional,
Global,
And,
Or,
Not,
EqEq,
Ne,
Le,
Ge,
Plus,
Minus,
Star,
Slash,
Percent,
Eq,
Lt,
Gt,
LeftParen,
RightParen,
LeftBrace,
RightBrace,
LeftBracket,
RightBracket,
Semicolon,
Colon,
Comma,
Dot,
Hash,
Dollar,
At,
Ampersand,
Exclamation,
Question,
Tilde,
}
impl TokenType for SassTokenType {
type Role = UniversalTokenRole;
const END_OF_STREAM: Self = Self::Eof;
fn is_ignored(&self) -> bool {
matches!(self, Self::Whitespace | Self::Newline | Self::LineComment | Self::BlockComment)
}
fn role(&self) -> Self::Role {
if self.is_keyword() {
return UniversalTokenRole::Keyword;
}
match self {
Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
Self::LineComment | Self::BlockComment => UniversalTokenRole::Comment,
Self::Eof => UniversalTokenRole::Eof,
Self::Error => UniversalTokenRole::Error,
Self::Identifier => UniversalTokenRole::Name,
Self::Variable => UniversalTokenRole::Name,
Self::StringLiteral | Self::CharLiteral | Self::NumberLiteral | Self::FloatLiteral | Self::ColorLiteral => UniversalTokenRole::Literal,
Self::LeftParen
| Self::RightParen
| Self::LeftBrace
| Self::RightBrace
| Self::LeftBracket
| Self::RightBracket
| Self::Semicolon
| Self::Colon
| Self::Comma
| Self::Dot
| Self::Hash
| Self::Dollar
| Self::At
| Self::Ampersand
| Self::Exclamation
| Self::Question
| Self::Tilde => UniversalTokenRole::Punctuation,
Self::EqEq | Self::Ne | Self::Le | Self::Ge | Self::Plus | Self::Minus | Self::Star | Self::Slash | Self::Percent | Self::Eq | Self::Lt | Self::Gt => UniversalTokenRole::Operator,
_ => UniversalTokenRole::None,
}
}
}
impl SassTokenType {
pub fn is_keyword(&self) -> bool {
matches!(
self,
Self::Import
| Self::Include
| Self::Extend
| Self::Mixin
| Self::Function
| Self::Return
| Self::If
| Self::Else
| Self::ElseIf
| Self::For
| Self::Each
| Self::While
| Self::Default
| Self::Important
| Self::Optional
| Self::Global
| Self::And
| Self::Or
| Self::Not
)
}
}