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.
This commit is contained in:
Yorick Peterse 2014-09-25 22:58:06 +02:00
parent 8db77c0a09
commit 3307e2f4d2
2 changed files with 7 additions and 60 deletions

View File

@ -267,41 +267,8 @@ end
def on_error(type, value, stack) def on_error(type, value, stack)
name = token_to_str(type) name = token_to_str(type)
name = TOKEN_ERROR_MAPPING[name] || name name = TOKEN_ERROR_MAPPING[name] || name
index = @line - 1
index_range = (index - 5)..(index + 5)
code = ''
# For IO we sadly have to re-read the input :< raise Racc::ParseError, "Unexpected #{name} on line #{@line}"
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
end end
## ##

View File

@ -16,32 +16,12 @@ describe Oga::XML::Parser do
expect { parse(@invalid_xml) }.to raise_error(Racc::ParseError) expect { parse(@invalid_xml) }.to raise_error(Racc::ParseError)
end end
example 'include the offending input when using String as input' do example 'include the line number when using a String as input' do
# Racc basically reports errors at the last moment instead of where they parse_error(@invalid_xml).should =~ /on line 5/
# *actually* occur.
partial = <<-EOF.strip
1. <person>
2. <name>Alice</name>
3. <age>25
4. <nationality>Dutch</nationality>
=> 5. </person>
EOF
parse_error(@invalid_xml).should =~ /#{partial}/
end end
example 'include the offending input when using IO as input' do example 'include the line number when using an IO as input' do
# Racc basically reports errors at the last moment instead of where they parse_error(StringIO.new(@invalid_xml)).should =~ /on line 5/
# *actually* occur.
partial = <<-EOF.strip
1. <person>
2. <name>Alice</name>
3. <age>25
4. <nationality>Dutch</nationality>
=> 5. </person>
EOF
parse_error(StringIO.new(@invalid_xml)).should =~ /#{partial}/
end end
example 'use more friendly error messages when available' do example 'use more friendly error messages when available' do