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.



181
182
183
184
185
186
# File 'lib/modl/parser/parsed.rb', line 181

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

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



174
175
176
# File 'lib/modl/parser/parsed.rb', line 174

def array
  @array
end

#finalObject

Returns the value of attribute final.



179
180
181
# File 'lib/modl/parser/parsed.rb', line 179

def final
  @final
end

#keyObject

Returns the value of attribute key.



172
173
174
# File 'lib/modl/parser/parsed.rb', line 172

def key
  @key
end

#key_listsObject

Returns the value of attribute key_lists.



176
177
178
# File 'lib/modl/parser/parsed.rb', line 176

def key_lists
  @key_lists
end

#mapObject

Returns the value of attribute map.



173
174
175
# File 'lib/modl/parser/parsed.rb', line 173

def map
  @map
end

#textObject

The simple text value rather than the object



178
179
180
# File 'lib/modl/parser/parsed.rb', line 178

def text
  @text
end

#typeObject

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



177
178
179
# File 'lib/modl/parser/parsed.rb', line 177

def type
  @type
end

#valueItemObject

Returns the value of attribute valueItem.



175
176
177
# File 'lib/modl/parser/parsed.rb', line 175

def valueItem
  @valueItem
end

Instance Method Details

#enterModl_pair(ctx) ⇒ Object

Raises:



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

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_string = ctx.NUMBER
  @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

  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

  set_pair_type

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

  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

  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
  when 'index'
    IndexExtractor.extract(self, @global)
  when 'hidden'
    extract_value
    invoke_deref
  else
    extract_value
    invoke_deref
  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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/modl/parser/parsed.rb', line 243

def extract_hash

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

  if value.is_a?(String) && (value.start_with?('%') || value.start_with?('`'))
    @text, _ignore = RefProcessor.deref(@text, @global)
  else
    @text = value
  end

  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



188
189
190
191
192
193
194
# File 'lib/modl/parser/parsed.rb', line 188

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



197
198
199
200
201
202
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
# File 'lib/modl/parser/parsed.rb', line 197

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