Class: Hocon::Impl::ConfigParser::ParseContext

Inherits:
Object
  • Object
show all
Defined in:
lib/hocon/impl/config_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(flavor, origin, document, includer, include_context) ⇒ ParseContext

Returns a new instance of ParseContext.



47
48
49
50
51
52
53
54
55
56
# File 'lib/hocon/impl/config_parser.rb', line 47

def initialize(flavor, origin, document, includer, include_context)
  @line_number = 1
  @document = document
  @flavor = flavor
  @base_origin = origin
  @includer = includer
  @include_context = include_context
  @path_stack = []
  @array_count = 0
end

Instance Method Details

#create_value_under_path(path, value) ⇒ Object



123
124
125
126
127
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
156
# File 'lib/hocon/impl/config_parser.rb', line 123

def create_value_under_path(path, value)
  # for path foo.bar, we are creating
  # { "foo" : { "bar" : value } }
  keys = []

  key = path.first
  remaining = path.remainder
  until key.nil?
    keys.push(key)
    if remaining.nil?
      break
    else
      key = remaining.first
      remaining = remaining.remainder
    end
  end

  # the setComments(null) is to ensure comments are only
  # on the exact leaf node they apply to.
  # a comment before "foo.bar" applies to the full setting
  # "foo.bar" not also to "foo"
  keys = keys.reverse
  # this is just a ruby means for doing first/rest
  deepest, *rest = *keys
  o = SimpleConfigObject.new(value.origin.with_comments(nil),
                             {deepest => value})
  while !rest.empty?
    deepest, *rest = *rest
    o = SimpleConfigObject.new(value.origin.with_comments(nil),
                               {deepest => o})
  end

  o
end

#full_current_pathObject



87
88
89
90
91
92
93
94
# File 'lib/hocon/impl/config_parser.rb', line 87

def full_current_path
  # pathStack has top of stack at front
  if @path_stack.empty?
    raise ConfigBugOrBrokenError, "Bug in parser; tried to get current path when at root"
  else
    Path.from_path_list(@path_stack.reverse)
  end
end

#line_originObject



79
80
81
# File 'lib/hocon/impl/config_parser.rb', line 79

def line_origin
  @base_origin.with_line_number(@line_number)
end

#parseObject



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/hocon/impl/config_parser.rb', line 374

def parse
  result = nil
  comments = []
  last_was_new_line = false
  @document.children.each do |node|
    if node.is_a?(ConfigNodeComment)
      comments << node.comment_text
      last_was_new_line = false
    elsif node.is_a?(ConfigNodeSingleToken)
      t = node.token
      if Tokens.newline?(t)
        @line_number += 1
        if last_was_new_line && result.nil?
          comments.clear
        elsif !result.nil?
          result = result.with_origin(result.origin.append_comments(comments.clone))
          comments.clear
          break
        end
        last_was_new_line = true
      end
    elsif node.is_a?(Hocon::Impl::ConfigNodeComplexValue)
      result = parse_value(node, comments)
      last_was_new_line = false
    end
  end
  result
end

#parse_array(n) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/hocon/impl/config_parser.rb', line 332

def parse_array(n)
  @array_count += 1

  array_origin = line_origin
  values = []

  last_was_new_line = false
  comments = []

  v = nil

  n.children.each do |node|
    if node.is_a?(ConfigNodeComment)
      comments << node.comment_text
      last_was_new_line = false
    elsif node.is_a?(ConfigNodeSingleToken) && Tokens.newline?(node.token)
      @line_number += 1
      if last_was_new_line && v.nil?
        comments.clear
      elsif !v.nil?
        values << v.with_origin(v.origin.append_comments(comments.clone))
        comments.clear
        v = nil
      end
      last_was_new_line = true
    elsif node.is_a?(Hocon::Impl::AbstractConfigNodeValue)
      last_was_new_line = false
      unless v.nil?
        values << v.with_origin(v.origin.append_comments(comments.clone))
        comments.clear
      end
      v = parse_value(node, comments)
    end
  end
  # There shouldn't be any comments at this point, but add them just in case
  unless v.nil?
    values << v.with_origin(v.origin.append_comments(comments.clone))
  end
  @array_count -= 1
  SimpleConfigList.new(array_origin, values)
