3 # AbstractSyntaxTree provides methods to parse Ruby code into
4 # abstract syntax trees. The nodes in the tree
5 # are instances of RubyVM::AbstractSyntaxTree::Node.
7 # This module is MRI specific as it exposes implementation details
8 # of the MRI abstract syntax tree.
10 # This module is experimental and its API is not stable, therefore it might
11 # change without notice. As examples, the order of children nodes is not
12 # guaranteed, the number of children nodes might change, there is no way to
13 # access children nodes by name, etc.
15 # If you are looking for a stable API or an API working under multiple Ruby
16 # implementations, consider using the _parser_ gem or Ripper. If you would
17 # like to make RubyVM::AbstractSyntaxTree stable, please join the discussion
18 # at https://bugs.ruby-lang.org/issues/14844.
20 module RubyVM::AbstractSyntaxTree
23 # RubyVM::AbstractSyntaxTree.parse(string, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
25 # Parses the given _string_ into an abstract syntax tree,
26 # returning the root node of that tree.
28 # RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
29 # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-1:9>
31 # If <tt>keep_script_lines: true</tt> option is provided, the text of the parsed
32 # source is associated with nodes and is available via Node#script_lines.
34 # If <tt>keep_tokens: true</tt> option is provided, Node#tokens are populated.
36 # SyntaxError is raised if the given _string_ is invalid syntax. To overwrite this
37 # behavior, <tt>error_tolerant: true</tt> can be provided. In this case, the parser
38 # will produce a tree where expressions with syntax errors would be represented by
39 # Node with <tt>type=:ERROR</tt>.
41 # root = RubyVM::AbstractSyntaxTree.parse("x = 1; p(x; y=2")
42 # # <internal:ast>:33:in `parse': syntax error, unexpected ';', expecting ')' (SyntaxError)
46 # root = RubyVM::AbstractSyntaxTree.parse("x = 1; p(x; y=2", error_tolerant: true)
50 # # body: (BLOCK@1:0-1:15 (LASGN@1:0-1:5 :x (LIT@1:4-1:5 1)) (ERROR@1:7-1:11) (LASGN@1:12-1:15 :y (LIT@1:14-1:15 2))))
51 # root.children.last.children
52 # # [(LASGN@1:0-1:5 :x (LIT@1:4-1:5 1)),
54 # # (LASGN@1:12-1:15 :y (LIT@1:14-1:15 2))]
56 # Note that parsing continues even after the errored expression.
58 def self.parse string, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false
59 Primitive.ast_s_parse string, keep_script_lines, error_tolerant, keep_tokens
63 # RubyVM::AbstractSyntaxTree.parse_file(pathname, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
65 # Reads the file from _pathname_, then parses it like ::parse,
66 # returning the root node of the abstract syntax tree.
68 # SyntaxError is raised if _pathname_'s contents are not
71 # RubyVM::AbstractSyntaxTree.parse_file("my-app/app.rb")
72 # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-31:3>
74 # See ::parse for explanation of keyword argument meaning and usage.
75 def self.parse_file pathname, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false
76 Primitive.ast_s_parse_file pathname, keep_script_lines, error_tolerant, keep_tokens
80 # RubyVM::AbstractSyntaxTree.of(proc, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
81 # RubyVM::AbstractSyntaxTree.of(method, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
83 # Returns AST nodes of the given _proc_ or _method_.
85 # RubyVM::AbstractSyntaxTree.of(proc {1 + 2})
86 # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:35-1:42>
92 # RubyVM::AbstractSyntaxTree.of(method(:hello))
93 # # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-3:3>
95 # See ::parse for explanation of keyword argument meaning and usage.
96 def self.of body, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false
97 Primitive.ast_s_of body, keep_script_lines, error_tolerant, keep_tokens
101 # RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(backtrace_location) -> integer
103 # Returns the node id for the given backtrace location.
108 # loc = e.backtrace_locations.first
109 # RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(loc)
111 def self.node_id_for_backtrace_location backtrace_location
112 Primitive.node_id_for_backtrace_location backtrace_location
115 # RubyVM::AbstractSyntaxTree::Node instances are created by parse methods in
116 # RubyVM::AbstractSyntaxTree.
118 # This class is MRI specific.
123 # node.type -> symbol
125 # Returns the type of this node as a symbol.
127 # root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2")
128 # root.type # => :SCOPE
129 # lasgn = root.children[2]
130 # lasgn.type # => :LASGN
131 # call = lasgn.children[1]
132 # call.type # => :OPCALL
134 Primitive.ast_node_type
138 # node.first_lineno -> integer
140 # The line number in the source code where this AST's text began.
142 Primitive.ast_node_first_lineno
146 # node.first_column -> integer
148 # The column number in the source code where this AST's text began.
150 Primitive.ast_node_first_column
154 # node.last_lineno -> integer
156 # The line number in the source code where this AST's text ended.
158 Primitive.ast_node_last_lineno
162 # node.last_column -> integer
164 # The column number in the source code where this AST's text ended.
166 Primitive.ast_node_last_column
170 # node.tokens -> array
172 # Returns tokens corresponding to the location of the node.
173 # Returns +nil+ if +keep_tokens+ is not enabled when #parse method is called.
175 # root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2", keep_tokens: true)
176 # root.tokens # => [[0, :tIDENTIFIER, "x", [1, 0, 1, 1]], [1, :tSP, " ", [1, 1, 1, 2]], ...]
177 # root.tokens.map{_1[2]}.join # => "x = 1 + 2"
179 # Token is an array of:
184 # - location [ first_lineno, first_column, last_lineno, last_column ]
186 return nil unless all_tokens
188 all_tokens.each_with_object([]) do |token, a|
190 if ([first_lineno, first_column] <=> [loc[0], loc[1]]) <= 0 &&
191 ([last_lineno, last_column] <=> [loc[2], loc[3]]) >= 0
198 # node.all_tokens -> array
200 # Returns all tokens for the input script regardless the receiver node.
201 # Returns +nil+ if +keep_tokens+ is not enabled when #parse method is called.
203 # root = RubyVM::AbstractSyntaxTree.parse("x = 1 + 2", keep_tokens: true)
204 # root.all_tokens # => [[0, :tIDENTIFIER, "x", [1, 0, 1, 1]], [1, :tSP, " ", [1, 1, 1, 2]], ...]
205 # root.children[-1].all_tokens # => [[0, :tIDENTIFIER, "x", [1, 0, 1, 1]], [1, :tSP, " ", [1, 1, 1, 2]], ...]
207 Primitive.ast_node_all_tokens
211 # node.children -> array
213 # Returns AST nodes under this one. Each kind of node
214 # has different children, depending on what kind of node it is.
216 # The returned array may contain other nodes or <code>nil</code>.
218 Primitive.ast_node_children
222 # node.inspect -> string
224 # Returns debugging information about this node as a string.
226 Primitive.ast_node_inspect
230 # node.node_id -> integer
232 # Returns an internal node_id number.
233 # Note that this is an API for ruby internal use, debugging,
234 # and research. Do not use this for any other purpose.
235 # The compatibility is not guaranteed.
237 Primitive.ast_node_node_id
241 # node.script_lines -> array
243 # Returns the original source code as an array of lines.
245 # Note that this is an API for ruby internal use, debugging,
246 # and research. Do not use this for any other purpose.
247 # The compatibility is not guaranteed.
249 Primitive.ast_node_script_lines
253 # node.source -> string
255 # Returns the code fragment that corresponds to this AST.
257 # Note that this is an API for ruby internal use, debugging,
258 # and research. Do not use this for any other purpose.
259 # The compatibility is not guaranteed.
261 # Also note that this API may return an incomplete code fragment
262 # that does not parse; for example, a here document following
263 # an expression may be dropped.
267 lines = lines[first_lineno - 1 .. last_lineno - 1]
268 lines[-1] = lines[-1].byteslice(0...last_column)
269 lines[0] = lines[0].byteslice(first_column..-1)