1use oak_core::{Source, Token, TokenType, UniversalElementRole, UniversalTokenRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5pub type SolidityToken = Token<SolidityTokenType>;
6
7impl TokenType for SolidityTokenType {
8 type Role = UniversalTokenRole;
9 const END_OF_STREAM: Self = Self::Eof;
10
11 fn role(&self) -> Self::Role {
12 match self {
13 Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
14 Self::LineComment | Self::BlockComment => UniversalTokenRole::Comment,
15 Self::Contract
16 | Self::Interface
17 | Self::Library
18 | Self::Function
19 | Self::Modifier
20 | Self::Event
21 | Self::Struct
22 | Self::Enum
23 | Self::Mapping
24 | Self::Public
25 | Self::Private
26 | Self::Internal
27 | Self::External
28 | Self::Pure
29 | Self::View
30 | Self::Payable
31 | Self::Constant
32 | Self::Bool
33 | Self::String
34 | Self::Bytes
35 | Self::Address
36 | Self::Uint
37 | Self::Int
38 | Self::Fixed
39 | Self::Ufixed
40 | Self::If
41 | Self::Else
42 | Self::For
43 | Self::While
44 | Self::Do
45 | Self::Break
46 | Self::Continue
47 | Self::Return
48 | Self::Try
49 | Self::Catch
50 | Self::Import
51 | Self::Pragma
52 | Self::Using
53 | Self::Is
54 | Self::Override
55 | Self::Virtual
56 | Self::Abstract => UniversalTokenRole::Keyword,
57 Self::NumberLiteral | Self::StringLiteral | Self::BooleanLiteral | Self::AddressLiteral | Self::HexLiteral => UniversalTokenRole::Literal,
58 Self::Identifier => UniversalTokenRole::Name,
59 Self::Plus
60 | Self::Minus
61 | Self::Star
62 | Self::Slash
63 | Self::Percent
64 | Self::Power
65 | Self::Equal
66 | Self::NotEqual
67 | Self::Less
68 | Self::Greater
69 | Self::LessEqual
70 | Self::GreaterEqual
71 | Self::And
72 | Self::Or
73 | Self::Not
74 | Self::BitAnd
75 | Self::BitOr
76 | Self::BitXor
77 | Self::BitNot
78 | Self::LeftShift
79 | Self::RightShift
80 | Self::Assign
81 | Self::PlusAssign
82 | Self::MinusAssign
83 | Self::StarAssign
84 | Self::SlashAssign
85 | Self::PercentAssign => UniversalTokenRole::Operator,
86 Self::LeftParen | Self::RightParen | Self::LeftBrace | Self::RightBrace | Self::LeftBracket | Self::RightBracket | Self::Semicolon | Self::Comma | Self::Dot | Self::Arrow => UniversalTokenRole::Punctuation,
87 Self::Error => UniversalTokenRole::Error,
88 Self::Eof => UniversalTokenRole::Eof,
89 _ => UniversalTokenRole::None,
90 }
91 }
92
93 fn is_ignored(&self) -> bool {
94 matches!(self, Self::Whitespace | Self::Newline | Self::LineComment | Self::BlockComment)
95 }
96
97 fn is_comment(&self) -> bool {
98 matches!(self, Self::LineComment | Self::BlockComment)
99 }
100
101 fn is_whitespace(&self) -> bool {
102 matches!(self, Self::Whitespace | Self::Newline)
103 }
104}
105
106impl SolidityTokenType {
107 pub fn is_token_type(&self) -> bool {
108 !matches!(self, Self::SourceFile)
109 }
110
111 pub fn is_element_type(&self) -> bool {
112 matches!(self, Self::SourceFile)
113 }
114}
115
116#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
117#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
118pub enum SolidityTokenType {
119 Whitespace,
121 Newline,
122
123 LineComment,
125 BlockComment,
126
127 Contract,
129 Interface,
130 Library,
131 Function,
132 Modifier,
133 Event,
134 Struct,
135 Enum,
136 Mapping,
137 Array,
138
139 Public,
141 Private,
142 Internal,
143 External,
144
145 Pure,
147 View,
148 Payable,
149 Constant,
150
151 Bool,
153 String,
154 Bytes,
155 Address,
156 Uint,
157 Int,
158 Fixed,
159 Ufixed,
160
161 If,
163 Else,
164 For,
165 While,
166 Do,
167 Break,
168 Continue,
169 Return,
170 Try,
171 Catch,
172
173 Import,
175 Pragma,
176 Using,
177 Is,
178 Override,
179 Virtual,
180 Abstract,
181
182 NumberLiteral,
184 StringLiteral,
185 BooleanLiteral,
186 AddressLiteral,
187 HexLiteral,
188
189 Identifier,
191
192 Plus,
194 Minus,
195 Star,
196 Slash,
197 Percent,
198 Power,
199 Equal,
200 NotEqual,
201 Less,
202 Greater,
203 LessEqual,
204 GreaterEqual,
205 And,
206 Or,
207 Not,
208 BitAnd,
209 BitOr,
210 BitXor,
211 BitNot,
212 LeftShift,
213 RightShift,
214 Assign,
215 PlusAssign,
216 MinusAssign,
217 StarAssign,
218 SlashAssign,
219 PercentAssign,
220
221 LeftParen,
223 RightParen,
224 LeftBrace,
225 RightBrace,
226 LeftBracket,
227 RightBracket,
228 Semicolon,
229 Comma,
230 Dot,
231 Arrow,
232
233 SourceFile,
235
236 Error,
238 Eof,
239}