class PuppetStrings::Yard::Parsers::Puppet::Parser

Implements the Puppet language parser.

Attributes

file[R]
source[R]

Public Class Methods

new(source, filename) click to toggle source

Initializes the parser. @param [String] source The source being parsed. @param [String] filename The file name of the file being parsed. @return [void]

# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 17
def initialize(source, filename) # rubocop:disable Lint/MissingSuper
  @source = source
  @file = filename
  @visitor = ::Puppet::Pops::Visitor.new(self, 'transform')
end

Public Instance Methods

enumerator() click to toggle source

Gets an enumerator for the statements that were parsed. @return Returns an enumerator for the statements that were parsed.

# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 43
def enumerator
  @statements
end
parse() click to toggle source

Parses the source. @return [void]

# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 25
def parse
  begin
    if @file.to_s.match?(%r{^plans|/plans/}) && Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') < 0
      log.warn "Skipping #{@file}: Puppet Plans require Puppet 5 or greater."
      return
    end
    Puppet[:tasks] = true if Puppet.settings.include?(:tasks)
    @statements ||= (@visitor.visit(::Puppet::Pops::Parser::Parser.new.parse_string(source)) || []).compact
  rescue ::Puppet::ParseError => e
    log.error "Failed to parse #{@file}: #{e.message}"
    @statements = []
  end
  @statements.freeze
  self
end

Private Instance Methods

transform_Factory(o) click to toggle source
# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 55
def transform_Factory(o)
  @visitor.visit(o.current)
end
transform_FunctionDefinition(o) click to toggle source
# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 71
def transform_FunctionDefinition(o)
  statement = PuppetStrings::Yard::Parsers::Puppet::FunctionStatement.new(o, @file)
  statement.extract_docstring(@lines)
  statement
end
transform_HostClassDefinition(o) click to toggle source
# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 59
def transform_HostClassDefinition(o)
  statement = PuppetStrings::Yard::Parsers::Puppet::ClassStatement.new(o, @file)
  statement.extract_docstring(@lines)
  statement
end
transform_Object(o) click to toggle source
# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 89
def transform_Object(o)
  # Ignore anything else (will be compacted out of the resulting array)
end
transform_PlanDefinition(o) click to toggle source
# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 77
def transform_PlanDefinition(o)
  statement = PuppetStrings::Yard::Parsers::Puppet::PlanStatement.new(o, @file)
  statement.extract_docstring(@lines)
  statement
end
transform_Program(o) click to toggle source
# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 49
def transform_Program(o)
  # Cache the lines of the source text; we'll use this to locate comments
  @lines = o.source_text.lines.to_a
  o.definitions.map { |d| @visitor.visit(d) }
end
transform_ResourceTypeDefinition(o) click to toggle source
# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 65
def transform_ResourceTypeDefinition(o)
  statement = PuppetStrings::Yard::Parsers::Puppet::DefinedTypeStatement.new(o, @file)
  statement.extract_docstring(@lines)
  statement
end
transform_TypeAlias(o) click to toggle source
# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 83
def transform_TypeAlias(o)
  statement = PuppetStrings::Yard::Parsers::Puppet::DataTypeAliasStatement.new(o, @file)
  statement.extract_docstring(@lines)
  statement
end