Fancier error messages for the parser.
The error messages of the parser now contain surrounding lines of code instead of only the offending line of code. This should make debugging a bit easier. Line numbers are also shown for each line.
This commit is contained in:
parent
74bc11a239
commit
ba17996bfc
|
@ -146,18 +146,30 @@ end
|
|||
|
||||
def on_error(type, value, stack)
|
||||
name = token_to_str(type)
|
||||
line_str = @lines[@line - 1]
|
||||
index = @line - 1
|
||||
lines = ''
|
||||
|
||||
raise Racc::ParseError, <<-EOF.strip
|
||||
Unexpected #{name} with value #{value.inspect} on line #{@line}
|
||||
# Show up to 2 lines before and after the offending line (if they exist).
|
||||
(-2..2).each do |offset|
|
||||
line = @lines[index + offset]
|
||||
|
||||
Offending code:
|
||||
if line
|
||||
number = @line + offset
|
||||
|
||||
#{line_str}
|
||||
if offset == 0
|
||||
prefix = '=> '
|
||||
else
|
||||
prefix = ' '
|
||||
end
|
||||
|
||||
Current stack:
|
||||
lines << "#{prefix}#{number}: #{line}"
|
||||
end
|
||||
end
|
||||
|
||||
#{stack.inspect}
|
||||
raise Racc::ParseError, <<-EOF
|
||||
Unexpected #{name} with value #{value.inspect} on line #{@line}:
|
||||
|
||||
#{lines}
|
||||
EOF
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue