Class: Packcr::Parser::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/packcr/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(charnum = 0, linenum = 0) ⇒ Location

Returns a new instance of Location.



8
9
10
11
# File 'lib/packcr/parser.rb', line 8

def initialize(charnum = 0, linenum = 0)
  @charnum = charnum
  @linenum = linenum
end

Instance Attribute Details

#charnumObject (readonly)

Returns the value of attribute charnum.



6
7
8
# File 'lib/packcr/parser.rb', line 6

def charnum
  @charnum
end

#linenumObject (readonly)

Returns the value of attribute linenum.



6
7
8
# File 'lib/packcr/parser.rb', line 6

def linenum
  @linenum
end

Instance Method Details

#+(other) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/packcr/parser.rb', line 13

def +(other)
  if other.linenum.zero?
    Location.new(@charnum + other.charnum, @linenum + other.linenum)
  else
    Location.new(           other.charnum, @linenum + other.linenum)
  end
end

#-(other) ⇒ Object



21
22
23
24
25
# File 'lib/packcr/parser.rb', line 21

def -(other)
  raise "unexpected location #{inspect} - #{other.inspect}" unless other.linenum == linenum || other.charnum.zero?

  Location.new(@charnum - other.charnum, @linenum - other.linenum)
end

#forward(buffer, cur, n) ⇒ Object



27
28
29
# File 'lib/packcr/parser.rb', line 27

def forward(buffer, cur, n)
  Location.new(@charnum, @linenum).forward!(buffer, cur, n)
end

#forward!(buffer, cur, n) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/packcr/parser.rb', line 31

def forward!(buffer, cur, n)
  buffer[cur, n].scan(/(.*)(\n)?/) do
    if Regexp.last_match[2]
      @linenum += 1
      @charnum = 0
    else
      @charnum += Regexp.last_match[1].length
    end
  end
  self
end