Class: Parser::Source::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/source/range.rb

Overview

A range of characters in a particular source buffer.

The range is always exclusive, i.e. a range with begin_pos of 3 and end_pos of 5 will contain the following characters:

example
   ^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_buffer, begin_pos, end_pos) ⇒ Range

Returns a new instance of Range

Parameters:

  • source_buffer (Buffer)
  • begin_pos (Integer)
  • end_pos (Integer)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/parser/source/range.rb', line 33

def initialize(source_buffer, begin_pos, end_pos)
  if end_pos < begin_pos
    raise ArgumentError, 'Parser::Source::Range: end_pos must not be less than begin_pos'
  end
  if source_buffer.nil?
    raise ArgumentError, 'Parser::Source::Range: source_buffer must not be nil'
  end

  @source_buffer       = source_buffer
  @begin_pos, @end_pos = begin_pos, end_pos

  freeze
end

Instance Attribute Details

#begin_posInteger (readonly)

Returns index of the first character in the range

Returns:

  • (Integer)

    index of the first character in the range



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/parser/source/range.rb', line 24

class Range
  attr_reader :source_buffer
  attr_reader :begin_pos, :end_pos

  ##
  # @param [Buffer]  source_buffer
  # @param [Integer] begin_pos
  # @param [Integer] end_pos
  #
  def initialize(source_buffer, begin_pos, end_pos)
    if end_pos < begin_pos
      raise ArgumentError, 'Parser::Source::Range: end_pos must not be less than begin_pos'
    end
    if source_buffer.nil?
      raise ArgumentError, 'Parser::Source::Range: source_buffer must not be nil'
    end

    @source_buffer       = source_buffer
    @begin_pos, @end_pos = begin_pos, end_pos

    freeze
  end

  ##
  # @return [Range] a zero-length range located just before the beginning
  #   of this range.
  #
  def begin
    with(end_pos: @begin_pos)
  end

  ##
  # @return [Range] a zero-length range located just after the end
  #   of this range.
  #
  def end
    with(begin_pos: @end_pos)
  end

  ##
  # @return [Integer] amount of characters included in this range.
  #
  def size
    @end_pos - @begin_pos
  end

  alias length size

  ##
  # Line number of the beginning of this range. By default, the first line
  # of a buffer is 1; as such, line numbers are most commonly one-based.
  #
  # @see Buffer
  # @return [Integer] line number of the beginning of this range.
  #
  def line
    @source_buffer.line_for_position(@begin_pos)
  end

  alias_method :first_line, :line

  ##
  # @return [Integer] zero-based column number of the beginning of this range.
  #
  def column
    @source_buffer.column_for_position(@begin_pos)
  end

  ##
  # @return [Integer] line number of the end of this range.
  #
  def last_line
    @source_buffer.line_for_position(@end_pos)
  end

  ##
  # @return [Integer] zero-based column number of the end of this range.
  #
  def last_column
    @source_buffer.column_for_position(@end_pos)
  end

  ##
  # @return [::Range] a range of columns spanned by this range.
  # @raise RangeError
  #
  def column_range
    if self.begin.line != self.end.line
      raise RangeError, "#{self.inspect} spans more than one line"
    end

    self.begin.column...self.end.column
  end

  ##
  # @return [String] a line of source code containing the beginning of this range.
  #
  def source_line
    @source_buffer.source_line(line)
  end

  ##
  # @return [String] all source code covered by this range.
  #
  def source
    @source_buffer.slice(self.begin_pos...self.end_pos)
  end

  ##
  # `is?` provides a concise way to compare the source corresponding to this range.
  # For example, `r.source == '(' || r.source == 'begin'` is equivalent to
  # `r.is?('(', 'begin')`.
  #
  def is?(*what)
    what.include?(source)
  end

  ##
  # @return [Array(Integer)] a set of character indexes contained in this range.
  #
  def to_a
    (@begin_pos...@end_pos).to_a
  end

  ##
  # Composes a GNU/Clang-style string representation of the beginning of this
  # range.
  #
  # For example, for the following range in file `foo.rb`,
  #
  #     def foo
  #         ^^^
  #
  # `to_s` will return `foo.rb:1:5`.
  # Note that the column index is one-based.
  #
  # @return [String]
  #
  def to_s
    line, column = @source_buffer.decompose_position(@begin_pos)

    [@source_buffer.name, line, column + 1].join(':')
  end

  ##
  # @param [Hash] Endpoint(s) to change, any combination of :begin_pos or :end_pos
  # @return [Range] the same range as this range but with the given end point(s) changed
  # to the given value(s).
  #
  def with(begin_pos: @begin_pos, end_pos: @end_pos)
    Range.new(@source_buffer, begin_pos, end_pos)
  end

  ##
  # @param [Hash] Endpoint(s) to change, any combination of :begin_pos or :end_pos
  # @return [Range] the same range as this range but with the given end point(s) adjusted
  # by the given amount(s)
  #
  def adjust(begin_pos: 0, end_pos: 0)
    Range.new(@source_buffer, @begin_pos + begin_pos, @end_pos + end_pos)
  end

  ##
  # @param [Integer] new_size
  # @return [Range] a range beginning at the same point as this range and length `new_size`.
  #
  def resize(new_size)
    with(end_pos: @begin_pos + new_size)
  end

  ##
  # @param [Range] other
  # @return [Range] smallest possible range spanning both this range and `other`.
  #
  def join(other)
    Range.new(@source_buffer,
        [@begin_pos, other.begin_pos].min,
        [@end_pos,   other.end_pos].max)
  end

  ##
  # @param [Range] other
  # @return [Range] overlapping region of this range and `other`, or `nil`
  #   if they do not overlap
  #
  def intersect(other)
    unless disjoint?(other)
      Range.new(@source_buffer,
        [@begin_pos, other.begin_pos].max,
        [@end_pos,   other.end_pos].min)
    end
  end

  ##
  # Return `true` iff this range and `other` are disjoint.
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def disjoint?(other)
    if empty? && other.empty?
      @begin_pos != other.begin_pos
    else
      @begin_pos >= other.end_pos || other.begin_pos >= @end_pos
    end
  end

  ##
  # Return `true` iff this range is not disjoint from `other`.
  #
  # @param [Range] other
  # @return [Boolean] `true` if this range and `other` overlap
  #
  def overlaps?(other)
    !disjoint?(other)
  end

  ##
  # Returns true iff this range contains (strictly) `other`.
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def contains?(other)
    (other.begin_pos <=> @begin_pos) + (@end_pos <=> other.end_pos) >= (other.empty? ? 2 : 1)
  end

  ##
  # Return `other.contains?(self)`
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def contained?(other)
    other.contains?(self)
  end

  ##
  # Returns true iff both ranges intersect and also have different elements from one another.
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def crossing?(other)
    return false unless overlaps?(other)
    (@begin_pos <=> other.begin_pos) * (@end_pos <=> other.end_pos) == 1
  end

  ##
  # Checks if a range is empty; if it contains no characters
  # @return [Boolean]
  def empty?
    @begin_pos == @end_pos
  end

  ##
  # Compares ranges.
  # @return [Boolean]
  #
  def ==(other)
    other.is_a?(Range) &&
      @source_buffer == other.source_buffer &&
      @begin_pos     == other.begin_pos     &&
      @end_pos       == other.end_pos
  end

  ##
  # @return [String] a human-readable representation of this range.
  #
  def inspect
    "#<Parser::Source::Range #{@source_buffer.name} #{@begin_pos}...#{@end_pos}>"
  end
