Class: MODL::Parser::Parsed::ParsedPair

Inherits:
MODLParserBaseListener show all
Defined in:
lib/modl/parser/parsed.rb

Overview

Class to represent a parsed grammar object

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from MODLParserBaseListener

#enterModl, #enterModl_array, #enterModl_array_conditional, #enterModl_array_conditional_return, #enterModl_array_item, #enterModl_array_value_item, #enterModl_condition, #enterModl_condition_group, #enterModl_condition_test, #enterModl_map, #enterModl_map_conditional, #enterModl_map_conditional_return, #enterModl_map_item, #enterModl_nb_array, #enterModl_operator, #enterModl_primitive, #enterModl_structure, #enterModl_top_level_conditional, #enterModl_top_level_conditional_return, #enterModl_value, #enterModl_value_conditional, #enterModl_value_conditional_return, #enterModl_value_item, #enter_every_rule, #exitModl, #exitModl_array, #exitModl_array_conditional, #exitModl_array_conditional_return, #exitModl_array_item, #exitModl_array_value_item, #exitModl_condition, #exitModl_condition_group, #exitModl_condition_test, #exitModl_map, #exitModl_map_conditional, #exitModl_map_conditional_return, #exitModl_map_item, #exitModl_nb_array, #exitModl_operator, #exitModl_pair, #exitModl_primitive, #exitModl_structure, #exitModl_top_level_conditional, #exitModl_top_level_conditional_return, #exitModl_value, #exitModl_value_conditional, #exitModl_value_conditional_return, #exitModl_value_item, #exit_every_rule, #visit_error_node, #visit_terminal

Methods inherited from MODLParserListener

#enterModl, #enterModl_array, #enterModl_array_conditional, #enterModl_array_conditional_return, #enterModl_array_item, #enterModl_array_value_item, #enterModl_condition, #enterModl_condition_group, #enterModl_condition_test, #enterModl_map, #enterModl_map_conditional, #enterModl_map_conditional_return, #enterModl_map_item, #enterModl_nb_array, #enterModl_operator, #enterModl_primitive, #enterModl_structure, #enterModl_top_level_conditional, #enterModl_top_level_conditional_return, #enterModl_value, #enterModl_value_conditional, #enterModl_value_conditional_return, #enterModl_value_item, #exitModl, #exitModl_array, #exitModl_array_conditional, #exitModl_array_conditional_return, #exitModl_array_item, #exitModl_array_value_item, #exitModl_condition, #exitModl_condition_group, #exitModl_condition_test, #exitModl_map, #exitModl_map_conditional, #exitModl_map_conditional_return, #exitModl_map_item, #exitModl_nb_array, #exitModl_operator, #exitModl_pair, #exitModl_primitive, #exitModl_structure, #exitModl_top_level_conditional, #exitModl_top_level_conditional_return, #exitModl_value, #exitModl_value_conditional, #exitModl_value_conditional_return, #exitModl_value_item

Constructor Details

#initialize(global) ⇒ ParsedPair

Returns a new instance of ParsedPair.



208
209
210
211
212
213
214
# File 'lib/modl/parser/parsed.rb', line 208

def initialize(global)
  @global = global
  @needs_defref = true
  @final = false
  @file_importer = FileImporter.instance
  @loaded = false
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



199
200
201
# File 'lib/modl/parser/parsed.rb', line 199

def array
  @array
end

#finalObject

Returns the value of attribute final.



204
205
206
# File 'lib/modl/parser/parsed.rb', line 204

def final
  @final
end

#keyObject

Returns the value of attribute key.



197
198
199
# File 'lib/modl/parser/parsed.rb', line 197

def key
  @key
end

#key_listsObject

Returns the value of attribute key_lists.



201
202
203
# File 'lib/modl/parser/parsed.rb', line 201

def key_lists
  @key_lists
end

#loadedObject

For *load instructions so we don’t load twice



205
206
207
# File 'lib/modl/parser/parsed.rb', line 205

def loaded
  @loaded
end

#mapObject

Returns the value of attribute map.



198
199
200
# File 'lib/modl/parser/parsed.rb', line 198

def map
  @map
end

#textObject

The simple text value rather than the object



203
204
205
# File 'lib/modl/parser/parsed.rb', line 203

def text
  @text
end

#typeObject

A string set to the type of pair that we have found bases on its key



202
203
204
# File 'lib/modl/parser/parsed.rb', line 202

def type
  @type
end

#valueItemObject

Returns the value of attribute valueItem.



200
201
202
# File 'lib/modl/parser/parsed.rb', line 200

def valueItem
  @valueItem
end

Instance Method Details

#enterModl_pair(ctx) ⇒ Object

Raises:



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
331
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/modl/parser/parsed.rb', line 291

