Skip to main content

oak_bash/lexer/
token_type.rs

1use oak_core::{TokenType, UniversalTokenRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7#[repr(u16)]
8/// Represents all possible token kinds in the Bash shell scripting language.
9pub enum BashTokenType {
10    /// Whitespace characters (spaces, tabs)
11    Whitespace,
12    /// Newline characters
13    Newline,
14    /// Comments (starting with #)
15    Comment,
16    /// String literals enclosed in quotes
17    StringLiteral,
18    /// Variable references (e.g., $VAR)
19    Variable,
20    /// Numeric literals
21    NumberLiteral,
22    /// Identifiers (variable names, function names, etc.)
23    Identifier,
24    /// Bash keywords (if, then, else, etc.)
25    Keyword,
26    /// Operators (&&, ||, >, <, etc.)
27    Operator,
28    /// Delimiters (, (, ), {, }, etc.)
29    Delimiter,
30    /// Command names
31    Command,
32    /// File system paths
33    Path,
34    /// Here documents
35    Heredoc,
36    /// Glob patterns (*, ?, [])
37    GlobPattern,
38    /// Special characters with specific meaning
39    SpecialChar,
40    /// Plain text content
41    Text,
42    /// Error token
43    Error,
44    /// End of file marker
45    Eof,
46}
47
48impl TokenType for BashTokenType {
49    const END_OF_STREAM: Self = Self::Eof;
50    type Role = UniversalTokenRole;
51
52    fn is_ignored(&self) -> bool {
53        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
54    }
55
56    fn is_comment(&self) -> bool {
57        matches!(self, Self::Comment)
58    }
59
60    fn is_whitespace(&self) -> bool {
61        matches!(self, Self::Whitespace | Self::Newline)
62    }
63
64    fn role(&self) -> Self::Role {
65        match self {
66            Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
67            Self::Comment => UniversalTokenRole::Comment,
68            Self::Keyword => UniversalTokenRole::Keyword,
69            Self::Identifier | Self::Variable | Self::Command => UniversalTokenRole::Name,
70            Self::StringLiteral | Self::NumberLiteral => UniversalTokenRole::Literal,
71            Self::Operator => UniversalTokenRole::Operator,
72            Self::Delimiter | Self::SpecialChar => UniversalTokenRole::Punctuation,
73            Self::Eof => UniversalTokenRole::Eof,
74            Self::Error => UniversalTokenRole::Error,
75            _ => UniversalTokenRole::None,
76        }
77    }
78}