Skip to content

Commit

Permalink
Fix lexing hexadecimal numbers containing "e"
Browse files Browse the repository at this point in the history
These numbers would incorrectly be lexed as a float.
  • Loading branch information
Yorick Peterse committed Jun 14, 2019
1 parent 2c96d0d commit 23f2e7f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 10 additions & 4 deletions compiler/lib/inkoc/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.'
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions compiler/spec/inkoc/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 23f2e7f

Please sign in to comment.