diff --git a/lib/lrama/lexer.rb b/lib/lrama/lexer.rb index d9b53810..f725a87f 100644 --- a/lib/lrama/lexer.rb +++ b/lib/lrama/lexer.rb @@ -198,15 +198,11 @@ def lex_c_code # @rbs () -> void def lex_comment - until @scanner.eos? do - case - when @scanner.scan_until(/[\s\S]*?\*\//) - @scanner.matched.count("\n").times { newline } - return - when @scanner.scan_until(/\n/) - newline - end + unless @scanner.scan_until(/[\s\S]*?\*\//) + raise ParseError, location.generate_error_message("Unterminated comment") # steep:ignore UnknownConstant end + + @scanner.matched.count("\n").times { newline } end # @rbs () -> void diff --git a/spec/lrama/lexer_spec.rb b/spec/lrama/lexer_spec.rb index 0aa8214a..2aaaaf90 100644 --- a/spec/lrama/lexer_spec.rb +++ b/spec/lrama/lexer_spec.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "timeout" + RSpec.describe Lrama::Lexer do let(:token_class) { Lrama::Lexer::Token } @@ -429,6 +431,15 @@ expect(lexer.next_token).to be_nil end + it 'raises an error for an unterminated block comment' do + ["/* unterminated\n%%\nprogram: ;", "/* unterminated\n%%\nprogram: ;\n"].each do |text| + grammar_file = Lrama::Lexer::GrammarFile.new("comment.y", text) + lexer = Lrama::Lexer.new(grammar_file) + + expect { Timeout.timeout(1) { lexer.next_token } }.to raise_error(ParseError, /Unterminated comment/) + end + end + it 'lex a trailing space with newline' do grammar_file = Lrama::Lexer::GrammarFile.new("trailing_space.y", " \n%require") lexer = Lrama::Lexer.new(grammar_file)