Class: Ibex::Runtime::LocationSpan

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/runtime/location_span.rb

Overview

The deterministic source span assigned to a reduced nonterminal.

Terminal stack entries retain the application-owned location object supplied by the lexer. A reduction wraps the outer boundaries in this immutable value so actions can distinguish synthesized spans.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start:, finish:, empty: false) ⇒ LocationSpan

Returns a new instance of LocationSpan.



16
17
18
19
20
21
# File 'lib/ibex/runtime/location_span.rb', line 16

def initialize(start:, finish:, empty: false)
  @start = start
  @finish = finish
  @empty = empty
  freeze
end

Instance Attribute Details

#finishObject (readonly)

: untyped



13
14
15
# File 'lib/ibex/runtime/location_span.rb', line 13

def finish
  @finish
end

#startObject (readonly)

: untyped



12
13
14
# File 'lib/ibex/runtime/location_span.rb', line 12

def start
  @start
end

Class Method Details

.for_reduction(locations, lookahead:) ⇒ Object

Build a span for an LR reduction. Nonempty reductions cover the first through last located RHS entry. Empty reductions are zero-width at the current lookahead location.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ibex/runtime/location_span.rb', line 27

def self.for_reduction(locations, lookahead:)
  if locations.empty?
    return unless lookahead

    boundary = boundary_start(lookahead)
    return new(start: boundary, finish: boundary, empty: true)
  end

  located = locations.compact
  return if located.empty?

  new(start: boundary_start(located.first), finish: boundary_finish(located.last))
end

Instance Method Details

#columnObject



54
# File 'lib/ibex/runtime/location_span.rb', line 54

def column = location_value(@start, :column)

#empty?Boolean

Whether this is the zero-width span of an empty production.

Returns:

  • (Boolean)


43
44
45
# File 'lib/ibex/runtime/location_span.rb', line 43

def empty?
  @empty
end

#end_columnObject



74
75
76
77
78
# File 'lib/ibex/runtime/location_span.rb', line 74

def end_column
  return column if empty?

  location_value(@finish, :end_column) || location_value(@finish, :column)
end

#end_fileObject



60
61
62
63
64
# File 'lib/ibex/runtime/location_span.rb', line 60

def end_file
  return file if empty?

  location_value(@finish, :end_file) || location_value(@finish, :file)
end

#end_lineObject



67
68
69
70
71
# File 'lib/ibex/runtime/location_span.rb', line 67

def end_line
  return line if empty?

  location_value(@finish, :end_line) || location_value(@finish, :line)
end

#fileObject



48
# File 'lib/ibex/runtime/location_span.rb', line 48

def file = location_value(@start, :file)

#lineObject



51
# File 'lib/ibex/runtime/location_span.rb', line 51

def line = location_value(@start, :line)

#source_lineObject



57
# File 'lib/ibex/runtime/location_span.rb', line 57

def source_line = location_value(@start, :source_line)

#to_hObject



81
82
83
84
85
86
87
# File 'lib/ibex/runtime/location_span.rb', line 81

def to_h
  {
    file: file, line: line, column: column,
    end_file: end_file, end_line: end_line, end_column: end_column,
    empty: empty?
  }
end