end

#end_posInteger (readonly)

Returns index of the character after the last character in the range

Returns:

  • (Integer)

    index of the character after the last character in the range



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/parser/source/range.rb', line 24

class Range
  attr_reader :source_buffer
  attr_reader :begin_pos, :end_pos

  ##
  # @param [Buffer]  source_buffer
  # @param [Integer] begin_pos
  # @param [Integer] end_pos
  #
  def initialize(source_buffer, begin_pos, end_pos)
    if end_pos < begin_pos
      raise ArgumentError, 'Parser::Source::Range: end_pos must not be less than begin_pos'
    end
    if source_buffer.nil?
      raise ArgumentError, 'Parser::Source::Range: source_buffer must not be nil'
    end

    @source_buffer       = source_buffer
    @begin_pos, @end_pos = begin_pos, end_pos

    freeze
  end

  ##
  # @return [Range] a zero-length range located just before the beginning
  #   of this range.
  #
  def begin
    with(end_pos: @begin_pos)
  end

  ##
  # @return [Range] a zero-length range located just after the end
  #   of this range.
  #
  def end
    with(begin_pos: @end_pos)
  end

  ##
  # @return [Integer] amount of characters included in this range.
  #
  def size
    @end_pos - @begin_pos
  end

  alias length size

  ##
  # Line number of the beginning of this range. By default, the first line
  # of a buffer is 1; as such, line numbers are most commonly one-based.
  #
  # @see Buffer
  # @return [Integer] line number of the beginning of this range.
  #
  def line
    @source_buffer.line_for_position(@begin_pos)
  end

  alias_method :first_line, :line

  ##
  # @return [Integer] zero-based column number of the beginning of this range.
  #
  def column
    @source_buffer.column_for_position(@begin_pos)
  end

  ##
  # @return [Integer] line number of the end of this range.
  #
  def last_line
    @source_buffer.line_for_position(@end_pos)
  end

  ##
  # @return [Integer] zero-based column number of the end of this range.
  #
  def last_column
    @source_buffer.column_for_position(@end_pos)
  end

  ##
  # @return [::Range] a range of columns spanned by this range.
  # @raise RangeError
  #
  def column_range
    if self.begin.line != self.end.line
      raise RangeError, "#{self.inspect} spans more than one line"
    end

    self.begin.column...self.end.column
  end

  ##
  # @return [String] a line of source code containing the beginning of this range.
  #
  def source_line
    @source_buffer.source_line(line)
  end

  ##
  # @return [String] all source code covered by this range.
  #
  def source
    @source_buffer.slice(self.begin_pos...self.end_pos)
  end

  ##
  # `is?` provides a concise way to compare the source corresponding to this range.
  # For example, `r.source == '(' || r.source == 'begin'` is equivalent to
  # `r.is?('(', 'begin')`.
  #
  def is?(*what)
    what.include?(source)
  end

  ##
  # @return [Array(Integer)] a set of character indexes contained in this range.
  #
  def to_a
    (@begin_pos...@end_pos).to_a
  end

  ##
  # Composes a GNU/Clang-style string representation of the beginning of this
  # range.
  #
  # For example, for the following range in file `foo.rb`,
  #
  #     def foo
  #         ^^^
  #
  # `to_s` will return `foo.rb:1:5`.
  # Note that the column index is one-based.
  #
  # @return [String]
  #
  def to_s
    line, column = @source_buffer.decompose_position(@begin_pos)

    [@source_buffer.name, line, column + 1].join(':')
  end

  ##
  # @param [Hash] Endpoint(s) to change, any combination of :begin_pos or :end_pos
  # @return [Range] the same range as this range but with the given end point(s) changed
  # to the given value(s).
  #
  def with(begin_pos: @begin_pos, end_pos: @end_pos)
    Range.new(@source_buffer, begin_pos, end_pos)
  end

  ##
  # @param [Hash] Endpoint(s) to change, any combination of :begin_pos or :end_pos
  # @return [Range] the same range as this range but with the given end point(s) adjusted
  # by the given amount(s)
  #
  def adjust(begin_pos: 0, end_pos: 0)
    Range.new(@source_buffer, @begin_pos + begin_pos, @end_pos + end_pos)
  end

  ##
  # @param [Integer] new_size
  # @return [Range] a range beginning at the same point as this range and length `new_size`.
  #
  def resize(new_size)
    with(end_pos: @begin_pos + new_size)
  end

  ##
  # @param [Range] other
  # @return [Range] smallest possible range spanning both this range and `other`.
  #
  def join(other)
    Range.new(@source_buffer,
        [@begin_pos, other.begin_pos].min,
        [@end_pos,   other.end_pos].max)
  end

  ##
  # @param [Range] other
  # @return [Range] overlapping region of this range and `other`, or `nil`
  #   if they do not overlap
  #
  def intersect(other)
    unless disjoint?(other)
      Range.new(@source_buffer,
        [@begin_pos, other.begin_pos].max,
        [@end_pos,   other.end_pos].min)
    end
  end

  ##
  # Return `true` iff this range and `other` are disjoint.
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def disjoint?(other)
    if empty? && other.empty?
      @begin_pos != other.begin_pos
    else
      @begin_pos >= other.end_pos || other.begin_pos >= @end_pos
    end
  end

  ##
  # Return `true` iff this range is not disjoint from `other`.
  #
  # @param [Range] other
  # @return [Boolean] `true` if this range and `other` overlap
  #
  def overlaps?(other)
    !disjoint?(other)
  end

  ##
  # Returns true iff this range contains (strictly) `other`.
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def contains?(other)
    (other.begin_pos <=> @begin_pos) + (@end_pos <=> other.end_pos) >= (other.empty? ? 2 : 1)
  end

  ##
  # Return `other.contains?(self)`
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def contained?(other)
    other.contains?(self)
  end

  ##
  # Returns true iff both ranges intersect and also have different elements from one another.
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def crossing?(other)
    return false unless overlaps?(other)
    (@begin_pos <=> other.begin_pos) * (@end_pos <=> other.end_pos) == 1
  end

  ##
  # Checks if a range is empty; if it contains no characters
  # @return [Boolean]
  def empty?
    @begin_pos == @end_pos
  end

  ##
  # Compares ranges.
  # @return [Boolean]
  #
  def ==(other)
    other.is_a?(Range) &&
      @source_buffer == other.source_buffer &&
      @begin_pos     == other.begin_pos     &&
      @end_pos       == other.end_pos
  end

  ##
  # @return [String] a human-readable representation of this range.
  #
  def inspect
    "#<Parser::Source::Range #{@source_buffer.name} #{@begin_pos}...#{@end_pos}>"
  end
