123456789_123456789_123456789_123456789_123456789_

Class: Prism::ASCIISource

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Source
Instance Chain:
self, Source
Inherits: Prism::Source
Defined in: lib/prism/parse_result.rb

Overview

Specialized version of Source for source code that includes ASCII characters only. This class is used to apply performance optimizations that cannot be applied to sources that include multibyte characters.

In the extremely rare case that a source includes multi-byte characters but is marked as binary because of a magic encoding comment and it cannot be eagerly converted to UTF-8, this class will be used as well. This is because at that point we will treat everything as single-byte characters.

Class Method Summary

Source - Inherited

.for

Create a new source object with the given source code.

.new

Create a new source object with the given source code.

Instance Attribute Summary

Source - Inherited

#offsets

The list of newline byte offsets in the source code.

#source

The source code that this source object represents.

#start_line

The line number where this source starts.

Instance Method Summary

Source - Inherited

#character_column

Return the column number in characters for the given byte offset.

#character_offset

Return the character offset for the given byte offset.

#code_units_cache

Generate a cache that targets a specific encoding for calculating code unit offsets.

#code_units_column

Returns the column number in code units for the given encoding for the given byte offset.

#code_units_offset

Returns the offset from the start of the file for the given byte offset counting in code units for the given encoding.

#column

Return the column number for the given byte offset.

#encoding

Returns the encoding of the source code, which is set by parameters to the parser or by the encoding magic comment.

#line

Binary search through the offsets to find the line number for the given byte offset.

#line_end

Returns the byte offset of the end of the line corresponding to the given byte offset.

#line_start

Return the byte offset of the start of the line corresponding to the given byte offset.

#lines

Returns the lines of the source code as an array of strings.

#slice

Perform a byteslice on the source code using the given byte offset and byte length.

#find_line

Binary search through the offsets to find the line number for the given byte offset.

Constructor Details

This class inherits a constructor from Prism::Source

Instance Method Details

#character_column(byte_offset)

Return the column number in characters for the given byte offset.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 243

def character_column(byte_offset)
  byte_offset - line_start(byte_offset)
end

#character_offset(byte_offset)

Return the character offset for the given byte offset.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 238

def character_offset(byte_offset)
  byte_offset
end

#code_units_cache(encoding)

Returns a cache that is the identity function in order to maintain the same interface. We can do this because code units are always equivalent to byte offsets for ASCII-only sources.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 260

def code_units_cache(encoding)
  ->(byte_offset) { byte_offset }
end

#code_units_column(byte_offset, encoding)

Specialized version of code_units_column that does not depend on #code_units_offset, which is a more expensive operation. This is essentially the same as Source#column.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 267

def code_units_column(byte_offset, encoding)
  byte_offset - line_start(byte_offset)
end

#code_units_offset(byte_offset, encoding)

Returns the offset from the start of the file for the given byte offset counting in code units for the given encoding.

This method is tested with UTF-8, UTF-16, and UTF-32. If there is the concept of code units that differs from the number of characters in other encodings, it is not captured here.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 253

def code_units_offset(byte_offset, encoding)
  byte_offset
end