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
5 changes: 2 additions & 3 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def next_token

# @rbs () -> Integer
def column
@scanner.pos - @head
@scanner.string.byteslice(@head...@scanner.pos).length
end

# @rbs () -> Location
Expand All @@ -104,8 +104,7 @@ def lex_token
case
when @scanner.scan(/\n/)
newline
when @scanner.scan(/\s+/)
@scanner.matched.count("\n").times { newline }
when @scanner.scan(/[^\S\n]+/)
when @scanner.scan(/\/\*/)
lex_comment
when @scanner.scan(/\/\/.*(?<newline>\n)?/)
Expand Down
22 changes: 22 additions & 0 deletions spec/lrama/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,28 @@
end
end

it 'reports an unexpected token after indentation at its actual column' do
grammar_file = Lrama::Lexer::GrammarFile.new("indented.y", "%%\nprogram: \n @@@ ;")
lexer = Lrama::Lexer.new(grammar_file)

expect { loop { lexer.next_token } }.to raise_error(ParseError, <<~MSG)
indented.y:3:4: Unexpected token
3 | @@@ ;
| ^
MSG
end

it 'reports columns as character offsets' do
grammar_file = Lrama::Lexer::GrammarFile.new("multibyte.y", '"あ" @')
lexer = Lrama::Lexer.new(grammar_file)

expect { loop { lexer.next_token } }.to raise_error(ParseError, <<~MSG)
multibyte.y:1:4: Unexpected token
1 | "あ" @
| ^
MSG
end

context 'unexpected_c_code.y' do
it do
grammar_file = Lrama::Lexer::GrammarFile.new("invalid.y", "@invalid")
Expand Down