Class: SyntaxTreeBuilder

Inherits:
Object show all
Includes:
SourceCodeDumpable
Defined in:
lib/rpdf2txt-rockit/syntax_tree.rb

Overview

Base class for objects that build the syntax tree. Each has a name of the node they build, a list of names of the children and a list of the childrens that are inactive. No value will be specified for inactive childrens even though their names are still available for the built node. Children named “_” are semi-inactive; values for the need to be specified when creating trees but the will be deleted when the tree is compacted. If a tree is not compacted their value can be accessed via their index number.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SourceCodeDumpable

as_code, as_method_named, as_module_method_named, #create_new, indent_lines, name_hash, #new_of_my_type, #parameter_named, #to_compact_src, #to_src_in_module, #type_to_src

Constructor Details

#initialize(nodeName, childrenNames, inactiveChildrenIndices = []) ⇒ SyntaxTreeBuilder

Returns a new instance of SyntaxTreeBuilder.



19
20
21
22
23
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 19

def initialize(nodeName, childrenNames, inactiveChildrenIndices = [])
  @node_name = nodeName.to_s
  @inactive_children_indices = inactiveChildrenIndices.sort
  init_children_names(childrenNames)
end

Instance Attribute Details

#children_namesObject (readonly)

Returns the value of attribute children_names.



17
18
19
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 17

def children_names
  @children_names
end

#inactive_children_indicesObject (readonly)

Returns the value of attribute inactive_children_indices.



17
18
19
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 17

def inactive_children_indices
  @inactive_children_indices
end

#node_nameObject

Returns the value of attribute node_name.



16
17
18
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 16

def node_name
  @node_name
end

Instance Method Details

#==(other) ⇒ Object



76
77
78
79
80
81
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 76

def ==(other)
  other.class == self.class and
    other.node_name == node_name and
    other.children_names == children_names and
    other.inactive_children_indices == inactive_children_indices
end

#activate_child(anIntegerOrString) ⇒ Object



71
72
73
74
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 71

def activate_child(anIntegerOrString)
  position, child = position_and_child(anIntegerOrString)
  @inactive_children_indices.delete position
end

#active_childrensObject



43
44
45
46
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 43

def active_childrens
  inactive = inactive_childrens
  children_names.select {|c| not inactive.include?(c)}
end

#copyObject



39
40
41
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 39

def copy
  SyntaxTreeBuilder.new(node_name, children_names, inactive_children_indices)
end

#create_tree(childrenValues) ⇒ Object



83
84
85
86
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 83

def create_tree(childrenValues)
  childrenValues = insert_nil_for_inactive_children(childrenValues)
  SyntaxTree.new(node_name, children_names, childrenValues)
end

#inactivate_child(anIntegerOrString) ⇒ Object



52
53
54
55
56
57
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 52

def inactivate_child(anIntegerOrString)
  position, child = position_and_child(anIntegerOrString)
  @inactive_children_indices.push(position)
  @inactive_children_indices.uniq!
  @inactive_children_indices.sort!
end

#inactive_childrensObject



48
49
50
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 48

def inactive_childrens
  children_names.values_at(*@inactive_children_indices)
end

#to_src(name = nil, nameHash = {}) ⇒ Object



102
103
104
105
106
107
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 102

def to_src(name = nil, nameHash = {})
  assign_to(name,
     new_of_my_type(node_name, 
	     as_code(children_names.to_compact_src), 
	     as_code(inactive_children_indices.to_compact_src)))
end