On this page
Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
RubyVM::AbstractSyntaxTree
AbstractSyntaxTree provides methods to parse Ruby code into abstract syntax trees. The nodes in the tree are instances of RubyVM::AbstractSyntaxTree::Node.
This module is MRI specific as it exposes implementation details of the MRI abstract syntax tree.
This module is experimental and its API is not stable, therefore it might change without notice. As examples, the order of children nodes is not guaranteed, the number of children nodes might change, there is no way to access children nodes by name, etc.
Prism is the official Ruby API to parse Ruby code. It has a stable API and is available on most Ruby implementations. Therefore, it is the recommended replacement. Using RubyVM::AbstractSyntaxTree is discouraged for new code for the reasons above and because AbstractSyntaxTree might become deprecated in the future.
Class methods 4
Ruby version changes 4
-
3.4
-
Add
RubyVM::AbstractSyntaxTree::Node#locationsmethod which returns location objects associated with the AST node. [Feature #20624] -
Add
RubyVM::AbstractSyntaxTree::Locationclass which holds location information. [Feature #20624]
-
-
3.2
-
Add
error_tolerantoption forparse,parse_fileandof. [Feature #19013] With this option-
SyntaxErroris suppressed -
AST is returned for invalid input
-
endis complemented when a parser reaches to the end of input butendis insufficient -
endis treated as keyword based on indent
Example# Without error_tolerant option root = RubyVM::AbstractSyntaxTree.parse(<<~RUBY) def m a = 10 if end RUBY # => <internal:ast>:33:in `parse': syntax error, unexpected `end' (SyntaxError) # With error_tolerant option root = RubyVM::AbstractSyntaxTree.parse(<<~RUBY, error_tolerant: true) def m a = 10 if end RUBY p root # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-4:3> # `end` is treated as keyword based on indent root = RubyVM::AbstractSyntaxTree.parse(<<~RUBY, error_tolerant: true) module Z class Foo foo. end def bar end end RUBY p root.children[-1].children[-1].children[-1].children[-2..-1] # => [#<RubyVM::AbstractSyntaxTree::Node:CLASS@2:2-4:5>, #<RubyVM::AbstractSyntaxTree::Node:DEFN@6:2-7:5>]
-
-
Add
keep_tokensoption forparse,parse_fileandof. Add tokens and all_tokens forRubyVM::AbstractSyntaxTree::Node[Feature #19070]Exampleroot = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2", keep_tokens: true) root.tokens # => [[0, :tIDENTIFIER, "x", [1, 0, 1, 1]], [1, :tSP, " ", [1, 1, 1, 2]], ...] root.tokens.map{_1[2]}.join # => "x = 1 + 2"
-