Class: Treetop::Runtime::SyntaxNode

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

Overview

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

Direct Known Subclasses

LogStash::Config::AST::Node

Instance Method Summary collapse

Instance Method Details

#_inspect(indent = "") ⇒ Object



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/logstash/config/config_ast.rb', line 520

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



7
8
9
10
# File 'lib/logstash/config/config_ast.rb', line 7

def compile
  return "" if elements.nil?
  return elements.collect(&:compile).reject(&:empty?).join("")
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).



15
16
17
18
19
# File 'lib/logstash/config/config_ast.rb', line 15

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



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/logstash/config/config_ast.rb', line 21

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



44
45
46
47
48
49
50
51
52
53
# File 'lib/logstash/config/config_ast.rb', line 44

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) ⇒ 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.



40
41
42
# File 'lib/logstash/config/config_ast.rb', line 40

def recursive_select(klass)
  return recursive_inject { |e| e.is_a?(klass) }
end

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



55
56
57
# File 'lib/logstash/config/config_ast.rb', line 55

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