Class: Treetop::Runtime::SyntaxNode

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/syntax_node.rb

Direct Known Subclasses

SbuilderSexp::Root

Instance Method Summary collapse

Instance Method Details

#node_typeString

Returns name of syntax node (as bare class name).

Returns:

  • (String)

    name of syntax node (as bare class name)



8
9
10
# File 'lib/utils/syntax_node.rb', line 8

def node_type
  self.class.name.split('::').last
end

#node_valueString|Array|Hash

To abstact

Returns:

  • (String|Array|Hash)

    of node value



14
15
16
# File 'lib/utils/syntax_node.rb', line 14

def node_value
  text_value
end

#recursive_inject(results = [], deep = false, &block) ⇒ Object

Traverse the syntax tree recursively. The order should respect the order of the configuration file as it is read and written by humans (and the order in which it is parsed).



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/utils/syntax_node.rb', line 45

def recursive_inject(results=[], deep=false, &block)
  if !elements.nil?
    elements.each do |element|
      if block.call(element)
        results << element
        if  deep
          element.recursive_inject(results, deep, &block)
        end
      else
        element.recursive_inject(results, &block)
      end
    end
  end
  return results
end

#recursive_inject_parent(results = [], &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/utils/syntax_node.rb', line 31

def recursive_inject_parent(results=[], &block)
  if !parent.nil?
    if block.call(parent)
      results << parent
    else
      parent.recursive_inject_parent(results, &block)
    end
  end
  return results
end

#recursive_select(klass, deep = false) ⇒ Object

When Treetop parses the configuration file it will generate a tree, the generated tree will contain a few ‘Empty` nodes to represent the actual space/tab or newline in the file. Some of theses node will point to our concrete class. To fetch a specific types of object we need to follow each branch and ignore the empty nodes.



67
68
69
# File 'lib/utils/syntax_node.rb', line 67

def recursive_select(klass, deep=false)
  return recursive_inject([], deep ) { |e| e.is_a?(klass) }
end

#valueString

Default implementation of syntax node value (override in sub-classes)

Returns:

  • (String)

    value representeation of the node



22
23
24
25
26
27
# File 'lib/utils/syntax_node.rb', line 22

def value
  {
    :node_type => node_type,
    :value => node_value,
  }
end