diff --git a/ruby/lox.rb b/ruby/lox.rb index e1dde32..c05554e 100755 --- a/ruby/lox.rb +++ b/ruby/lox.rb @@ -107,7 +107,8 @@ module Lox when state.scan(/=/) then state.add_token(:GREATER_EQUAL) when state.scan(/>/) then state.add_token(:GREATER) - when state.scan(/\/\/(?~\n)+/) # ignore comment + when state.scan(/\/\/(?~\n)+/) # ignore line comment + when state.scan(/\/\*(?~\*\/)\*\//m) # ignore block comment when state.scan(/\//) then state.add_token(:SLASH) when state.scan(/[ \r\t]/) # ignore whitespace when state.scan(/\n/) then state.line += 1 @@ -118,7 +119,7 @@ module Lox when identifier = state.scan(/[a-zA-Z_]\w+/) type = KEYWORDS.fetch(identifier, :IDENTIFIER) state.add_token(type) - else + else state.scan(/./) # keep scanning state.errors << Error.new(line: state.line, message: "Unexpected character.") end end diff --git a/ruby/test_lox.rb b/ruby/test_lox.rb index b9aee89..d89a137 100644 --- a/ruby/test_lox.rb +++ b/ruby/test_lox.rb @@ -99,4 +99,17 @@ class TestScanner < Minitest::Test Lox::Token.new(:IDENTIFIER, "orchid", nil, 1), ], @scanner.scan("or orchid") end + + def test_block_comments + tokens = @scanner.scan(<<~SRC) + foo + /* here lies a block comment */ + bar + SRC + + assert_equal [ + Lox::Token.new(:IDENTIFIER, "foo", nil, 1), + Lox::Token.new(:IDENTIFIER, "bar", nil, 3), + ], tokens + end end