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.



4391
4392
4393
4394
# File 'lib/packcr/parser.rb', line 4391

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

Instance Attribute Details

#charnumObject (readonly)

Returns the value of attribute charnum.



4389
4390
4391
# File 'lib/packcr/parser.rb', line 4389

def charnum
  @charnum
end

#linenumObject (readonly)

Returns the value of attribute linenum.



4389
4390
4391
# File 'lib/packcr/parser.rb', line 4389

def linenum
  @linenum
end

Instance Method Details

#+(other) ⇒ Object



4396
4397
4398
4399
4400
4401
4402
# File 'lib/packcr/parser.rb', line 4396

def +(other)
  if other.linenum == 0
    Location.new(@charnum + other.charnum, @linenum + other.linenum)
  else
    Location.new(           other.charnum, @linenum + other.linenum)
  end
end

#-(other) ⇒ Object



4404
4405
4406
4407
4408
4409
4410
4411
4412
# File 'lib/packcr/parser.rb', line 4404

def -(other)
  if other.linenum == self.linenum
    Location.new(@charnum - other.charnum, @linenum - other.linenum)
  elsif other.charnum == 0
    Location.new(@charnum - other.charnum, @linenum - other.linenum)
  else
    raise "unexpected location #{self.inspect} - #{other.inspect}"
  end
end

#forward(buffer, cur, n) ⇒ Object



4414
4415
4416
# File 'lib/packcr/parser.rb', line 4414

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

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



4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
# File 'lib/packcr/parser.rb', line 4418

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