From 23f2e7fb77318a38779cbd49bb48b75a357cc214 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Sat, 15 Jun 2019 00:15:37 +0200 Subject: [PATCH] Fix lexing hexadecimal numbers containing "e" These numbers would incorrectly be lexed as a float. --- compiler/lib/inkoc/lexer.rb | 14 ++++++++++---- compiler/spec/inkoc/lexer_spec.rb | 8 ++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/compiler/lib/inkoc/lexer.rb b/compiler/lib/inkoc/lexer.rb index 9a4ab27b9..696396651 100644 --- a/compiler/lib/inkoc/lexer.rb +++ b/compiler/lib/inkoc/lexer.rb @@ -279,6 +279,9 @@ def number(skip_first: false) @position += 1 if skip_first + next_char = @input[@position + 1] + is_hex = @input[@position] == '0' && (next_char == 'x' || next_char == 'X') + loop do case @input[@position] when '.' @@ -290,10 +293,13 @@ def number(skip_first: false) @position += 1 when 'e', 'E' - next_char = @input[@position + 1] - type = :float - - @position += next_char == '+' ? 2 : 1 + if is_hex + @position += 1 + else + type = :float + next_char = @input[@position + 1] + @position += next_char == '+' ? 2 : 1 + end when NUMBER_RANGE, *NUMBER_ALLOWED_LETTERS @position += 1 else diff --git a/compiler/spec/inkoc/lexer_spec.rb b/compiler/spec/inkoc/lexer_spec.rb index 567b38c80..082305b6b 100644 --- a/compiler/spec/inkoc/lexer_spec.rb +++ b/compiler/spec/inkoc/lexer_spec.rb @@ -291,6 +291,14 @@ expect(token.type).to eq(:float) expect(token.value).to eq('1e+2') end + + it 'tokenizes a hexadecimal integer containing the letter "e"' do + lexer = described_class.new('0x1e2') + token = lexer.number + + expect(token.type).to eq(:integer) + expect(token.value).to eq('0x1e2') + end end describe '#curly_open' do