module Parsby::Tree

Attributes

children[W]
markers[R]
parent[RW]

Public Instance Methods

<<(*ts) click to toggle source
# File lib/parsby.rb, line 134
def <<(*ts)
  ts.each do |t|
    t.parent = self
    children << t
  end
end
children() click to toggle source
# File lib/parsby.rb, line 130
def children
  @children ||= []
end
dup(currently_descending: false) click to toggle source
Calls superclass method
# File lib/parsby.rb, line 185
def dup(currently_descending: false)
  self_path = path
  if parent && !currently_descending
    root.dup.get self_path
  else
    super().tap do |d|
      d.children = d.children.map do |c|
        c.dup(currently_descending: true).tap do |dc|
          dc.parent = d
        end
      end
    end
  end
end
each(&b) click to toggle source
# File lib/parsby.rb, line 167
def each(&b)
  b.call self
  children.each {|c| c.each(&b) }
  self
end
flatten() click to toggle source
# File lib/parsby.rb, line 157
def flatten
  [self, *children.map(&:flatten).flatten]
end
Also aliased as: self_and_descendants
get(path) click to toggle source
# File lib/parsby.rb, line 254
def get(path)
  return self if path.empty?
  idx, *sub_path = path
  child = children[idx]
  child&.get sub_path
end
path() click to toggle source
# File lib/parsby.rb, line 163
def path
  [*parent&.path, *sibling_index]
end
right_tree_slice() click to toggle source
# File lib/parsby.rb, line 181
def right_tree_slice
  "*" + "|" * right_uncles
end
right_uncles() click to toggle source
# File lib/parsby.rb, line 173
def right_uncles
  if parent
    sibling_reverse_index + parent.right_uncles
  else
    0
  end
end
root() click to toggle source
# File lib/parsby.rb, line 141
def root
  if parent == nil
    self 
  else
    parent.root
  end
end
select(&b) click to toggle source
# File lib/parsby.rb, line 237
def select(&b)
  r = []
  each do |n|
    if b.call n
      r << n
    end
  end
  r
end
select_paths(&b) click to toggle source
# File lib/parsby.rb, line 247
def select_paths(&b)
  root_path = path
  select(&b).map do |n|
    n.path.drop root_path.length
  end
end
self_and_ancestors() click to toggle source
# File lib/parsby.rb, line 261
def self_and_ancestors
  [self, *parent&.self_and_ancestors]
end
self_and_descendants()
Alias for: flatten
sibling_index() click to toggle source
# File lib/parsby.rb, line 153
def sibling_index
  parent&.children&.index self
end
sibling_reverse_index() click to toggle source
# File lib/parsby.rb, line 149
def sibling_reverse_index
  parent&.children&.reverse&.index self
end
splice(*paths) click to toggle source
# File lib/parsby.rb, line 214
def splice(*paths)
  dup.splice!(*paths)
end
splice!(*paths) click to toggle source
# File lib/parsby.rb, line 207
def splice!(*paths)
  self.children = paths
    .map {|p| get(p)&.tap {|d| d.parent = self } }
    .reject(&:nil?)
  self
end
splice_self!() click to toggle source
# File lib/parsby.rb, line 200
def splice_self!
  idx = sibling_index
  parent.children.delete_at(idx)
  parent.children.insert(idx, *children.each {|c| c.parent = parent })
  parent
end
splice_to!(marker) click to toggle source
# File lib/parsby.rb, line 126
def splice_to!(marker)
  splice!(*select_paths {|n| n.markers.include? marker })
end
trim_to_just!(*paths, &rejecting) click to toggle source
# File lib/parsby.rb, line 218
def trim_to_just!(*paths, &rejecting)
  max_sibling = paths.map(&:first).reject(&:nil?).max
  self.children = if max_sibling.nil?
    []
  else
    children[0..max_sibling]
      .map.with_index {|c, i| [c, i] }
      .reject {|(c, i)| rejecting.call c, i, max_sibling if rejecting }
      .each do |(child, i)|
        subpaths = paths
          .select {|p| p.first == i}
          .map {|p| p.drop 1 }
        child.trim_to_just!(*subpaths, &rejecting)
      end
      .map(&:first)
  end
  self
end