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

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

Overview

Does not support “import” and parsing ruby files

Direct Known Subclasses

Transitional

Defined Under Namespace

Classes: EvaluatingEppParser, Transitional

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEvaluatingParser

Returns a new instance of EvaluatingParser.



8
9
10
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 8

def initialize()
  @parser = Puppet::Pops::Parser::Parser.new()
end

Instance Attribute Details

#parserObject (readonly)



6
7
8
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 6

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

Parameters:

  • x (String)

    The string to quote and “unparse”

Returns:

  • (String)

    The quoted string



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 142

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



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

def acceptor()
  Puppet::Pops::Validation::Acceptor.new
end

#assert_and_report(parse_result) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 69

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)

  max_errors = Puppet[:max_errors]
  max_warnings = Puppet[:max_warnings] + 1
  max_deprecations = Puppet[:max_deprecations] + 1

  # If there are warnings output them
  warnings = validation_result.warnings
  if warnings.size > 0
    formatter = Puppet::Pops::Validation::DiagnosticFormatterPuppetStyle.new
    emitted_w = 0
    emitted_dw = 0
    validation_result.warnings.each {|w|
      if w.severity == :deprecation
        # Do *not* call Puppet.deprecation_warning it is for internal deprecation, not
        # deprecation of constructs in manifests! (It is not designed for that purpose even if
        # used throughout the code base).
        #
        Puppet.warning(formatter.format(w)) if emitted_dw < max_deprecations
        emitted_dw += 1
      else
        Puppet.warning(formatter.format(w)) if emitted_w < max_warnings
        emitted_w += 1
      end
      break if emitted_w > max_warnings && emitted_dw > max_deprecations # but only then
    }
  end

  # If there were errors, report the first found. Use a puppet style formatter.
  errors = validation_result.errors
  if errors.size > 0
    formatter = Puppet::Pops::Validation::DiagnosticFormatterPuppetStyle.new
    if errors.size == 1 || max_errors <= 1
      # raise immediately
      raise Puppet::ParseError.new(formatter.format(errors[0]))
    end
    emitted = 0
    errors.each do |e|
      Puppet.err(formatter.format(e))
      emitted += 1
      break if emitted >= max_errors
    end
    warnings_message = warnings.size > 0 ? ", and #{warnings.size} warnings" : ""
    giving_up_message = "Found #{errors.size} errors#{warnings_message}. Giving up"
    exception = Puppet::ParseError.new(giving_up_message)
    exception.file = errors[0].file
    raise exception
  end
  parse_result
end

#clearObject



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

def clear()
  @acceptor = nil
end

#evaluate(scope, model) ⇒ Object



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

def evaluate(scope, model)
  return nil unless model
  ast = Puppet::Pops::Model::AstTransformer.new(@file_source, nil).transform(model)
  return nil unless ast
  ast.safeevaluate(scope)
end

#evaluate_file(file) ⇒ Object



40
41
42
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 40

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

#evaluate_string(scope, s, file_source = 'unknown') ⇒ Object



36
37
38
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 36

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

#parse_file(file) ⇒ Object



30
31
32
33
34
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 30

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

#parse_string(s, file_source = 'unknown') ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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::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



125
126
127
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 125

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

#validate(parse_result) ⇒ Object



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

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

#validator(acceptor) ⇒ Object



65
66
67
# File 'lib/puppet/pops/parser/evaluating_parser.rb', line 65

def validator(acceptor)
  Puppet::Pops::Validation::ValidatorFactory_3_1.new().validator(acceptor)
end