Class: Ibex::Runtime::CST::GreenNode

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/runtime/cst/green/node.rb

Overview

Immutable position-independent syntax node.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#annotationsObject (readonly)

: Array



17
18
19
# File 'lib/ibex/runtime/cst/green/node.rb', line 17

def annotations
  @annotations
end

#childrenObject (readonly)

: Array



14
15
16
# File 'lib/ibex/runtime/cst/green/node.rb', line 14

def children
  @children
end

#descendant_countObject (readonly)

: Integer



21
22
23
# File 'lib/ibex/runtime/cst/green/node.rb', line 21

def descendant_count
  @descendant_count
end

#flagsObject (readonly)

: Integer



15
16
17
# File 'lib/ibex/runtime/cst/green/node.rb', line 15

def flags
  @flags
end

#full_widthObject (readonly)

: Integer



18
19
20
# File 'lib/ibex/runtime/cst/green/node.rb', line 18

def full_width
  @full_width
end

#intrinsic_flagsObject (readonly)

: Integer



16
17
18
# File 'lib/ibex/runtime/cst/green/node.rb', line 16

def intrinsic_flags
  @intrinsic_flags
end

#kindObject (readonly)

@rbs! type child = GreenNode | GreenToken



13
14
15
# File 'lib/ibex/runtime/cst/green/node.rb', line 13

def kind
  @kind
end

#leading_widthObject (readonly)

: Integer



19
20
21
# File 'lib/ibex/runtime/cst/green/node.rb', line 19

def leading_width
  @leading_width
end

#trailing_widthObject (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

#hashObject



64
# File 'lib/ibex/runtime/cst/green/node.rb', line 64

def hash = [@kind, @children, @intrinsic_flags, @annotations].hash

#to_sourceObject



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