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
5589 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5589 def initialize(expression); end |
Instance Method Details
#column ⇒ Integer
A shortcut for self.expression.column
.
source://parser//lib/parser/source/map.rb#109
5605 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5605 def column; end |
#expression ⇒ Range
source://parser//lib/parser/source/map.rb#72
5611 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5611 def expression; end |
#first_line ⇒ Integer
A shortcut for self.expression.line
.
source://parser//lib/parser/source/map.rb#99
5619 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5619 def first_line; end |
#last_column ⇒ Integer
A shortcut for self.expression.last_column
.
source://parser//lib/parser/source/map.rb#125
5627 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5627 def last_column; end |
#last_line ⇒ Integer
A shortcut for self.expression.last_line
.
source://parser//lib/parser/source/map.rb#117
5635 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5635 def last_line; end |
#line ⇒ Integer
A shortcut for self.expression.line
.
source://parser//lib/parser/source/map.rb#99
5643 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5643 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
5651 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5651 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
5656 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5656 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
5676 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5676 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
5681 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5681 def with_expression(expression_l); end |