Class: Ibex::Runtime::CST::SyntaxToken
- Inherits:
-
Object
- Object
- Ibex::Runtime::CST::SyntaxToken
- 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
- #green ⇒ GreenToken readonly
- #index ⇒ Integer readonly
- #offset ⇒ Integer readonly
- #parent ⇒ SyntaxNode readonly
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #children ⇒ Array[untyped]
- #contains_error? ⇒ Boolean
- #deconstruct ⇒ Array[untyped]
- #deconstruct_keys(_keys) ⇒ Hash[Symbol, untyped]
- #ensure_coordinates! ⇒ void
- #error? ⇒ Boolean
- #full_span ⇒ Range[Integer]
- #full_text ⇒ String (also: #to_source)
-
#initialize(green:, parent:, index:, offset:) ⇒ SyntaxToken
constructor
A new instance of SyntaxToken.
- #kind ⇒ Integer
- #kind_name ⇒ String
- #leading_trivia ⇒ Array[GreenTrivia]
- #location ⇒ Ibex::Location
- #missing? ⇒ Boolean
- #next_sibling ⇒ SyntaxNode, ...
- #prev_sibling ⇒ SyntaxNode, ...
- #replace_with(replacement) ⇒ SyntaxNode
- #root ⇒ SyntaxNode
- #same_node?(other) ⇒ Boolean
- #span ⇒ Range[Integer]
- #symbol ⇒ String
- #text ⇒ String
- #to_h ⇒ Hash[Symbol, untyped]
-
#value ⇒ String
Compatibility projection for the legacy token API.
- #with_leading(trivia) ⇒ SyntaxNode
- #with_text(value) ⇒ SyntaxNode
- #with_trailing(trivia) ⇒ SyntaxNode
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
#green ⇒ GreenToken (readonly)
14 15 16 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 14 def green @green end |
#index ⇒ Integer (readonly)
16 17 18 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 16 def index @index end |
#offset ⇒ Integer (readonly)
17 18 19 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 17 def offset @offset end |
#parent ⇒ SyntaxNode (readonly)
15 16 17 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 15 def parent @parent end |
Instance Method Details
#==(other) ⇒ Boolean
131 132 133 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 131 def ==(other) other.is_a?(SyntaxToken) && @green == other.green end |
#children ⇒ Array[untyped]
49 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 49 def children = [] |
#contains_error? ⇒ Boolean
77 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 77 def contains_error? = @green.flags.anybits?(Flags::CONTAINS_ERROR) |
#deconstruct ⇒ Array[untyped]
136 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 136 def deconstruct = [] |
#deconstruct_keys(_keys) ⇒ 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.
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
71 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 71 def error? = @parent.kinds.error?(kind) |
#full_span ⇒ 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_text ⇒ String Also known as: to_source
52 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 52 def full_text = @green.to_source |
#kind ⇒ Integer
29 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 29 def kind = @green.kind |
#kind_name ⇒ String
32 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 32 def kind_name = @parent.kinds.name(kind) |
#leading_trivia ⇒ Array[GreenTrivia]
46 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 46 def leading_trivia = @green.leading |
#location ⇒ Ibex::Location
68 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 68 def location = @parent.source_text.location(span) |
#missing? ⇒ Boolean
74 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 74 def missing? = @green.flags.anybits?(Flags::CONTAINS_MISSING) |
#next_sibling ⇒ SyntaxNode, ...
113 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 113 def next_sibling = @parent.children[@index + 1] |
#prev_sibling ⇒ SyntaxNode, ...
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
80 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 80 def replace_with(replacement) = Editing.replace(self, replacement) |
#root ⇒ SyntaxNode
123 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 123 def root = @parent.root |
#same_node?(other) ⇒ 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 |
#span ⇒ 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 |
#symbol ⇒ String
35 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 35 def symbol = kind_name |
#text ⇒ String
38 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 38 def text = @green.text |
#to_h ⇒ Hash[Symbol, untyped]
147 |
# File 'lib/ibex/runtime/cst/syntax_token.rb', line 147 def to_h = deconstruct_keys(nil) |
#value ⇒ String
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) ⇒ SyntaxNode
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
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
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 |