Class: Ibex::Runtime::LocationSpan

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/generated_parser.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.

RBS:

  • (start: Object?, finish: Object?, ?empty: bool) -> void



34
35
36
37
38
39
# File 'lib/json5/generated_parser.rb', line 34

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

Instance Attribute Details

#finishObject (readonly)

Signature:

  • Object?



31
32
33
# File 'lib/json5/generated_parser.rb', line 31

def finish
  @finish
end

#startObject (readonly)

Signature:

  • Object?



30
31
32
# File 'lib/json5/generated_parser.rb', line 30

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.

RBS:

  • (Array[Object?] locations, lookahead: Object?) -> LocationSpan?



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/json5/generated_parser.rb', line 45

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

RBS:

  • () -> Object?



72
# File 'lib/json5/generated_parser.rb', line 72

def column = location_value(@start, :column)

#empty?Boolean

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

RBS:

  • () -> bool

Returns:

  • (Boolean)


61
62
63
# File 'lib/json5/generated_parser.rb', line 61

def empty?
  @empty
end

#end_columnObject

RBS:

  • () -> Object?



92
93
94
95
96
# File 'lib/json5/generated_parser.rb', line 92

def end_column
  return column if empty?

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

#end_fileObject

RBS:

  • () -> Object?



78
79
80
81
82
# File 'lib/json5/generated_parser.rb', line 78

def end_file
  return file if empty?

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

#end_lineObject

RBS:

  • () -> Object?



85
86
87
88
89
# File 'lib/json5/generated_parser.rb', line 85

def end_line
  return line if empty?

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

#fileObject

RBS:

  • () -> Object?



66
# File 'lib/json5/generated_parser.rb', line 66

def file = location_value(@start, :file)

#lineObject

RBS:

  • () -> Object?



69
# File 'lib/json5/generated_parser.rb', line 69

def line = location_value(@start, :line)

#source_lineObject

RBS:

  • () -> Object?



75
# File 'lib/json5/generated_parser.rb', line 75

def source_line = location_value(@start, :source_line)

#to_hObject

RBS:

  • () -> Hash[Symbol, Object?]



99
100
101
102
103
104
105
# File 'lib/json5/generated_parser.rb', line 99

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