Skip to main content

oak_java/lexer/
token_type.rs

1use oak_core::{Token, TokenType, UniversalTokenRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5pub type JavaToken = Token<JavaTokenType>;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub enum JavaTokenType {
10    Whitespace,
11    LineComment,
12    BlockComment,
13    Identifier,
14    StringLiteral,
15    CharacterLiteral,
16    IntegerLiteral,
17    FloatingPointLiteral,
18    BooleanLiteral,
19    NullLiteral,
20
21    // Keywords
22    Abstract,
23    Assert,
24    Boolean,
25    Break,
26    Byte,
27    Case,
28    Catch,
29    Char,
30    Class,
31    Const,
32    Continue,
33    Default,
34    Do,
35    Double,
36    Else,
37    Enum,
38    Extends,
39    Final,
40    Finally,
41    Float,
42    For,
43    If,
44    Goto,
45    Implements,
46    Import,
47    Instanceof,
48    Int,
49    Interface,
50    Long,
51    Native,
52    New,
53    Package,
54    Private,
55    Protected,
56    Public,
57    Record,
58    Return,
59    Short,
60    Static,
61    Strictfp,
62    Struct,
63    Super,
64    Switch,
65    Synchronized,
66    This,
67    Throw,
68    Throws,
69    Transient,
70    Try,
71    Void,
72    Volatile,
73    While,
74
75    // Operators and Delimiters
76    Plus,
77    PlusPlus,
78    PlusEquals,
79    Minus,
80    MinusMinus,
81    MinusEquals,
82    Asterisk,
83    AsteriskEquals,
84    Slash,
85    SlashEquals,
86    Percent,
87    PercentEquals,
88    Assign,
89    Equals,
90    Bang,
91    BangEquals,
92    LessThan,
93    LessThanEquals,
94    LeftShift,
95    LeftShiftEquals,
96    GreaterThan,
97    GreaterThanEquals,
98    RightShift,
99    RightShiftEquals,
100    UnsignedRightShift,
101    UnsignedRightShiftEquals,
102    Ampersand,
103    AmpersandAmpersand,
104    AmpersandEquals,
105    Pipe,
106    PipePipe,
107    PipeEquals,
108    Caret,
109    CaretEquals,
110    Tilde,
111    Question,
112    Colon,
113    Semicolon,
114    Comma,
115    Dot,
116    Ellipsis,
117    LeftParen,
118    RightParen,
119    LeftBrace,
120    RightBrace,
121    LeftBracket,
122    RightBracket,
123    At,
124    DoubleColon,
125
126    Error,
127    EndOfFile,
128}
129
130impl TokenType for JavaTokenType {
131    type Role = UniversalTokenRole;
132    const END_OF_STREAM: Self = Self::EndOfFile;
133
134    fn role(&self) -> Self::Role {
135        use UniversalTokenRole::*;
136        match self {
137            Self::Whitespace => Whitespace,
138            Self::LineComment | Self::BlockComment => Comment,
139            Self::Identifier => Name,
140            Self::StringLiteral | Self::CharacterLiteral | Self::IntegerLiteral | Self::FloatingPointLiteral | Self::BooleanLiteral | Self::NullLiteral => Literal,
141            _ if self.is_keyword() => Keyword,
142            Self::Plus
143            | Self::PlusPlus
144            | Self::PlusEquals
145            | Self::Minus
146            | Self::MinusMinus
147            | Self::MinusEquals
148            | Self::Asterisk
149            | Self::AsteriskEquals
150            | Self::Slash
151            | Self::SlashEquals
152            | Self::Percent
153            | Self::PercentEquals
154            | Self::Assign
155            | Self::Equals
156            | Self::Bang
157            | Self::BangEquals
158            | Self::LessThan
159            | Self::LessThanEquals
160            | Self::LeftShift
161            | Self::LeftShiftEquals
162            | Self::GreaterThan
163            | Self::GreaterThanEquals
164            | Self::RightShift
165            | Self::RightShiftEquals
166            | Self::UnsignedRightShift
167            | Self::UnsignedRightShiftEquals
168            | Self::Ampersand
169            | Self::AmpersandAmpersand
170            | Self::AmpersandEquals
171            | Self::Pipe
172            | Self::PipePipe
173            | Self::PipeEquals
174            | Self::Caret
175            | Self::CaretEquals
176            | Self::Tilde => Operator,
177            Self::Question | Self::Colon | Self::Semicolon | Self::Comma | Self::Dot | Self::Ellipsis | Self::LeftParen | Self::RightParen | Self::LeftBrace | Self::RightBrace | Self::LeftBracket | Self::RightBracket | Self::At | Self::DoubleColon => {
178                Punctuation
179            }
180            Self::Error => Error,
181            Self::EndOfFile => Eof,
182            _ if self.is_keyword() => Keyword,
183            _ => None,
184        }
185    }
186}
187
188impl JavaTokenType {
189    pub fn is_keyword(&self) -> bool {
190        matches!(
191            self,
192            Self::Abstract
193                | Self::Assert
194                | Self::Boolean
195                | Self::Break
196                | Self::Byte
197                | Self::Case
198                | Self::Catch
199                | Self::Char
200                | Self::Class
201                | Self::Const
202                | Self::Continue
203                | Self::Default
204                | Self::Do
205                | Self::Double
206                | Self::Else
207                | Self::Enum
208                | Self::Extends
209                | Self::Final
210                | Self::Finally
211                | Self::Float
212                | Self::For
213                | Self::If
214                | Self::Goto
215                | Self::Implements
216                | Self::Import
217                | Self::Instanceof
218                | Self::Int
219                | Self::Interface
220                | Self::Long
221                | Self::Native
222                | Self::New
223                | Self::Package
224                | Self::Private
225                | Self::Protected
226                | Self::Public
227                | Self::Record
228                | Self::Return
229                | Self::Short
230                | Self::Static
231                | Self::Strictfp
232                | Self::Struct
233                | Self::Super
234                | Self::Switch
235                | Self::Synchronized
236                | Self::This
237                | Self::Throw
238                | Self::Throws
239                | Self::Transient
240                | Self::Try
241                | Self::Void
242                | Self::Volatile
243                | Self::While
244        )
245    }
246}