block comments
FossilOrigin-Name: 458eec710cd910f7a578fe4d0e7b767dad921be34f796f3034ce730c6829e3db
change xswqtmlztoyzzzmxomwnoxymutyytlpk
commit 2b057feda887e1ddbc456cb1459398f22c77b315
author alpha <alpha@kejadlen.dev>
date
parent wszpxpwq
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(:LESS)
         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