class Raabro::Tree

Attributes

children[RW]
input[RW]
length[RW]
name[RW]
offset[RW]
parter[RW]
result[RW]

Public Class Methods

new(name, parter, input) click to toggle source
# File lib/raabro.rb, line 48
def initialize(name, parter, input)

  @result = 0
  @name = name
  @parter = parter
  @input = input
  @offset = input.offset
  @length = 0
  @children = []
end

Public Instance Methods

c0() click to toggle source
# File lib/raabro.rb, line 59
def c0; @children[0]; end
c1() click to toggle source
# File lib/raabro.rb, line 60
def c1; @children[1]; end
c2() click to toggle source
# File lib/raabro.rb, line 61
def c2; @children[2]; end
c3() click to toggle source
# File lib/raabro.rb, line 62
def c3; @children[3]; end
c4() click to toggle source
# File lib/raabro.rb, line 63
def c4; @children[4]; end
clast() click to toggle source
# File lib/raabro.rb, line 64
def clast; @children.last; end
empty?() click to toggle source
# File lib/raabro.rb, line 66
def empty?

  @result == 1 && @length == 0
end
even_children() click to toggle source
# File lib/raabro.rb, line 161
def even_children

  cs = []; @children.each_with_index { |c, i| cs << c if i.even? }; cs
end
extract_error() click to toggle source
# File lib/raabro.rb, line 166
    def extract_error

#Raabro.pp(self, colors: true)
      err_tree, stack = lookup_error || lookup_all_error

      line, column = line_and_column(err_tree.offset)

      err_message =
        if stack
          path = stack
           .compact.reverse.take(3).reverse
           .collect(&:inspect).join('/')
          "parsing failed .../#{path}"
        else
          "parsing failed, not all input was consumed"
        end
      visual =
        visual(line, column)

      [ line, column, err_tree.offset, err_message, visual ]
    end
gather(name=nil, acc=[]) click to toggle source
# File lib/raabro.rb, line 107
def gather(name=nil, acc=[])

  name = name ? name.to_s : nil

  if (@name && name == nil) || (@name.to_s == name)
    acc << self
  else
    subgather(name, acc)
  end

  acc
end
line_and_column(offset) click to toggle source
# File lib/raabro.rb, line 211
def line_and_column(offset)

  line = 1
  column = 0

  (0..offset).each do |off|

    column += 1
    next unless @input.at(off) == "\n"

    line += 1
    column = 0
  end

  [ line, column ]
end
lookup(name=nil) click to toggle source
# File lib/raabro.rb, line 91
def lookup(name=nil)

  name = name ? name.to_s : nil

  return self if @name && name == nil
  return self if @name.to_s == name
  sublookup(name)
end
lookup_all_error() click to toggle source

Not “lookup all errors” but “lookup all error”, in other words lookup the point up until which the parser stopped (not consuming all the input)

# File lib/raabro.rb, line 203
    def lookup_all_error

#print "lae(): "; Raabro.pp(self, colors: true)
      @children.each { |c| return [ c, nil ] if c.result == 0 }
      @children.reverse.each { |c| es = c.lookup_all_error; return es if es }
      nil
    end
lookup_error(stack=[]) click to toggle source
# File lib/raabro.rb, line 188
    def lookup_error(stack=[])

#print "le(): "; Raabro.pp(self, colors: true)
      return nil if @result != 0
      return [ self, stack ] if @children.empty?
      @children.each { |c|
        es = c.lookup_error(stack.dup.push(self.name))
        return es if es }
      nil
    end
nonstring(l=7) click to toggle source
# File lib/raabro.rb, line 86
def nonstring(l=7)

  @input.string[@offset, l]
end
odd_children() click to toggle source
# File lib/raabro.rb, line 156
def odd_children

  cs = []; @children.each_with_index { |c, i| cs << c if i.odd? }; cs
end
prune!() click to toggle source
# File lib/raabro.rb, line 76
def prune!

  @children = successful_children
end
string() click to toggle source
# File lib/raabro.rb, line 81
def string

  @input.string[@offset, @length]
end
subgather(name=nil, acc=[]) click to toggle source
# File lib/raabro.rb, line 120
def subgather(name=nil, acc=[])

  @children.each { |c| c.gather(name, acc) }

  acc
end
sublookup(name=nil) click to toggle source
# File lib/raabro.rb, line 100
def sublookup(name=nil)

  @children.each { |c| if n = c.lookup(name); return n; end }

  nil
end
successful_children() click to toggle source
# File lib/raabro.rb, line 71
def successful_children

  @children.select { |c| c.result == 1 }
end
to_a(opts={}) click to toggle source
# File lib/raabro.rb, line 127
def to_a(opts={})

  opts = Array(opts).inject({}) { |h, e| h[e] = true; h }          unless opts.is_a?(Hash)

  cn =
    if opts[:leaves] && (@result == 1) && @children.empty?
      string
    elsif opts[:children] != false
      @children.collect { |e| e.to_a(opts) }
    else
      @children.length
    end

  [ @name, @result, @offset, @length, @note, @parter, cn ]
end
to_s(depth=0, io=StringIO.new) click to toggle source
# File lib/raabro.rb, line 144
def to_s(depth=0, io=StringIO.new)

  io.print "\n" if depth > 0
  io.print '  ' * depth
  io.print "#{@result} #{@name.inspect} #{@offset},#{@length}"
  io.print result == 1 && children.size == 0 ? ' ' + string.inspect : ''

  @children.each { |c| c.to_s(depth + 1, io) }

  depth == 0 ? io.string : nil
end
visual(line, column) click to toggle source
# File lib/raabro.rb, line 228
def visual(line, column)

  @input.string.split("\n")[line - 1] + "\n" +
  ' ' * (column - 1) + '^---'
end