Class: Ibex::Frontend::SourceSpan

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

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.

Raises:

  • (ArgumentError)


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

#fileObject (readonly)

: String



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

def file
  @file
end

#finishObject (readonly)

: SourcePosition



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

def finish
  @finish
end

#startObject (readonly)

: SourcePosition



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

def start
  @start
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  length.zero?
end

#end_byteObject



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

def end_byte
  finish.byte_offset
end

#lengthObject



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

def length
  end_byte - start_byte
end

#start_byteObject



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

def start_byte
  start.byte_offset
end

#to_hObject



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