Class: Parser::Source::Buffer

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

Overview

A buffer with source code. Buffer contains the source code itself, associated location information (name and first line), and takes care of encoding.

A source buffer is immutable once populated.

Constant Summary

ENCODING_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\A\#\s*(en)?coding\s*[:=]\s*
  (
    # Special-case: there's a UTF8-MAC encoding.
    (utf8-mac)
  |
    # Chew the suffix; it's there for emacs compat.
    ([A-Za-z0-9_-]+?)(-unix|-dos|-mac)
  |
    ([A-Za-z0-9_-]+)
  )
/x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, first_line = 1) ⇒ Buffer

Returns a new instance of Buffer



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/parser/source/buffer.rb', line 103

def initialize(name, first_line = 1)
  @name        = name
  @source      = nil
  @first_line  = first_line

  @lines       = nil
  @line_begins = nil

  # UTF-32-reencoded source for O(1) slicing
  @slice_source = nil

  # Cache for fast lookup
  @line_for_position   = {}
  @column_for_position = {}
end

Instance Attribute Details

#first_lineInteger (readonly)

First line of the buffer, 1 by default.

Returns:

  • (Integer)

    first line



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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/parser/source/buffer.rb', line 25

class Buffer
  attr_reader :name, :first_line

  ##
  # @api private
  #
  ENCODING_RE =
    /\A\#\s*(en)?coding\s*[:=]\s*
      (
        # Special-case: there's a UTF8-MAC encoding.
        (utf8-mac)
      |
        # Chew the suffix; it's there for emacs compat.
        ([A-Za-z0-9_-]+?)(-unix|-dos|-mac)
      |
        ([A-Za-z0-9_-]+)
      )
    /x

  ##
  # Try to recognize encoding of `string` as Ruby would, i.e. by looking for
  # magic encoding comment or UTF-8 BOM. `string` can be in any encoding.
  #
  # @param [String]  string
  # @return [String|nil] encoding name, if recognized
  #
  def self.recognize_encoding(string)
    return if string.empty?

    # extract the first two lines in an efficient way
    string =~ /\A(.*)\n?(.*\n)?/
    first_line, second_line = $1, $2

    if first_line.start_with?("\xef\xbb\xbf".freeze) # BOM
      return Encoding::UTF_8
    elsif first_line[0, 2] == '#!'.freeze
      encoding_line = second_line
    else
      encoding_line = first_line
    end

    if (result = ENCODING_RE.match(encoding_line))
      Encoding.find(result[3] || result[4] || result[6])
    else
      nil
    end
  end

  ##
  # Recognize encoding of `input` and process it so it could be lexed.
  #
  #  * If `input` does not contain BOM or magic encoding comment, it is
  #    kept in the original encoding.
  #  * If the detected encoding is binary, `input` is kept in binary.
  #  * Otherwise, `input` is re-encoded into UTF-8 and returned as a
  #    new string.
  #
  # This method mutates the encoding of `input`, but not its content.
  #
  # @param  [String] input
  # @return [String]
  # @raise  [EncodingError]
  #
  def self.reencode_string(input)
    original_encoding = input.encoding
    detected_encoding = recognize_encoding(input.force_encoding(Encoding::BINARY))

    if detected_encoding.nil?
      input.force_encoding(original_encoding)
    elsif detected_encoding == Encoding::BINARY
      input
    else
      input.
        force_encoding(detected_encoding).
        encode(Encoding::UTF_8)
    end
  end

  def initialize(name, first_line = 1)
    @name        = name
    @source      = nil
    @first_line  = first_line

    @lines       = nil
    @line_begins = nil

    # UTF-32-reencoded source for O(1) slicing
    @slice_source = nil

    # Cache for fast lookup
    @line_for_position   = {}
    @column_for_position = {}
  end

  ##
  # Populate this buffer from correspondingly named file.
  #
  # @example
  #  Parser::Source::Buffer.new('foo/bar.rb').read
  #
  # @return [Buffer] self
  # @raise  [ArgumentError] if already populated
  #
  def read
    File.open(@name, 'rb') do |io|
      self.source = io.read
    end

    self
  end

  ##
  # Source code contained in this buffer.
  #
  # @return [String] source code
  # @raise  [RuntimeError] if buffer is not populated yet
  #
  def source
    if @source.nil?
      raise RuntimeError, 'Cannot extract source from uninitialized Source::Buffer'
    end

    @source
  end

  ##
  # Populate this buffer from a string with encoding autodetection.
  # `input` is mutated if not frozen.
  #
  # @param [String] input
  # @raise [ArgumentError] if already populated
  # @raise [EncodingError] if `input` includes invalid byte sequence for the encoding
  # @return [String]
  #
  def source=(input)
    input = input.dup if input.frozen?
    input = self.class.reencode_string(input)

    unless input.valid_encoding?
      raise EncodingError, "invalid byte sequence in #{input.encoding.name}"
    end

    self.raw_source = input
  end

  ##
  # Populate this buffer from a string without encoding autodetection.
  #
  # @param [String] input
  # @raise [ArgumentError] if already populated
  # @return [String]
  #
  def raw_source=(input)
    if @source
      raise ArgumentError, 'Source::Buffer is immutable'
    end

    @source = input.gsub("\r\n".freeze, "\n".freeze).freeze

    if !@source.ascii_only? &&
       @source.encoding != Encoding::UTF_32LE &&
       @source.encoding != Encoding::BINARY
      @slice_source = @source.encode(Encoding::UTF_32LE)
    end
  end

  def slice(range)
    if @slice_source.nil?
      @source[range]
    else
      @slice_source[range].encode(@source.encoding)
    end
  end

  ##
  # Convert a character index into the source to a `[line, column]` tuple.
  #
  # @param  [Integer] position
  # @return [[Integer, Integer]] `[line, column]`
  #
  def decompose_position(position)
    line_no, line_begin = line_for(position)

    [ @first_line + line_no, position - line_begin ]
  end

  ##
  # Convert a character index into the source to a line number.
  #
  # @param  [Integer] position
  # @return [Integer] line
  # @api private
  #
  def line_for_position(position)
    @line_for_position[position] ||= begin
      line_no, _ = line_for(position)
      @first_line + line_no
    end
  end

  ##
  # Convert a character index into the source to a column number.
  #
  # @param  [Integer] position
  # @return [Integer] column
  # @api private
  #
  def column_for_position(position)
    @column_for_position[position] ||= begin
      _, line_begin = line_for(position)
      position - line_begin
    end
  end

  ##
  # Return an `Array` of source code lines.
  #
  # @return [Array<String>]
  #
  def source_lines
    @lines ||= begin
      lines = @source.lines.to_a
      lines << ''.dup if @source.end_with?("\n".freeze)

      lines.each do |line|
        line.chomp!("\n".freeze)
        line.freeze
      end

      lines.freeze
    end
  end

  ##
  # Extract line `lineno` from source, taking `first_line` into account.
  #
  # @param  [Integer] lineno
  # @return [String]
  # @raise  [IndexError] if `lineno` is out of bounds
  #
  def source_line(lineno)
    source_lines.fetch(lineno - @first_line).dup
  end

  ##
  # Extract line `lineno` as a new `Range`, taking `first_line` into account.
  #
  # @param  [Integer] lineno
  # @return [Range]
  # @raise  [IndexError] if `lineno` is out of bounds
  #
  def line_range(lineno)
    index = lineno - @first_line + 1
    if index <= 0 || index > line_begins.size
      raise IndexError, 'Parser::Source::Buffer: range for line ' \
        "#{lineno} requested, valid line numbers are #{@first_line}.." \
        "#{@first_line + line_begins.size - 1}"
    elsif index == line_begins.size
      Range.new(self, line_begins[-index][1], @source.size)
    else
      Range.new(self, line_begins[-index][1], line_begins[-index - 1][1] - 1)
    end
  end

  ##
  # @return [Range] A range covering the whole source
  #
  def source_range
    @source_range ||= Range.new(self, 0, source.size)
  end

  ##
  # Number of last line in the buffer
  #
  # @return [Integer]
  #
  def last_line
    line_begins.size + @first_line - 1
  end

  private

  def line_begins
    unless @line_begins
      @line_begins, index = [ [ 0, 0 ] ], 0

      while index = @source.index("\n".freeze, index)
        index += 1
        @line_begins.unshift [ @line_begins.length, index ]
      end
    end

    @line_begins
  end

  def line_for(position)
    line_begins.bsearch do |line, line_begin|
      line_begin <= position
    end
  end
