Sha256: c08982ab81afaf2742723b22ddd55e05e62a8b6ae5284f3bc26afe6128a70b51

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

# A tree structure that contains parse error messages. This can be used to
# give the user a detailed report on what went wrong during a parse. 
#
class Parslet::ErrorTree
  # The parslet that caused the error stored here. 
  attr_reader :parslet
  # All errors that were encountered when parsing part of this +parslet+. 
  attr_reader :children
    
  def initialize(parslet, *children)
    @parslet = parslet
    @children = children.compact
  end
  
  def nodes
    1 + children.inject(0) { |sum, node| sum + node.nodes }
  end
  
  def cause
    parslet.cause || "Unknown error in #{parslet.inspect}"
  end
  
  # Returns an ascii tree representation of the causes of this node and its
  # children. 
  #
  def ascii_tree
    StringIO.new.tap { |io| 
      recursive_ascii_tree(self, io, [true]) }.
      string
  end
  alias to_s ascii_tree
private
  def recursive_ascii_tree(node, stream, curved)
    append_prefix(stream, curved)
    stream.puts node.cause
    
    node.children.each do |child|
      last_child = (node.children.last == child)

      recursive_ascii_tree(child, stream, curved + [last_child])
    end
  end
  def append_prefix(stream, curved)
    curved[0..-2].each do |c|
      stream.print c ? "   " : "|  "
    end
    stream.print curved.last ? "`- " : "|- "
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
parslet-0.11.0 lib/parslet/error_tree.rb
parslet-0.10.1 lib/parslet/error_tree.rb