Class: ASTTransform::Node

Inherits:
Parser::AST::Node
  • Object
show all
Defined in:
lib/ast_transform/node.rb

Overview

Base class for custom IR nodes. Transform authors subclass it and register a custom node type to get type-routed construction from s with domain accessors:

class InteractionNode < ASTTransform::Node
register :rspock_interaction

def cardinality = children[0]
end

s(:rspock_interaction, ...) # => InteractionNode

Custom node types are IR between stages that understand them and must be lowered before emission (the emitter enforces this). Standard-typed nodes deliberately stay plain Parser::AST::Node everywhere — parsed and +s+-built alike: AST::Node#eql? compares class, and Unparser verifies dynamic-string emission by re-parsing and eql?-comparing, so a custom class on a standard type breaks emission.

Direct Known Subclasses

Thunk

Class Method Summary collapse

Class Method Details

.build(type, children, properties = {}) ⇒ Parser::AST::Node

Builds a node of type: registered types construct their custom class, everything else a plain Parser::AST::Node.

Parameters:

  • type (Symbol)

    node type

  • children (Array)

    child nodes / literals

  • properties (Hash) (defaults to: {})

    node properties (e.g. location:)

Returns:

  • (Parser::AST::Node)


38
39
40
41
# File 'lib/ast_transform/node.rb', line 38

def build(type, children, properties = {})
  klass = Node.registry.fetch(type, ::Parser::AST::Node)
  klass.new(type, children, properties)
end

.register(type) ⇒ void

This method returns an undefined value.

Registers self as the class to construct for type nodes.

Parameters:

  • type (Symbol)

    the custom node type routed to this class



27
28
29
# File 'lib/ast_transform/node.rb', line 27

def register(type)
  Node.registry[type] = self
end

.registryObject



43
44
45
# File 'lib/ast_transform/node.rb', line 43

def registry
  @registry ||= {}
end