class MODL::Parser::ClassExtractor

Extract a class from a ParsedPair object

Public Class Methods

extract(pair, global) click to toggle source
# 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) click to toggle source

Check for a reserved class name

# File lib/modl/parser/modl_class.rb, line 133
def self.reserved?(str)
  %w[map str arr num].include?(str)
end