end

#nameString (readonly)

Buffer name. If the buffer was created from a file, the name corresponds to relative path to the file.

Returns:

  • (String)

    buffer name



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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/parser/source/buffer.rb', line 25

class Buffer
  attr_reader :name, :first_line

  ##
  # @api private
  #
  ENCODING_RE =
    /\A\#\s*(en)?coding\s*[:=]\s*
      (
        # Special-case: there's a UTF8-MAC encoding.
        (utf8-mac)
      |
        # Chew the suffix; it's there for emacs compat.
        ([A-Za-z0-9_-]+?)(-unix|-dos|-mac)
      |
        ([A-Za-z0-9_-]+)
      )
    /x

  ##
  # Try to recognize encoding of `string` as Ruby would, i.e. by looking for
  # magic encoding comment or UTF-8 BOM. `string` can be in any encoding.
  #
  # @param [String]  string
  # @return [String|nil] encoding name, if recognized
  #
  def self.recognize_encoding(string)
    return if string.empty?

    # extract the first two lines in an efficient way
    string =~ /\A(.*)\n?(.*\n)?/
    first_line, second_line = $1, $2

    if first_line.start_with?("\xef\xbb\xbf".freeze) # BOM
      return Encoding::UTF_8
    elsif first_line[0, 2] == '#!'.freeze
      encoding_line = second_line
    else
      encoding_line = first_line
    end

    if (result = ENCODING_RE.match(encoding_line))
      Encoding.find(result[3] || result[4] || result[6])
    else
      nil
    end
  end

  ##
  # Recognize encoding of `input` and process it so it could be lexed.
  #
  #  * If `input` does not contain BOM or magic encoding comment, it is
  #    kept in the original encoding.
  #  * If the detected encoding is binary, `input` is kept in binary.
  #  * Otherwise, `input` is re-encoded into UTF-8 and returned as a
  #    new string.
  #
  # This method mutates the encoding of `input`, but not its content.
  #
  # @param  [String] input
  # @return [String]
  # @raise  [EncodingError]
  #
  def self.reencode_string(input)
    original_encoding = input.encoding
    detected_encoding = recognize_encoding(input.force_encoding(Encoding::BINARY))

    if detected_encoding.nil?
      input.force_encoding(original_encoding)
    elsif detected_encoding == Encoding::BINARY
      input
    else
      input.
        force_encoding(detected_encoding).
        encode(Encoding::UTF_8)
    end
  end

  def initialize(name, first_line = 1)
    @name        = name
    @source      = nil
    @first_line  = first_line

    @lines       = nil
    @line_begins = nil

    # UTF-32-reencoded source for O(1) slicing
    @slice_source = nil

    # Cache for fast lookup
    @line_for_position   = {}
    @column_for_position = {}
  end

  ##
  # Populate this buffer from correspondingly named file.
  #
  # @example
  #  Parser::Source::Buffer.new('foo/bar.rb').read
  #
  # @return [Buffer] self
  # @raise  [ArgumentError] if already populated
  #
  def read
    File.open(@name, 'rb') do |io|
      self.source = io.read
    end

    self
  end

  ##
  # Source code contained in this buffer.
  #
  # @return [String] source code
  # @raise  [RuntimeError] if buffer is not populated yet
  #
  def source
    if @source.nil?
      raise RuntimeError, 'Cannot extract source from uninitialized Source::Buffer'
    end

    @source
  end

  ##
  # Populate this buffer from a string with encoding autodetection.
  # `input` is mutated if not frozen.
  #
  # @param [String] input
  # @raise [ArgumentError] if already populated
  # @raise [EncodingError] if `input` includes invalid byte sequence for the encoding
  # @return [String]
  #
  def source=(input)
    input = input.dup if input.frozen?
    input = self.class.reencode_string(input)

    unless input.valid_encoding?
      raise EncodingError, "invalid byte sequence in #{input.encoding.name}"
    end

    self.raw_source = input
  end

  ##
  # Populate this buffer from a string without encoding autodetection.
  #
  # @param [String] input
  # @raise [ArgumentError] if already populated
  # @return [String]
  #
  def raw_source=(input)
    if @source
      raise ArgumentError, 'Source::Buffer is immutable'
    end

    @source = input.gsub("\r\n".freeze, "\n".freeze).freeze

    if !@source.ascii_only? &&
       @source.encoding != Encoding::UTF_32LE &&
       @source.encoding != Encoding::BINARY
      @slice_source = @source.encode(Encoding::UTF_32LE)
    end
  end

  def slice(range)
    if @slice_source.nil?
      @source[range]
    else
      @slice_source[range].encode(@source.encoding)
    end
  end

  ##
  # Convert a character index into the source to a `[line, column]` tuple.
  #
  # @param  [Integer] position
  # @return [[Integer, Integer]] `[line, column]`
  #
  def decompose_position(position)
    line_no, line_begin = line_for(position)

    [ @first_line + line_no, position - line_begin ]
  end

  ##
  # Convert a character index into the source to a line number.
  #
  # @param  [Integer] position
  # @return [Integer] line
  # @api private
  #
  def line_for_position(position)
    @line_for_position[position] ||= begin
      line_no, _ = line_for(position)
      @first_line + line_no
    end
  end

  ##
  # Convert a character index into the source to a column number.
  #
  # @param  [Integer] position
  # @return [Integer] column
  # @api private
  #
  def column_for_position(position)
    @column_for_position[position] ||= begin
      _, line_begin = line_for(position)
      position - line_begin
    end
  end

  ##
  # Return an `Array` of source code lines.
  #
  # @return [Array<String>]
  #
  def source_lines
    @lines ||= begin
      lines = @source.lines.to_a
      lines << ''.dup if @source.end_with?("\n".freeze)

      lines.each do |line|
        line.chomp!("\n".freeze)
        line.freeze
      end

      lines.freeze
    end
  end

  ##
  # Extract line `lineno` from source, taking `first_line` into account.
  #
  # @param  [Integer] lineno
  # @return [String]
  # @raise  [IndexError] if `lineno` is out of bounds
  #
  def source_line(lineno)
    source_lines.fetch(lineno - @first_line).dup
  end

  ##
  # Extract line `lineno` as a new `Range`, taking `first_line` into account.
  #
  # @param  [Integer] lineno
  # @return [Range]
  # @raise  [IndexError] if `lineno` is out of bounds
  #
  def line_range(lineno)
    index = lineno - @first_line + 1
    if index <= 0 || index > line_begins.size
      raise IndexError, 'Parser::Source::Buffer: range for line ' \
        "#{lineno} requested, valid line numbers are #{@first_line}.." \
        "#{@first_line + line_begins.size - 1}"
    elsif index == line_begins.size
      Range.new(self, line_begins[-index][1], @source.size)
    else
      Range.new(self, line_begins[-index][1], line_begins[-index - 1][1] - 1)
    end
  end

  ##
  # @return [Range] A range covering the whole source
  #
  def source_range
    @source_range ||= Range.new(self, 0, source.size)
  end

  ##
  # Number of last line in the buffer
  #
  # @return [Integer]
  #
  def last_line
    line_begins.size + @first_line - 1
  end

  private

  def line_begins
    unless @line_begins
      @line_begins, index = [ [ 0, 0 ] ], 0

      while index = @source.index("\n".freeze, index)
        index += 1
        @line_begins.unshift [ @line_begins.length, index ]
      end
    end

    @line_begins
  end

  def line_for(position)
    line_begins.bsearch do |line, line_begin|
      line_begin <= position
    end
  end
