要約
Prism::ParseResult#errors で得られる、構文解析中に発生したエラーを表すクラスです。
例外クラスではないことに注意してください。prism はエラー耐性のあるパーサであり、構文エラーがあっても例外を発生させず、見つかったエラーをこのクラスのインスタンスとして Prism::ParseResult#errors に蓄積します。
例require "prism"
error = Prism.parse("1 +\n").errors.first
p error.class # => Prism::ParseError
p error.message # => "unexpected end-of-input; expected an expression after the operator"
p error.location.start_line # => 1
[SEE_ALSO] Prism::ParseResult#errors, Prism::ParseWarning
目次
- インスタンスメソッド
インスタンスメソッド
deconstruct_keys(keys) -> HashRuby 3.3 から[permalink][rdoc][edit]-
パターンマッチのハッシュパターン(
case error; in {message:})で使われます。message・locationをキーに持つハッシュを返します。- [PARAM]
keys: - 取り出したいキーの配列を指定します。すべて取り出す場合は nil を指定します。
require "prism" error = Prism.parse("1 +\n").errors.first case error in {message:} p message end # => "expected an expression after the operator" - [PARAM]
location -> Prism::LocationRuby 3.3 から[permalink][rdoc][edit]-
エラーが発生したソースコード上の位置を表す Prism::Location を返します。該当箇所の文字列そのものは
location.sliceで取得できます。 message -> StringRuby 3.3 から[permalink][rdoc][edit]-
エラーメッセージを返します。