Class: ASTTransform::Node
- Inherits:
-
Parser::AST::Node
- Object
- Parser::AST::Node
- ASTTransform::Node
- 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
Class Method Summary collapse
-
.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. -
.register(type) ⇒ void
Registers
selfas the class to construct fortypenodes. - .registry ⇒ Object
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.
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.
27 28 29 |
# File 'lib/ast_transform/node.rb', line 27 def register(type) Node.registry[type] = self end |
.registry ⇒ Object
43 44 45 |
# File 'lib/ast_transform/node.rb', line 43 def registry @registry ||= {} end |