Method: FuPeg::Parser#position

Defined in:
lib/fupeg/parser.rb

#position(bytepos: @scan.pos) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fupeg/parser.rb', line 130

def position(bytepos: @scan.pos)
  lineno = @line_ends.bsearch_index { |x| x >= bytepos }
  case lineno
  when nil
    raise "Position #{bytepos} is larger than string byte size #{@str.bytesize}"
  else
    prev_end = @line_ends[lineno - 1]
    line_start = prev_end + 1
    column = @str.byteslice(line_start, bytepos - prev_end).size
  end
  if bytepos == @str.bytesize
    if @str[-1] == "\n"
      lineno, column = lineno + 1, 1
    else
      column += 1
    end
  end
  line = @str.byteslice(line_start..@line_ends[lineno])
  Position.new(lineno, column, line, charpos(bytepos))
end