class Parser::Source::Map
@api public
@return [Range]
@!attribute [r] expression
@return [Parser::AST::Node]
The node that is described by this map. Nodes and maps have 1:1 correspondence.
@!attribute [r] node
# @expression=#<Source::Range (string) 0…6>>
# @begin=#<Source::Range (string) 0…1>,
# @end=#<Source::Range (string) 5…6>,
# => #<Parser::Source::Map::Collection:0x007f14b80eccd8
p Parser::CurrentRuby.parse(‘[1, 2]’).loc
require ‘parser/current’
@example
You can visualize the source maps with ‘ruby-parse -E` command-line tool.
will be kept compatible.
subclasses may not be preserved between versions, but their interfaces
check whether the map features the range you need. Concrete {Map}
several subclasses of {Map}), use `respond_to?` instead of `is_a?` to
If you want to write code polymorphic by the source map (i.e. accepting
`node.loc.begin` returns a range pointing at the opening bracket, and so on.
then `node.loc` responds to `begin`, `end` and `expression`, and
means that if `node` is an {Parser::AST::Node} `(array (int 1) (int 2))`,
~~~~~~ expression
^ end
^ begin
“[1, 2]”
(array (int 1) (int 2))
code tokens. For example, the entry
The {file:doc/AST_FORMAT.md} document describes how ranges associated to source
# @expression=#<Source::Range (string) 1…7>>
# @end=nil, @begin=nil,
# => <Parser::Source::Map::Collection:0x007f5492b547d8
p Parser::CurrentRuby.parse(’[1 => 2]‘).children.loc
braces, and so would its source map.
not present in source. For example, a hash may not have opening/closing
Ranges (except `expression`) can be `nil` if the corresponding token is
All ranges except `expression` are defined by {Map} subclasses.
not the here document itself.
not include other ranges. It only covers the heredoc marker (`<<HERE`),
Note that the {Map::Heredoc} map is the only one whose `expression` does
pointing to various interesting tokens corresponding to the node.
* other ranges (`begin`, `end`, `operator`, …): node-specific ranges
to the node and all `expression` ranges of its children.
* `expression`: smallest range which includes all source corresponding
More specifically, a {Map} or its subclass contains a set of ranges:
{Map} relates AST nodes to the source code they were parsed from.
#
def ==(other)
-
(Boolean)
-
def ==(other) other.class == self.class && instance_variables.map do |ivar| instance_variable_get(ivar) == other.send(:instance_variable_get, ivar) end.reduce(:&) end
def column
-
(Integer)
-
def column @expression.column end
def initialize(expression)
-
expression
(Range
) --
def initialize(expression) @expression = expression end
def initialize_copy(other)
- Api: - private
def initialize_copy(other) super @node = nil end
def last_column
-
(Integer)
-
def last_column @expression.last_column end
def last_line
-
(Integer)
-
def last_line @expression.last_line end
def line
-
(Integer)
-
def line @expression.line end
def node=(node)
- Api: - private
def node=(node) @node = node freeze @node end
def to_hash
-
(Hash(Symbol, Parser::Source::Range))
-
def to_hash instance_variables.inject({}) do |hash, ivar| next hash if ivar.to_sym == :@node hash[ivar[1..-1].to_sym] = instance_variable_get(ivar) hash end end
def update_expression(expression_l)
def update_expression(expression_l) @expression = expression_l end
def with(&block)
def with(&block) dup.tap(&block) end
def with_expression(expression_l)
- Api: - private
def with_expression(expression_l) with { |map| map.update_expression(expression_l) } end