Class: ASMREPL::Lexer
- Inherits:
-
Object
- Object
- ASMREPL::Lexer
- Defined in:
- lib/asmrepl/parser.rb
Constant Summary collapse
- INTEGER =
/(?:[-+]?0b[0-1_,]+ (?# base 2) |[-+]?(?:0(?!x)|[1-9][0-9_,]*) (?# base 10) |[-+]?0x[0-9a-fA-F_,]+ (?# base 16))/x- REGISTERS =
Set.new Fisk::Registers.constants.map(&:to_s)
- INSTRUCTIONS =
Set.new Fisk::Instructions.constants.map(&:to_s)
Instance Method Summary collapse
-
#initialize(input) ⇒ Lexer
constructor
A new instance of Lexer.
- #next_token ⇒ Object
Constructor Details
#initialize(input) ⇒ Lexer
Returns a new instance of Lexer.
13 14 15 16 |
# File 'lib/asmrepl/parser.rb', line 13 def initialize input @input = input @scanner = StringScanner.new input end |
Instance Method Details
#next_token ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/asmrepl/parser.rb', line 21 def next_token return if @scanner.eos? if @scanner.scan(INTEGER) [:on_int, @scanner.matched] elsif @scanner.scan(/\[/) [:on_lbracket, @scanner.matched] elsif @scanner.scan(/\]/) [:on_rbracket, @scanner.matched] elsif @scanner.scan(/,/) [:on_comma, @scanner.matched] elsif @scanner.scan(/qword/) [:qword, @scanner.matched] elsif @scanner.scan(/dword/) [:dword, @scanner.matched] elsif @scanner.scan(/word/) [:word, @scanner.matched] elsif @scanner.scan(/byte/) [:byte, @scanner.matched] elsif @scanner.scan(/ptr/) [:ptr, @scanner.matched] elsif @scanner.scan(/rip/i) [:on_rip, @scanner.matched] elsif @scanner.scan(/int/i) [:on_instruction, Fisk::Instructions::INT] elsif @scanner.scan(/movabs/i) [:on_instruction, Fisk::Instructions::MOV] elsif @scanner.scan(/\w+/) ident = @scanner.matched if INSTRUCTIONS.include?(ident.upcase) [:on_instruction, Fisk::Instructions.const_get(ident.upcase)] elsif REGISTERS.include?(ident.upcase) [:on_register, Fisk::Registers.const_get(ident.upcase)] else [:on_ident, @scanner.matched] end elsif @scanner.scan(/\s+/) [:on_sp, @scanner.matched] elsif @scanner.scan(/\+/) [:plus, @scanner.matched] elsif @scanner.scan(/-/) [:minus, @scanner.matched] else raise end end |