end

#source_bufferParser::Diagnostic::Engine (readonly)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/parser/source/range.rb', line 24

class Range
  attr_reader :source_buffer
  attr_reader :begin_pos, :end_pos

  ##
  # @param [Buffer]  source_buffer
  # @param [Integer] begin_pos
  # @param [Integer] end_pos
  #
  def initialize(source_buffer, begin_pos, end_pos)
    if end_pos < begin_pos
      raise ArgumentError, 'Parser::Source::Range: end_pos must not be less than begin_pos'
    end
    if source_buffer.nil?
      raise ArgumentError, 'Parser::Source::Range: source_buffer must not be nil'
    end

    @source_buffer       = source_buffer
    @begin_pos, @end_pos = begin_pos, end_pos

    freeze
  end

  ##
  # @return [Range] a zero-length range located just before the beginning
  #   of this range.
  #
  def begin
    with(end_pos: @begin_pos)
  end

  ##
  # @return [Range] a zero-length range located just after the end
  #   of this range.
  #
  def end
    with(begin_pos: @end_pos)
  end

  ##
  # @return [Integer] amount of characters included in this range.
  #
  def size
    @end_pos - @begin_pos
  end

  alias length size

  ##
  # Line number of the beginning of this range. By default, the first line
  # of a buffer is 1; as such, line numbers are most commonly one-based.
  #
  # @see Buffer
  # @return [Integer] line number of the beginning of this range.
  #
  def line
    @source_buffer.line_for_position(@begin_pos)
  end

  alias_method :first_line, :line

  ##
  # @return [Integer] zero-based column number of the beginning of this range.
  #
  def column
    @source_buffer.column_for_position(@begin_pos)
  end

  ##
  # @return [Integer] line number of the end of this range.
  #
  def last_line
    @source_buffer.line_for_position(@end_pos)
  end

  ##
  # @return [Integer] zero-based column number of the end of this range.
  #
  def last_column
    @source_buffer.column_for_position(@end_pos)
  end

  ##
  # @return [::Range] a range of columns spanned by this range.
  # @raise RangeError
  #
  def column_range
    if self.begin.line != self.end.line
      raise RangeError, "#{self.inspect} spans more than one line"
    end

    self.begin.column...self.end.column
  end

  ##
  # @return [String] a line of source code containing the beginning of this range.
  #
  def source_line
    @source_buffer.source_line(line)
  end

  ##
  # @return [String] all source code covered by this range.
  #
  def source
    @source_buffer.slice(self.begin_pos...self.end_pos)
  end

  ##
  # `is?` provides a concise way to compare the source corresponding to this range.
  # For example, `r.source == '(' || r.source == 'begin'` is equivalent to
  # `r.is?('(', 'begin')`.
  #
  def is?(*what)
    what.include?(source)
  end

  ##
  # @return [Array(Integer)] a set of character indexes contained in this range.
  #
  def to_a
    (@begin_pos...@end_pos).to_a
  end

  ##
  # Composes a GNU/Clang-style string representation of the beginning of this
  # range.
  #
  # For example, for the following range in file `foo.rb`,
  #
  #     def foo
  #         ^^^
  #
  # `to_s` will return `foo.rb:1:5`.
  # Note that the column index is one-based.
  #
  # @return [String]
  #
  def to_s
    line, column = @source_buffer.decompose_position(@begin_pos)

    [@source_buffer.name, line, column + 1].join(':')
  end

  ##
  # @param [Hash] Endpoint(s) to change, any combination of :begin_pos or :end_pos
  # @return [Range] the same range as this range but with the given end point(s) changed
  # to the given value(s).
  #
  def with(begin_pos: @begin_pos, end_pos: @end_pos)
    Range.new(@source_buffer, begin_pos, end_pos)
  end

  ##
  # @param [Hash] Endpoint(s) to change, any combination of :begin_pos or :end_pos
  # @return [Range] the same range as this range but with the given end point(s) adjusted
  # by the given amount(s)
  #
  def adjust(begin_pos: 0, end_pos: 0)
    Range.new(@source_buffer, @begin_pos + begin_pos, @end_pos + end_pos)
  end

  ##
  # @param [Integer] new_size
  # @return [Range] a range beginning at the same point as this range and length `new_size`.
  #
  def resize(new_size)
    with(end_pos: @begin_pos + new_size)
  end

  ##
  # @param [Range] other
  # @return [Range] smallest possible range spanning both this range and `other`.
  #
  def join(other)
    Range.new(@source_buffer,
        [@begin_pos, other.begin_pos].min,
        [@end_pos,   other.end_pos].max)
  end

  ##
  # @param [Range] other
  # @return [Range] overlapping region of this range and `other`, or `nil`
  #   if they do not overlap
  #
  def intersect(other)
    unless disjoint?(other)
      Range.new(@source_buffer,
        [@begin_pos, other.begin_pos].max,
        [@end_pos,   other.end_pos].min)
    end
  end

  ##
  # Return `true` iff this range and `other` are disjoint.
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def disjoint?(other)
    if empty? && other.empty?
      @begin_pos != other.begin_pos
    else
      @begin_pos >= other.end_pos || other.begin_pos >= @end_pos
    end
  end

  ##
  # Return `true` iff this range is not disjoint from `other`.
  #
  # @param [Range] other
  # @return [Boolean] `true` if this range and `other` overlap
  #
  def overlaps?(other)
    !disjoint?(other)
  end

  ##
  # Returns true iff this range contains (strictly) `other`.
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def contains?(other)
    (other.begin_pos <=> @begin_pos) + (@end_pos <=> other.end_pos) >= (other.empty? ? 2 : 1)
  end

  ##
  # Return `other.contains?(self)`
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def contained?(other)
    other.contains?(self)
  end

  ##
  # Returns true iff both ranges intersect and also have different elements from one another.
  #
  # Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?
  #
  # @param [Range] other
  # @return [Boolean]
  #
  def crossing?(other)
    return false unless overlaps?(other)
    (@begin_pos <=> other.begin_pos) * (@end_pos <=> other.end_pos) == 1
  end

  ##
  # Checks if a range is empty; if it contains no characters
  # @return [Boolean]
  def empty?
    @begin_pos == @end_pos
  end

  ##
  # Compares ranges.
  # @return [Boolean]
  #
  def ==(other)
    other.is_a?(Range) &&
      @source_buffer == other.source_buffer &&
      @begin_pos     == other.begin_pos     &&
      @end_pos       == other.end_pos
  end

  ##
  # @return [String] a human-readable representation of this range.
  #
  def inspect
    "#<Parser::Source::Range #{@source_buffer.name} #{@begin_pos}...#{@end_pos}>"
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Compares ranges.

