class Puppet::Pops::Adapters::SourcePosAdapter

A SourcePosAdapter holds a reference to a Positioned object (object that has offset and length). This somewhat complex structure makes it possible to correctly refer to a source position in source that is embedded in some resource; a parser only sees the embedded snippet of source text and does not know where it was embedded. It also enables lazy evaluation of source positions (they are rarely needed - typically just when there is an error to report.

@note It is relatively expensive to compute line and position on line - it is not something that

should be done for every token or model object.

@see Puppet::Pops::Utils#find_adapter, Puppet::Pops::Utils#find_closest_positioned

Attributes

locator[RW]

Public Instance Methods

extract_text() click to toggle source

Extracts the text represented by this source position (the string is obtained from the locator)

# File lib/puppet/pops/adapters.rb, line 84
def extract_text
  locator.string.slice(offset, length)
end
length() click to toggle source
# File lib/puppet/pops/adapters.rb, line 64
def length
  @adapted.length
end
line() click to toggle source

Produces the line number for the given offset. @note This is an expensive operation

# File lib/puppet/pops/adapters.rb, line 71
def line
  # Optimization: manual inlining of locator accessor since this method is frequently called
  (@locator ||= find_locator(@adapted.eContainer)).line_for_offset(offset)
end
offset() click to toggle source
# File lib/puppet/pops/adapters.rb, line 60
def offset
  @adapted.offset
end
pos() click to toggle source

Produces the position on the line of the given offset. @note This is an expensive operation

# File lib/puppet/pops/adapters.rb, line 79
def pos
  locator.pos_on_line(offset)
end
to_uri() click to toggle source

Produces an URI with path?line=n&pos=n. If origin is unknown the URI is string:?line=n&pos=n

# File lib/puppet/pops/adapters.rb, line 89
def to_uri
  f = locator.file
  f = 'string:' if f.nil? || f.empty?
  URI("#{f}?line=#{line.to_s}&pos=#{pos.to_s}")
end

Public Class Methods

create_adapter(o) click to toggle source
# File lib/puppet/pops/adapters.rb, line 27
def self.create_adapter(o)
  new(o)
end
new(o) click to toggle source
# File lib/puppet/pops/adapters.rb, line 31
def initialize(o)
  @adapted = o
end