Class: Treetop::Runtime::SyntaxNode
- Inherits:
-
Object
- Object
- Treetop::Runtime::SyntaxNode
- Defined in:
- lib/wortsammler/class.treetopHelper.rb,
lib/wortsammler/class.treetopHelper.rb
Overview
this mixin provides a convenient interface for the syntaxtree
Instance Method Summary collapse
-
#ancestor ⇒ Object
returns all nodes to up to the AST root.
- #as_xml ⇒ Object
-
#child ⇒ Object
this delivers a child node of the AST.
-
#clean_tree(root_node) ⇒ Object
clean the tree by removing garbage nodes which are not part of the intended AST.
-
#descendant ⇒ Object
returns an array of all descendants of the current node in the AST in document order.
- #getLabel ⇒ Object
-
#has_rule_name? ⇒ Boolean
indicates if a meaningful name for the node in the AST is available.
-
#is_ast? ⇒ Boolean
indicates if the current treetop node is important enough to be in the intended AST.
-
#rule_name ⇒ Object
returns a meaning name for the node in the AST.
-
#thisdescendant ⇒ Object
returns this and all descendant in document order.
-
#to_info ⇒ Object
another quick info for a node.
-
#to_xml ⇒ Object
exposes a node in the AST as xml.
- #wrap(tag, body) ⇒ Object
-
#xml_end_tag ⇒ Object
get the XML end tag.
-
#xml_start_tag ⇒ Object
get the XML start tag.
Instance Method Details
#ancestor ⇒ Object
returns all nodes to up to the AST root
26 27 28 29 30 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 26 def ancestor if ! parent.nil? [parent, parent.ancestor].flatten end end |
#as_xml ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 95 def as_xml [(["<", getLabel, ">" ].join if getLabel), (if elements elements.map { |e| e.as_xml }.join else text_value end), (["</", getLabel, ">" ].join if getLabel) ].join end |
#child ⇒ Object
this delivers a child node of the AST
10 11 12 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 10 def child elements.select{|e| e.is_ast?} if ! elements.nil? end |
#clean_tree(root_node) ⇒ Object
clean the tree by removing garbage nodes which are not part of the intended AST
86 87 88 89 90 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 86 def clean_tree(root_node) return if(root_node.elements.nil?) root_node.elements.delete_if{|node| not node.is_ast? } root_node.elements.each{|e| e.clean_tree(e)} end |
#descendant ⇒ Object
returns an array of all descendants of the current node in the AST in document order
16 17 18 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 16 def descendant child.map{|e| [e, e.descendant]}.flatten.compact if not child.nil? end |
#getLabel ⇒ Object
110 111 112 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 110 def getLabel nil end |
#has_rule_name? ⇒ Boolean
indicates if a meaningful name for the node in the AST is available
40 41 42 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 40 def has_rule_name? not (extension_modules.nil? or extension_modules.empty?) end |
#is_ast? ⇒ Boolean
indicates if the current treetop node is important enough to be in the intended AST
34 35 36 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 34 def is_ast? true # nonterminal? # parent.nil? or extension_modules.include?(Xmine) end |
#rule_name ⇒ Object
returns a meaning name for the node in the AST
45 46 47 48 49 50 51 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 45 def rule_name if has_rule_name? then extension_modules.first.name.split("::").last.gsub(/[0-9]/,"") else "###" end end |
#thisdescendant ⇒ Object
returns this and all descendant in document order
21 22 23 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 21 def thisdescendant [self, descendant].flatten end |
#to_info ⇒ Object
another quick info for a node
54 55 56 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 54 def to_info rule_name + ": "+ text_value end |
#to_xml ⇒ Object
exposes a node in the AST as xml
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 59 def to_xml if child.nil? or child.empty? "#>" +interval.to_s + ":"+ text_value + "<#" else [ xml_start_tag, (child.nil? ? [] : child).map{|x| x.to_xml}, xml_end_tag ].join end end |
#wrap(tag, body) ⇒ Object
106 107 108 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 106 def wrap(tag,body) "<#{tag}>#{body}</#{tag}>" end |
#xml_end_tag ⇒ Object
get the XML end tag
78 79 80 81 82 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 78 def xml_end_tag if has_rule_name? then "</" + rule_name + ">" end end |
#xml_start_tag ⇒ Object
get the XML start tag
71 72 73 74 75 |
# File 'lib/wortsammler/class.treetopHelper.rb', line 71 def xml_start_tag if has_rule_name? then "<" + rule_name + ">" end end |