Returns:

  • (Boolean)


291
292
293
294
295
296
# File 'lib/parser/source/range.rb', line 291

def ==(other)
  other.is_a?(Range) &&
    @source_buffer == other.source_buffer &&
    @begin_pos     == other.begin_pos     &&
    @end_pos       == other.end_pos
end

#adjust(begin_pos: 0, end_pos: 0) ⇒ Range

by the given amount(s)

Parameters:

  • Endpoint(s) (Hash)

    to change, any combination of :begin_pos or :end_pos

Returns:

  • (Range)

    the same range as this range but with the given end point(s) adjusted



182
183
184
# File 'lib/parser/source/range.rb', line 182

def adjust(begin_pos: 0, end_pos: 0)
  Range.new(@source_buffer, @begin_pos + begin_pos, @end_pos + end_pos)
end

#beginRange

Returns a zero-length range located just before the beginning of this range.

Returns:

  • (Range)

    a zero-length range located just before the beginning of this range.



51
52
53
# File 'lib/parser/source/range.rb', line 51

def begin
  with(end_pos: @begin_pos)
end

#columnInteger

Returns zero-based column number of the beginning of this range.

Returns:

  • (Integer)

    zero-based column number of the beginning of this range.



88
89
90
# File 'lib/parser/source/range.rb', line 88

