struct

TopDown::Location

Inherits Comparable < Struct < Value < Object

Represents a location into a parser source.

pos is the position on string (in number of bytes).

line_number start by 0.

line_pos is the position on line (in number of characters).

Constructors

new(pos : Int32, line_number : Int32, line_pos : Int32, token_pos : Int32)
Source

Instance methods

-(other : Location)

Gives the relative location between two others.

NOTE: line_pos may be negative even self > other.

l1 = TopDown::Location.new(5, line_number: 0, line_pos: 5)
l2 = TopDown::Location.new(17, line_number: 0, line_pos: 17)
l3 = TopDown::Location.new(32, line_number: 1, line_pos: 13)

l2 - l1 # => TopDown::Location(@pos=12, @line_number=0, @line_pos=12)
l3 - l2 # => TopDown::Location(@pos=15, @line_number=1, @line_pos=-4)
Source
<=>(other : Location)

Compares two locations by their pos only.

Returns self.pos <=> other.pos.

Source
clone
Source
copy_with(pos _pos = @pos, line_number _line_number = @line_number, line_pos _line_pos = @line_pos, token_pos _token_pos = @token_pos)
Source
line_number
line_pos
pos
show_in_source(io : IO, source : String, radius : Int32 = 2, end_location : Location = self)

Displays this location in source.

  • io : the output io.
  • source : the source string.
  • radius : how many lines above and below to show.
  • end_location : if set other than self, the function show the range location between self and end_location.
l1 = TopDown::Location.new(5, line_number: 0, line_pos: 5)
l2 = TopDown::Location.new(17, line_number: 0, line_pos: 17)
source = <<-SOURCE
  puts "Hello World"
  puts "Hello ๐Ÿ’Ž"
  puts "Hello โ™ฅ"
  SOURCE

l1.show_in_source(STDOUT, source)
#   0 | puts "Hello World"
#            ^
#   1 | puts "Hello ๐Ÿ’Ž"
#   2 | puts "Hello โ™ฅ"\n

l1.show_in_source(STDOUT, source, end_location: l2)
#   0 | puts "Hello World"
#            ^~~~~~~~~~~~
#   1 | puts "Hello ๐Ÿ’Ž"
#   2 | puts "Hello โ™ฅ"\n
Source
token_pos