Class: PuppetStrings::Yard::Parsers::Puppet::Parser

Inherits:
YARD::Parser::Base
  • Object
show all
Defined in:
lib/puppet-strings/yard/parsers/puppet/parser.rb

Overview

Implements the Puppet language parser.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, filename) ⇒ void

Initializes the parser.

Parameters:

  • source (String)

    The source being parsed.

  • filename (String)

    The file name of the file being parsed.



13
14
15
16
17
# File 'lib/puppet-strings/yard/parsers/puppet/parser.rb', line 13

def initialize(source, filename)
  @source = source
  @file = filename
  @visitor = ::Puppet::Pops::Visitor.new(self, 'transform')
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/puppet-strings/yard/parsers/puppet/parser.rb', line 7

def file
  @file
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/puppet-strings/yard/parsers/puppet/parser.rb', line 7

def source
  @source
end

Instance Method Details

#enumeratorObject

Gets an enumerator for the statements that were parsed.

Returns:

  • Returns an enumerator for the statements that were parsed.



39
40
41
# File 'lib/puppet-strings/yard/parsers/puppet/parser.rb', line 39

def enumerator
  @statements
end

#parsevoid

This method returns an undefined value.

Parses the source.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/puppet-strings/yard/parsers/puppet/parser.rb', line 21

def parse
  begin
    Puppet[:tasks] = true if Puppet.settings.include?(:tasks)
    if Puppet::Util::Package.versioncmp(Puppet.version, "5.0.0") < 0 && @file.to_s.match(/^plans\//)
      log.warn "Skipping #{@file}: Puppet Plans require Puppet 5 or greater."
      return
    end
    @statements ||= (@visitor.visit(::Puppet::Pops::Parser::Parser.new.parse_string(source)) || []).compact
  rescue ::Puppet::ParseError => ex
    log.error "Failed to parse #{@file}: #{ex.message}"
    @statements = []
  end
  @statements.freeze
  self
end