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.



4739
4740
4741
4742
# File 'lib/packcr/parser.rb', line 4739

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

Instance Attribute Details

#charnumObject (readonly)

Returns the value of attribute charnum.



4737
4738
4739
# File 'lib/packcr/parser.rb', line 4737

def charnum
  @charnum
end

#linenumObject (readonly)

Returns the value of attribute linenum.



4737
4738
4739
# File 'lib/packcr/parser.rb', line 4737

def linenum
  @linenum
end

Instance Method Details

#+(other) ⇒ Object



4744
4745
4746
4747
4748
4749
4750
# File 'lib/packcr/parser.rb', line 4744

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



4752
4753
4754
4755
4756
# File 'lib/packcr/parser.rb', line 4752

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



4758
4759
4760
# File 'lib/packcr/parser.rb', line 4758

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

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



4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
# File 'lib/packcr/parser.rb', line 4762

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