def column
  @source_buffer.column_for_position(@begin_pos)
end

#column_range::Range

Returns a range of columns spanned by this range.

Returns:

  • (::Range)

    a range of columns spanned by this range.

Raises:

  • RangeError



110
111
112
113
114
115
116
# File 'lib/parser/source/range.rb', line 110

def column_range
  if self.begin.line != self.end.line
    raise RangeError, "#{self.inspect} spans more than one line"
  end

  self.begin.column...self.end.column
end

#contained?(other) ⇒ Boolean

Return other.contains?(self)

Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?

Parameters:

Returns:

  • (Boolean)


263
264
265
# File 'lib/parser/source/range.rb', line 263

def contained?(other)
  other.contains?(self)
end

#contains?(other) ⇒ Boolean

Returns true iff this range contains (strictly) other.

Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?

Parameters:

Returns:

  • (Boolean)


251
252
253
# File 'lib/parser/source/range.rb', line 251

def contains?(other)
  (other.begin_pos <=> @begin_pos) + (@end_pos <=> other.end_pos) >= (other.empty? ? 2 : 1)
end

#crossing?(other) ⇒ Boolean

Returns true iff both ranges intersect and also have different elements from one another.

Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?

Parameters:

Returns:

  • (Boolean)


275
276
277
278
# File 'lib/parser/source/range.rb', line 275

