Class: Ibex::Frontend::Segment

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/frontend/source_document.rb

Overview

An immutable lexical unit or trivia slice in a concrete syntax tree.

Constant Summary collapse

TRIVIA_KINDS =

: Array

%i[whitespace newline line_comment block_comment].freeze
OPAQUE_KINDS =

: Array

%i[action user_code_body].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, span:, text:, token_type: nil, token_index: nil) ⇒ Segment

Returns a new instance of Segment.

Raises:

  • (ArgumentError)


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

#kindObject (readonly)

: Symbol



10
11
12
# File 'lib/ibex/frontend/source_document.rb', line 10

def kind
  @kind
end

#spanObject (readonly)

: SourceSpan



11
12
13
# File 'lib/ibex/frontend/source_document.rb', line 11

def span
  @span
end

#textObject (readonly)

: String



12
13
14
# File 'lib/ibex/frontend/source_document.rb', line 12

def text
  @text
end

#token_indexObject (readonly)

: Integer?



14
15
16
# File 'lib/ibex/frontend/source_document.rb', line 14

def token_index
  @token_index
end

#token_typeObject (readonly)

: Symbol?



13
14
15
# File 'lib/ibex/frontend/source_document.rb', line 13

def token_type
  @token_type
end

Instance Method Details

#opaque?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/ibex/frontend/source_document.rb', line 35

def opaque?
  OPAQUE_KINDS.include?(kind)
end

#renderObject



45
46
47
# File 'lib/ibex/frontend/source_document.rb', line 45

def render
  text
end

#token?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/ibex/frontend/source_document.rb', line 40

def token?
  !token_index.nil?
end

#trivia?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/ibex/frontend/source_document.rb', line 30

def trivia?
  TRIVIA_KINDS.include?(kind)
end