struct TopDown::Location
- TopDown::Location
- Struct
- Value
- Object
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
- Comparable(TopDown::Location)
Defined in:
location.crConstructors
Instance Method Summary
-
#-(other : Location)
Gives the relative location between two others.
-
#<=>(other : Location)
Compares two locations by their
#pos
only. - #clone
- #copy_with(pos _pos = @pos, line_number _line_number = @line_number, line_pos _line_pos = @line_pos)
- #line_number : Int32
- #line_pos : Int32
- #pos : Int32
-
#show_in_source(io : IO, source : String, radius : Int32 = 2, end_location : Location = self)
Displays this location in source.
Constructor Detail
Instance Method Detail
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)
def copy_with(pos _pos = @pos, line_number _line_number = @line_number, line_pos _line_pos = @line_pos)
#
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 betweenself
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