Class: Ibex::Frontend::Segment
- Inherits:
-
Object
- Object
- Ibex::Frontend::Segment
- Defined in:
- lib/ibex/frontend/source_document.rb,
sig/ibex/frontend/source_document.rbs
Overview
An immutable lexical unit or trivia slice in a concrete syntax tree.
Constant Summary collapse
- TRIVIA_KINDS =
%i[whitespace newline line_comment block_comment].freeze
- OPAQUE_KINDS =
%i[action user_code_body].freeze
Instance Attribute Summary collapse
- #kind ⇒ Symbol readonly
- #span ⇒ SourceSpan readonly
- #text ⇒ String readonly
- #token_index ⇒ Integer? readonly
- #token_type ⇒ Symbol? readonly
Instance Method Summary collapse
-
#initialize(kind:, span:, text:, token_type: nil, token_index: nil) ⇒ Segment
constructor
A new instance of Segment.
- #opaque? ⇒ Boolean
- #render ⇒ String
- #token? ⇒ Boolean
- #trivia? ⇒ Boolean
Constructor Details
#initialize(kind:, span:, text:, token_type: nil, token_index: nil) ⇒ Segment
Returns a new instance of Segment.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ibex/frontend/source_document.rb', line 18 def initialize(kind:, span:, text:, token_type: nil, token_index: nil) raise ArgumentError, "segment text does not match its span" unless text.bytesize == span.length @kind = kind @span = span @text = text.dup.freeze @token_type = token_type @token_index = token_index freeze end |
Instance Attribute Details
#kind ⇒ Symbol (readonly)
10 11 12 |
# File 'lib/ibex/frontend/source_document.rb', line 10 def kind @kind end |
#span ⇒ SourceSpan (readonly)
11 12 13 |
# File 'lib/ibex/frontend/source_document.rb', line 11 def span @span end |
#text ⇒ String (readonly)
12 13 14 |
# File 'lib/ibex/frontend/source_document.rb', line 12 def text @text end |
#token_index ⇒ Integer? (readonly)
14 15 16 |
# File 'lib/ibex/frontend/source_document.rb', line 14 def token_index @token_index end |
#token_type ⇒ Symbol? (readonly)
13 14 15 |
# File 'lib/ibex/frontend/source_document.rb', line 13 def token_type @token_type end |
Instance Method Details
#opaque? ⇒ Boolean
35 36 37 |
# File 'lib/ibex/frontend/source_document.rb', line 35 def opaque? OPAQUE_KINDS.include?(kind) end |
#render ⇒ String
45 46 47 |
# File 'lib/ibex/frontend/source_document.rb', line 45 def render text end |
#token? ⇒ Boolean
40 41 42 |
# File 'lib/ibex/frontend/source_document.rb', line 40 def token? !token_index.nil? end |
#trivia? ⇒ Boolean
30 31 32 |
# File 'lib/ibex/frontend/source_document.rb', line 30 def trivia? TRIVIA_KINDS.include?(kind) end |