Class: Puppet::Pops::Parser::EvaluatingParser

Inherits:
Object
  • Object
show all
Extended by:
Concurrent::ThreadLocalSingleton
Defined in:
lib/puppet/pops/parser/evaluating_parser.rb

Overview

Does not support “import” and parsing ruby files

Direct Known Subclasses

EvaluatingEppParser

Defined Under Namespace

Classes: EvaluatingEppParser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concurrent::ThreadLocalSingleton

singleton

Constructor Details

#initializeEvaluatingParser

Returns a new instance of EvaluatingParser.


13
14
15
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 13

def initialize()
  @parser = Parser.new()
end

Instance Attribute Details

#parserObject (readonly)


11
12
13
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 11

def parser
  @parser
end

Class Method Details

.quote(x) ⇒ String

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 ??


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 128

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

Instance Method Details

#acceptorObject


91
92
93
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 91

def acceptor()
  Validation::Acceptor.new
end

#assert_and_report(parse_result) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 99

def assert_and_report(parse_result)
  return nil unless parse_result
  if parse_result['source_ref'].nil? || parse_result['source_ref'] == ''
    parse_result['source_ref'] = @file_source
  end
  validation_result = validate(parse_result.model)

  IssueReporter.assert_and_report(validation_result,
                                        :emit_warnings => true)
  parse_result
end

#clearObject


51
52
53
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 51

def clear()
  @acceptor = nil
end

#closure(model, scope) ⇒ Object

Create a closure that can be called in the given scope


56
57
58
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 56

def closure(model, scope)
  Evaluator::Closure::Dynamic.new(evaluator, model, scope)
end

#convert_to_3x(object, scope) ⇒ Object


81
82
83
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 81

def convert_to_3x(object, scope)
  evaluator.convert(object, scope, nil)
end

#evaluate(scope, model) ⇒ Object


60
61
62
63
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 60

def evaluate(scope, model)
  return nil unless model
  evaluator.evaluate(model, scope)
end

#evaluate_expression_with_bindings(scope, variable_bindings, expression) ⇒ Object

Evaluates the given expression in a local scope with the given variable bindings set in this local scope, returns what the expression returns.


68
69
70
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 68

def evaluate_expression_with_bindings(scope, variable_bindings, expression)
  evaluator.evaluate_block_with_bindings(scope, variable_bindings, expression)
end

#evaluate_file(scope, file) ⇒ Object


47
48
49
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 47

def evaluate_file(scope, file)
  evaluate(scope, parse_file(file))
end

#evaluate_string(scope, s, file_source = nil) ⇒ Object


43
44
45
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 43

def evaluate_string(scope, s, file_source = nil)
  evaluate(scope, parse_string(s, file_source))
end

#evaluatorObject


72
73
74
75
76
77
78
79
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 72

def evaluator  # Do not use the cached evaluator if this is a migration run

  if (Puppet.lookup(:migration_checker) { nil })
    return Evaluator::EvaluatorImpl.new()
  end
  @@evaluator ||= Evaluator::EvaluatorImpl.new()
  @@evaluator
end

#parse_file(file) ⇒ Object


37
38
39
40
41
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 37

def parse_file(file)
  @file_source = file
  clear()
  assert_and_report(parser.parse_file(file)).model
end

#parse_string(s, file_source = nil) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 17

def parse_string(s, file_source = nil)
  @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, file_source)).model
  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) ⇒ Object


111
112
113
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 111

def quote(x)
  self.class.quote(x)
end

#validate(parse_result) ⇒ Object


85
86
87
88
89
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 85

def validate(parse_result)
  resulting_acceptor = acceptor()
  validator(resulting_acceptor).validate(parse_result)
  resulting_acceptor
end

#validator(acceptor) ⇒ Object


95
96
97
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 95

def validator(acceptor)
  Validation::ValidatorFactory_4_0.new().validator(acceptor)
end