Skip to content
Open
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
23 changes: 23 additions & 0 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ def lex_c_code

until @scanner.eos? do
case
when @scanner.check(/\/\*/)
start_line = @line
start_column = column

@scanner.scan(/\/\*/)
comment_start = @scanner.matched

comment_location = Location.new(
grammar_file: @grammar_file,
first_line: start_line, first_column: start_column,
last_line: @line, last_column: column
)

comment_body = @scanner.scan_until(/\*\//)

if comment_body
chunk = comment_start + comment_body
code += chunk
chunk.count("\n").times { newline }
else
raise ParseError, comment_location.generate_error_message('Missing ‘*/’ at end of file') # steep:ignore
end
when @scanner.scan(/{/)
code += @scanner.matched
nested += 1
Expand Down Expand Up @@ -202,6 +224,7 @@ def lex_comment
newline
end
end
raise "Unterminated comment."
end

# @rbs () -> void
Expand Down
14 changes: 14 additions & 0 deletions spec/lrama/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,20 @@
end
end

context 'missing_comment_end.y' do
it do
grammar_file = Lrama::Lexer::GrammarFile.new("invalid.y", "/* missing")
lexer = Lrama::Lexer.new(grammar_file)
lexer.status = :c_declaration

expect { lexer.next_token }.to raise_error(ParseError, <<~ERROR)
invalid.y:1:0: Missing ‘*/’ at end of file
/* missing
^^
ERROR
end
end

it 'lex a line comment without newline' do
grammar_file = Lrama::Lexer::GrammarFile.new("comment.y", "// foo")
lexer = Lrama::Lexer.new(grammar_file)
Expand Down