class Prism::Source
这代表了一个已解析的 Ruby 代码源。它与位置信息一起使用,以便能够解析行号和源代码范围。
属性
源代码中的换行符字节偏移列表。
此 Source 对象所代表的源代码。
此源的起始行号。
Public Class Methods
Source
# File lib/prism/parse_result.rb, line 13 def self.for(source, start_line = 1, offsets = []) if source.ascii_only? ASCIISource.new(source, start_line, offsets) elsif source.encoding == Encoding::BINARY source.force_encoding(Encoding::UTF_8) if source.valid_encoding? new(source, start_line, offsets) else # This is an extremely niche use case where the file is marked as # binary, contains multi-byte characters, and those characters are not # valid UTF-8. In this case we'll mark it as binary and fall back to # treating everything as a single-byte character. This _may_ cause # problems when asking for code units, but it appears to be the # cleanest solution at the moment. source.force_encoding(Encoding::BINARY) ASCIISource.new(source, start_line, offsets) end else new(source, start_line, offsets) end end
使用给定的源代码创建一个新的 Source 对象。此方法应代替 new 使用,它将返回一个 Source 对象,或者如果源代码中不存在多字节字符,则返回一个专门的、性能更优的 ASCIISource 对象。
Source
# File lib/prism/parse_result.rb, line 46 def initialize(source, start_line = 1, offsets = []) @source = source @start_line = start_line # set after parsing is done @offsets = offsets # set after parsing is done end
使用给定的源代码创建一个新的 Source 对象。
Public Instance Methods
Source
# File lib/prism/parse_result.rb, line 108 def character_column(byte_offset) character_offset(byte_offset) - character_offset(line_start(byte_offset)) end
返回给定字节偏移量的字符列号。
Source
# File lib/prism/parse_result.rb, line 103 def character_offset(byte_offset) (source.byteslice(0, byte_offset) or raise).length end
返回给定字节偏移量的字符偏移量。
Source
# File lib/prism/parse_result.rb, line 136 def code_units_cache(encoding) CodeUnitsCache.new(source, encoding) end
生成一个针对特定编码的目标缓存,用于计算代码单元偏移量。
Source
# File lib/prism/parse_result.rb, line 142 def code_units_column(byte_offset, encoding) code_units_offset(byte_offset, encoding) - code_units_offset(line_start(byte_offset), encoding) end
返回给定编码下,给定字节偏移量的代码单元列号。
Source
# File lib/prism/parse_result.rb, line 124 def code_units_offset(byte_offset, encoding) byteslice = (source.byteslice(0, byte_offset) or raise).encode(encoding, invalid: :replace, undef: :replace) if encoding == Encoding::UTF_16LE || encoding == Encoding::UTF_16BE byteslice.bytesize / 2 else byteslice.length end end
返回给定字节偏移量在文件中起始位置的偏移量,按给定编码的代码单元计数。
此方法已针对 UTF-8、UTF-16 和 UTF-32 进行了测试。如果存在与其他编码中的字符数不同的代码单元概念,则此处未捕获。
我们在转换过程中特意用替换字符替换了无效和未定义的字符。这有两个原因。首先,给定的字节偏移量可能不会落在字符边界上。其次,源代码可能包含一个在给定编码中没有等价物的字符。
Source
# File lib/prism/parse_result.rb, line 98 def column(byte_offset) byte_offset - line_start(byte_offset) end
返回给定字节偏移量的列号。
Source
# File lib/prism/parse_result.rb, line 147 def deep_freeze source.freeze offsets.freeze freeze end
冻结此对象及其包含的对象。
Source
# File lib/prism/parse_result.rb, line 64 def encoding source.encoding end
返回源代码的编码,该编码由解析器的参数或编码魔术注释设置。
Source
# File lib/prism/parse_result.rb, line 81 def line(byte_offset) start_line + find_line(byte_offset) end
通过二分查找偏移量来查找给定字节偏移量的行号。
Source
# File lib/prism/parse_result.rb, line 93 def line_end(byte_offset) offsets[find_line(byte_offset) + 1] || source.bytesize end
返回与给定字节偏移量对应的行结束位置的字节偏移量。
Source
# File lib/prism/parse_result.rb, line 87 def line_start(byte_offset) offsets[find_line(byte_offset)] end
返回与给定字节偏移量对应的行起始位置的字节偏移量。
Source
# File lib/prism/parse_result.rb, line 69 def lines source.lines end
将源代码的行作为字符串数组返回。
Source
# File lib/prism/parse_result.rb, line 58 def replace_offsets(offsets) @offsets.replace(offsets) end
将 offsets 的值替换为给定值。
Source
# File lib/prism/parse_result.rb, line 53 def replace_start_line(start_line) @start_line = start_line end
将 start_line 的值替换为给定值。
Source
# File lib/prism/parse_result.rb, line 75 def slice(byte_offset, length) source.byteslice(byte_offset, length) or raise end
使用给定的字节偏移量和字节长度对源代码执行字节切片。
私有实例方法
Source
# File lib/prism/parse_result.rb, line 157 def find_line(byte_offset) index = offsets.bsearch_index { |offset| offset > byte_offset } || offsets.length index - 1 end
通过二分查找偏移量来查找给定字节偏移量的行号。