Class: Parser::Source::Map

Inherits:
Object show all
Defined in:
sorbet/rbi/parser@3.3.7.4.rbi

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 all expression 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

Examples:

require 'parser/current'

p Parser::CurrentRuby.parse('[1, 2]').loc
# => #<Parser::Source::Map::Collection:0x007f14b80eccd8
#  @end=#<Source::Range (string) 5...6>,
#  @begin=#<Source::Range (string) 0...1>,
#  @expression=#<Source::Range (string) 0...6>>

Defined Under Namespace

Classes: Collection, Condition, Constant, Definition, For, Heredoc, Index, Keyword, MethodDefinition, ObjcKwarg, Operator, RescueBody, Send, Ternary, Variable

Instance Method Summary collapse

Constructor Details

#initialize(expression) ⇒ Map

source://parser//lib/parser/source/map.rb#76

Parameters:



5589
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5589

def initialize(expression); end

Instance Method Details

#columnInteger

A shortcut for self.expression.column.

source://parser//lib/parser/source/map.rb#109

Returns:

  • (Integer)


5605
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5605

def column; end

#expressionRange

source://parser//lib/parser/source/map.rb#72

Returns:



5611
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5611

def expression; end

#first_lineInteger

A shortcut for self.expression.line.

source://parser//lib/parser/source/map.rb#99

Returns:

  • (Integer)


5619
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5619

def first_line; end

#last_columnInteger

A shortcut for self.expression.last_column.

source://parser//lib/parser/source/map.rb#125

Returns:

  • (Integer)


5627
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5627

def last_column; end

#last_lineInteger

A shortcut for self.expression.last_line.

source://parser//lib/parser/source/map.rb#117

Returns:

  • (Integer)


5635
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5635

def last_line; end

#lineInteger

A shortcut for self.expression.line.

source://parser//lib/parser/source/map.rb#99

Returns:

  • (Integer)


5643
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 5643

def line; end

#nodeParser::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

Returns:



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_hashHash<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

Examples:

require 'parser/current'

p Parser::CurrentRuby.parse('[1, 2]').loc.to_hash
# => {
#   :begin => #<Source::Range (string) 0...1>,
#   :end => #<Source::Range (string) 5...6>,
#   :expression => #<Source::Range (string) 0...6>
# }

Returns:



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