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

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(kind:, children:, flags: 0, annotations: []) ⇒ GreenNode

Returns a new instance of GreenNode.

RBS:

  • (kind: Integer, children: Array[child], ?flags: Integer, ?annotations: Array[SyntaxAnnotation]) -> void

Parameters:

  • kind: (Integer)
  • children: (Array[child])
  • flags: (Integer) (defaults to: 0)
  • annotations: (Array[SyntaxAnnotation]) (defaults to: [])


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

#annotationsArray[SyntaxAnnotation] (readonly)

Signature:

  • Array[SyntaxAnnotation]

Returns:



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

def annotations
  @annotations
end

#childrenArray[child] (readonly)

Signature:

  • Array[child]

Returns:

  • (Array[child])


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

def children
  @children
end

#descendant_countInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


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

def descendant_count
  @descendant_count
end

#flagsInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


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

def flags
  @flags
end

#full_widthInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


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

def full_width
  @full_width
end

#intrinsic_flagsInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


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

def intrinsic_flags
  @intrinsic_flags
end

#kindInteger (readonly)

RBS:

  • type child = GreenNode | GreenToken

Returns:

  • (Integer)


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

def kind
  @kind
end

#leading_widthInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


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

def leading_width
  @leading_width
end

#trailing_widthInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


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?

RBS:

  • (untyped other) -> bool

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

RBS:

  • (Enumerable[child] children, Symbol reader) -> Integer

Parameters:

  • children (Enumerable[child])
  • reader (Symbol)

Returns:

  • (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

#hashInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


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

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

#to_sourceString

RBS:

  • () -> String

Returns:

  • (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