Sha256: 5f516e8939d5e4bdfd1522dc1362e9ac87d0022c1b00e85b45750d3e4333ae54

Contents?: true

Size: 1.96 KB

Versions: 6

Compression:

Stored size: 1.96 KB

Contents

require 'puppet'
require 'puppet/pops'
require 'puppet-strings/yard/parsers/puppet/statement'

# Implements the Puppet language parser.
class PuppetStrings::Yard::Parsers::Puppet::Parser < YARD::Parser::Base
  attr_reader :file, :source

  # Initializes the parser.
  # @param [String] source The source being parsed.
  # @param [String] filename The file name of the file being parsed.
  # @return [void]
  def initialize(source, filename)
    @source = source
    @file = filename
    @visitor = ::Puppet::Pops::Visitor.new(self, 'transform')
  end

  # Parses the source.
  # @return [void]
  def parse
    begin
      @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

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

  private
  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

  def transform_Factory(o)
    @visitor.visit(o.current)
  end

  def transform_HostClassDefinition(o)
    statement = PuppetStrings::Yard::Parsers::Puppet::ClassStatement.new(o, @file)
    statement.extract_docstring(@lines)
    statement
  end

  def transform_ResourceTypeDefinition(o)
    statement = PuppetStrings::Yard::Parsers::Puppet::DefinedTypeStatement.new(o, @file)
    statement.extract_docstring(@lines)
    statement
  end

  def transform_FunctionDefinition(o)
    statement = PuppetStrings::Yard::Parsers::Puppet::FunctionStatement.new(o, @file)
    statement.extract_docstring(@lines)
    statement
  end

  def transform_Object(o)
    # Ignore anything else (will be compacted out of the resulting array)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
puppet-strings-1.2.1 lib/puppet-strings/yard/parsers/puppet/parser.rb
puppet-strings-1.2.0 lib/puppet-strings/yard/parsers/puppet/parser.rb
puppet-strings-1.1.1 lib/puppet-strings/yard/parsers/puppet/parser.rb
puppet-strings-1.1.0 lib/puppet-strings/yard/parsers/puppet/parser.rb
puppet-strings-1.0.0 lib/puppet-strings/yard/parsers/puppet/parser.rb
puppet-strings-0.99.0 lib/puppet-strings/yard/parsers/puppet/parser.rb