From ba17996bfc8eb829b9e74d63ce8d94e101762f9f Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 20 Mar 2014 23:30:24 +0100 Subject: [PATCH] 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. --- lib/oga/parser.y | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/oga/parser.y b/lib/oga/parser.y index 7f04ee6..c4b7fec 100644 --- a/lib/oga/parser.y +++ b/lib/oga/parser.y @@ -145,19 +145,31 @@ end end def on_error(type, value, stack) - name = token_to_str(type) - line_str = @lines[@line - 1] + name = token_to_str(type) + 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