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 15
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 37
def enumerator
  @statements
end
parse() click to toggle source

Parses the source. @return [void]

# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 23
def parse
  begin
    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 51
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 67
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 55
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 85
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 73
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

TODO: Fix the rubocop violations in this file between the following rubocop:disable/enable lines rubocop:disable Naming/MethodName

# File lib/puppet-strings/yard/parsers/puppet/parser.rb, line 45
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 61
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 79
def transform_TypeAlias(o)
  statement = PuppetStrings::Yard::Parsers::Puppet::DataTypeAliasStatement.new(o, @file)
  statement.extract_docstring(@lines)
  statement
end