Ruby 3.3 リファレンスマニュアル

class Prism::Location

[edit]

要約

ソースコード上の範囲(開始バイトオフセットと長さ)を表すクラスです。構文木の各ノードのほか、Prism::CommentPrism::MagicCommentPrism::ParseErrorPrism::ParseWarningPrism::Token などの location メソッドから得られます。

行番号・桁位置への変換や、該当範囲の文字列の取り出しなどのメソッドを持ちます。桁位置を表すメソッドには「バイト単位」(start_column など)と「文字単位」(start_character_column など)の系列があります。

require "prism"

node = Prism.parse("x = 1 + 2").value.statements.body[0]
loc = node.location
p loc.slice        # => "x = 1 + 2"
p loc.start_line   # => 1
p loc.start_offset # => 0
p loc.length       # => 9

loc = node.value.location # 右辺の 1 + 2
p loc.slice        # => "1 + 2"
p loc.start_column # => 4

目次

インスタンスメソッド

インスタンスメソッド

self == other -> boolRuby 3.3 から[permalink][rdoc][edit]

other が同じ範囲を表す Prism::Location であれば true を返します。

[PARAM] other:
比較対象のオブジェクト
comments -> ArrayRuby 3.3 から[permalink][rdoc][edit]

この位置に関連付けられたコメント(Prism::Comment のサブクラスのインスタンス)の配列を返します。前に付くコメント、後ろに付くコメントの順に並びます。

コメントの関連付けは Prism::ParseResult#attach_comments! を呼び出したときに行われます。呼び出す前は空配列です。

copy(source: self.source, start_offset: self.start_offset, length: self.length) -> Prism::LocationRuby 3.3 から[permalink][rdoc][edit]

指定した属性だけを差し替えた新しい Prism::Location を返します。

end_character_column -> IntegerRuby 3.3 から[permalink][rdoc][edit]

範囲の終端位置の、行頭からの文字単位の桁位置(0 origin)を返します。

require "prism"

loc = Prism.parse('s = "あい" + x').value.statements.body[0].location
p loc.end_column           # => 16 (バイト単位)
p loc.end_character_column # => 12 (文字単位)
end_character_offset -> IntegerRuby 3.3 から[permalink][rdoc][edit]

範囲の終端位置の、ソースコード先頭からの文字単位のオフセットを返します。

end_column -> IntegerRuby 3.3 から[permalink][rdoc][edit]

範囲の終端位置の、行頭からのバイト単位の桁位置(0 origin)を返します。

end_line -> IntegerRuby 3.3 から[permalink][rdoc][edit]

範囲の終端位置がある行の行番号を返します。

end_offset -> IntegerRuby 3.3 から[permalink][rdoc][edit]

範囲の終端位置(最後のバイトの次)のバイトオフセットを返します。

join(other) -> Prism::LocationRuby 3.3 から[permalink][rdoc][edit]

自身の開始位置から other の終端位置までを表す新しい Prism::Location を返します。間にある文字列も範囲に含まれます。

[PARAM] other:
結合する Prism::Location。自身より後ろにある必要があります。
require "prism"

body = Prism.parse("foo = 1\nbar = 2\n").value.statements.body
p body[0].location.join(body[1].location).slice
# => "foo = 1\nbar = 2"
length -> IntegerRuby 3.3 から[permalink][rdoc][edit]

範囲のバイト数を返します。end_offset - start_offset と同じです。

slice -> StringRuby 3.3 から[permalink][rdoc][edit]

範囲に対応するソースコードの文字列を返します。

start_character_column -> IntegerRuby 3.3 から[permalink][rdoc][edit]

範囲の開始位置の、行頭からの文字単位の桁位置(0 origin)を返します。マルチバイト文字を含む行では Prism::Location#start_column と異なる値になります。

start_character_offset -> IntegerRuby 3.3 から[permalink][rdoc][edit]

範囲の開始位置の、ソースコード先頭からの文字単位のオフセットを返します。

start_column -> IntegerRuby 3.3 から[permalink][rdoc][edit]

範囲の開始位置の、行頭からのバイト単位の桁位置(0 origin)を返します。

start_line -> IntegerRuby 3.3 から[permalink][rdoc][edit]

範囲の開始位置がある行の行番号を返します。行番号は 1 から始まります。

start_line_slice -> StringRuby 3.3 から[permalink][rdoc][edit]

開始位置がある行の、行頭から開始位置の直前までの文字列を返します。

require "prism"

loc = Prism.parse("x = 1").value.statements.body[0].value.location
p loc.slice            # => "1"
p loc.start_line_slice # => "x = "
start_offset -> IntegerRuby 3.3 から[permalink][rdoc][edit]

範囲の開始位置のバイトオフセットを返します。