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

class Prism::Node

[edit]

要約

構文解析の結果得られる構文木の各ノードを表す抽象基底クラスです。 Prism.#parse などが返す構文木は、このクラスのサブクラス (150 種類以上)のインスタンスで構成されます。Prism::Node 自身のインスタンスが生成されることはありません。

個々のノードクラス(Prism::ProgramNode・Prism::CallNode など)に固有のフィールド(子ノードや値を取得するアクセサ)は、このページ末尾の ノードクラスの一覧 に名前だけを載せ、個別のエントリは作りません。このページで扱うのは、すべてのノードクラスに共通する API です。個々のノードクラスの詳細は公式ドキュメントを参照してください。

例
require "prism"

node = Prism.parse("1 + 2").value
p node.type           # => :program_node
p node.class          # => Prism::ProgramNode
p node.location.slice # => "1 + 2"

call = node.statements.body[0]
p call.type                    # => :call_node
p call.child_nodes.size        # => 3
p call.compact_child_nodes.size # => 2

[SEE_ALSO] Prism::ParseResult, Prism::Location

目次

インスタンスメソッド

インスタンスメソッド

copy(**params) -> Prism::NodeRuby 3.3 から[permalink][rdoc][edit]

自身と同じクラスの新しいノードを、指定したフィールドだけを差し替えて複製します。渡せるキーワードはノードクラスごとのフィールド名で、指定しなかったフィールドは自身の値を引き継ぎます。

例
require "prism"

call = Prism.parse("1 + 2").value.statements.body[0]
copied = call.copy
p copied.class        # => Prism::CallNode
p copied.equal?(call) # => false
deconstruct_keys(keys) -> HashRuby 3.3 から[permalink][rdoc][edit]

パターンマッチのハッシュパターン(case node; in {value:})で使われます。ノードの各フィールドをキーに持つハッシュを返します。

[PARAM] keys:
取り出したいキーの配列。すべて取り出す場合は nil を指定します。
inspect(inspector = NodeInspector.new) -> StringRuby 3.3 から[permalink][rdoc][edit]

構文木をツリー形式で表した、人間が読みやすい文字列を返します。

inspector は内部実装用の引数なので、通常は省略してください。

例
require "prism"

puts Prism.parse("1 + 2").value.inspect
location -> Prism::LocationRuby 3.3 から[permalink][rdoc][edit]

ノードのソースコード上の位置を表す Prism::Location を返します。

newline? -> boolRuby 3.3 から[permalink][rdoc][edit]

このノードが、TracePoint の :line イベントを発生させる行の位置としてマークされているかどうかを返します。

ノードクラスの一覧

prism の各ノードクラス(Prism::Node のサブクラス)と、そのフィールド(子ノードや値を取得するアクセサ)の名前の一覧です。 prism の config.yml(3.3= prism 0.19.0・3.4= 1.2.0・4.0= 1.7.0・4.1= 1.9.0)から機械的に生成しています。フィールドの構成が版によって異なるノードは、版ごとに行を分けています。各フィールドの意味は公式ドキュメントを参照してください。

