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.



15
16
17
18
19
# 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

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#sourceObject (readonly)

Returns the value of attribute source.



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

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.



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

def enumerator
  @statements
end

#parsevoid

This method returns an undefined value.

Parses the source.



23
24
25
26
27
28
29
30
31
32
33
# 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