Class: Trenni::Location
- Inherits:
-
Object
- Object
- Trenni::Location
- Defined in:
- lib/trenni/error.rb
Instance Attribute Summary collapse
-
#line_index ⇒ Object
readonly
The line that contains the @offset (base 0 indexing).
-
#line_range ⇒ Object
readonly
The byte offset to the start of that line.
-
#line_text ⇒ Object
readonly
Returns the value of attribute line_text.
Instance Method Summary collapse
-
#initialize(input, offset) ⇒ Location
constructor
A new instance of Location.
-
#line_number ⇒ Object
The line index, but base-1.
-
#line_offset ⇒ Object
The number of bytes from the start of the line to the given offset in the input.
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(input, offset) ⇒ Location
Returns a new instance of Location.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/trenni/error.rb', line 50 def initialize(input, offset) raise ArgumentError.new("Offset #{index} is past end of input #{input.bytesize}") if offset > input.bytesize @offset = offset @line_index = 0 line_offset = next_line_offset = 0 input.each_line do |line| line_offset = next_line_offset next_line_offset += line.bytesize # Is our input offset within this line? if next_line_offset >= offset @line_text = line.chomp @line_range = line_offset...next_line_offset break else @line_index += 1 end end end |
Instance Attribute Details
#line_index ⇒ Object (readonly)
The line that contains the @offset (base 0 indexing).
81 82 83 |
# File 'lib/trenni/error.rb', line 81 def line_index @line_index end |
#line_range ⇒ Object (readonly)
The byte offset to the start of that line.
89 90 91 |
# File 'lib/trenni/error.rb', line 89 def line_range @line_range end |
#line_text ⇒ Object (readonly)
Returns the value of attribute line_text.
96 97 98 |
# File 'lib/trenni/error.rb', line 96 def line_text @line_text end |
Instance Method Details
#line_number ⇒ Object
The line index, but base-1.
84 85 86 |
# File 'lib/trenni/error.rb', line 84 def line_number @line_index + 1 end |
#line_offset ⇒ Object
The number of bytes from the start of the line to the given offset in the input.
92 93 94 |
# File 'lib/trenni/error.rb', line 92 def line_offset @offset - @line_range.min end |
#to_i ⇒ Object
72 73 74 |
# File 'lib/trenni/error.rb', line 72 def to_i @offset end |
#to_s ⇒ Object
76 77 78 |
# File 'lib/trenni/error.rb', line 76 def to_s "[#{self.line_number}:#{self.line_offset}]" end |