end

Class Method Details

.recognize_encoding(string) ⇒ String|nil

Try to recognize encoding of `string` as Ruby would, i.e. by looking for magic encoding comment or UTF-8 BOM. `string` can be in any encoding.

Parameters:

  • string (String)

Returns:

  • (String|nil)

    encoding name, if recognized



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/parser/source/buffer.rb', line 51

def self.recognize_encoding(string)
  return if string.empty?

  # extract the first two lines in an efficient way
  string =~ /\A(.*)\n?(.*\n)?/
  first_line, second_line = $1, $2

  if first_line.start_with?("\xef\xbb\xbf".freeze) # BOM
    return Encoding::UTF_8
  elsif first_line[0, 2] == '#!'.freeze
    encoding_line = second_line
  else
    encoding_line = first_line
  end

  if (result = ENCODING_RE.match(encoding_line))
    Encoding.find(result[3] || result[4] || result[6])
  else
    nil
  end
end

.reencode_string(input) ⇒ String

Recognize encoding of `input` and process it so it could be lexed.

* If `input` does not contain BOM or magic encoding comment, it is
  kept in the original encoding.
* If the detected encoding is binary, `input` is kept in binary.
* Otherwise, `input` is re-encoded into UTF-8 and returned as a
  new string.

This method mutates the encoding of `input`, but not its content.

