Skip to main content

oak_bash/lexer/
token_type.rs

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