Exception: Parsby::ExpectationFailed

Inherits:
Error
  • Object
show all
Defined in:
lib/parsby.rb

Constant Summary collapse

INDENTATION =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ ExpectationFailed

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



288
289
290
# File 'lib/parsby.rb', line 288

def initialize(ctx)
  @ctx = ctx
end

Instance Attribute Details

#ctxObject (readonly)

Returns the value of attribute ctx.



283
284
285
# File 'lib/parsby.rb', line 283

def ctx
  @ctx
end

Instance Method Details

#failure_treeObject



297
298
299
300
301
302
303
304
305
306
307
# 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) ⇒ Object



362
363
364
365
366
367
# 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_graphObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# 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_preludeObject



313
314
315
316
317
318
# File 'lib/parsby.rb', line 313

def hunk_prelude
  <<~EOF
    line #{ctx.bio.line_number}:
    #{" " * INDENTATION}#{ctx.bio.current_line}
  EOF
end

#messageObject

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.



372
373
374
# File 'lib/parsby.rb', line 372

def message
  hunk_at parsed_range.start
end

#message_hunk(failure_tree) ⇒ Object



294
295
# File 'lib/parsby.rb', line 294

def message_hunk(failure_tree)
end

#parsed_rangeObject



309
310
311
# File 'lib/parsby.rb', line 309

def parsed_range
  @parsed_range ||= ctx.furthest_parsed_range
end