end

#parse_concatenation(n) ⇒ Object

merge a bunch of adjacent values into one value; change unquoted text into a string value.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/hocon/impl/config_parser.rb', line 61

def parse_concatenation(n)
  # this trick is not done in JSON
  if @flavor.equal?(ConfigSyntax::JSON)
    raise ConfigBugOrBrokenError, "Found a concatenation node in JSON"
  end

  values = []

  n.children.each do |node|
    if node.is_a?(Hocon::Impl::AbstractConfigNodeValue)
      v = parse_value(node, nil)
      values.push(v)
    end
  end

  ConfigConcatenation.concatenate(values)
end

#parse_error(message, cause = nil) ⇒ Object



83
84
85
# File 'lib/hocon/impl/config_parser.rb', line 83

def parse_error(message, cause = nil)
  ConfigParseError.new(line_origin, message, cause)
end

#parse_include(values, n) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/hocon/impl/config_parser.rb', line 158

def parse_include(values, n)
  case n.kind
    when ConfigIncludeKind::URL
      url = nil
      begin
        url = Hocon::Impl::Url.new(n.name)
      rescue Hocon::Impl::Url::MalformedUrlError => e
        raise parse_error("include url() specifies an invalid URL: #{n.name}", e)
      end
      obj = @includer.include_url(@include_context, url)
    when ConfigIncludeKind::FILE
      obj = @includer.include_file(@include_context, n.name)
    when ConfigIncludeKind::CLASSPATH
      obj = @includer.include_resources(@include_context, n.name)
    when ConfigIncludeKind::HEURISTIC
      obj = @includer.include(@include_context, n.name)
    else
      raise ConfigBugOrBrokenError, "should not be reached"
  end

  # we really should make this work, but for now throwing an
  # exception is better than producing an incorrect result.
  # See https://github.com/typesafehub/config/issues/160
  if @array_count > 0 && (obj.resolve_status != Hocon::Impl::ResolveStatus::RESOLVED)
    raise parse_error("Due to current limitations of the config parser, when an include statement is nested inside a list value, " +
                          "${} substitutions inside the included file cannot be resolved correctly. Either move the include outside of the list value or " +
                          "remove the ${} statements from the included file.")
  end

  if !(@path_stack.empty?)
    prefix = full_current_path
    obj = obj.relativized(prefix)
  end

  obj.key_set.each do |key|
    v = obj.get(key)
    existing = values[key]
    if !(existing.nil?)
      values[key] = v.with_fallback(existing)
    else
      values[key] = v
    end
  end
end

#parse_object(n) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/hocon/impl/config_parser.rb', line 203

