Class: Treetop::Runtime::SyntaxNode

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/config/config_ast.rb,
lib/logstash/compiler/treetop_monkeypatches.rb

Overview

Monkeypatch Treetop::Runtime::SyntaxNode’s inspect method to skip any Whitespace or SyntaxNodes with no children.

Instance Method Summary collapse

Instance Method Details

#_inspect(indent = "") ⇒ Object

Monkeypatch Treetop::Runtime::SyntaxNode’s inspect method to skip any Whitespace or SyntaxNodes with no children.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/logstash/compiler/treetop_monkeypatches.rb', line 68

def _inspect(indent="")
  em = extension_modules
  interesting_methods = methods-[em.last ? em.last.methods : nil]-self.class.instance_methods
  im = interesting_methods.size > 0 ? " (#{interesting_methods.join(",")})" : ""
  tv = text_value
  tv = "...#{tv[-20..-1]}" if tv.size > 20

  indent +
  self.class.to_s.sub(/.*:/,'') +
    em.map{|m| "+"+m.to_s.sub(/.*:/,'')}*"" +
    " offset=#{interval.first}" +
    ", #{tv.inspect}" +
    im +
    (elements && elements.size > 0 ?
      ":" +
        (elements.select { |e| !e.is_a?(LogStash::Config::AST::Whitespace) && e.elements && e.elements.size > 0 }||[]).map{|e|
    begin
      "\n"+e.inspect(indent+"  ")
    rescue  # Defend against inspect not taking a parameter
      "\n"+indent+" "+e.inspect
    end
        }.join("") :
      ""
    )
end

#compileObject



14
15
16
17
# File 'lib/logstash/compiler/treetop_monkeypatches.rb', line 14

def compile
  return "" if elements.nil?
  return elements.collect(&:compile).reject(&:empty?).join("")
end

#get_meta(key) ⇒ Object



2
3
4
5
6
7
# File 'lib/logstash/compiler/treetop_monkeypatches.rb', line 2

def get_meta(key)
  @ast_metadata ||= {}
  return @ast_metadata[key] if @ast_metadata[key] 
  return self.parent.get_meta(key) if self.parent
  nil
end

#recurse(e, depth = 0, &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).



22
23
24
25
26
# File 'lib/logstash/compiler/treetop_monkeypatches.rb', line 22

def recurse(e, depth=0, &block)
  r = block.call(e, depth)
  e.elements.each { |e| recurse(e, depth + 1, &block) } if r && e.elements
  nil
end

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



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/logstash/compiler/treetop_monkeypatches.rb', line 28

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

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



51
52
53
54
55
56
57
58
59
60
# File 'lib/logstash/compiler/treetop_monkeypatches.rb', line 51

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(*klasses) ⇒ 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.



47
48
49
# File 'lib/logstash/compiler/treetop_monkeypatches.rb', line 47

def recursive_select(*klasses)
  return recursive_inject { |e| klasses.any? {|k| e.is_a?(k)} }
end

#recursive_select_parent(results = [], klass) ⇒ Object



62
63
64
# File 'lib/logstash/compiler/treetop_monkeypatches.rb', line 62

def recursive_select_parent(results=[], klass)
  return recursive_inject_parent(results) { |e| e.is_a?(klass) }
end

#set_meta(key, value) ⇒ Object



9
10
11
12
# File 'lib/logstash/compiler/treetop_monkeypatches.rb', line 9

def set_meta(key, value)
  @ast_metadata ||= {}
  @ast_metadata[key] = value
end