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
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
# File lib/puppet/pops/adapters.rb, line 64 def length @adapted.length end
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
# File lib/puppet/pops/adapters.rb, line 60 def offset @adapted.offset end
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
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
# File lib/puppet/pops/adapters.rb, line 27 def self.create_adapter(o) new(o) end
# File lib/puppet/pops/adapters.rb, line 31 def initialize(o) @adapted = o end