Class Parser::Source::Buffer
In: lib/parser/source/buffer.rb
Parent: Object

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.

@!attribute [r] name

 Buffer name. If the buffer was created from a file, the name corresponds
 to relative path to the file.
 @return [String] buffer name

@!attribute [r] first_line

 First line of the buffer, 1 by default.
 @return [Integer] first line

@api public

Methods

Constants

ENCODING_RE = /\#.*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   @api private

Attributes

first_line  [R] 
name  [R] 

Public Class methods

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

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]

Public Instance methods

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

@param [Integer] position @return [Integer] column @api private

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

@param [Integer] position @return [[Integer, Integer]] `[line, column]`

Number of last line in the buffer

@return [Integer]

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

@param [Integer] position @return [Integer] line @api private

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

Populate this buffer from a string without encoding autodetection.

@param [String] input @raise [ArgumentError] if already populated @return [String]

Populate this buffer from correspondingly named file.

@example

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

@return [Buffer] self @raise [ArgumentError] if already populated

Source code contained in this buffer.

@return [String] source code @raise [RuntimeError] if buffer is not populated yet

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]

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

@param [Integer] lineno @return [String] @raise [IndexError] if `lineno` is out of bounds

Return an `Array` of source code lines.

@return [Array<String>]

[Validate]