Class: Puppet::Pops::Parser::Locator::Locator19

Inherits:
AbstractLocator show all
Includes:
Types::PuppetObject
Defined in:
lib/puppet/pops/parser/locator.rb

Overview

This implementation is for Ruby19 and Ruby20. It uses byteslice to get strings from byte based offsets. For Ruby20 this is faster than using the Stringscanner.charpos method (byteslice outperforms it, when strings are frozen).

Instance Attribute Summary

Attributes inherited from AbstractLocator

#file, #line_index, #string

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Types::PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Methods inherited from AbstractLocator

#ary_bsearch_i, #eql?, #hash, #initialize, #line_for_offset, #pos_on_line, #to_location_hash

Methods inherited from Puppet::Pops::Parser::Locator

compute_line_index, #extract_tree_text, #file, #line_for_offset, #line_index, locator, #pos_on_line, #string, #to_s, #to_uri

Constructor Details

This class inherits a constructor from Puppet::Pops::Parser::Locator::AbstractLocator

Class Method Details

._pcore_typeObject


318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/puppet/pops/parser/locator.rb', line 318

def self._pcore_type
  @type ||= Types::PObjectType.new('Puppet::AST::Locator', {
    'attributes' => {
      'string' => Types::PStringType::DEFAULT,
      'file' => Types::PStringType::DEFAULT,
      'line_index' => {
        Types::KEY_TYPE => Types::POptionalType.new(Types::PArrayType.new(Types::PIntegerType::DEFAULT)),
        Types::KEY_VALUE => nil
      }
    }
  })
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 offset


345
346
347
# File 'lib/puppet/pops/parser/locator.rb', line 345

def char_length(offset, end_offset)
  string.byteslice(offset, end_offset - offset).length
end

#char_offset(byte_offset) ⇒ Object

Returns the character offset for a given byte offset Ruby 19 is multibyte but has no character position methods, must use byteslice


340
341
342
# File 'lib/puppet/pops/parser/locator.rb', line 340

def char_offset(byte_offset)
  string.byteslice(0, byte_offset).length
end

#extract_text(offset, length) ⇒ Object

Extracts the text from byte offset with given byte length


351
352
353
# File 'lib/puppet/pops/parser/locator.rb', line 351

def extract_text(offset, length)
  string.byteslice(offset, length)
end

#offset_on_line(offset) ⇒ Object

Returns the offset on line (first offset on a line is 0). Ruby 19 is multibyte but has no character position methods, must use byteslice


333
334
335
336
# File 'lib/puppet/pops/parser/locator.rb', line 333

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