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

class Prism::ParseError

[edit]

要約

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 をキーに持つハッシュを返します。

Ruby 3.4 以降は type・level もキーに含まれます。

[PARAM] keys:
取り出したいキーの配列を指定します。すべて取り出す場合は nil を指定します。
例
require "prism"

error = Prism.parse("1 +\n").errors.first
case error
in {message:}
  p message
end
# => "unexpected end-of-input; expected an expression after the operator"
level -> SymbolRuby 3.4 から[permalink][rdoc][edit]

エラーの深刻度による分類を表すシンボル(例: :syntax)を返します。

location -> Prism::LocationRuby 3.3 から[permalink][rdoc][edit]

エラーが発生したソースコード上の位置を表す Prism::Location を返します。該当箇所の文字列そのものは location.slice で取得できます。

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

エラーメッセージを返します。

type -> SymbolRuby 3.4 から[permalink][rdoc][edit]

エラーの種類を表すシンボル(例: :expect_expression_after_operator)を返します。

これはエラーメッセージの変換レイヤーとの連携のために使われる内部用のシンボルであり、正式な公開 API ではありません。将来のバージョンで予告なく変わる可能性があります。