Class: Carbon::Compiler::Node::Base
- Inherits:
-
Object
- Object
- Carbon::Compiler::Node::Base
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/carbon/compiler/node/base.rb
Overview
A base-level node. Contains all of the basic behaviors that a node needs.
Direct Known Subclasses
Definition::Class, Definition::Class::Element, Definition::Directive, Definition::Directive::Function, Definition::Enum, Definition::Enum::Element, Definition::Function, Definition::Function::Body, Definition::Function::Name, Definition::Function::Parameter, Definition::Function::Parameters, Definition::Module, Definition::Struct, Definition::Struct::Element, EType, EType::Option, EType::Star, Expression::Assignment, Expression::Call::Access, Expression::Call::Attribute, Expression::Call::Enum, Expression::Call::Module, Expression::Call::Parameters, Expression::Call::Self, Expression::Call::Unified, Expression::Literal, Expression::Operation, Expression::Unit, Name, Root, Statement::Catch, Statement::ElsIf, Statement::Else, Statement::Finally, Statement::For, Statement::If, Statement::Let, Statement::Match, Statement::Return, Statement::Try, Statement::While
Instance Attribute Summary collapse
-
#children ⇒ Array<Node, Scanner::Token>
readonly
The node’s children.
-
#location ⇒ Location
readonly
The location describing the node.
Class Method Summary collapse
-
.mapping(data = nil) ⇒ Hash, void
(also: attribute, attributes)
Creates a “mapping.” This contructs a mapping between a child and a name.
Instance Method Summary collapse
-
#[](index) ⇒ Node, ...
Access a specific child at the given index.
-
#accept(visitor, *arguments) ⇒ Object
Accepts the current visitor unto itself.
-
#attributes!(attributes) ⇒ Base
Sets the attributes of a copy of this node to the given attributes.
-
#behavior? ⇒ Boolean
Returns true if this node is a behavior definition.
- #data ⇒ Object
-
#data? ⇒ Boolean
Returns true if this node is a data definition.
-
#each {|child| ... } ⇒ Enumerator<Node, Scanner::Token>
Yields all children one by one, if a block is given.
-
#identity? ⇒ Boolean
Returns true if this node is an identity definition.
-
#initialize(children, data = {}) ⇒ Base
constructor
Initialize the node with the given children and the data.
-
#map! ⇒ Base
Maps the children using the given block.
-
#merge!(options) ⇒ Base
(also: #merge)
Using the ClassMethods#mapping, it merges the given hash into the children.
-
#type!(type) ⇒ Base
Sets the type of a copy of this node to the given type.
-
#update!(children) ⇒ Base
(also: #update)
Sets the children of a copy of this node to the given children.
Constructor Details
#initialize(children, data = {}) ⇒ Base
Initialize the node with the given children and the data.
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/carbon/compiler/node/base.rb', line 94 def initialize(children, data = {}) @children = children.dup.freeze @type = data[:type] @attributes = data.fetch(:attributes, []) if data.key?(:location) @location = data[:location] else derive_location(data.fetch(:components, children)) end freeze end |
Instance Attribute Details
#children ⇒ Array<Node, Scanner::Token> (readonly)
The node’s children. This is most often an array of nodes, but can sometimes include Scanner tokens as well.
50 51 52 |
# File 'lib/carbon/compiler/node/base.rb', line 50 def children @children end |
#location ⇒ Location (readonly)
The location describing the node. This is derived solely from the children that make up the node, and so is considered auxillary.
56 57 58 |
# File 'lib/carbon/compiler/node/base.rb', line 56 def location @location end |
Class Method Details
.mapping(data = nil) ⇒ Hash, void Also known as: attribute, attributes
Creates a “mapping.” This contructs a mapping between a child and a name. For example, a for loop has four children: ‘initial`, `condition`, `increment`, and `body`. This creates the association between the numerical index of the child and the symbolic name of the child.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/carbon/compiler/node/base.rb', line 25 def self.mapping(data = nil) if data.nil? @mapping ||= Concurrent::Hash.new else @mapping = Concurrent::Hash.new.merge(data) data.each do |key, value| fail if key == :klass define_method(key) { @children[value] } end end end |
Instance Method Details
#[](index) ⇒ Node, ...
Access a specific child at the given index. If a child exists, it is returned; otherwise, ‘nil` is returned.
70 |
# File 'lib/carbon/compiler/node/base.rb', line 70 def_delegator :children, :[] |
#accept(visitor, *arguments) ⇒ Object
Accepts the current visitor unto itself.
170 171 172 |
# File 'lib/carbon/compiler/node/base.rb', line 170 def accept(visitor, *arguments) visitor.visit(self, *arguments) end |
#attributes!(attributes) ⇒ Base
Sets the attributes of a copy of this node to the given attributes. It then returns the copy.
113 114 115 |
# File 'lib/carbon/compiler/node/base.rb', line 113 def attributes!(attributes) self.class.new(children, data.merge(attributes: attributes)) end |
#behavior? ⇒ Boolean
Returns true if this node is a behavior definition.
184 185 186 |
# File 'lib/carbon/compiler/node/base.rb', line 184 def behavior? false end |
#data ⇒ Object
195 196 197 |
# File 'lib/carbon/compiler/node/base.rb', line 195 def data { location: @location, attributes: @attributes, type: @type } end |
#data? ⇒ Boolean
Returns true if this node is a data definition.
177 178 179 |
# File 'lib/carbon/compiler/node/base.rb', line 177 def data? false end |
#each {|child| ... } ⇒ Enumerator<Node, Scanner::Token>
Yields all children one by one, if a block is given. It then returns an enumerator that enumerates over all of the children.
80 |
# File 'lib/carbon/compiler/node/base.rb', line 80 def_delegator :children, :each |
#identity? ⇒ Boolean
Returns true if this node is an identity definition.
191 192 193 |
# File 'lib/carbon/compiler/node/base.rb', line 191 def identity? false end |
#map! ⇒ Base
Maps the children using the given block. It then returns a copy of the node with the new children.
141 142 143 |
# File 'lib/carbon/compiler/node/base.rb', line 141 def map! update!(children.map(&Proc.new)) end |
#merge!(options) ⇒ Base Also known as: merge
Using the ClassMethods#mapping, it merges the given hash into the children.
155 156 157 158 159 160 161 162 |
# File 'lib/carbon/compiler/node/base.rb', line 155 def merge!() merged = @children.dup .each do |key, value| merged[attributes.fetch(key)] = value end update!(merged) end |
#type!(type) ⇒ Base
Sets the type of a copy of this node to the given type. It then returns the copy.
122 123 124 |
# File 'lib/carbon/compiler/node/base.rb', line 122 def type!(type) self.class.new(children, data.merge(type: type)) end |
#update!(children) ⇒ Base Also known as: update
Sets the children of a copy of this node to the given children. It then returns the copy.
131 132 133 |
# File 'lib/carbon/compiler/node/base.rb', line 131 def update!(children) self.class.new(children, data) end |