Class: Ibex::Runtime::CST::Cursor

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

Overview

Allocation-light cursor over Green elements and absolute offsets. rubocop:disable Naming/PredicateMethod -- cursor movement names follow the public tree-cursor convention.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Cursor

Returns a new instance of Cursor.

RBS:

  • (SyntaxNode node) -> void

Parameters:



19
20
21
22
23
# File 'lib/ibex/runtime/cst/cursor.rb', line 19

def initialize(node)
  @green = node.green
  @offset = node.offset
  @frames = [] #: Array[frame]
end

Instance Attribute Details

#greengreen (readonly)

RBS:

  • type green = GreenNode | GreenToken
  • type frame = [GreenNode, Integer, Integer]

Returns:



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

def green
  @green
end

#offsetInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


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

def offset
  @offset
end

Instance Method Details

#goto_first_childBoolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
# File 'lib/ibex/runtime/cst/cursor.rb', line 26

def goto_first_child
  green = @green
  return false unless green.is_a?(GreenNode)
  return false if green.children.empty?

  parent = green
  @frames << [parent, 0, @offset]
  @green = parent.children.fetch(0)
  true
end

#goto_next_siblingBoolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ibex/runtime/cst/cursor.rb', line 38

def goto_next_sibling
  frame = @frames.last
  return false unless frame

  parent, index, parent_offset = frame
  next_index = index + 1
  return false if next_index >= parent.children.length

  frame[1] = next_index
  @offset = parent_offset + parent.children.first(next_index).sum(&:full_width)
  @green = parent.children.fetch(next_index)
  true
end

#goto_parentBoolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
# File 'lib/ibex/runtime/cst/cursor.rb', line 53

def goto_parent
  frame = @frames.pop
  return false unless frame

  @green = frame.fetch(0)
  @offset = frame.fetch(2)
  true
end

#kindInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


63
# File 'lib/ibex/runtime/cst/cursor.rb', line 63

def kind = @green.kind