Class: Puppet::Pops::Parser::Lexer::Locator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/parser/lexer.rb

Overview

Helper class that keeps track of where line breaks are located and can answer questions about positions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, multibyte) ⇒ Locator

Create a locator based on a content string, and a boolean indicating if ruby version support multi-byte strings or not.



796
797
798
799
800
# File 'lib/puppet/pops/parser/lexer.rb', line 796

def initialize(string, multibyte)
  @string = string
  @multibyte = multibyte
  compute_line_index
end

Instance Attribute Details

#line_indexObject (readonly)



790
791
792
# File 'lib/puppet/pops/parser/lexer.rb', line 790

def line_index
  @line_index
end

#stringObject (readonly)



791
792
793
# File 'lib/puppet/pops/parser/lexer.rb', line 791

def string
  @string
end

Instance Method Details

#char_length(offset, end_offset) ⇒ Object

Returns the length measured in number of characters from the given start and end byte offseta



854
855
856
857
858
859
860
# File 'lib/puppet/pops/parser/lexer.rb', line 854

def char_length(offset, end_offset)
  if multibyte?
    @string.byteslice(offset, end_offset - offset).length
  else
    end_offset - offset
  end
end

#char_offset(byte_offset) ⇒ Object

Returns the character offset for a given byte offset



845
846
847
848
849
850
851
# File 'lib/puppet/pops/parser/lexer.rb', line 845

def char_offset(byte_offset)
  if multibyte?
    @string.byteslice(0, byte_offset).length
  else
    byte_offset
  end
end

#compute_line_indexObject

Computes the start offset for each line.



810
811
812
813
814
815
816
817
# File 'lib/puppet/pops/parser/lexer.rb', line 810

def compute_line_index
  scanner = StringScanner.new(@string)
  result = [0] # first line starts at 0
  while scanner.scan_until(/\n/)
    result << scanner.pos
  end
  @line_index = result
end

#line_for_offset(offset) ⇒ Object

Returns the line number (first line is 1) for the given offset



820
821
822
823
824
825
826
# File 'lib/puppet/pops/parser/lexer.rb', line 820

def line_for_offset(offset)
  if line_nbr = line_index.index {|x| x > offset}
    return line_nbr
  end
  # If not found it is after last
  return line_index.size
end

#multibyte?Boolean

Returns whether this a ruby version that supports multi-byte strings or not

Returns:

  • (Boolean)


804
805
806
# File 'lib/puppet/pops/parser/lexer.rb', line 804

def multibyte?
  @multibyte
end

#offset_on_line(offset) ⇒ Object

Returns the offset on line (first offset on a line is 0).



830
831
832
833
834
835
836
837
# File 'lib/puppet/pops/parser/lexer.rb', line 830

def offset_on_line(offset)
  line_offset = line_index[line_for_offset(offset)-1]
  if multibyte?
    @string.byteslice(line_offset, offset-line_offset).length
  else
    offset - line_offset
  end
end

#pos_on_line(offset) ⇒ Object

Returns the position on line (first position on a line is 1)



840
841
842
# File 'lib/puppet/pops/parser/lexer.rb', line 840

def pos_on_line(offset)
  offset_on_line(offset) +1
end