Class: Ibex::Frontend::SourceSpan

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/frontend/source_span.rb,
sig/ibex/frontend/source_span.rbs

Overview

An immutable half-open byte span in one grammar source.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file:, start:, finish:) ⇒ SourceSpan

Returns a new instance of SourceSpan.

RBS:

  • (file: String, start: SourcePosition, finish: SourcePosition) -> void

Parameters:



46
47
48
49
50
51
52
53
# File 'lib/ibex/frontend/source_span.rb', line 46

def initialize(file:, start:, finish:)
  raise ArgumentError, "span end precedes its start" if finish.byte_offset < start.byte_offset

  @file = file.dup.freeze
  @start = start
  @finish = finish
  freeze
end

Instance Attribute Details

#fileString (readonly)

Signature:

  • String

Returns:

  • (String)


41
42
43
# File 'lib/ibex/frontend/source_span.rb', line 41

def file
  @file
end

#finishSourcePosition (readonly)

Signature:

  • SourcePosition

Returns:



43
44
45
# File 'lib/ibex/frontend/source_span.rb', line 43

def finish
  @finish
end

#startSourcePosition (readonly)

Signature:

  • SourcePosition

Returns:



42
43
44
# File 'lib/ibex/frontend/source_span.rb', line 42

def start
  @start
end

Instance Method Details

#empty?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


71
72
73
# File 'lib/ibex/frontend/source_span.rb', line 71

def empty?
  length.zero?
end

#end_byteInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


61
62
63
# File 'lib/ibex/frontend/source_span.rb', line 61

def end_byte
  finish.byte_offset
end

#lengthInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


66
67
68
# File 'lib/ibex/frontend/source_span.rb', line 66

def length
  end_byte - start_byte
end

#start_byteInteger

RBS:

  • () -> Integer

Returns:

  • (Integer)


56
57
58
# File 'lib/ibex/frontend/source_span.rb', line 56

def start_byte
  start.byte_offset
end

#to_hHash[Symbol, untyped]

RBS:

  • () -> Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


76
77
78
# File 'lib/ibex/frontend/source_span.rb', line 76

def to_h
  { file: file, start: start.to_h, end: finish.to_h }
end