diff --git a/ruby/lox.rb b/ruby/lox.rb index 851d100..9ac7357 100755 --- a/ruby/lox.rb +++ b/ruby/lox.rb @@ -117,7 +117,7 @@ module Lox scan_str(state) when number = state.scan(/\d+(\.\d+)?/) state.add_token(:NUMBER, literal: number.to_f) - when identifier = state.scan(/[a-zA-Z_]\w+/) + when identifier = state.scan(/[a-zA-Z_]\w*/) type = KEYWORDS.fetch(identifier, :IDENTIFIER) state.add_token(type) else state.scan(/./) # keep scanning diff --git a/ruby/test_lox.rb b/ruby/test_lox.rb index a3ff067..6114659 100644 --- a/ruby/test_lox.rb +++ b/ruby/test_lox.rb @@ -111,8 +111,9 @@ class TestScanner < Minitest::Test assert_equal [ Token.new(:OR, "or", nil, 1), Token.new(:IDENTIFIER, "orchid", nil, 1), + Token.new(:IDENTIFIER, "o", nil, 1), Token.new(:EOF, "", nil, 1), - ], @scanner.scan("or orchid") + ], @scanner.scan("or orchid o") end def test_block_comments