Class: Ibex::Runtime::CST::GreenNode
- Inherits:
-
Object
- Object
- Ibex::Runtime::CST::GreenNode
- Defined in:
- lib/ibex/runtime/cst/green/node.rb
Overview
Immutable position-independent syntax node.
Instance Attribute Summary collapse
-
#annotations ⇒ Object
readonly
: Array.
-
#children ⇒ Object
readonly
: Array.
-
#descendant_count ⇒ Object
readonly
: Integer.
-
#flags ⇒ Object
readonly
: Integer.
-
#full_width ⇒ Object
readonly
: Integer.
-
#intrinsic_flags ⇒ Object
readonly
: Integer.
-
#kind ⇒ Object
readonly
@rbs! type child = GreenNode | GreenToken.
-
#leading_width ⇒ Object
readonly
: Integer.
-
#trailing_width ⇒ Object
readonly
: Integer.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(kind:, children:, flags: 0, annotations: []) ⇒ GreenNode
constructor
A new instance of GreenNode.
- #to_source ⇒ Object
Constructor Details
#initialize(kind:, children:, flags: 0, annotations: []) ⇒ GreenNode
Returns a new instance of GreenNode.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 24 def initialize(kind:, children:, flags: 0, annotations: []) @kind = kind @children = children.dup.freeze @annotations = annotations.dup.freeze @intrinsic_flags = flags @intrinsic_flags |= Flags::HAS_ANNOTATION unless @annotations.empty? @flags = @children.reduce(@intrinsic_flags) { |value, child| value | child.flags } @full_width = @children.sum(&:full_width) @leading_width = edge_width(@children, :leading_width) @trailing_width = edge_width(@children.reverse_each, :trailing_width) @descendant_count = 1 + @children.sum(&:descendant_count) freeze end |
Instance Attribute Details
#annotations ⇒ Object (readonly)
: Array
17 18 19 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 17 def annotations @annotations end |
#children ⇒ Object (readonly)
: Array
14 15 16 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 14 def children @children end |
#descendant_count ⇒ Object (readonly)
: Integer
21 22 23 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 21 def descendant_count @descendant_count end |
#flags ⇒ Object (readonly)
: Integer
15 16 17 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 15 def flags @flags end |
#full_width ⇒ Object (readonly)
: Integer
18 19 20 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 18 def full_width @full_width end |
#intrinsic_flags ⇒ Object (readonly)
: Integer
16 17 18 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 16 def intrinsic_flags @intrinsic_flags end |
#kind ⇒ Object (readonly)
@rbs! type child = GreenNode | GreenToken
13 14 15 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 13 def kind @kind end |
#leading_width ⇒ Object (readonly)
: Integer
19 20 21 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 19 def leading_width @leading_width end |
#trailing_width ⇒ Object (readonly)
: Integer
20 21 22 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 20 def trailing_width @trailing_width end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
57 58 59 60 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 57 def ==(other) other.is_a?(GreenNode) && @kind == other.kind && @intrinsic_flags == other.intrinsic_flags && @annotations == other.annotations && @children == other.children end |
#hash ⇒ Object
64 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 64 def hash = [@kind, @children, @intrinsic_flags, @annotations].hash |
#to_source ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 39 def to_source source = String.new(encoding: Encoding::BINARY) pending = @children.reverse until pending.empty? child = pending.pop || raise("green source traversal underflow") if child.is_a?(GreenNode) child.children.reverse_each { |nested| pending << nested } next end child.leading.each { |trivia| source << trivia.text } source << child.text child.trailing.each { |trivia| source << trivia.text } end source end |