4
# abstruct class of node of document tree
15
raise RuntimeError, "#{self} doesn't have a parent." unless @parent
20
c = children.collect{|i| indent2(i.inspect)}.join("\n")
21
"<#{self.class.name}>" + (c.empty? ? "" : "\n") + c
25
# element which don't have children.
26
module TerminalElement
34
alias each each_element
37
# element which have children.
38
module NonterminalElement
40
@temporary_document_structure = nil
45
raise NotImplimentedError, "need #{self}#children."
54
def each_element(&block)
57
i.each_element(&block)
60
alias each each_element
63
add_child_under_document_struct(child, tree.document_struct)
66
def add_child_under_document_struct(child, document_struct)
67
if document_struct.is_valid?(self, child)
68
push_to_children(child)
71
"mismatched document structure, #{self} <-/- #{child}."
76
def add_children(children)
77
add_children_under_document_struct(children, tree.document_struct)
80
def add_children_under_document_struct(children, document_struct)
82
add_child_under_document_struct(i, document_struct)
87
def add_children_without_document_struct(new_children)
88
new_children.each do |i|
94
def push_to_children(child)
99
attr_accessor :temporary_document_structure
101
def build(document_struct = tree.document_struct, &block)
102
under_temporary_document_structure(document_struct) do
103
self.instance_eval(&block)
108
def make_child(child_class, *args_of_new, &block)
109
child = child_class.new(*args_of_new)
110
if self.temporary_document_structure
111
self.add_child_under_document_struct(child,
112
self.temporary_document_structure)
113
child.build(self.temporary_document_structure, &block) if block_given?
115
self.add_child(child)
116
child.build(&block) if block_given?
122
# NonterminalElement#new, not NonterminalElement.new
124
def under_temporary_document_structure(document_struct)
126
self.temporary_document_structure = document_struct
129
self.temporary_document_structure = nil
135
str.each_line{|i| buf << " " << i }
141
# root node of document tree
142
class DocumentElement < Element
143
include NonterminalElement
151
visitor.visit_DocumentElement(self)
154
alias each_block each_child