From 3307e2f4d23ec4870118bc8b0259d291054cfaf2 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 25 Sep 2014 22:58:06 +0200 Subject: [PATCH] Remove source lines from parser error messages. This was a gimmick in the first place. It doesn't work well with IO instances (= requires re-reading of the input), the code was too complex and it wasn't that useful in the end. Lets just get rid of it. This fixes #53. --- lib/oga/xml/parser.y | 39 +++---------------------------- spec/oga/xml/parser/error_spec.rb | 28 ++++------------------ 2 files changed, 7 insertions(+), 60 deletions(-) diff --git a/lib/oga/xml/parser.y b/lib/oga/xml/parser.y index b034909..d946988 100644 --- a/lib/oga/xml/parser.y +++ b/lib/oga/xml/parser.y @@ -265,43 +265,10 @@ end # @raise [Racc::ParseError] # def on_error(type, value, stack) - name = token_to_str(type) - name = TOKEN_ERROR_MAPPING[name] || name - index = @line - 1 - index_range = (index - 5)..(index + 5) - code = '' + name = token_to_str(type) + name = TOKEN_ERROR_MAPPING[name] || name - # For IO we sadly have to re-read the input :< - if @data.respond_to?(:rewind) - @data.rewind - end - - # Show up to 5 lines before and after the offending line (if they exist). - @data.each_line.with_index do |line, line_index| - next unless index_range.cover?(line_index) - - number = line_index + 1 - - if line_index == index - prefix = '=> ' - else - prefix = ' ' - end - - line = line.strip - - if line.length > 80 - line = line[0..79] + ' (more)' - end - - code << "#{prefix}#{number}: #{line}\n" - end - - raise Racc::ParseError, <<-EOF.strip -Unexpected #{name} on line #{@line}: - -#{code} - EOF + raise Racc::ParseError, "Unexpected #{name} on line #{@line}" end ## diff --git a/spec/oga/xml/parser/error_spec.rb b/spec/oga/xml/parser/error_spec.rb index 4fe557a..adb00f2 100644 --- a/spec/oga/xml/parser/error_spec.rb +++ b/spec/oga/xml/parser/error_spec.rb @@ -16,32 +16,12 @@ describe Oga::XML::Parser do expect { parse(@invalid_xml) }.to raise_error(Racc::ParseError) end - example 'include the offending input when using String as input' do - # Racc basically reports errors at the last moment instead of where they - # *actually* occur. - partial = <<-EOF.strip - 1. - 2. Alice - 3. 25 - 4. Dutch -=> 5. - EOF - - parse_error(@invalid_xml).should =~ /#{partial}/ + example 'include the line number when using a String as input' do + parse_error(@invalid_xml).should =~ /on line 5/ end - example 'include the offending input when using IO as input' do - # Racc basically reports errors at the last moment instead of where they - # *actually* occur. - partial = <<-EOF.strip - 1. - 2. Alice - 3. 25 - 4. Dutch -=> 5. - EOF - - parse_error(StringIO.new(@invalid_xml)).should =~ /#{partial}/ + example 'include the line number when using an IO as input' do + parse_error(StringIO.new(@invalid_xml)).should =~ /on line 5/ end example 'use more friendly error messages when available' do