Class: Parser::Source::Map
Overview
Map relates AST nodes to the source code they were parsed from. More specifically, a Map or its subclass contains a set of ranges:
expression
: smallest range which includes all source corresponding to the node and allexpression
ranges of its children.- other ranges (
begin
,end
,operator
, ...): node-specific ranges pointing to various interesting tokens corresponding to the node.
Note that the Heredoc map is the only one whose expression
does
not include other ranges. It only covers the heredoc marker (<<HERE
),
not the here document itself.
All ranges except expression
are defined by Map subclasses.
Ranges (except expression
) can be nil
if the corresponding token is
not present in source. For example, a hash may not have opening/closing
braces, and so would its source map.
p Parser::CurrentRuby.parse('[1 => 2]').children[0].loc
# => <Parser::Source::Map::Collection:0x007f5492b547d8
# @end=nil, @begin=nil,
# @expression=#<Source::Range (string) 1...7>>
The AST_FORMAT document describes how ranges associated to source code tokens. For example, the entry
(array (int 1) (int 2))
"[1, 2]"
^ begin
^ end
~~~~~~ expression
means that if node
is an AST::Node (array (int 1) (int 2))
,
then node.loc
responds to begin
, end
and expression
, and
node.loc.begin
returns a range pointing at the opening bracket, and so on.
If you want to write code polymorphic by the source map (i.e. accepting
several subclasses of Map), use respond_to?
instead of is_a?
to
check whether the map features the range you need. Concrete Map
subclasses may not be preserved between versions, but their interfaces
will be kept compatible.
You can visualize the source maps with ruby-parse -E
command-line tool.
source://parser//lib/parser/source/map.rb#70
Direct Known Subclasses
Collection, Condition, Constant, Definition, For, Heredoc, Index, Keyword, MethodDefinition, ObjcKwarg, Operator, RescueBody, Send, Ternary, Variable
Defined Under Namespace
Classes: Collection, Condition, Constant, Definition, For, Heredoc, Index, Keyword, MethodDefinition, ObjcKwarg, Operator, RescueBody, Send, Ternary, Variable
Instance Method Summary collapse
-
#column ⇒ Integer
A shortcut for
self.expression.column
. -
#expression ⇒ Range
source://parser//lib/parser/source/map.rb#72.
-
#first_line ⇒ Integer
A shortcut for
self.expression.line
. -
#initialize(expression) ⇒ Map
constructor
source://parser//lib/parser/source/map.rb#76.
-
#last_column ⇒ Integer
A shortcut for
self.expression.last_column
. -
#last_line ⇒ Integer
A shortcut for
self.expression.last_line
. -
#line ⇒ Integer
A shortcut for
self.expression.line
. -
#node ⇒ Parser::AST::Node
The node that is described by this map.
-
#node=(node) ⇒ Object
private
source://parser//lib/parser/source/map.rb#89.
-
#to_hash ⇒ Hash<Symbol, Parser::Source::Range>
Converts this source map to a hash with keys corresponding to ranges.
-
#with_expression(expression_l) ⇒ Object
private
source://parser//lib/parser/source/map.rb#132.
Constructor Details
#initialize(expression) ⇒ Map
source://parser//lib/parser/source/map.rb#76
5595 |
# File 'sorbet/rbi/parser@3.3.8.0.rbi', line 5595 def initialize(expression); end |
Instance Method Details
#column ⇒ Integer
A shortcut for self.expression.column
.
source://parser//lib/parser/source/map.rb#109
5611 |
# File 'sorbet/rbi/parser@3.3.8.0.rbi', line 5611 def column; end |
#expression ⇒ Range
source://parser//lib/parser/source/map.rb#72
5617 |
# File 'sorbet/rbi/parser@3.3.8.0.rbi', line 5617 def expression; end |
#first_line ⇒ Integer
A shortcut for self.expression.line
.
source://parser//lib/parser/source/map.rb#99
5625 |
# File 'sorbet/rbi/parser@3.3.8.0.rbi', line 5625 def first_line; end |
#last_column ⇒ Integer
A shortcut for self.expression.last_column
.
source://parser//lib/parser/source/map.rb#125
5633 |
# File 'sorbet/rbi/parser@3.3.8.0.rbi', line 5633 def last_column; end |
#last_line ⇒ Integer
A shortcut for self.expression.last_line
.
source://parser//lib/parser/source/map.rb#117
5641 |
# File 'sorbet/rbi/parser@3.3.8.0.rbi', line 5641 def last_line; end |
#line ⇒ Integer
A shortcut for self.expression.line
.
source://parser//lib/parser/source/map.rb#99
5649 |
# File 'sorbet/rbi/parser@3.3.8.0.rbi', line 5649 def line; end |
#node ⇒ Parser::AST::Node
The node that is described by this map. Nodes and maps have 1:1 correspondence.
source://parser//lib/parser/source/map.rb#71
5657 |
# File 'sorbet/rbi/parser@3.3.8.0.rbi', line 5657 def node; end |
#node=(node) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
source://parser//lib/parser/source/map.rb#89
5662 |
# File 'sorbet/rbi/parser@3.3.8.0.rbi', line 5662 def node=(node); end |
#to_hash ⇒ Hash<Symbol, Parser::Source::Range>
Converts this source map to a hash with keys corresponding to
ranges. For example, if called on an instance of Collection,
which adds the begin
and end
ranges, the resulting hash
will contain keys :expression
, :begin
and :end
.
source://parser//lib/parser/source/map.rb#166
5682 |
# File 'sorbet/rbi/parser@3.3.8.0.rbi', line 5682 def to_hash; end |
#with_expression(expression_l) ⇒ Object
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
source://parser//lib/parser/source/map.rb#132
5687 |
# File 'sorbet/rbi/parser@3.3.8.0.rbi', line 5687 def with_expression(expression_l); end |