Skip to main content

oak_lua/lexer/
token_type.rs

1use oak_core::{Token, TokenType, UniversalTokenRole};
2
3/// Token type for Lua.
4pub type LuaToken = Token<LuaTokenType>;
5
6impl TokenType for LuaTokenType {
7    type Role = UniversalTokenRole;
8    const END_OF_STREAM: Self = Self::EndOfStream;
9
10    fn is_ignored(&self) -> bool {
11        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
12    }
13
14    fn role(&self) -> Self::Role {
15        match self {
16            Self::And
17            | Self::Break
18            | Self::Do
19            | Self::Else
20            | Self::Elseif
21            | Self::End
22            | Self::False
23            | Self::For
24            | Self::Function
25            | Self::Goto
26            | Self::If
27            | Self::In
28            | Self::Local
29            | Self::Nil
30            | Self::Not
31            | Self::Or
32            | Self::Repeat
33            | Self::Return
34            | Self::Then
35            | Self::True
36            | Self::Until
37            | Self::While => UniversalTokenRole::Keyword,
38
39            Self::Identifier => UniversalTokenRole::Name,
40            Self::Number => UniversalTokenRole::Literal,
41            Self::String => UniversalTokenRole::Literal,
42
43            Self::Plus
44            | Self::Minus
45            | Self::Star
46            | Self::Slash
47            | Self::Percent
48            | Self::Caret
49            | Self::Hash
50            | Self::Ampersand
51            | Self::Tilde
52            | Self::Pipe
53            | Self::LtLt
54            | Self::GtGt
55            | Self::SlashSlash
56            | Self::EqEq
57            | Self::TildeEq
58            | Self::LtEq
59            | Self::GtEq
60            | Self::Lt
61            | Self::Gt
62            | Self::Eq
63            | Self::DotDot
64            | Self::DotDotDot => UniversalTokenRole::Operator,
65
66            Self::LeftParen | Self::RightParen | Self::LeftBrace | Self::RightBrace | Self::LeftBracket | Self::RightBracket | Self::ColonColon | Self::Semicolon | Self::Colon | Self::Comma | Self::Dot => UniversalTokenRole::Punctuation,
67
68            Self::Comment => UniversalTokenRole::Comment,
69            _ => UniversalTokenRole::None,
70        }
71    }
72}
73
74/// Token types for Lua.
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
76#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
77#[repr(u16)]
78pub enum LuaTokenType {
79    /// Root node.
80    Root,
81    // Keywords
82    /// The `and` keyword.
83    And,
84    /// The `break` keyword.
85    Break,
86    /// The `do` keyword.
87    Do,
88    /// The `else` keyword.
89    Else,
90    /// The `elseif` keyword.
91    Elseif,
92    /// The `end` keyword.
93    End,
94    /// The `false` keyword.
95    False,
96    /// The `for` keyword.
97    For,
98    /// The `function` keyword.
99    Function,
100    /// The `goto` keyword.
101    Goto,
102    /// The `if` keyword.
103    If,
104    /// The `in` keyword.
105    In,
106    /// The `local` keyword.
107    Local,
108    /// The `nil` keyword.
109    Nil,
110    /// The `not` keyword.
111    Not,
112    /// The `or` keyword.
113    Or,
114    /// The `repeat` keyword.
115    Repeat,
116    /// The `return` keyword.
117    Return,
118    /// The `then` keyword.
119    Then,
120    /// The `true` keyword.
121    True,
122    /// The `until` keyword.
123    Until,
124    /// The `while` keyword.
125    While,
126
127    // Identifiers and literals
128    /// An identifier.
129    Identifier,
130    /// A numeric literal.
131    Number,
132    /// A string literal.
133    String,
134
135    // Operators
136    /// The `+` operator.
137    Plus,
138    /// The `-` operator.
139    Minus,
140    /// The `*` operator.
141    Star,
142    /// The `/` operator.
143    Slash,
144    /// The `%` operator.
145    Percent,
146    /// The `^` operator.
147    Caret,
148    /// The `#` operator.
149    Hash,
150    /// The `&` operator.
151    Ampersand,
152    /// The `~` operator.
153    Tilde,
154    /// The `|` operator.
155    Pipe,
156    /// The `<<` operator.
157    LtLt,
158    /// The `>>` operator.
159    GtGt,
160    /// The `//` operator.
161    SlashSlash,
162    /// The `==` operator.
163    EqEq,
164    /// The `~=` operator.
165    TildeEq,
166    /// The `<=` operator.
167    LtEq,
168    /// The `>=` operator.
169    GtEq,
170    /// The `<` operator.
171    Lt,
172    /// The `>` operator.
173    Gt,
174    /// The `=` operator.
175    Eq,
176
177    // Delimiters
178    /// The `(` punctuation.
179    LeftParen,
180    /// The `)` punctuation.
181    RightParen,
182    /// The `{` punctuation.
183    LeftBrace,
184    /// The `}` punctuation.
185    RightBrace,
186    /// The `[` punctuation.
187    LeftBracket,
188    /// The `]` punctuation.
189    RightBracket,
190    /// The `::` punctuation.
191    ColonColon,
192    /// The `;` punctuation.
193    Semicolon,
194    /// The `:` punctuation.
195    Colon,
196    /// The `,` punctuation.
197    Comma,
198    /// The `.` punctuation.
199    Dot,
200    /// The `..` punctuation.
201    DotDot,
202    /// The `...` punctuation.
203    DotDotDot,
204
205    // Whitespace and comments
206    /// Whitespace.
207    Whitespace,
208    /// Newline.
209    Newline,
210    /// A comment.
211    Comment,
212
213    // Special markers
214    /// End of stream marker.
215    EndOfStream,
216    /// Error marker.
217    Error,
218}