Method: MessagePack::IDL::ParsletParser#print_error

Defined in:
lib/msgpack/idl/parser/rule.rb


450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/msgpack/idl/parser/rule.rb', line 450

def print_error(error, fname, out=STDERR)
  error_tree = self.root.error_tree

  last = error_tree
  until last.children.empty?
    last = last.children.last
  end
  last_cause = last.parslet.instance_eval('@last_cause')  # FIXME
  source = last_cause.source

  row, col = source.line_and_column(last_cause.pos)

  old_pos = source.pos
  begin
    source.pos = last_cause.pos - col + 1
    line, *after = source.read(AFTER_BUFFER).to_s.split("\n")
    after = after[0,AFTER_LINES]

    source.pos = last_cause.pos - col - BEFORE_BUFFER
    before = source.read(BEFORE_BUFFER).to_s.split("\n")
    before = before[-BEFORE_LINES,BEFORE_LINES] || []
  ensure
    source.pos = old_pos
  end

  if m = /[ \t\r\n]*/.match(line)
    heading = m[0]
  else
    heading = ""
  end

  out.puts "syntax error:"
  (
    error.to_s.split("\n") +
    error_tree.to_s.split("\n")
  ).each {|ln|
    out.puts "  "+ln
  }

  out.puts ""
  out.puts "around line #{row} column #{heading.size}-#{col}:"
  out.puts ""

  before.each_with_index {|ln,i|
    l = row - after.size - 1 + i
    out.print LINE_HEAD_FORMAT % l
    out.puts ln
  }

  out.print LINE_HEAD_FORMAT % row
  out.puts line
  out.print " "*LINE_HEAD_SIZE
  out.puts heading + '^'*(col - heading.size)

  after.each_with_index {|ln,i|
    l = row + 1 + i
    out.print LINE_HEAD_FORMAT % l
    out.puts ln
  }

  out
end