Parameters:

  • input (String)

Returns:

  • (String)

Raises:

  • (EncodingError)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/parser/source/buffer.rb', line 88

def self.reencode_string(input)
  original_encoding = input.encoding
  detected_encoding = recognize_encoding(input.force_encoding(Encoding::BINARY))

  if detected_encoding.nil?
    input.force_encoding(original_encoding)
  elsif detected_encoding == Encoding::BINARY
    input
  else
    input.
      force_encoding(detected_encoding).
      encode(Encoding::UTF_8)
  end
end

Instance Method Details

#column_for_position(position) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert a character index into the source to a column number.

Parameters:

  • position (Integer)

Returns:

  • (Integer)

    column



232
233
234
235
236
237
# File 'lib/parser/source/buffer.rb', line 232

def column_for_position(position)
  @column_for_position[position] ||= begin
    _, line_begin = line_for(position)
    position - line_begin
  end
end

#decompose_position(position) ⇒ [Integer, Integer]

Convert a character index into the source to a `[line, column]` tuple.

Parameters:

  • position (Integer)

Returns:

  • ([Integer, Integer])

    `[line, column]`



205
206
207
208
209
# File 'lib/parser/source/buffer.rb', line 205

def decompose_position(position)
  line_no, line_begin = line_for(position)

  [ @first_line + line_no, position - line_begin ]
