struct TopDown::Location

Overview

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).

Included Modules

Defined in:

location.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(pos : Int32, line_number : Int32, line_pos : Int32) #

[View source]

Instance Method Detail

def -(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)

[View source]
def <=>(other : Location) #

Compares two locations by their #pos only.

Returns self.pos <=> other.pos.


[View source]
def clone #

[View source]
def copy_with(pos _pos = @pos, line_number _line_number = @line_number, line_pos _line_pos = @line_pos) #

[View source]
def line_number : Int32 #

def line_pos : Int32 #

def pos : Int32 #

def 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

[View source]