class Puppet::Pops::Parser::EvaluatingParser

Does not support “import” and parsing ruby files

Attributes

parser[R]

Public Instance Methods

acceptor() click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 79
def acceptor()
  Puppet::Pops::Validation::Acceptor.new
end
assert_and_report(parse_result) click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 87
def assert_and_report(parse_result)
  return nil unless parse_result
  if parse_result.source_ref.nil? or parse_result.source_ref == ''
    parse_result.source_ref = @file_source
  end
  validation_result = validate(parse_result)

  Puppet::Pops::IssueReporter.assert_and_report(validation_result,
                                        :emit_warnings => true)
  parse_result
end
clear() click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 46
def clear()
  @acceptor = nil
end
closure(model, scope) click to toggle source

Create a closure that can be called in the given scope

# File lib/puppet/pops/parser/evaluating_parser.rb, line 51
def closure(model, scope)
  Puppet::Pops::Evaluator::Closure.new(evaluator, model, scope)
end
convert_to_3x(object, scope) click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 69
def convert_to_3x(object, scope)
  val = evaluator.convert(object, scope, nil)
end
evaluate(scope, model) click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 55
def evaluate(scope, model)
  return nil unless model
  evaluator.evaluate(model, scope)
end
evaluate_file(file) click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 42
def evaluate_file(file)
  evaluate(parse_file(file))
end
evaluate_string(scope, s, file_source='unknown') click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 38
def evaluate_string(scope, s, file_source='unknown')
  evaluate(scope, parse_string(s, file_source))
end
evaluator() click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 60
def evaluator
  # Do not use the cached evaluator if this is a migration run
  if (Puppet.lookup(:migration_checker) { nil })
    return Puppet::Pops::Evaluator::EvaluatorImpl.new()
  end
  @@evaluator ||= Puppet::Pops::Evaluator::EvaluatorImpl.new()
  @@evaluator
end
parse_file(file) click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 32
def parse_file(file)
  @file_source = file
  clear()
  assert_and_report(parser.parse_file(file))
end
parse_string(s, file_source = 'unknown') click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 12
def parse_string(s, file_source = 'unknown')
  @file_source = file_source
  clear()
  # Handling of syntax error can be much improved (in general), now it bails out of the parser
  # and does not have as rich information (when parsing a string), need to update it with the file source
  # (ideally, a syntax error should be entered as an issue, and not just thrown - but that is a general problem
  # and an improvement that can be made in the eparser (rather than here).
  # Also a possible improvement (if the YAML parser returns positions) is to provide correct output of position.
  #
  begin
    assert_and_report(parser.parse_string(s))
  rescue Puppet::ParseErrorWithIssue => e
    raise e
  rescue Puppet::ParseError => e
    # TODO: This is not quite right, why does not the exception have the correct file?
    e.file = @file_source unless e.file.is_a?(String) && !e.file.empty?
    raise e
  end
end
quote(x) click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 99
def quote(x)
  self.class.quote(x)
end
validate(parse_result) click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 73
def validate(parse_result)
  resulting_acceptor = acceptor()
  validator(resulting_acceptor).validate(parse_result)
  resulting_acceptor
end
validator(acceptor) click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 83
def validator(acceptor)
  Puppet::Pops::Validation::ValidatorFactory_4_0.new().validator(acceptor)
end

Public Class Methods

new() click to toggle source
# File lib/puppet/pops/parser/evaluating_parser.rb, line 8
def initialize()
  @parser = Puppet::Pops::Parser::Parser.new()
end
quote(x) click to toggle source

Translates an already parsed string that contains control characters, quotes and backslashes into a quoted string where all such constructs have been escaped. Parsing the return value of this method using the puppet parser should yield exactly the same string as the argument passed to this method

The method makes an exception for the two character sequences $ and s. They will not be escaped since they have a special meaning in puppet syntax.

TODO: Handle uXXXX characters ??

@param x [String] The string to quote and “unparse” @return [String] The quoted string

# File lib/puppet/pops/parser/evaluating_parser.rb, line 116
def self.quote(x)
  escaped = '"'
  p = nil
  x.each_char do |c|
    case p
    when nil
      # do nothing
    when "\t"
      escaped << '\t'
    when "\n"
      escaped << '\n'
    when "\f"
      escaped << '\f'
    # TODO: \cx is a range of characters - skip for now
    #      when "\c"
    #        escaped << '\\c'
    when '"'
      escaped << '\"'
    when '\'
      escaped << if c == '$' || c == 's'; p; else '\\'; end # don't escape \ when followed by s or $
    else
      escaped << p
    end
    p = c
  end
  escaped << p unless p.nil?
  escaped << '"'
end