[Feature #20624] Enhance RubyVM::AbstractSyntaxTree::Node#locations

This commit introduce `RubyVM::AbstractSyntaxTree::Node#locations` method
and `RubyVM::AbstractSyntaxTree::Location` class.

Ruby AST node will hold multiple locations information.
`RubyVM::AbstractSyntaxTree::Node#locations` provides a way to access
these locations information.

`RubyVM::AbstractSyntaxTree::Location` is a class which holds these location information:

* `#first_lineno`
* `#first_column`
* `#last_lineno`
* `#last_column`
This commit is contained in:
yui-knk 2024-07-10 22:28:22 +09:00 committed by Yuichiro Kaneko
parent 5617fec1f8
commit f23485a8d6
Notes: git 2024-07-23 03:36:17 +00:00
4 changed files with 206 additions and 0 deletions

57
ast.rb
View file

@ -272,5 +272,62 @@ module RubyVM::AbstractSyntaxTree
nil
end
end
# call-seq:
# node.locations -> array
#
# Returns location objects associated with the AST node.
# The returned array contains RubyVM::AbstractSyntaxTree::Location.
def locations
Primitive.ast_node_locations
end
end
# RubyVM::AbstractSyntaxTree::Location instances are created by
# RubyVM::AbstractSyntaxTree#locations.
#
# This class is MRI specific.
#
class Location
# call-seq:
# location.first_lineno -> integer
#
# The line number in the source code where this AST's text began.
def first_lineno
Primitive.ast_location_first_lineno
end
# call-seq:
# location.first_column -> integer
#
# The column number in the source code where this AST's text began.
def first_column
Primitive.ast_location_first_column
end
# call-seq:
# location.last_lineno -> integer
#
# The line number in the source code where this AST's text ended.
def last_lineno
Primitive.ast_location_last_lineno
end
# call-seq:
# location.last_column -> integer
#
# The column number in the source code where this AST's text ended.
def last_column
Primitive.ast_location_last_column
end
# call-seq:
# location.inspect -> string
#
# Returns debugging information about this location as a string.
def inspect
Primitive.ast_location_inspect
end
end
end