Class: Ibex::Runtime::CST::TypedNode

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

Overview

Grammar-generated typed view over one Red syntax node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ TypedNode

Returns a new instance of TypedNode.



16
17
18
19
# File 'lib/ibex/runtime/cst/typed_node.rb', line 16

def initialize(node)
  @node = node
  freeze
end

Instance Attribute Details

#nodeObject (readonly)

@rbs! type element = SyntaxNode | SyntaxToken



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

def node
  @node
end

Instance Method Details

#child_at_slot(index) ⇒ Object



28
# File 'lib/ibex/runtime/cst/typed_node.rb', line 28

def child_at_slot(index) = @node.child_at(index)

#deconstructObject



47
# File 'lib/ibex/runtime/cst/typed_node.rb', line 47

def deconstruct = @node.deconstruct

#deconstruct_keys(keys) ⇒ Object



50
# File 'lib/ibex/runtime/cst/typed_node.rb', line 50

def deconstruct_keys(keys) = @node.deconstruct_keys(keys)

#kindObject



22
# File 'lib/ibex/runtime/cst/typed_node.rb', line 22

def kind = @node.kind

#kind_nameObject



25
# File 'lib/ibex/runtime/cst/typed_node.rb', line 25

def kind_name = @node.kind_name

#list_items(index, separated:, separators: false) ⇒ Object (protected)

Flatten a lowered repetition helper and enumerate elements or separators.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ibex/runtime/cst/typed_node.rb', line 59

def list_items(index, separated:, separators: false)
  root = node_at_slot(index)
  flattened = [] #: Array[element]
  flatten_list(root, root.kind, flattened)
  return flattened.each unless separated

  selected = [] #: Array[element]
  flattened.each_with_index do |item, item_index|
    selected << item if separators == item_index.odd?
  end
  selected.each
end

#node_at_slot(index) ⇒ Object

Raises:

  • (TypeError)


31
32
33
34
35
36
# File 'lib/ibex/runtime/cst/typed_node.rb', line 31

def node_at_slot(index)
  child = child_at_slot(index)
  return child if child.is_a?(SyntaxNode)

  raise TypeError, "CST slot #{index} is not a syntax node"
end

#to_sourceObject



53
# File 'lib/ibex/runtime/cst/typed_node.rb', line 53

def to_source = @node.to_source

#token_at_slot(index) ⇒ Object

Raises:

  • (TypeError)


39
40
41
42
43
44
# File 'lib/ibex/runtime/cst/typed_node.rb', line 39

def token_at_slot(index)
  child = child_at_slot(index)
  return child if child.is_a?(SyntaxToken)

  raise TypeError, "CST slot #{index} is not a syntax token"
end