end

#last_lineInteger

Number of last line in the buffer

Returns:

  • (Integer)


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

def last_line
  line_begins.size + @first_line - 1
end

#line_for_position(position) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert a character index into the source to a line number.

Parameters:

  • position (Integer)

Returns:

  • (Integer)

    line



218
219
220
221
222
223
# File 'lib/parser/source/buffer.rb', line 218

def line_for_position(position)
  @line_for_position[position] ||= begin
    line_no, _ = line_for(position)
    @first_line + line_no
  end
end

#line_range(lineno) ⇒ Range

Extract line `lineno` as a new `Range`, taking `first_line` into account.

Parameters:

  • lineno (Integer)

Returns:

Raises:

  • (IndexError)

    if `lineno` is out of bounds



276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/parser/source/buffer.rb', line 276

def line_range(lineno)
  index = lineno - @first_line + 1
  if index <= 0 || index > line_begins.size
    raise IndexError, 'Parser::Source::Buffer: range for line ' \
      "#{lineno} requested, valid line numbers are #{@first_line}.." \
      "#{@first_line + line_begins.size - 1}"
  elsif index == line_begins.size
    Range.new(self, line_begins[-index][1], @source.size)
  else
    Range.new(self, line_begins[-index][1], line_begins[-index - 1][1] - 1)
  end
