Class: Ibex::Runtime::CST::SyntaxToken

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

Overview

Lightweight Red facade for one Green token occurrence.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(green:, parent:, index:, offset:) ⇒ SyntaxToken

Returns a new instance of SyntaxToken.



20
21
22
23
24
25
26
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 20

def initialize(green:, parent:, index:, offset:)
  @green = green
  @parent = parent
  @index = index
  @offset = offset
  freeze
end

Instance Attribute Details

#greenObject (readonly)

: GreenToken



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

def green
  @green
end

#indexObject (readonly)

: Integer



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

def index
  @index
end

#offsetObject (readonly)

: Integer



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

def offset
  @offset
end

#parentObject (readonly)

: SyntaxNode



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

def parent
  @parent
end

Instance Method Details

#==(other) ⇒ Object



131
132
133
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 131

def ==(other)
  other.is_a?(SyntaxToken) && @green == other.green
end

#childrenObject



49
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 49

def children = []

#contains_error?Boolean

Returns:

  • (Boolean)


77
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 77

def contains_error? = @green.flags.anybits?(Flags::CONTAINS_ERROR)

#deconstructObject



136
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 136

def deconstruct = []

#deconstruct_keys(_keys) ⇒ Object



139
140
141
142
143
144
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 139

def deconstruct_keys(_keys)
  {
    kind: :token, symbol: symbol, value: value, location: location,
    leading_trivia: leading_trivia
  }.freeze
end

#error?Boolean

Returns:

  • (Boolean)


71
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 71

def error? = @parent.kinds.error?(kind)

#full_spanObject



56
57
58
59
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 56

def full_span
  ensure_coordinates!
  @offset...(@offset + @green.full_width)
end

#full_textObject Also known as: to_source



52
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 52

def full_text = @green.to_source

#kindObject



29
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 29

def kind = @green.kind

#kind_nameObject



32
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 32

def kind_name = @parent.kinds.name(kind)

#leading_triviaObject



46
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 46

def leading_trivia = @green.leading

#locationObject



68
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 68

def location = @parent.source_text.location(span)

#missing?Boolean

Returns:

  • (Boolean)


74
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 74

def missing? = @green.flags.anybits?(Flags::CONTAINS_MISSING)

#next_siblingObject



113
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 113

def next_sibling = @parent.children[@index + 1]

#prev_siblingObject



116
117
118
119
120
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 116

def prev_sibling
  return if @index.zero?

  @parent.children[@index - 1]
end

#replace_with(replacement) ⇒ Object



80
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 80

def replace_with(replacement) = Editing.replace(self, replacement)

#rootObject



123
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 123

def root = @parent.root

#same_node?(other) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 126

def same_node?(other)
  root.equal?(other.root) && @green.equal?(other.green) && @offset == other.offset
end

#spanObject



62
63
64
65
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 62

def span
  ensure_coordinates!
  (@offset + @green.leading_width)...(@offset + @green.full_width - @green.trailing_width)
end

#symbolObject



35
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 35

def symbol = kind_name

#textObject



38
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 38

def text = @green.text

#to_hObject



147
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 147

def to_h = deconstruct_keys(nil)

#valueObject

Compatibility projection for the legacy token API. Semantic lexer values remain on the parser value path; syntax exposes source bytes.



43
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 43

def value = text

#with_leading(trivia) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 93

def with_leading(trivia)
  replace_with(
    GreenToken.new(
      kind: @green.kind, text: @green.text, leading: trivia, trailing: @green.trailing,
      flags: @green.flags, expected_kind: @green.expected_kind
    )
  )
end

#with_text(value) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 83

def with_text(value)
  replace_with(
    GreenToken.new(
      kind: @green.kind, text: value, leading: @green.leading, trailing: @green.trailing,
      flags: @green.flags, expected_kind: @green.expected_kind
    )
  )
end

#with_trailing(trivia) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 103

def with_trailing(trivia)
  replace_with(
    GreenToken.new(
      kind: @green.kind, text: @green.text, leading: @green.leading, trailing: trivia,
      flags: @green.flags, expected_kind: @green.expected_kind
    )
  )
end