Class: Ibex::Runtime::CST::TypedNode
- Inherits:
-
Object
- Object
- Ibex::Runtime::CST::TypedNode
- 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
- #node ⇒ SyntaxNode readonly
Instance Method Summary collapse
- #child_at_slot(index) ⇒ element
- #deconstruct ⇒ Array[element]
- #deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]
- #flatten_list(current, helper_kind, result) ⇒ void
-
#initialize(node) ⇒ TypedNode
constructor
A new instance of TypedNode.
- #kind ⇒ Integer
- #kind_name ⇒ String
-
#list_items(index, separated:, separators: false) ⇒ Enumerator[element, void]
Flatten a lowered repetition helper and enumerate elements or separators.
- #node_at_slot(index) ⇒ SyntaxNode
- #to_source ⇒ String
- #token_at_slot(index) ⇒ SyntaxToken
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
#node ⇒ SyntaxNode (readonly)
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
28 |
# File 'lib/ibex/runtime/cst/typed_node.rb', line 28 def child_at_slot(index) = @node.child_at(index) |
#deconstruct ⇒ Array[element]
47 |
# File 'lib/ibex/runtime/cst/typed_node.rb', line 47 def deconstruct = @node.deconstruct |
#deconstruct_keys(keys) ⇒ 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.
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 |
#kind ⇒ Integer
22 |
# File 'lib/ibex/runtime/cst/typed_node.rb', line 22 def kind = @node.kind |
#kind_name ⇒ 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.
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
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_source ⇒ String
53 |
# File 'lib/ibex/runtime/cst/typed_node.rb', line 53 def to_source = @node.to_source |
#token_at_slot(index) ⇒ SyntaxToken
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 |