Module: KPeg::Position

Included in:
CompiledParser, Parser
Defined in:
lib/kpeg/position.rb

Instance Method Summary collapse

Instance Method Details

#current_column(target = pos) ⇒ Object

STANDALONE START



5
6
7
8
9
10
11
# File 'lib/kpeg/position.rb', line 5

def current_column(target=pos)
  if c = string.rindex("\n", target-1)
    return target - c - 1
  end

  target + 1
end

#current_line(target = pos) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kpeg/position.rb', line 14

def current_line(target=pos)
  unless @line_offsets
    @line_offsets = [-1]
    total = 0
    string.each_line do |line|
      @line_offsets << total
      total += line.size
    end
    @line_offsets << total
  end

  @line_offsets.bsearch_index {|x| x >= target } || -1
end

#linesObject



42
43
44
45
46
# File 'lib/kpeg/position.rb', line 42

def lines
  lines = []
  string.each_line { |l| lines << l }
  lines
end