1use crate::file::file_source::FileSource;
2use crate::file::span::Span;
3use crate::{opcodes::Opcode, types::PrimitiveEVMType};
4use std::fmt;
5use std::sync::Arc;
6
7pub type Literal = [u8; 32];
9
10#[derive(Debug, PartialEq, Eq, Clone)]
12pub struct Token {
13 pub kind: TokenKind,
15 pub span: Span,
17}
18
19impl Token {
20 pub fn new(kind: TokenKind, span: Span) -> Self {
22 Self { kind, span }
23 }
24}
25
26#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
28pub enum TokenKind {
29 HexLiteral(String),
31 Eof,
33 Comment(String),
35 Div,
38 Define,
40 Include,
42 Macro,
44 Fn,
46 Test,
48 Function,
50 Event,
52 Constant,
54 Error,
56 Takes,
58 Returns,
60 View,
62 Pure,
64 Payable,
66 NonPayable,
68 Indexed,
70 FreeStoragePointer,
72 Noop,
74 Ident(String),
76 Assign,
78 OpenParen,
80 CloseParen,
82 OpenBracket,
84 CloseBracket,
86 OpenBrace,
88 CloseBrace,
90 LeftAngle,
92 RightAngle,
94 Add,
96 Sub,
98 Mul,
100 Mod,
102 EqualEqual,
104 NotEqual,
106 LessEqual,
108 GreaterEqual,
110 Not,
112 Comma,
114 Colon,
116 Pound,
118 Integer(usize),
120 Whitespace,
122 Str(String),
124 Opcode(Opcode),
126 Label(String),
128 PrimitiveType(PrimitiveEVMType),
133 ArrayType(PrimitiveEVMType, Vec<usize>),
136 JumpTable,
138 JumpTablePacked,
140 CodeTable,
142 BuiltinFunction(String),
144 Calldata,
146 Memory,
148 Storage,
150 For,
152 In,
154 Step,
156 DoubleDot,
158 If,
160 Else,
162}
163
164impl TokenKind {
165 pub fn into_single_span(self, position: u32, file: Option<Arc<FileSource>>) -> Token {
167 self.into_span(position, position, file)
168 }
169
170 pub fn into_span(self, start: u32, end: u32, file: Option<Arc<FileSource>>) -> Token {
172 Token { kind: self, span: Span { start: start as usize, end: end as usize, file } }
173 }
174
175 pub fn into_token_with_span(self, span: Span) -> Token {
177 Token { kind: self, span }
178 }
179}
180
181impl fmt::Display for TokenKind {
182 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183 let x = match self {
184 TokenKind::HexLiteral(s) => return write!(f, "0x{s}"),
185 TokenKind::Eof => "EOF",
186 TokenKind::Comment(s) => return write!(f, "Comment({s})"),
187 TokenKind::Div => "/",
188 TokenKind::Define => "#define",
189 TokenKind::Include => "#include",
190 TokenKind::Macro => "macro",
191 TokenKind::Fn => "fn",
192 TokenKind::Test => "test",
193 TokenKind::Function => "function",
194 TokenKind::Event => "event",
195 TokenKind::Constant => "constant",
196 TokenKind::Error => "error",
197 TokenKind::View => "view",
198 TokenKind::Pure => "pure",
199 TokenKind::Payable => "payable",
200 TokenKind::NonPayable => "nonpayable",
201 TokenKind::Indexed => "indexed",
202 TokenKind::Takes => "takes",
203 TokenKind::Returns => "returns",
204 TokenKind::FreeStoragePointer => "FREE_STORAGE_POINTER()",
205 TokenKind::Noop => "__NOOP",
206 TokenKind::Ident(s) => return write!(f, "{s}"),
207 TokenKind::Assign => "=",
208 TokenKind::OpenParen => "(",
209 TokenKind::CloseParen => ")",
210 TokenKind::OpenBracket => "[",
211 TokenKind::CloseBracket => "]",
212 TokenKind::OpenBrace => "{",
213 TokenKind::CloseBrace => "}",
214 TokenKind::LeftAngle => "<",
215 TokenKind::RightAngle => ">",
216 TokenKind::Add => "+",
217 TokenKind::Sub => "-",
218 TokenKind::Mul => "*",
219 TokenKind::Mod => "%",
220 TokenKind::EqualEqual => "==",
221 TokenKind::NotEqual => "!=",
222 TokenKind::LessEqual => "<=",
223 TokenKind::GreaterEqual => ">=",
224 TokenKind::Not => "!",
225 TokenKind::Colon => ":",
226 TokenKind::Comma => ",",
227 TokenKind::Pound => "#",
228 TokenKind::Integer(num) => return write!(f, "{num}"),
229 TokenKind::Whitespace => " ",
230 TokenKind::Str(str) => str,
231 TokenKind::Opcode(o) => return write!(f, "{o}"),
232 TokenKind::Label(s) => return write!(f, "{s}"),
233 TokenKind::PrimitiveType(pt) => return write!(f, "{pt}"),
234 TokenKind::ArrayType(pt, size_vec) => {
235 let mut s = String::new();
236 for size in size_vec {
237 let brackets = if size > &0 { format!("[{size}]") } else { "[]".to_string() };
238 s.push_str(&brackets);
239 }
240 return write!(f, "{pt}{s}");
241 }
242 TokenKind::JumpTable => "jumptable",
243 TokenKind::JumpTablePacked => "jumptable__packed",
244 TokenKind::CodeTable => "table",
245 TokenKind::BuiltinFunction(s) => return write!(f, "BuiltinFunction({s})"),
246 TokenKind::Calldata => return write!(f, "calldata"),
247 TokenKind::Memory => return write!(f, "memory"),
248 TokenKind::Storage => return write!(f, "storage"),
249 TokenKind::For => "for",
250 TokenKind::In => "in",
251 TokenKind::Step => "step",
252 TokenKind::DoubleDot => "..",
253 TokenKind::If => "if",
254 TokenKind::Else => "else",
255 };
256
257 write!(f, "{x}")
258 }
259}