Class: Ibex::Frontend::Segment

Inherits:
Object
  • Object
show all
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 =

Signature:

  • Array[Symbol]

Returns:

  • (Array[Symbol])
%i[whitespace newline line_comment block_comment].freeze
OPAQUE_KINDS =

Signature:

  • Array[Symbol]

Returns:

  • (Array[Symbol])
%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.

RBS:

  • (kind: Symbol, span: SourceSpan, text: String, ?token_type: Symbol?, ?token_index: Integer?) -> void

Parameters:

  • kind: (Symbol)
  • span: (SourceSpan)
  • text: (String)
  • token_type: (Symbol, nil) (defaults to: nil)
  • token_index: (Integer, nil) (defaults to: nil)


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

#kindSymbol (readonly)

Signature:

  • Symbol

Returns:

  • (Symbol)


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

def kind
  @kind
end

#spanSourceSpan (readonly)

Signature:

  • SourceSpan

Returns:



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

def span
  @span
end

#textString (readonly)

Signature:

  • String

Returns:

  • (String)


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

def text
  @text
end

#token_indexInteger? (readonly)

Signature:

  • Integer?

Returns:

  • (Integer, nil)


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

def token_index
  @token_index
end

#token_typeSymbol? (readonly)

Signature:

  • Symbol?

Returns:

  • (Symbol, nil)


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

def token_type
  @token_type
end

Instance Method Details

#opaque?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


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

def opaque?
  OPAQUE_KINDS.include?(kind)
end

#renderString

RBS:

  • () -> String

Returns:

  • (String)


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

def render
  text
end

#token?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


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

def token?
  !token_index.nil?
end

#trivia?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


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

def trivia?
  TRIVIA_KINDS.include?(kind)
end