/usr/lib/ruby/1.8/rd/element.rb is in librd-ruby1.8 0.6.22-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | module RD
# abstruct class of node of document tree
class Element
include Enumerable
attr_accessor :parent
def initialize
@parent = nil
end
def tree
raise RuntimeError, "#{self} doesn't have a parent." unless @parent
@parent.tree
end
def inspect
c = children.collect{|i| indent2(i.inspect)}.join("\n")
"<#{self.class.name}>" + (c.empty? ? "" : "\n") + c
end
end # Element
# element which don't have children.
module TerminalElement
def children
[]
end
def each_element
yield(self)
end
alias each each_element
end
# element which have children.
module NonterminalElement
def initialize(*arg)
@temporary_document_structure = nil
super
end
def children
raise NotImplimentedError, "need #{self}#children."
end
def each_child
children.each do |i|
yield(i)
end
end
def each_element(&block)
yield(self)
children.each do |i|
i.each_element(&block)
end
end
alias each each_element
def add_child(child)
add_child_under_document_struct(child, tree.document_struct)
end
def add_child_under_document_struct(child, document_struct)
if document_struct.is_valid?(self, child)
push_to_children(child)
else
raise ArgumentError,
"mismatched document structure, #{self} <-/- #{child}."
end
return self
end
def add_children(children)
add_children_under_document_struct(children, tree.document_struct)
end
def add_children_under_document_struct(children, document_struct)
children.each do |i|
add_child_under_document_struct(i, document_struct)
end
return self
end
def add_children_without_document_struct(new_children)
new_children.each do |i|
push_to_children(i)
end
return self
end
def push_to_children(child)
children.push(child)
child.parent = self
end
attr_accessor :temporary_document_structure
def build(document_struct = tree.document_struct, &block)
under_temporary_document_structure(document_struct) do
self.instance_eval(&block)
end
self
end
def make_child(child_class, *args_of_new, &block)
child = child_class.new(*args_of_new)
if self.temporary_document_structure
self.add_child_under_document_struct(child,
self.temporary_document_structure)
child.build(self.temporary_document_structure, &block) if block_given?
else
self.add_child(child)
child.build(&block) if block_given?
end
child
end
alias new make_child
private :new
# NonterminalElement#new, not NonterminalElement.new
def under_temporary_document_structure(document_struct)
begin
self.temporary_document_structure = document_struct
yield
ensure
self.temporary_document_structure = nil
end
end
def indent2(str)
buf = ''
str.each_line{|i| buf << " " << i }
buf
end
private :indent2
end
# root node of document tree
class DocumentElement < Element
include NonterminalElement
attr_reader :blocks
def initialize()
@blocks = []
end
def accept(visitor)
visitor.visit_DocumentElement(self)
end
alias each_block each_child
def children
@blocks
end
end
end
|