Class: MODL::Parser::Parsed

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

Overview

This class represents a MODL parse tree for a given MODL object. It tries to process the parse tree as it is generated as much as possible to save revisiting nodes unnecessarily.

Many of the method names are generated by ANTLR4 so are not ruby style.

Defined Under Namespace

Classes: ParsedArray, ParsedArrayConditional, ParsedArrayConditionalReturn, ParsedArrayItem, ParsedArrayValueItem, ParsedCondition, ParsedConditionGroup, ParsedConditionTest, ParsedFalse, ParsedMap, ParsedMapConditional, ParsedMapConditionalReturn, ParsedMapItem, ParsedNbArray, ParsedNull, ParsedNumber, ParsedPair, ParsedPrimitive, ParsedQuoted, ParsedString, ParsedStructure, ParsedTopLevelConditional, ParsedTopLevelConditionalReturn, ParsedTrue, ParsedValue, ParsedValueConditional, ParsedValueConditionalReturn, ParsedValueItem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MODLParserBaseListener

#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_pair, #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_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_pair, #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 = nil) ⇒ Parsed

Returns a new instance of Parsed.



27
28
29
30
# File 'lib/modl/parser/parsed.rb', line 27

def initialize(global = nil)
  @global = global
  @structures = []
end

Instance Attribute Details

#globalObject

Returns the value of attribute global.



25
26
27
# File 'lib/modl/parser/parsed.rb', line 25

def global
  @global
end

#structuresObject

Returns the value of attribute structures.



24
25
26
# File 'lib/modl/parser/parsed.rb', line 24

def structures
  @structures
end

Class Method Details

.additional_string_processing(text) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/modl/parser/parsed.rb', line 46

def self.additional_string_processing(text)
  text = Substitutions.process text
  # Special case for a possibly empty graved string ``
  unless text.nil?
    match_data = /^`([^`]*)`$/.match text
    return match_data[1] if match_data&.length&.positive?
  end
  text
end

.handle_empty_array_itemObject



1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
# File 'lib/modl/parser/parsed.rb', line 1250

def self.handle_empty_array_item
  # Create something for the blank array item
  #
  # The problem is that we might not have any context to tell us what type we need to create
  # so this currently defaults to the nil value
  #
  # TODO : Is there a way to know the type to create or is nil always acceptable?
  array_item = ParsedArrayItem.new @global
  array_item.arrayValueItem = ParsedArrayValueItem.new @global
  array_item.arrayValueItem.primitive = ParsedPrimitive.new @global
  array_item.arrayValueItem.primitive.nilVal = ParsedNull.instance
  array_item
end

Instance Method Details

#enterModl(ctx) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/modl/parser/parsed.rb', line 32

def enterModl(ctx)

  @global = GlobalParseContext.new if @global.nil?

  ctx_modl_structure = ctx.modl_structure
  ctx_modl_structure.each do |str|
    structure = ParsedStructure.new @global
    str.enter_rule(structure)
    @structures << structure
  end

  @global
end

#extract_hashObject

Convert the parse tree to a simpler structure suitable for JSON.generate.



1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
# File 'lib/modl/parser/parsed.rb', line 1392

def extract_hash
  result = []
  if @structures.length.positive?
    @structures.each do |s|
      value = s.extract_hash
      result << value unless value.nil?
    end
  else
    result = {}
  end
  case result.length
  when 0
    return ''
  when 1
    return result[0]
  end
  result
end