def enterModl_pair(ctx)
  @type = 'pair' # default the type to an ordinary pair

  ctx_string = ctx.STRING
  @key = ctx_string.to_s unless ctx_string.nil?
  ctx_quoted = ctx.QUOTED
  unless ctx_quoted.nil?
    @key = ctx_quoted.to_s
    @key = Sutil.toptail(@key) # remove the quotes
  end

  raise InterpreterError, 'Invalid key - null, true, or false keys are not allowed.' if @key.nil?

  if @key.include?('%') || @key.include?('`')
    @key, new_value = RefProcessor.deref @key, @global
    unless @key.is_a?(String)
      @key = new_value.is_a?(String) ? new_value : new_value.text
    end
    raise InterpreterError, "Error: '" + @key.to_s + "' should de-ref to a string." unless key.is_a?(String)
  end

  @final = true if @key.upcase == @key

  modl_array = ctx.modl_array
  modl_map = ctx.modl_map
  modl_value_item = ctx.modl_value_item

  if !modl_array.nil?
    @array = ParsedArray.new @global
    modl_array.enter_rule(@array)
  elsif !modl_map.nil?
    @map = ParsedMap.new @global
    modl_map.enter_rule(@map)
  elsif !modl_value_item.nil?
    @valueItem = ParsedValueItem.new @global
    modl_value_item.enter_rule(@valueItem)
  end

  set_pair_type
  raise InterpreterError, 'Invalid keyword: ' + @key if @type == 'pair' && @key.start_with?('*')

  validate_key if @type == 'pair' || @type == 'hidden'

  # Type-specific processing
  case @type
  when 'class'
    ClassExtractor.extract(self, @global)
  when 'id'
    extract_value
  when 'name'
    extract_value
  when 'superclass'
    extract_value
  when 'allow'
    extract_value
  when 'keylist'
    KeylistExtractor.extract(self, @valueItem) if @valueItem
    KeylistExtractor.extract(self, @array) if @array
  when 'version'
    extract_value

    raise InterpreterError, 'Invalid MODL version: nil' if @valueItem.value.primitive.number.nil?
    raise InterpreterError, 'Invalid MODL version: ' + @valueItem.value.primitive.number.num.to_s if @valueItem.value.primitive.number.num.is_a? Float
    raise InterpreterError, 'Invalid MODL version: ' + @valueItem.value.primitive.number.num.to_s if @valueItem.value.primitive.number.num.zero?
    raise InterpreterError, 'MODL version should be on the first line if specified.' if @global.has_pairs?
    @global.syntax_version = @valueItem.value.primitive.number.num

  when 'method'
    MethodExtractor.extract(self, @global)
  when 'transform'
    extract_transform @valueItem
  when 'import'
    files = @valueItem.extract_hash if @valueItem
    files = @array.extract_hash if @array
    @file_importer.import_files files, @global unless @global.in_condition?
  when 'index'
    IndexExtractor.extract(self, @global)
  when 'hidden'
    extract_value
  else
    extract_value
  end

  return if @global.in_condition? # Don't store pairs in conditionals until we evaluate the conditions

  if @key.start_with? '_'
    k = Sutil.tail(@key)
    existing = @global.pair(k)
    raise InterpreterError, 'Already defined ' + k + ' as final.' if existing&.final

    @global.pair(k, self)
  end
  existing = @global.pair(@key)
  raise InterpreterError, 'Already defined ' + @key + ' as final.' if existing&.final

  @global.pair(@key, self)
end

#extract_hashObject

Convert this object to a simple hash ready for JSON.generate



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/modl/parser/parsed.rb', line 271

def extract_hash

  value = @array.extract_hash if @array
  value = @valueItem.extract_hash if @valueItem
  value = @map.extract_hash if @map

  @text = value

  return if @type == 'index'
  return if @type == 'hidden'
  return if @type == 'version'
  return if @type == 'class'
  return if @type == 'method'
  return if @type == 'import'
  return if @type == 'allow'
  return if @type == 'expect'

  {@key => @text}
end

#find_property(key) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/modl/parser/parsed.rb', line 216

def find_property(key)
  return self if key == @key

  return @map.find_property(key) if @map
  return @array.find_property(key) if @array
  return @valueItem.find_property(key) if @valueItem
end

#set_value(value) ⇒ Object

Set the appropriate field base on the value type



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
# File 'lib/modl/parser/parsed.rb', line 225

def set_value(value)
  if value.is_a? Array
    @map = nil
    @array = ParsedArray.new @global
    @array.abstractArrayItems = []
    value.each do |item|
      array_item = ParsedArrayItem.new @global
      array_item.arrayValueItem = ParsedArrayValueItem.new @global
      array_item.arrayValueItem.primitive = ParsedPrimitive.new(@global)
      array_item.arrayValueItem.primitive.string = ParsedString.new(item)
      array_item.arrayValueItem.primitive.text = item
      @array.abstractArrayItems << array_item
    end
    @valueItem = nil
    @text = @array.extract_hash
    return
  elsif value.is_a?(ParsedPair)
    @map = value.map ? value.map : nil
    @array = value.array ? value.array : nil
    @valueItem = value.valueItem ? value.valueItem : nil
    return
  elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
    @map = nil
    @array = nil
    @valueItem = ParsedValueItem.new @global
    @valueItem.value = ParsedValue.new @global
    @valueItem.value.primitive = ParsedPrimitive.new(@global)
    if value
      @valueItem.value.primitive.trueVal = ParsedTrue.instance
    else
      @valueItem.value.primitive.falseVal = ParsedFalse.instance
    end
    @valueItem.value.primitive.text = value
    @valueItem.value.text = value
    @text = value
    return
  end
  value = value.extract_hash unless value.is_a?(String) || value.is_a?(Integer)
  @map = nil
  @array = nil
  @valueItem = ParsedValueItem.new @global
  @valueItem.value = ParsedString.new(value)
  @text = value
end