Class: Bade::AST::NodeRegistrator

Inherits:
Object
  • Object
show all
Defined in:
lib/bade/ast/node_registrator.rb

Class Method Summary collapse

Class Method Details

.create(type, parent, lineno: nil, filename: nil) ⇒ Bade::AST::Node

Method to create node backing instance

Parameters:

  • type (Symbol)

    type of the node

  • lineno (Fixnum) (defaults to: nil)

    line number of the node appearance

Returns:

Raises:

  • (::KeyError)


41
42
43
44
45
46
47
# File 'lib/bade/ast/node_registrator.rb', line 41

def create(type, parent, lineno: nil, filename: nil)
  klass = registered_types[type]

  raise ::KeyError, "Undefined node type #{type.inspect}" if klass.nil?

  klass.new(type, parent, lineno: lineno, filename: filename)
end

.register_type(klass, type) ⇒ nil

Method to map some node type to backing node class

Parameters:

  • type (Symbol)

    type of the node

  • klass (Class)

    registering class

Returns:

  • (nil)

Raises:

  • (StandardError)


28
29
30
31
32
# File 'lib/bade/ast/node_registrator.rb', line 28

def register_type(klass, type)
  raise StandardError, "Class #{klass} should be subclass of #{Node}" unless klass <= Node

  registered_types[type] = klass
end

.registered_typesHash<Symbol, Class>

Returns:

  • (Hash<Symbol, Class>)


17
18
19
# File 'lib/bade/ast/node_registrator.rb', line 17

def registered_types
  @registered_types ||= {}
end