class Parsby::ExpectationFailed

Constants

INDENTATION

Attributes

ctx[R]

Public Class Methods

new(ctx) click to toggle source

Initializes an ExpectationFailed from a backed_io and an optional expectation with which to start the list of expectations that lead to this failure.

# File lib/parsby.rb, line 288
def initialize(ctx)
  @ctx = ctx
end

Public Instance Methods

failure_tree() click to toggle source
# File lib/parsby.rb, line 297
def failure_tree
  @failure_tree ||= begin
    other_ranges = ctx.parsed_ranges.flatten.select do |range|
      range.start == parsed_range.start && range != parsed_range
    end
    relevant_paths = [parsed_range, *other_ranges].map(&:path)
    parsed_range.dup.root.trim_to_just!(*relevant_paths) do |c, i, max_sibling|
      c.failed && i != max_sibling && c.start != parsed_range.start
    end
  end
end
hunk_at(pos) click to toggle source
# File lib/parsby.rb, line 362
def hunk_at(pos)
  ctx.bio.with_saved_pos do
    ctx.bio.seek pos
    hunk_prelude + hunk_graph
  end
end
hunk_graph() click to toggle source
# File lib/parsby.rb, line 320
def hunk_graph
  line_range = ctx.bio.current_line_range
  line_length = ctx.bio.current_line.length
  tree_lines = []
  max_tree_slice_length = failure_tree.flatten.map {|t| t.right_tree_slice.length }.max
  prev_slice_length = nil
  failure_tree.each do |range|
    line = ""
    line << " " * INDENTATION
    line << range.underline(line_range)
    line << " " * (line_length + INDENTATION - line.length)
    this_slice_length = range.right_tree_slice.length
    # If previous slice was a parent with multiple children (current
    # slice being the first child), we'll want to draw the forking
    # line.
    if prev_slice_length && this_slice_length > prev_slice_length
      # Current line already has the correct width to start drawing the
      # tree. Copy it and substitute the rendered range with spaces.
      fork_line = line.gsub(/./, " ")
      fork_line << " "
      i = 0
      fork_line << range.right_tree_slice.rjust(max_tree_slice_length).gsub(/[*|]/) do |c|
        i += 1
        if i <= this_slice_length - prev_slice_length
          "\\"
        else
          c 
        end
      end
      fork_line << "\n"
    else
      fork_line = ""
    end
    prev_slice_length = this_slice_length
    line << " #{range.right_tree_slice.rjust(max_tree_slice_length)}"
    line << " #{range.failed ? "failure" : "success"}: #{range.label}"
    line << "\n"
    tree_lines << fork_line << line
  end
  tree_lines.reverse.join
end
hunk_prelude() click to toggle source
# File lib/parsby.rb, line 313
    def hunk_prelude
      <<~EOF
        line #{ctx.bio.line_number}:
        #{" " * INDENTATION}#{ctx.bio.current_line}
      EOF
    end
message() click to toggle source

The message of the exception. It's the current line, with a kind-of backtrace showing the failed expectations with a visualization of their range in the current line.

# File lib/parsby.rb, line 372
def message
  hunk_at parsed_range.start
end
message_hunk(failure_tree) click to toggle source
# File lib/parsby.rb, line 294
def message_hunk(failure_tree)
end
parsed_range() click to toggle source
# File lib/parsby.rb, line 309
def parsed_range
  @parsed_range ||= ctx.furthest_parsed_range
end