Provide somewhat more meaningful parser errors.
While still a bit cryptic this is probably as best as we can get it. An example:
    Oga.parse_xml("<namefoo:bar=\"10\"")
    parser.rb:116:in `on_error': Unexpected string on line 1: (Racc::ParseError)
    => 1: <namefoo:bar="10"
This fixes #43.
			
			
This commit is contained in:
		
							parent
							
								
									32b11ef1e2
								
							
						
					
					
						commit
						cdfeeed85f
					
				| 
						 | 
				
			
			@ -193,6 +193,35 @@ rule
 | 
			
		|||
end
 | 
			
		||||
 | 
			
		||||
---- inner
 | 
			
		||||
  ##
 | 
			
		||||
  # Hash mapping token types and dedicated error labels.
 | 
			
		||||
  #
 | 
			
		||||
  # @return [Hash]
 | 
			
		||||
  #
 | 
			
		||||
  TOKEN_ERROR_MAPPING = {
 | 
			
		||||
    'T_STRING'         => 'string',
 | 
			
		||||
    'T_TEXT'           => 'text',
 | 
			
		||||
    'T_DOCTYPE_START'  => 'doctype start',
 | 
			
		||||
    'T_DOCTYPE_END'    => 'doctype closing tag',
 | 
			
		||||
    'T_DOCTYPE_TYPE'   => 'doctype type',
 | 
			
		||||
    'T_DOCTYPE_NAME'   => 'doctype name',
 | 
			
		||||
    'T_DOCTYPE_INLINE' => 'inline doctype rules',
 | 
			
		||||
    'T_CDATA'          => 'CDATA',
 | 
			
		||||
    'T_COMMENT'        => 'comment',
 | 
			
		||||
    'T_ELEM_START'     => 'element start',
 | 
			
		||||
    'T_ELEM_NAME'      => 'element name',
 | 
			
		||||
    'T_ELEM_NS'        => 'element namespace',
 | 
			
		||||
    'T_ELEM_END'       => 'element closing tag',
 | 
			
		||||
    'T_ATTR'           => 'attribute',
 | 
			
		||||
    'T_ATTR_NS'        => 'attribute namespace',
 | 
			
		||||
    'T_XML_DECL_START' => 'XML declaration start',
 | 
			
		||||
    'T_XML_DECL_END'   => 'XML declaration end',
 | 
			
		||||
    'T_PROC_INS_START' => 'processing-instruction start',
 | 
			
		||||
    'T_PROC_INS_NAME'  => 'processing-instruction name',
 | 
			
		||||
    'T_PROC_INS_END'   => 'processing-instruction closing tag',
 | 
			
		||||
    '$end'             => 'end of input'
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ##
 | 
			
		||||
  # @param [String|IO] data The input to parse.
 | 
			
		||||
  # @param [Hash] options
 | 
			
		||||
| 
						 | 
				
			
			@ -237,6 +266,7 @@ end
 | 
			
		|||
  #
 | 
			
		||||
  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        = ''
 | 
			
		||||
| 
						 | 
				
			
			@ -268,7 +298,7 @@ end
 | 
			
		|||
    end
 | 
			
		||||
 | 
			
		||||
    raise Racc::ParseError, <<-EOF.strip
 | 
			
		||||
Unexpected #{name} with value #{value.inspect} on line #{@line}:
 | 
			
		||||
Unexpected #{name} on line #{@line}:
 | 
			
		||||
 | 
			
		||||
#{code}
 | 
			
		||||
    EOF
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,5 +43,9 @@ describe Oga::XML::Parser do
 | 
			
		|||
 | 
			
		||||
      parse_error(StringIO.new(@invalid_xml)).should =~ /#{partial}/
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    example 'use more friendly error messages when available' do
 | 
			
		||||
      parse_error('</foo>').should =~ /Unexpected element closing tag/
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue