Class: Puppet_X::Binford2k::Itemize::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/binford2k/itemize/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = {}) ⇒ Parser

Returns a new instance of Parser.



8
9
10
11
12
13
14
15
16
17
# File 'lib/puppet_x/binford2k/itemize/parser.rb', line 8

def initialize(filename, options = {})
  @@visitor ||= Puppet::Pops::Visitor.new(nil, "count", 0, 0)
  @filename   = filename
  @options    = options
  @results    = {
    :types     => {},
    :classes   => {},
    :functions => {},
  }
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



6
7
8
# File 'lib/puppet_x/binford2k/itemize/parser.rb', line 6

def results
  @results
end

Instance Method Details

#compute(target) ⇒ Object

Start walking the tree and count each tracked token



42
43
44
45
46
47
48
49
# File 'lib/puppet_x/binford2k/itemize/parser.rb', line 42

def compute(target)
  @path = []
  count(target)
  target._pcore_all_contents(@path) { |element| count(element) }
# Puppet 4.x version
#    target.eAllContents.each {|m|  abc(m) }
  @results
end

#count(o) ⇒ Object



51
52
53
# File 'lib/puppet_x/binford2k/itemize/parser.rb', line 51

def count(o)
  @@visitor.visit_this_0(self, o)
end

#count_CallNamedFunctionExpression(o) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/puppet_x/binford2k/itemize/parser.rb', line 73

def count_CallNamedFunctionExpression(o)
  function_name = o.functor_expr.value
  case function_name
  when 'include'
    o.arguments.each do |klass|
      case klass
      when Puppet::Pops::Model::ConcatenatedString
        # Because this is pre-compilation, we cannot resolve variables. So just tag w/ a marker
        # TODO: This should go somewhere else, but I'm not entirely sure where just now.
        record(:classes, klass.segments.map {|t| t.value rescue nil }.join('<??>'))
      else
        record(:classes, klass.value)
      end
    end

  when 'create_resources'
    Puppet.warning 'create_resources detected. Please update to use iteration instead.'
    record(:functions, function_name)
    record(:types, o.arguments.first.value)

  else
    record(:functions, function_name)
  end
end

#count_Object(o) ⇒ Object



55
56
57
# File 'lib/puppet_x/binford2k/itemize/parser.rb', line 55

def count_Object(o)
  # nop
end

#count_ResourceExpression(o) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/puppet_x/binford2k/itemize/parser.rb', line 59

def count_ResourceExpression(o)
  resource_name = o.type_name.value
  case resource_name
  when 'class'
    # for classes declared as resource-style, we have to traverse back up the
    # tree to see if this resource body was declared by a class resource.
    o.bodies.each do |klass|
      record(:classes, klass.title.value)
    end
  else
    record(:types, resource_name)
  end
end

#dump!Object



98
99
100
101
# File 'lib/puppet_x/binford2k/itemize/parser.rb', line 98

def dump!
  require 'json'
  puts JSON.pretty_generate(@results)
end

#parse!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet_x/binford2k/itemize/parser.rb', line 27

def parse!
  begin
    parser = Puppet::Pops::Parser::EvaluatingParser.new
    source = Puppet::FileSystem.read(@filename)
    result = parser.parse_string(source, @filename)
    compute(result)
  rescue => e
    Puppet.err "Parse error for #{@filename}."
    Puppet.err e.message
    Puppet.debug e.backtrace.join "\n"
  end
  self
end

#record(kind, thing) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/puppet_x/binford2k/itemize/parser.rb', line 19

def record(kind, thing)
  fail("Unknown kind #{kind}") unless @results.keys.include? kind

  thing = thing.sub(/^::/, '')
  @results[kind][thing] ||= 0
  @results[kind][thing]  += 1
end