def crossing?(other)
  return false unless overlaps?(other)
  (@begin_pos <=> other.begin_pos) * (@end_pos <=> other.end_pos) == 1
end

#disjoint?(other) ⇒ Boolean

Return true iff this range and other are disjoint.

Two ranges must be one and only one of ==, disjoint?, contains?, contained? or crossing?

Parameters:

Returns:

  • (Boolean)


225
226
227
228
229
230
231
# File 'lib/parser/source/range.rb', line 225

def disjoint?(other)
  if empty? && other.empty?
    @begin_pos != other.begin_pos
  else
    @begin_pos >= other.end_pos || other.begin_pos >= @end_pos
  end
end

#empty?Boolean

Checks if a range is empty; if it contains no characters

Returns:

  • (Boolean)


283
284
285
# File 'lib/parser/source/range.rb', line 283

def empty?
  @begin_pos == @end_pos
end

#endRange

Returns a zero-length range located just after the end of this range.

Returns:

  • (Range)

    a zero-length range located just after the end of this range.



59
60
61
# File 'lib/parser/source/range.rb', line 59

def end
  with(begin_pos: @end_pos)
end

#inspectString

Returns a human-readable representation of this range.

Returns:

  • (String)

    a human-readable representation of this range.



301
302
303
# File 'lib/parser/source/range.rb', line 301

def inspect
  "#<Parser::Source::Range #{@source_buffer.name} #{@begin_pos}...#{@end_pos}>"
end

#intersect(other) ⇒ Range

Returns overlapping region of this range and other, or nil if they do not overlap

Parameters:

Returns:

  • (Range)

    overlapping region of this range and other, or nil if they do not overlap



209
210
211
212
213
214
215
# File 'lib/parser/source/range.rb', line 209

def intersect(other)
  unless disjoint?(other)
    Range.new(@source_buffer,
      [@begin_pos, other.begin_pos].max,
      [@end_pos,   other.end_pos].min)
  end
end

#is?(*what) ⇒ Boolean

