module Raabro

def self.pp(tree, depth=0, opts={})

def self.pp(tree, depth=0, opts={})
  fail ArgumentError.new(
    'tree is not an instance of Raabro::Tree'
  ) unless tree.is_a?(Raabro::Tree)
  depth, opts = 0, depth if depth.is_a?(Hash)
  _rs, _dg, _gn, _yl, _bl, _lg =
    (opts[:colors] || opts[:colours] || $stdout.tty?) ?
    [ "", "", "", "", "", "" ] :
    [ '', '', '', '', '', '' ]
  lc = tree.result == 1 ? _gn : _dg
  nc = tree.result == 1 ? _bl : _lg
  nc = lc if tree.name == nil
  sc = tree.result == 1 ? _yl : _dg
  str =
    if tree.children.size == 0
      " #{sc}#{tree.string.length == 0 ?
      "#{_dg} >#{tree.nonstring(14).inspect[1..-2]}<" :
      tree.string.inspect}"
    else
      ''
    end
  print "#{_dg}t---\n" if depth == 0
  #print "#{'  ' * depth}"
  depth.times do |i|
    pipe = i % 3 == 0 ? ': ' : '| '
    print i.even? ? "#{_dg}#{pipe} " : '  '
  end
  print "#{lc}#{tree.result}"
  print " #{nc}#{tree.name.inspect} #{lc}#{tree.offset},#{tree.length}"
  print str
  print "#{_rs}\n"
  tree.children.each { |c| self.pp(c, depth + 1, opts) }
  if depth == 0
    print _dg
    print "input ln: #{tree.input.string.length}, tree ln: #{tree.length} "
    print "---t\n"
    print _rs
  end
end