Exception: Parsby::ExpectationFailed
- Defined in:
- lib/parsby.rb
Constant Summary collapse
- INDENTATION =
2
Instance Attribute Summary collapse
-
#ctx ⇒ Object
readonly
Returns the value of attribute ctx.
Instance Method Summary collapse
- #failure_tree ⇒ Object
- #hunk_at(pos) ⇒ Object
- #hunk_graph ⇒ Object
- #hunk_prelude ⇒ Object
-
#initialize(ctx) ⇒ ExpectationFailed
constructor
Initializes an ExpectationFailed from a backed_io and an optional expectation with which to start the list of expectations that lead to this failure.
-
#message ⇒ Object
The message of the exception.
- #message_hunk(failure_tree) ⇒ Object
- #parsed_range ⇒ Object
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.
289 290 291 |
# File 'lib/parsby.rb', line 289 def initialize(ctx) @ctx = ctx end |
Instance Attribute Details
#ctx ⇒ Object (readonly)
Returns the value of attribute ctx.
284 285 286 |
# File 'lib/parsby.rb', line 284 def ctx @ctx end |
Instance Method Details
#failure_tree ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/parsby.rb', line 298 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
363 364 365 366 367 368 |
# File 'lib/parsby.rb', line 363 def hunk_at(pos) ctx.bio.with_saved_pos do ctx.bio.seek pos hunk_prelude + hunk_graph end end |
#hunk_graph ⇒ Object
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 361 |
# File 'lib/parsby.rb', line 321 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 ⇒ Object
314 315 316 317 318 319 |
# File 'lib/parsby.rb', line 314 def hunk_prelude <<~EOF line #{ctx.bio.line_number}: #{" " * INDENTATION}#{ctx.bio.current_line} EOF end |
#message ⇒ Object
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.
373 374 375 |
# File 'lib/parsby.rb', line 373 def hunk_at parsed_range.start end |
#message_hunk(failure_tree) ⇒ Object
295 296 |
# File 'lib/parsby.rb', line 295 def (failure_tree) end |
#parsed_range ⇒ Object
310 311 312 |
# File 'lib/parsby.rb', line 310 def parsed_range @parsed_range ||= ctx.furthest_parsed_range end |