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



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

def current_line(target=pos)
  cur_offset = 0
  cur_line = 0

  string.each_line do |line|
    cur_line += 1
    cur_offset += line.size
    return cur_line if cur_offset >= target
  end

  -1
end

#linesObject



26
27
28
29
30
# File 'lib/kpeg/position.rb', line 26

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