Class: SyntaxTree
Overview
Nodes in a syntax tree have a name and can have childrens. The childrens are also named. The childrens can be accessed as methods with their name or via [] with their name or order number. Syntax tree nodes can also have attributes in a hash.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#children_names ⇒ Object
readonly
Returns the value of attribute children_names.
-
#childrens ⇒ Object
readonly
Returns the value of attribute childrens.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#raw_src ⇒ Object
ywesee.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](anIntegerOrChildrenName) ⇒ Object
-
#compact! ⇒ Object
(recursively) delete childrens whose name is “_” or nil.
- #compact_childrens ⇒ Object
-
#each_breadth_first(&b) ⇒ Object
Breadt first node visiting.
-
#each_node(&b) ⇒ Object
(also: #each_depth_first)
Depth first node visiting.
-
#initialize(name, childrenNames = [], childrens = []) ⇒ SyntaxTree
constructor
A new instance of SyntaxTree.
-
#inspect_compact ⇒ Object
(also: #inspect)
Compact inspect without newlines.
-
#inspect_multi(indentLevel = 0) ⇒ Object
Print as multi-line string with children indented.
- #method_missing(methodId, *args, &block) ⇒ Object
- #to_graph ⇒ Object
Constructor Details
#initialize(name, childrenNames = [], childrens = []) ⇒ SyntaxTree
Returns a new instance of SyntaxTree.
304 305 306 307 308 309 310 311 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 304 def initialize(name, childrenNames = [], childrens = []) @name = name unless childrenNames.length == childrens.length raise ArgumentError, "All childrens must be given a name" end @children_names, @childrens = childrenNames, childrens @attributes = Hash.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(methodId, *args, &block) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 332 def method_missing(methodId, *args, &block) begin child_with_name(methodId.id2name) rescue ArgumentError if @childrens.respond_to?(methodId) @childrens.send(methodId, *args, &block) else super end end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
301 302 303 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 301 def attributes @attributes end |
#children_names ⇒ Object (readonly)
Returns the value of attribute children_names.
301 302 303 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 301 def children_names @children_names end |
#childrens ⇒ Object (readonly)
Returns the value of attribute childrens.
301 302 303 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 301 def childrens @childrens end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
301 302 303 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 301 def name @name end |
#raw_src ⇒ Object
ywesee
302 303 304 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 302 def raw_src @raw_src end |
Instance Method Details
#==(other) ⇒ Object
313 314 315 316 317 318 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 313 def ==(other) other.class == self.class and other.name == name and other.children_names == children_names and other.childrens == childrens end |
#[](anIntegerOrChildrenName) ⇒ Object
324 325 326 327 328 329 330 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 324 def [](anIntegerOrChildrenName) if anIntegerOrChildrenName.kind_of?(Integer) @childrens[anIntegerOrChildrenName] else child_with_name(anIntegerOrChildrenName) end end |
#compact! ⇒ Object
(recursively) delete childrens whose name is “_” or nil
350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 350 def compact! new_childrens, new_children_names = Array.new, Array.new @children_names.each_with_index do |childname, i| if childname != "_" and childname != nil new_childrens.push @childrens[i] new_children_names.push @children_names[i] end end @childrens, @children_names = new_childrens, new_children_names compact_childrens self end |
#compact_childrens ⇒ Object
363 364 365 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 363 def compact_childrens @childrens.each {|c| c.compact! if c.kind_of?(SyntaxTree)} end |
#each_breadth_first(&b) ⇒ Object
Breadt first node visiting
375 376 377 378 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 375 def each_breadth_first(&b) b.call(self) @childrens.each {|c| c.each_node(&b) if c.kind_of?(SyntaxTree)} end |
#each_node(&b) ⇒ Object Also known as: each_depth_first
Depth first node visiting
368 369 370 371 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 368 def each_node(&b) @childrens.each {|c| c.each_node(&b) if c.kind_of?(SyntaxTree)} b.call(self) end |
#inspect_compact ⇒ Object Also known as: inspect
Compact inspect without newlines
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 381 def inspect_compact if ["lexeme", "value"].sort == @children_names.sort return @childrens[0].inspect end str = "#{name}" if @childrens.length > 0 str += ":[" @childrens.each_with_index do |child, i| str += "," if i > 0 str += child.inspect end str += "]" end str end |
#inspect_multi(indentLevel = 0) ⇒ Object
Print as multi-line string with children indented
399 400 401 402 403 404 405 406 407 408 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 399 def inspect_multi(indentLevel = 0) str = "#{@name}" @childrens.each_with_index do |child, i| str += "\n" + (" " * (indentLevel+1)) + @children_names[i] + ": " str += child.kind_of?(SyntaxTree) ? child.inspect_multi(indentLevel+1) : child.inspect #str += "\n" end str end |
#to_graph ⇒ Object
320 321 322 |
# File 'lib/rpdf2txt-rockit/syntax_tree.rb', line 320 def to_graph syntaxtree_as_dot_digraph(self) end |