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

- (Range) initialize(source_buffer, begin_pos, end_pos)

Returns a new instance of Range

Parameters:

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


33
34
35
36
37
38
# File 'lib/parser/source/range.rb', line 33

def initialize(source_buffer, begin_pos, end_pos)
  @source_buffer       = source_buffer
  @begin_pos, @end_pos = begin_pos, end_pos

  freeze
end

Instance Attribute Details

- (Integer) begin_pos (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
# 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)
    @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
    Range.new(@source_buffer, @begin_pos, @begin_pos)
  end

  ##
  # @return [Range] a zero-length range located just after the end
  #   of this range.
  #
  def end
    Range.new(@source_buffer, @end_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
    line, _ = @source_buffer.decompose_position(@begin_pos)

    line
  end

  ##
  # @return [Integer] zero-based column number of the beginning of this range.
  #
  def column
    _, column = @source_buffer.decompose_position(@begin_pos)

    column
  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.source[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 [Integer] new_size
  # @return [Range] a range beginning at the same point as this range and length `new_size`.
  #
  def resize(new_size)
    Range.new(@source_buffer, @begin_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

  ##
  # 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

- (Integer) end_pos (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
# 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)
    @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
    Range.new(@source_buffer, @begin_pos, @begin_pos)
  end

  ##
  # @return [Range] a zero-length range located just after the end
  #   of this range.
  #
  def end
    Range.new(@source_buffer, @end_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
    line, _ = @source_buffer.decompose_position(@begin_pos)

    line
  end

  ##
  # @return [Integer] zero-based column number of the beginning of this range.
  #
  def column
    _, column = @source_buffer.decompose_position(@begin_pos)

    column
  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.source[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 [Integer] new_size
  # @return [Range] a range beginning at the same point as this range and length `new_size`.
  #
  def resize(new_size)
    Range.new(@source_buffer, @begin_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

  ##
  # 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

- (Parser::Diagnostic::Engine) source_buffer (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
# 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)
    @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
    Range.new(@source_buffer, @begin_pos, @begin_pos)
  end

  ##
  # @return [Range] a zero-length range located just after the end
  #   of this range.
  #
  def end
    Range.new(@source_buffer, @end_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
    line, _ = @source_buffer.decompose_position(@begin_pos)

    line
  end

  ##
  # @return [Integer] zero-based column number of the beginning of this range.
  #
  def column
    _, column = @source_buffer.decompose_position(@begin_pos)

    column
  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.source[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 [Integer] new_size
  # @return [Range] a range beginning at the same point as this range and length `new_size`.
  #
  def resize(new_size)
    Range.new(@source_buffer, @begin_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

  ##
  # 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

- (Boolean) ==(other)

Compares ranges.

Returns:

  • (Boolean)


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

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

- (Range) begin

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.



44
45
46
# File 'lib/parser/source/range.rb', line 44

def begin
  Range.new(@source_buffer, @begin_pos, @begin_pos)
end

- (Integer) column

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

Returns:

  • (Integer)

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



81
82
83
84
85
# File 'lib/parser/source/range.rb', line 81

def column
  _, column = @source_buffer.decompose_position(@begin_pos)

  column
end

- (::Range) column_range

Returns a range of columns spanned by this range.

Returns:

  • (::Range)

    a range of columns spanned by this range.

Raises:

  • RangeError



91
92
93
94
95
96
97
# File 'lib/parser/source/range.rb', line 91

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

- (Range) end

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.



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

def end
  Range.new(@source_buffer, @end_pos, @end_pos)
end

- (String) inspect

Returns a human-readable representation of this range.

Returns:

  • (String)

    a human-readable representation of this range.



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

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

- (Boolean) is?(*what)

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)


118
119
120
# File 'lib/parser/source/range.rb', line 118

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

- (Range) join(other)

Returns smallest possible range spanning both this range and other.

Parameters:

Returns:

  • (Range)

    smallest possible range spanning both this range and other.



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

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

- (Integer) 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:



72
73
74
75
76
# File 'lib/parser/source/range.rb', line 72

def line
  line, _ = @source_buffer.decompose_position(@begin_pos)

  line
end

- (Range) resize(new_size)

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.



153
154
155
# File 'lib/parser/source/range.rb', line 153

def resize(new_size)
  Range.new(@source_buffer, @begin_pos, @begin_pos + new_size)
end

- (Integer) size Also known as: length

Returns amount of characters included in this range.

Returns:

  • (Integer)

    amount of characters included in this range.



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

def size
  @end_pos - @begin_pos
end

- (String) source

Returns all source code covered by this range.

Returns:

  • (String)

    all source code covered by this range.



109
110
111
# File 'lib/parser/source/range.rb', line 109

def source
  @source_buffer.source[self.begin_pos...self.end_pos]
end

- (String) source_line

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.



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

def source_line
  @source_buffer.source_line(line)
end

- (Array(Integer)) to_a

Returns a set of character indexes contained in this range.

Returns:

  • (Array(Integer))

    a set of character indexes contained in this range.



125
126
127
# File 'lib/parser/source/range.rb', line 125

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

- (String) to_s

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:



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

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

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