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

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

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.

RBS:

  • (green: GreenToken, parent: SyntaxNode, index: Integer, offset: Integer) -> void

Parameters:



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

#greenGreenToken (readonly)

Signature:

  • GreenToken

Returns:



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

def green
  @green
end

#indexInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


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

def index
  @index
end

#offsetInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


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

def offset
  @offset
end

#parentSyntaxNode (readonly)

Signature:

  • SyntaxNode

Returns:



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

def parent
  @parent
end

Instance Method Details

#==(other) ⇒ Boolean

RBS:

  • (untyped other) -> bool

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

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

#childrenArray[untyped]

RBS:

  • () -> Array[untyped]

Returns:

  • (Array[untyped])


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

def children = []

#contains_error?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


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

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

#deconstructArray[untyped]

RBS:

  • () -> Array[untyped]

Returns:

  • (Array[untyped])


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

def deconstruct = []

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

RBS:

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

Parameters:

  • (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


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

#ensure_coordinates!void

This method returns an undefined value.

RBS:

  • () -> void



152
153
154
155
156
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 152

def ensure_coordinates!
  return unless @parent.trivia_policy == :drop

  raise TriviaDroppedError, "source coordinates are unavailable when CST trivia is dropped"
end

#error?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


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

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

#full_spanRange[Integer]

RBS:

  • () -> Range[Integer]

Returns:

  • (Range[Integer])


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_textString Also known as: to_source

RBS:

  • () -> String

Returns:

  • (String)


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

def full_text = @green.to_source

#kindInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


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

def kind = @green.kind

#kind_nameString

RBS:

  • () -> String

Returns:

  • (String)


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

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

#leading_triviaArray[GreenTrivia]

RBS:

  • () -> Array[GreenTrivia]

Returns:



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

def leading_trivia = @green.leading

#locationIbex::Location

RBS:

  • () -> Ibex::Location

Returns:

  • (Ibex::Location)


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

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

#missing?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


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

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

#next_siblingSyntaxNode, ...

RBS:

  • () -> (SyntaxNode | SyntaxToken)?

Returns:



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

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

#prev_siblingSyntaxNode, ...

RBS:

  • () -> (SyntaxNode | SyntaxToken)?

Returns:



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

RBS:

  • (GreenNode | GreenToken | SyntaxNode | SyntaxToken replacement) -> SyntaxNode

Parameters:

Returns:



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

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

#rootSyntaxNode

RBS:

  • () -> SyntaxNode

Returns:



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

def root = @parent.root

#same_node?(other) ⇒ Boolean

RBS:

  • (SyntaxToken other) -> bool

Parameters:

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

#spanRange[Integer]

RBS:

  • () -> Range[Integer]

Returns:

  • (Range[Integer])


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

#symbolString

RBS:

  • () -> String

Returns:

  • (String)


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

def symbol = kind_name

#textString

RBS:

  • () -> String

Returns:

  • (String)


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

def text = @green.text

#to_hHash[Symbol, untyped]

RBS:

  • () -> Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


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

def to_h = deconstruct_keys(nil)

#valueString

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

RBS:

  • () -> String

Returns:

  • (String)


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

def value = text

#with_leading(trivia) ⇒ SyntaxNode

RBS:

  • (Array[GreenTrivia] trivia) -> SyntaxNode

Parameters:

Returns:



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

RBS:

  • (String value) -> SyntaxNode

Parameters:

  • value (String)

Returns:



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

RBS:

  • (Array[GreenTrivia] trivia) -> SyntaxNode

Parameters:

Returns:



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