Class: MODL::Parser::ClassExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/modl/parser/modl_class.rb

Overview

Extract a class from a ParsedPair object

Class Method Summary collapse

Class Method Details

.extract(pair, global) ⇒ Object

Raises:



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
124
125
126
127
128
129
130
# File 'lib/modl/parser/modl_class.rb', line 74

def self.extract(pair, global)
  return unless pair.type == 'class'

  clazz = MODLClass.new
  map = pair.map if pair.map
  map = pair.valueItem&.value&.map if pair.valueItem&.value&.map

  map.mapItems.each do |item|
    next unless item&.pair&.type

    case item&.pair&.type
    when 'id'
      str_value = item.pair.valueItem.value.primitive.string.string
      raise InterpreterError, 'Reserved class id - cannot redefine: ' + str_value if reserved?(str_value)

      clazz.id = str_value
    when 'name'
      str_value = item.pair.valueItem.value.primitive.string.string
      raise InterpreterError, 'Reserved class name - cannot redefine: ' + str_value if reserved?(str_value)

      clazz.name = str_value
    when 'superclass'
      str_value = item.pair.valueItem.value.primitive.string.string
      clazz.superclass = str_value
    when 'keylist'
      clazz.assign = item.pair.key_lists
    when 'allow'
      clazz.allow = item.pair.array if item.pair.array
      clazz.allow = item.pair.valueItem.value.array if item.pair.valueItem.value.array
    when 'expect'
      clazz.expect = item.pair.array if item.pair.array
      clazz.expect = item.pair.valueItem.value.array if item.pair.valueItem.value.array
    else
      clazz.content[item.pair.key] = item.pair.array if item.pair.array
      clazz.content[item.pair.key] = item.pair.map if item.pair.map
      clazz.content[item.pair.key] = item.pair.valueItem.value if item.pair.valueItem.value
    end
  end

  superclass = clazz.superclass

  if superclass && !reserved?(superclass) && !global.has_class?(superclass)
    raise InterpreterError, 'Invalid superclass: ' + superclass.to_s
  end
  raise InterpreterError, 'Missing id for class' if clazz.id.nil?

  # Make sure the class name isn't redefining an existing class
  if !global.has_class?(clazz.id) && !global.has_class?(clazz.name)

    # store the classes by id and name to make them easier to find later
    global.classs(clazz)
  else
    id = clazz.id.nil? ? 'undefined' : clazz.id
    name = clazz.name.nil? ? 'undefined' : clazz.name
    raise InterpreterError, 'Class name or id already defined - cannot redefine: ' + id + ', ' + name
  end
end

.reserved?(str) ⇒ Boolean

Check for a reserved class name

Returns:

  • (Boolean)


133
134
135
# File 'lib/modl/parser/modl_class.rb', line 133

def self.reserved?(str)
  %w[map str arr num].include?(str)
end