end

#raw_source=(input) ⇒ String

Populate this buffer from a string without encoding autodetection.

Parameters:

  • input (String)

Returns:

  • (String)

Raises:

  • (ArgumentError)

    if already populated



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/parser/source/buffer.rb', line 177

def raw_source=(input)
  if @source
    raise ArgumentError, 'Source::Buffer is immutable'
  end

  @source = input.gsub("\r\n".freeze, "\n".freeze).freeze

  if !@source.ascii_only? &&
     @source.encoding != Encoding::UTF_32LE &&
     @source.encoding != Encoding::BINARY
    @slice_source = @source.encode(Encoding::UTF_32LE)
  end
end

#readBuffer

Populate this buffer from correspondingly named file.

Examples:

Parser::Source::Buffer.new('foo/bar.rb').read

Returns:

Raises:

  • (ArgumentError)

    if already populated



128
129
130
131
132
133
134
# File 'lib/parser/source/buffer.rb', line 128

def read
  File.open(@name, 'rb') do |io|
    self.source = io.read
  end

  self
end

#slice(range) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/parser/source/buffer.rb', line 191

def slice(range)
  if @slice_source.nil?
    @source[range]
  else
    @slice_source[range].encode(@source.encoding)
  end
end

#sourceString

Source code contained in this buffer.

Returns:

  • (String)

    source code

Raises:

  • (RuntimeError)

    if buffer is not populated yet



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

def source
  if @source.nil?
    raise RuntimeError, 'Cannot extract source from uninitialized Source::Buffer'
  end

  @source
end

#source=(input) ⇒ String

Populate this buffer from a string with encoding autodetection. `input` is mutated if not frozen.

Parameters:

  • input (String)

Returns:

  • (String)

Raises:

  • (ArgumentError)

    if already populated

  • (EncodingError)

    if `input` includes invalid byte sequence for the encoding



159
160
161
162
163
164
165
166
167
168
# File 'lib/parser/source/buffer.rb', line 159

def source=(input)
  input = input.dup if input.frozen?
  input = self.class.reencode_string(input)

  unless input.valid_encoding?
    raise EncodingError, "invalid byte sequence in #{input.encoding.name}"
  end

  self.raw_source = input
end

#source_line(lineno) ⇒ String

Extract line `lineno` from source, taking `first_line` into account.

Parameters:

  • lineno (Integer)

Returns:

  • (String)

Raises:

  • (IndexError)

    if `lineno` is out of bounds



265
266
267
# File 'lib/parser/source/buffer.rb', line 265

def source_line(lineno)
  source_lines.fetch(lineno - @first_line).dup
end

#source_linesArray<String>

Return an `Array` of source code lines.

Returns:

  • (Array<String>)


244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/parser/source/buffer.rb', line 244

def source_lines
  @lines ||= begin
    lines = @source.lines.to_a
    lines << ''.dup if @source.end_with?("\n".freeze)

    lines.each do |line|
      line.chomp!("\n".freeze)
      line.freeze
    end

    lines.freeze
  end
end

#source_rangeRange

Returns A range covering the whole source

Returns:

  • (Range)

    A range covering the whole source



292
293
294
# File 'lib/parser/source/buffer.rb', line 292

def source_range
  @source_range ||= Range.new(self, 0, source.size)
end