Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions spec/lrama/lexer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "timeout"

RSpec.describe Lrama::Lexer do
let(:token_class) { Lrama::Lexer::Token }

Expand Down Expand Up @@ -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)
Expand Down