11use huff_neo_utils:: prelude:: * ;
2+ use lazy_static:: lazy_static;
23use regex:: Regex ;
4+ use std:: collections:: HashMap ;
35use std:: {
46 iter:: { Peekable , Zip } ,
57 ops:: RangeFrom ,
68 str:: Chars ,
79} ;
810
11+ lazy_static ! {
12+ static ref TOKEN : HashMap <String , TokenKind > = HashMap :: from_iter( vec![
13+ ( TokenKind :: Macro . to_string( ) , TokenKind :: Macro ) ,
14+ ( TokenKind :: Fn . to_string( ) , TokenKind :: Fn ) ,
15+ ( TokenKind :: Test . to_string( ) , TokenKind :: Test ) ,
16+ ( TokenKind :: Function . to_string( ) , TokenKind :: Function ) ,
17+ ( TokenKind :: Constant . to_string( ) , TokenKind :: Constant ) ,
18+ ( TokenKind :: Error . to_string( ) , TokenKind :: Error ) ,
19+ ( TokenKind :: Takes . to_string( ) , TokenKind :: Takes ) ,
20+ ( TokenKind :: Returns . to_string( ) , TokenKind :: Returns ) ,
21+ ( TokenKind :: Event . to_string( ) , TokenKind :: Event ) ,
22+ ( TokenKind :: NonPayable . to_string( ) , TokenKind :: NonPayable ) ,
23+ ( TokenKind :: Payable . to_string( ) , TokenKind :: Payable ) ,
24+ ( TokenKind :: Indexed . to_string( ) , TokenKind :: Indexed ) ,
25+ ( TokenKind :: View . to_string( ) , TokenKind :: View ) ,
26+ ( TokenKind :: Pure . to_string( ) , TokenKind :: Pure ) ,
27+ // First check for packed jump table
28+ ( TokenKind :: JumpTablePacked . to_string( ) , TokenKind :: JumpTablePacked ) ,
29+ // Match with jump table if not
30+ ( TokenKind :: JumpTable . to_string( ) , TokenKind :: JumpTable ) ,
31+ ( TokenKind :: CodeTable . to_string( ) , TokenKind :: CodeTable ) ,
32+ ] ) ;
33+ }
34+
935/// Defines a context in which the lexing happens.
1036/// Allows to differentiate between EVM types and opcodes that can either
1137/// be identical or the latter being a substring of the former (example : bytes32 and byte)
@@ -168,37 +194,9 @@ impl<'a> Lexer<'a> {
168194 let ( word, start, mut end) = self . eat_while ( Some ( ch) , |c| c. is_alphanumeric ( ) || c == '_' ) ;
169195
170196 let mut found_kind: Option < TokenKind > = None ;
171- let keys = [
172- TokenKind :: Macro ,
173- TokenKind :: Fn ,
174- TokenKind :: Test ,
175- TokenKind :: Function ,
176- TokenKind :: Constant ,
177- TokenKind :: Error ,
178- TokenKind :: Takes ,
179- TokenKind :: Returns ,
180- TokenKind :: Event ,
181- TokenKind :: NonPayable ,
182- TokenKind :: Payable ,
183- TokenKind :: Indexed ,
184- TokenKind :: View ,
185- TokenKind :: Pure ,
186- // First check for packed jump table
187- TokenKind :: JumpTablePacked ,
188- // Match with jump table if not
189- TokenKind :: JumpTable ,
190- TokenKind :: CodeTable ,
191- ] ;
192- for kind in keys. into_iter ( ) {
193- if self . context == Context :: MacroBody {
194- break ;
195- }
196- let key = kind. to_string ( ) ;
197- let peeked = word. clone ( ) ;
198-
199- if key == peeked {
200- found_kind = Some ( kind) ;
201- break ;
197+ if self . context != Context :: MacroBody {
198+ if let Some ( kind) = TOKEN . get ( & word) {
199+ found_kind = Some ( kind. clone ( ) ) ;
202200 }
203201 }
204202
@@ -209,6 +207,7 @@ impl<'a> Lexer<'a> {
209207 found_kind = None ;
210208 }
211209
210+ // Set the context based on the found token kind
212211 if let Some ( kind) = & found_kind {
213212 match kind {
214213 TokenKind :: Macro | TokenKind :: Fn | TokenKind :: Test => self . context = Context :: MacroDefinition ,
@@ -437,10 +436,9 @@ impl<'a> Lexer<'a> {
437436 let ( integer_str, start, end) = self . eat_while ( Some ( initial_char) , |ch| ch. is_ascii_digit ( ) ) ;
438437
439438 let integer = integer_str. parse ( ) . unwrap ( ) ;
440-
441439 let integer_token = TokenKind :: Num ( integer) ;
442- let span = self . source . relative_span_by_pos ( start , end ) ;
443- Ok ( Token { kind : integer_token, span } )
440+
441+ Ok ( Token { kind : integer_token, span : self . source . relative_span_by_pos ( start , end ) } )
444442 }
445443
446444 fn eat_hex_digit ( & mut self , initial_char : char ) -> TokenResult {
@@ -461,8 +459,8 @@ impl<'a> Lexer<'a> {
461459 } ;
462460
463461 start += 2 ;
464- let span = self . source . relative_span_by_pos ( start , end ) ;
465- Ok ( Token { kind, span } )
462+
463+ Ok ( Token { kind, span : self . source . relative_span_by_pos ( start , end ) } )
466464 }
467465
468466 /// Skips white space. They are not significant in the source language
0 commit comments