ノードクラスフィールド
AliasGlobalVariableNodenew_name, old_name, keyword_loc
AliasMethodNodenew_name, old_name, keyword_loc
AlternationPatternNodeleft, right, operator_loc
AndNodeleft, right, operator_loc
ArgumentsNodeflags, arguments
ArrayNodeflags, elements, opening_loc, closing_loc
ArrayPatternNodeconstant, requireds, rest, posts, opening_loc, closing_loc
AssocNodekey, value, operator_loc
AssocSplatNodevalue, operator_loc
BackReferenceReadNodename
BeginNodebegin_keyword_loc, statements, rescue_clause, else_clause, ensure_clause, end_keyword_loc
BlockArgumentNodeexpression, operator_loc
BlockLocalVariableNodename
BlockNodelocals, locals_body_index, parameters, body, opening_loc, closing_loc
BlockParameterNodename, name_loc, operator_loc
BlockParametersNodeparameters, locals, opening_loc, closing_loc
BreakNodearguments, keyword_loc
CallAndWriteNodeflags, receiver, call_operator_loc, message_loc, read_name, write_name, operator_loc, value
CallNodeflags, receiver, call_operator_loc, name, message_loc, opening_loc, arguments, closing_loc, block
CallOperatorWriteNodeflags, receiver, call_operator_loc, message_loc, read_name, write_name, operator, operator_loc, value
CallOrWriteNodeflags, receiver, call_operator_loc, message_loc, read_name, write_name, operator_loc, value
CallTargetNodeflags, receiver, call_operator_loc, name, message_loc
CapturePatternNodevalue, target, operator_loc
CaseMatchNodepredicate, conditions, consequent, case_keyword_loc, end_keyword_loc
CaseNodepredicate, conditions, consequent, case_keyword_loc, end_keyword_loc
ClassNodelocals, class_keyword_loc, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name
ClassVariableAndWriteNodename, name_loc, operator_loc, value
ClassVariableOperatorWriteNodename, name_loc, operator_loc, value, operator
ClassVariableOrWriteNodename, name_loc, operator_loc, value
ClassVariableReadNodename
ClassVariableTargetNodename
ClassVariableWriteNodename, name_loc, value, operator_loc
ConstantAndWriteNodename, name_loc, operator_loc, value
ConstantOperatorWriteNodename, name_loc, operator_loc, value, operator
ConstantOrWriteNodename, name_loc, operator_loc, value
ConstantPathAndWriteNodetarget, operator_loc, value
ConstantPathNodeparent, child, delimiter_loc
ConstantPathOperatorWriteNodetarget, operator_loc, value, operator
ConstantPathOrWriteNodetarget, operator_loc, value
ConstantPathTargetNodeparent, child, delimiter_loc
ConstantPathWriteNodetarget, operator_loc, value
ConstantReadNodename
ConstantTargetNodename
ConstantWriteNodename, name_loc, value, operator_loc
DefNodename, name_loc, receiver, parameters, body, locals, locals_body_index, def_keyword_loc, operator_loc, lparen_loc, rparen_loc, equal_loc, end_keyword_loc
DefinedNodelparen_loc, value, rparen_loc, keyword_loc
ElseNodeelse_keyword_loc, statements, end_keyword_loc
EmbeddedStatementsNodeopening_loc, statements, closing_loc
EmbeddedVariableNodeoperator_loc, variable
EnsureNodeensure_keyword_loc, statements, end_keyword_loc
FalseNode(なし)
FindPatternNodeconstant, left, requireds, right, opening_loc, closing_loc
FlipFlopNodeflags, left, right, operator_loc
FloatNode(なし)
ForNodeindex, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc
ForwardingArgumentsNode(なし)
ForwardingParameterNode(なし)
ForwardingSuperNodeblock
GlobalVariableAndWriteNodename, name_loc, operator_loc, value
GlobalVariableOperatorWriteNodename, name_loc, operator_loc, value, operator
GlobalVariableOrWriteNodename, name_loc, operator_loc, value
GlobalVariableReadNodename
GlobalVariableTargetNodename
GlobalVariableWriteNodename, name_loc, value, operator_loc
HashNodeopening_loc, elements, closing_loc
HashPatternNodeconstant, elements, rest, opening_loc, closing_loc
IfNodeif_keyword_loc, predicate, then_keyword_loc, statements, consequent, end_keyword_loc
ImaginaryNodenumeric
ImplicitNodevalue
ImplicitRestNode(なし)
InNodepattern, statements, in_loc, then_loc
IndexAndWriteNodeflags, receiver, call_operator_loc, opening_loc, arguments, closing_loc, block, operator_loc, value
IndexOperatorWriteNodeflags, receiver, call_operator_loc, opening_loc, arguments, closing_loc, block, operator, operator_loc, value
IndexOrWriteNodeflags, receiver, call_operator_loc, opening_loc, arguments, closing_loc, block, operator_loc, value
IndexTargetNodeflags, receiver, opening_loc, arguments, closing_loc, block
InstanceVariableAndWriteNodename, name_loc, operator_loc, value
InstanceVariableOperatorWriteNodename, name_loc, operator_loc, value, operator
InstanceVariableOrWriteNodename, name_loc, operator_loc, value
InstanceVariableReadNodename
InstanceVariableTargetNodename
InstanceVariableWriteNodename, name_loc, value, operator_loc
IntegerNodeflags
InterpolatedMatchLastLineNodeflags, opening_loc, parts, closing_loc
InterpolatedRegularExpressionNodeflags, opening_loc, parts, closing_loc
InterpolatedStringNodeopening_loc, parts, closing_loc
InterpolatedSymbolNodeopening_loc, parts, closing_loc
InterpolatedXStringNodeopening_loc, parts, closing_loc
KeywordHashNodeflags, elements
KeywordRestParameterNodename, name_loc, operator_loc
LambdaNodelocals, locals_body_index, operator_loc, opening_loc, closing_loc, parameters, body
LocalVariableAndWriteNodename_loc, operator_loc, value, name, depth
LocalVariableOperatorWriteNodename_loc, operator_loc, value, name, operator, depth
LocalVariableOrWriteNodename_loc, operator_loc, value, name, depth
LocalVariableReadNodename, depth
LocalVariableTargetNodename, depth
LocalVariableWriteNodename, depth, name_loc, value, operator_loc
MatchLastLineNodeflags, opening_loc, content_loc, closing_loc, unescaped
MatchPredicateNodevalue, pattern, operator_loc
MatchRequiredNodevalue, pattern, operator_loc
MatchWriteNodecall, targets
MissingNode(なし)
ModuleNodelocals, module_keyword_loc, constant_path, body, end_keyword_loc, name
MultiTargetNodelefts, rest, rights, lparen_loc, rparen_loc
MultiWriteNodelefts, rest, rights, lparen_loc, rparen_loc, operator_loc, value
NextNodearguments, keyword_loc
NilNode(なし)
NoKeywordsParameterNodeoperator_loc, keyword_loc
NumberedParametersNodemaximum
NumberedReferenceReadNodenumber
OptionalKeywordParameterNodename, name_loc, value
OptionalParameterNodename, name_loc, operator_loc, value
OrNodeleft, right, operator_loc
ParametersNoderequireds, optionals, rest, posts, keywords, keyword_rest, block
ParenthesesNodebody, opening_loc, closing_loc
PinnedExpressionNodeexpression, operator_loc, lparen_loc, rparen_loc
PinnedVariableNodevariable, operator_loc
PostExecutionNodestatements, keyword_loc, opening_loc, closing_loc
PreExecutionNodestatements, keyword_loc, opening_loc, closing_loc
ProgramNodelocals, statements
RangeNodeflags, left, right, operator_loc
RationalNodenumeric
RedoNode(なし)
RegularExpressionNodeflags, opening_loc, content_loc, closing_loc, unescaped
RequiredKeywordParameterNodename, name_loc
RequiredParameterNodename
RescueModifierNodeexpression, keyword_loc, rescue_expression
RescueNodekeyword_loc, exceptions, operator_loc, reference, statements, consequent
RestParameterNodename, name_loc, operator_loc
RetryNode(なし)
ReturnNodekeyword_loc, arguments
SelfNode(なし)
SingletonClassNodelocals, class_keyword_loc, operator_loc, expression, body, end_keyword_loc
SourceEncodingNode(なし)
SourceFileNodefilepath
SourceLineNode(なし)
SplatNodeoperator_loc, expression
StatementsNodebody
StringNodeflags, opening_loc, content_loc, closing_loc, unescaped
SuperNodekeyword_loc, lparen_loc, arguments, rparen_loc, block
SymbolNodeflags, opening_loc, value_loc, closing_loc, unescaped
TrueNode(なし)
UndefNodenames, keyword_loc
UnlessNodekeyword_loc, predicate, then_keyword_loc, statements, consequent, end_keyword_loc
UntilNodeflags, keyword_loc, closing_loc, predicate, statements
WhenNodekeyword_loc, conditions, statements
WhileNodeflags, keyword_loc, closing_loc, predicate, statements
XStringNodeflags, opening_loc, content_loc, closing_loc, unescaped
YieldNodekeyword_loc, lparen_loc, arguments, rparen_loc
pretty_print(q) -> ()Ruby 3.3 から[permalink][rdoc][edit]

pp ライブラリからの呼び出しに対応します。Prism::Node#inspect の出力を、現在のインデントレベルを保ったまま表示します。

set_newline_flag(newline_marked) -> ()Ruby 3.3 から Ruby 3.4 で削除[permalink][rdoc][edit]

このノードの開始行にまだ印が付けられていなければ、Prism::Node#newline? が true を返すように印を付けます。

newline_marked は行番号をインデックスとする真偽値の配列です。Prism::ParseResult#mark_newlines! が構文木を巡回する際に、行ごとに最初に見つかったノードだけに印を付けるために内部で使用します。

[PARAM] newline_marked:
行番号をインデックスとする真偽値の配列を指定します。すでに印が付いている行には true が入っています。

[SEE_ALSO] Prism::Node#newline?

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

ノードの位置に対応するソースコードの文字列を返します。 location.slice と同じです。

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

構文木を Graphviz の DOT 言語形式の文字列に変換します。

例
require "prism"

dot = Prism.parse("1 + 2").value.to_dot
p dot.start_with?("digraph") # => true