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

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

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.

RBS:

  • (SyntaxNode node) -> void

Parameters:



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

def initialize(node)
  @node = node
  freeze
end

Instance Attribute Details

#nodeSyntaxNode (readonly)

RBS:

  • type element = SyntaxNode | SyntaxToken

Returns:



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

def node
  @node
end

Instance Method Details

#child_at_slot(index) ⇒ element

RBS:

  • (Integer index) -> element

Parameters:

  • index (Integer)

Returns:

  • (element)


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

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

#deconstructArray[element]

RBS:

  • () -> Array[element]

Returns:

  • (Array[element])


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

def deconstruct = @node.deconstruct

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

RBS:

  • (Array[Symbol]?) -> Hash[Symbol, untyped]

Parameters:

  • (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


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

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

#flatten_list(current, helper_kind, result) ⇒ void

This method returns an undefined value.

RBS:

  • (SyntaxNode current, Integer helper_kind, Array[element] result) -> void

Parameters:

  • current (SyntaxNode)
  • helper_kind (Integer)
  • result (Array[element])


75
76
77
78
79
80
81
82
83
# File 'lib/ibex/runtime/cst/typed_node.rb', line 75

def flatten_list(current, helper_kind, result)
  current.children.each do |child|
    if child.is_a?(SyntaxNode) && child.kind == helper_kind
      flatten_list(child, helper_kind, result)
    else
      result << child
    end
  end
end

#kindInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


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

def kind = @node.kind

#kind_nameString

RBS:

  • () -> String

Returns:

  • (String)


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

def kind_name = @node.kind_name

#list_items(index, separated:, separators: false) ⇒ Enumerator[element, void]

Flatten a lowered repetition helper and enumerate elements or separators.

RBS:

  • (Integer index, separated: bool, ?separators: bool) -> Enumerator[element, void]

Parameters:

  • index (Integer)
  • separated: (Boolean)
  • separators: (Boolean) (defaults to: false)

Returns:

  • (Enumerator[element, void])


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) ⇒ SyntaxNode

RBS:

  • (Integer index) -> SyntaxNode

Parameters:

  • index (Integer)

Returns:



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_sourceString

RBS:

  • () -> String

Returns:

  • (String)


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

def to_source = @node.to_source

#token_at_slot(index) ⇒ SyntaxToken

RBS:

  • (Integer index) -> SyntaxToken

Parameters:

  • index (Integer)

Returns:



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