From dcdd0981f576886ab84d3cbd4b0008d607b69ce4 Mon Sep 17 00:00:00 2001 From: alpha Date: Sun, 17 Jul 2022 22:28:15 +0000 Subject: [PATCH] block comments FossilOrigin-Name: 458eec710cd910f7a578fe4d0e7b767dad921be34f796f3034ce730c6829e3db --- ruby/lox.rb | 5 +++-- ruby/test_lox.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) 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