module KPeg::Position

Constants

KpegPosInfo

Public Instance Methods

current_character(target=pos) click to toggle source
# File lib/kpeg/position.rb, line 44
def current_character(target=pos)
  if target < 0 || target >= string.size
    raise "Target position #{target} is outside of string"
  end
  string[target, 1]
end
current_column(target=pos) click to toggle source

STANDALONE START

# File lib/kpeg/position.rb, line 5
def current_column(target=pos)
  if string[target] == "\n" && (c = string.rindex("\n", target-1) || -1)
    return target - c
  elsif c = string.rindex("\n", target)
    return target - c
  end

  target + 1
end
current_line(target=pos) click to toggle source
# File lib/kpeg/position.rb, line 28
def current_line(target=pos)
  if line = position_line_offsets.bsearch_index {|x| x > target }
    return line + 1
  end
  raise "Target position #{target} is outside of string"
end
current_pos_info(target=pos) click to toggle source
# File lib/kpeg/position.rb, line 53
def current_pos_info(target=pos)
  l = current_line target
  c = current_column target
  ln = get_line(l-1)
  chr = string[target,1]
  KpegPosInfo.new(target, l, c, ln, chr)
end
get_line(no) click to toggle source
# File lib/kpeg/position.rb, line 65
def get_line(no)
  loff = position_line_offsets
  if no < 0
    raise "Line No is out of range: #{no} < 0"
  elsif no >= loff.size
    raise "Line No is out of range: #{no} >= #{loff.size}"
  end
  lend = loff[no]-1
  lstart = no > 0 ? loff[no-1] : 0
  string[lstart..lend]
end
lines() click to toggle source
# File lib/kpeg/position.rb, line 61
def lines
  string.lines
end
position_line_offsets() click to toggle source
# File lib/kpeg/position.rb, line 15
def position_line_offsets
  unless @position_line_offsets
    @position_line_offsets = []
    total = 0
    string.each_line do |line|
      total += line.size
      @position_line_offsets << total
    end
  end
  @position_line_offsets
end