Module: KPeg::Position
- Included in:
- CompiledParser, Parser
- Defined in:
- lib/kpeg/position.rb
Defined Under Namespace
Classes: KpegPosInfo
Instance Method Summary collapse
- #current_character(target = pos) ⇒ Object
-
#current_column(target = pos) ⇒ Object
STANDALONE START.
- #current_line(target = pos) ⇒ Object
- #current_pos_info(target = pos) ⇒ Object
- #get_line(no) ⇒ Object
- #lines ⇒ Object
- #position_line_offsets ⇒ Object
Instance Method Details
#current_character(target = pos) ⇒ Object
44 45 46 47 48 49 |
# 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) ⇒ Object
STANDALONE START
5 6 7 8 9 10 11 12 13 |
# 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) ⇒ Object
28 29 30 31 32 33 |
# 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) ⇒ Object
53 54 55 56 57 58 59 |
# 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) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# 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 ⇒ Object
61 62 63 |
# File 'lib/kpeg/position.rb', line 61 def lines string.lines end |
#position_line_offsets ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# 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 |