Skip to main content

oak_cmd/lexer/
token_type.rs

1use oak_core::UniversalTokenRole;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[repr(u16)]
6/// Represents all possible token kinds in the Windows Command (CMD) scripting language.
7pub enum CmdTokenType {
8    /// Whitespace characters
9    Whitespace,
10    /// Newline characters
11    Newline,
12    /// Comments (starting with REM or ::)
13    Comment,
14    /// String literals
15    StringLiteral,
16    /// Variable references (e.g., %VAR%)
17    Variable,
18    /// Numeric literals
19    NumberLiteral,
20    /// Identifiers
21    Identifier,
22    /// CMD keywords (IF, FOR, SET, etc.)
23    Keyword,
24    /// Operators (==, EQU, NEQ, etc.)
25    Operator,
26    /// Delimiters
27    Delimiter,
28    /// Command names
29    Command,
30    /// Labels (starting with :)
31    Label,
32    /// Plain text content
33    Text,
34    /// Error token
35    Error,
36    /// End of file marker
37    Eof,
38}
39
40impl oak_core::TokenType for CmdTokenType {
41    const END_OF_STREAM: Self = Self::Eof;
42    type Role = UniversalTokenRole;
43
44    fn is_ignored(&self) -> bool {
45        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
46    }
47
48    fn is_comment(&self) -> bool {
49        matches!(self, Self::Comment)
50    }
51
52    fn is_whitespace(&self) -> bool {
53        matches!(self, Self::Whitespace | Self::Newline)
54    }
55
56    fn role(&self) -> Self::Role {
57        match self {
58            Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
59            Self::Comment => UniversalTokenRole::Comment,
60            Self::Keyword => UniversalTokenRole::Keyword,
61            Self::Identifier | Self::Variable | Self::Command | Self::Label => UniversalTokenRole::Name,
62            Self::StringLiteral | Self::NumberLiteral => UniversalTokenRole::Literal,
63            Self::Operator => UniversalTokenRole::Operator,
64            Self::Delimiter => UniversalTokenRole::Punctuation,
65            Self::Eof => UniversalTokenRole::Eof,
66            Self::Error => UniversalTokenRole::Error,
67            _ => UniversalTokenRole::None,
68        }
69    }
70}