instance method
RubyVM::AbstractSyntaxTree::Node#tokens
() -> ::Array[[ ::Integer, ::Symbol, ::String, [ ::Integer, ::Integer, ::Integer, ::Integer ] ]]?
node.tokens → array
ast.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/ast.rb, line 184
def tokens
return nil unless all_tokens
all_tokens.each_with_object([]) do |token, a|
loc = token.last
if ([first_lineno, first_column] <=> [loc[0], loc[1]]) <= 0 &&
([last_lineno, last_column] <=> [loc[2], loc[3]]) >= 0
a << token
end
end
end
Returns tokens corresponding to the location of the node. Returns nil if keep_tokens is not enabled when parse method is called.
Example
root = 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"
Token is an array of:
-
id
-
token type
-
source code text
-
location [
first_lineno,first_column,last_lineno,last_column]