block comments

FossilOrigin-Name: 458eec710cd910f7a578fe4d0e7b767dad921be34f796f3034ce730c6829e3db
private
alpha 2 years ago
parent f98866b04e
commit dcdd0981f5

@ -107,7 +107,8 @@ module Lox
when state.scan(/</) then state.add_token(:LESS) 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_EQUAL)
when state.scan(/>/) then state.add_token(:GREATER) 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(/\//) then state.add_token(:SLASH)
when state.scan(/[ \r\t]/) # ignore whitespace when state.scan(/[ \r\t]/) # ignore whitespace
when state.scan(/\n/) then state.line += 1 when state.scan(/\n/) then state.line += 1
@ -118,7 +119,7 @@ module Lox
when identifier = state.scan(/[a-zA-Z_]\w+/) when identifier = state.scan(/[a-zA-Z_]\w+/)
type = KEYWORDS.fetch(identifier, :IDENTIFIER) type = KEYWORDS.fetch(identifier, :IDENTIFIER)
state.add_token(type) state.add_token(type)
else else state.scan(/./) # keep scanning
state.errors << Error.new(line: state.line, message: "Unexpected character.") state.errors << Error.new(line: state.line, message: "Unexpected character.")
end end
end end

@ -99,4 +99,17 @@ class TestScanner < Minitest::Test
Lox::Token.new(:IDENTIFIER, "orchid", nil, 1), Lox::Token.new(:IDENTIFIER, "orchid", nil, 1),
], @scanner.scan("or orchid") ], @scanner.scan("or orchid")
end 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 end

Loading…
Cancel
Save