class Treetop::Runtime::SyntaxNode

this mixin provides a convenient interface for the syntaxtree

Public Instance Methods

ancestor() click to toggle source

returns all nodes to up to the AST root

# File lib/wortsammler/class.treetopHelper.rb, line 26
def ancestor
    if ! parent.nil?
        [parent, parent.ancestor].flatten
    end
end
as_xml() click to toggle source
# 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() click to toggle source

this delivers a child node of the AST

# File lib/wortsammler/class.treetopHelper.rb, line 10
def child
    elements.select{|e| e.is_ast?} if ! elements.nil?
end
clean_tree(root_node) click to toggle source

clean the tree by removing garbage nodes which are not part of the intended AST

# 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() click to toggle source

returns an array of all descendants of the current node in the AST in document order

# File lib/wortsammler/class.treetopHelper.rb, line 16
def descendant
    child.map{|e| [e, e.descendant]}.flatten.compact if not child.nil?
end
getLabel() click to toggle source
# File lib/wortsammler/class.treetopHelper.rb, line 110
def getLabel
    nil
end
has_rule_name?() click to toggle source

indicates if a meaningful name for the node in the AST is available

# File lib/wortsammler/class.treetopHelper.rb, line 40
def has_rule_name?
    not (extension_modules.nil? or extension_modules.empty?)
end
is_ast?() click to toggle source

indicates if the current treetop node is important enough to be in the intended AST

# File lib/wortsammler/class.treetopHelper.rb, line 34
def is_ast?
    true # nonterminal? # parent.nil? or extension_modules.include?(Xmine)
end
rule_name() click to toggle source

returns a meaning name for the node in the AST

# 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() click to toggle source

returns this and all descendant in document order

# File lib/wortsammler/class.treetopHelper.rb, line 21
def thisdescendant
    [self, descendant].flatten
end
to_info() click to toggle source

another quick info for a node

# File lib/wortsammler/class.treetopHelper.rb, line 54
def to_info
    rule_name + ": "+ text_value
end
to_xml() click to toggle source

exposes a node in the AST as xml

# 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) click to toggle source
# File lib/wortsammler/class.treetopHelper.rb, line 106
def wrap(tag,body)
    "<#{tag}>#{body}</#{tag}>"
end
xml_end_tag() click to toggle source

get the XML end tag

# File lib/wortsammler/class.treetopHelper.rb, line 78
def xml_end_tag
    if has_rule_name? then
        "</" + rule_name + ">"
    end
end
xml_start_tag() click to toggle source

get the XML start tag

# File lib/wortsammler/class.treetopHelper.rb, line 71
def xml_start_tag
    if has_rule_name? then
        "<" + rule_name + ">"
    end
end