def parse_object(n)
  values = Hash.new
  object_origin = line_origin
  last_was_new_line = false

  nodes = n.children.clone
  comments = []
  i = 0
  while i < nodes.size
    node = nodes[i]
    if node.is_a?(ConfigNodeComment)
      last_was_new_line = false
      comments.push(node.comment_text)
    elsif node.is_a?(ConfigNodeSingleToken) && Tokens.newline?(node.token)
      @line_number += 1
      if last_was_new_line
        # Drop all comments if there was a blank line and start a new comment block
        comments.clear
      end
      last_was_new_line = true
    elsif !@flavor.equal?(ConfigSyntax::JSON) && node.is_a?(ConfigNodeInclude)
      parse_include(values, node)
      last_was_new_line = false
    elsif node.is_a?(Hocon::Impl::ConfigNodeField)
      last_was_new_line = false
      path = node.path.value
      comments += node.comments

      # path must be on-stack while we parse the value
      # Note that, in upstream, pathStack is a LinkedList, so use unshift instead of push
      @path_stack.unshift(path)
      if node.separator.equal?(Tokens::PLUS_EQUALS)
        # we really should make this work, but for now throwing
        # an exception is better than producing an incorrect
        # result. See
        # https://github.com/typesafehub/config/issues/160
        if @array_count > 0
          raise parse_error("Due to current limitations of the config parser, += does not work nested inside a list. " +
                                "+= expands to a ${} substitution and the path in ${} cannot currently refer to list elements. " +
                                "You might be able to move the += outside of the list and then refer to it from inside the list with ${}.")
        end

        # because we will put it in an array after the fact so
        # we want this to be incremented during the parseValue
        # below in order to throw the above exception.
        @array_count += 1
      end

      value_node = node.value

      # comments from the key token go to the value token
      new_value = parse_value(value_node, comments)

      if node.separator.equal?(Tokens::PLUS_EQUALS)
        @array_count -= 1

        concat = []
        previous_ref = ConfigReference.new(new_value.origin,
                                           Hocon::Impl::SubstitutionExpression.new(full_current_path, true))
        list = SimpleConfigList.new(new_value.origin, [new_value])
        concat << previous_ref
        concat << list
        new_value = ConfigConcatenation.concatenate(concat)
      end

      # Grab any trailing comments on the same line
      if i < nodes.size - 1
        i += 1
        while i < nodes.size
          if nodes[i].is_a?(ConfigNodeComment)
            comment = nodes[i]
            new_value = new_value.with_origin(new_value.origin.append_comments([comment.comment_text]))
            break
          elsif nodes[i].is_a?(ConfigNodeSingleToken)
            curr = nodes[i]
            if curr.token.equal?(Tokens::COMMA) || Tokens.ignored_whitespace?(curr.token)
              # keep searching, as there could still be a comment
            else
              i -= 1
              break
            end
          else
            i -= 1
            break
          end
          i += 1
        end
      end

      @path_stack.shift

      key = path.first
      remaining = path.remainder

      if remaining.nil?
        existing = values[key]
        unless existing.nil?
          # In strict JSON, dups should be an error; while in
          # our custom config language, they should be merged
          # if the value is an object (or substitution that
          # could become an object).

          if @flavor.equal?(ConfigSyntax::JSON)
            raise parse_error("JSON does not allow duplicate fields: '#{key}'" +
                                  " was already seen at #{existing.origin().description()}")
          else
            new_value = new_value.with_fallback(existing)
          end
        end
        values[key] = new_value
      else
        if @flavor == ConfigSyntax::JSON
          raise Hocon::ConfigError::ConfigBugOrBrokenError, "somehow got multi-element path in JSON mode"
        end

        obj = create_value_under_path(remaining, new_value)
        existing = values[key]
        if !existing.nil?
          obj = obj.with_fallback(existing)
        end
        values[key] = obj
      end
    end
    i += 1
  end

  SimpleConfigObject.new(object_origin, values)
end

#parse_value(n, comments) ⇒ Object



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
# File 'lib/hocon/impl/config_parser.rb', line 96

def parse_value(n, comments)
  starting_array_count = @array_count

  if n.is_a?(Hocon::Impl::ConfigNodeSimpleValue)
    v = n.value
  elsif n.is_a?(Hocon::Impl::ConfigNodeObject)
    v = parse_object(n)
  elsif n.is_a?(Hocon::Impl::ConfigNodeArray)
    v = parse_array(n)
  elsif n.is_a?(Hocon::Impl::ConfigNodeConcatenation)
    v = parse_concatenation(n)
  else
    raise parse_error("Expecting a value but got wrong node type: #{n.class}")
  end

  unless comments.nil? || comments.empty?
    v = v.with_origin(v.origin.prepend_comments(comments.clone))
    comments.clear
  end

  unless @array_count == starting_array_count
    raise ConfigBugOrBrokenError, "Bug in config parser: unbalanced array count"
  end

  v
end