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)
SourceInstance 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)
<=>(other : Location)
Compares two locations by their pos only.
Returns self.pos <=> other.pos.
clone
Sourcecopy_with(pos _pos = @pos, line_number _line_number = @line_number, line_pos _line_pos = @line_pos, token_pos _token_pos = @token_pos)
Sourceline_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 betweenselfand 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
token_pos