is? provides a concise way to compare the source corresponding to this range. For example, r.source == '(' || r.source == 'begin' is equivalent to r.is?('(', 'begin').

Returns:

  • (Boolean)


137
138
139
# File 'lib/parser/source/range.rb', line 137

def is?(*what)
  what.include?(source)
end

#join(other) ⇒ Range

Returns smallest possible range spanning both this range and other.

Parameters:

Returns:

  • (Range)

    smallest possible range spanning both this range and other.



198
199
200
201
202
# File 'lib/parser/source/range.rb', line 198

def join(other)
  Range.new(@source_buffer,
      [@begin_pos, other.begin_pos].min,
      [@end_pos,   other.end_pos].max)
end

#last_columnInteger

Returns zero-based column number of the end of this range.

Returns:

  • (Integer)

    zero-based column number of the end of this range.



102
103
104
# File 'lib/parser/source/range.rb', line 102

def last_column
  @source_buffer.column_for_position(@end_pos)
end

#last_lineInteger

Returns line number of the end of this range.

Returns:

  • (Integer)

    line number of the end of this range.



95
96
97
# File 'lib/parser/source/range.rb', line 95

def last_line
  @source_buffer.line_for_position(@end_pos)
end

#lineInteger Also known as: first_line

Line number of the beginning of this range. By default, the first line of a buffer is 1; as such, line numbers are most commonly one-based.

Returns:

  • (Integer)

    line number of the beginning of this range.

See Also:



79
80
81
# File 'lib/parser/source/range.rb', line 79

def line
  @source_buffer.line_for_position(@begin_pos)
end

#overlaps?(other) ⇒ Boolean

Return true iff this range is not disjoint from other.

Parameters:

Returns:

  • (Boolean)

    true if this range and other overlap



239
240
241
# File 'lib/parser/source/range.rb', line 239

def overlaps?(other)
  !disjoint?(other)
end

#resize(new_size) ⇒ Range

Returns a range beginning at the same point as this range and length new_size.

Parameters:

  • new_size (Integer)

Returns:

  • (Range)

    a range beginning at the same point as this range and length new_size.



190
191
192
# File 'lib/parser/source/range.rb', line 190

def resize(new_size)
  with(end_pos: @begin_pos + new_size)
end

#sizeInteger Also known as: length

Returns amount of characters included in this range.

Returns:

  • (Integer)

    amount of characters included in this range.



66
67
68
# File 'lib/parser/source/range.rb', line 66

def size
  @end_pos - @begin_pos
end

#sourceString

Returns all source code covered by this range.

Returns:

  • (String)

    all source code covered by this range.



128
129
130
# File 'lib/parser/source/range.rb', line 128

def source
  @source_buffer.slice(self.begin_pos...self.end_pos)
end

#source_lineString

Returns a line of source code containing the beginning of this range.

Returns:

  • (String)

    a line of source code containing the beginning of this range.



121
122
123
# File 'lib/parser/source/range.rb', line 121

def source_line
  @source_buffer.source_line(line)
end

#to_aArray(Integer)

Returns a set of character indexes contained in this range.

Returns:

  • (Array(Integer))

    a set of character indexes contained in this range.



144
145
146
# File 'lib/parser/source/range.rb', line 144

def to_a
  (@begin_pos...@end_pos).to_a
end

#to_sString

Composes a GNU/Clang-style string representation of the beginning of this range.

For example, for the following range in file foo.rb,

def foo
    ^^^

to_s will return foo.rb:1:5. Note that the column index is one-based.

Returns:

  • (String)


162
163
164
165
166
# File 'lib/parser/source/range.rb', line 162

def to_s
  line, column = @source_buffer.decompose_position(@begin_pos)

  [@source_buffer.name, line, column + 1].join(':')
end

#with(begin_pos: @begin_pos, end_pos: @end_pos) ⇒ Range

to the given value(s).

Parameters:

  • Endpoint(s) (Hash)

    to change, any combination of :begin_pos or :end_pos

Returns:

  • (Range)

    the same range as this range but with the given end point(s) changed



173
174
175
# File 'lib/parser/source/range.rb', line 173

def with(begin_pos: @begin_pos, end_pos: @end_pos)
  Range.new(@source_buffer, begin_pos, end_pos)
end