Class: Ibex::Runtime::CST::GreenNode
- Inherits:
-
Object
- Object
- Ibex::Runtime::CST::GreenNode
- Defined in:
- lib/ibex/runtime/cst/green/node.rb,
sig/ibex/runtime/cst/green/node.rbs
Overview
Immutable position-independent syntax node.
Instance Attribute Summary collapse
- #annotations ⇒ Array[SyntaxAnnotation] readonly
- #children ⇒ Array[child] readonly
- #descendant_count ⇒ Integer readonly
- #flags ⇒ Integer readonly
- #full_width ⇒ Integer readonly
- #intrinsic_flags ⇒ Integer readonly
- #kind ⇒ Integer readonly
- #leading_width ⇒ Integer readonly
- #trailing_width ⇒ Integer readonly
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
- #edge_width(children, reader) ⇒ Integer
- #hash ⇒ Integer
-
#initialize(kind:, children:, flags: 0, annotations: []) ⇒ GreenNode
constructor
A new instance of GreenNode.
- #to_source ⇒ String
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 ⇒ Array[SyntaxAnnotation] (readonly)
17 18 19 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 17 def annotations @annotations end |
#children ⇒ Array[child] (readonly)
14 15 16 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 14 def children @children end |
#descendant_count ⇒ Integer (readonly)
21 22 23 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 21 def descendant_count @descendant_count end |
#flags ⇒ Integer (readonly)
15 16 17 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 15 def flags @flags end |
#full_width ⇒ Integer (readonly)
18 19 20 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 18 def full_width @full_width end |
#intrinsic_flags ⇒ Integer (readonly)
16 17 18 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 16 def intrinsic_flags @intrinsic_flags end |
#kind ⇒ Integer (readonly)
13 14 15 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 13 def kind @kind end |
#leading_width ⇒ Integer (readonly)
19 20 21 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 19 def leading_width @leading_width end |
#trailing_width ⇒ Integer (readonly)
20 21 22 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 20 def trailing_width @trailing_width end |
Instance Method Details
#==(other) ⇒ Boolean 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 |
#edge_width(children, reader) ⇒ Integer
69 70 71 72 73 74 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 69 def edge_width(children, reader) child = children.find { |candidate| candidate.full_width.positive? } return 0 unless child reader == :leading_width ? child.leading_width : child.trailing_width end |
#hash ⇒ Integer
64 |
# File 'lib/ibex/runtime/cst/green/node.rb', line 64 def hash = [@kind, @children, @intrinsic_flags, @annotations].hash |
#to_source ⇒ String
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 |