Class: Treetop::Runtime::SyntaxNode

Inherits:
Object
  • Object
show all
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

Instance Method Details

#ancestorObject

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_xmlObject



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

#childObject

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

#descendantObject

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

#getLabelObject



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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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_nameObject

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

#thisdescendantObject

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_infoObject

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_xmlObject

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_tagObject

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_tagObject

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