Class: Puppet::Pops::Adapters::SourcePosAdapter

Inherits:
Puppet::Pops::Adaptable::Adapter show all
Defined in:
lib/puppet/pops/adapters.rb

Overview

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.

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.

See Also:

  • Puppet::Pops::Utils#find_closest_positioned

Constant Summary

Constants inherited from Puppet::Pops::Adaptable::Adapter

Puppet::Pops::Adaptable::Adapter::DOUBLE_COLON, Puppet::Pops::Adaptable::Adapter::USCORE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Puppet::Pops::Adaptable::Adapter

adapt, adapt_new, associate_adapter, clear, get, instance_var_name, self_attr_name

Constructor Details

#initialize(o) ⇒ SourcePosAdapter

Returns a new instance of SourcePosAdapter.



31
32
33
# File 'lib/puppet/pops/adapters.rb', line 31

def initialize(o)
  @adapted = o
end

Instance Attribute Details

#locatorObject



25
26
27
# File 'lib/puppet/pops/adapters.rb', line 25

def locator
  @locator
end

Class Method Details

.create_adapter(o) ⇒ Object



27
28
29
# File 'lib/puppet/pops/adapters.rb', line 27

def self.create_adapter(o)
  new(o)
end

Instance Method Details

#extract_textObject

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



84
85
86
# File 'lib/puppet/pops/adapters.rb', line 84

def extract_text
  locator.string.slice(offset, length)
end

#extract_tree_textObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/puppet/pops/adapters.rb', line 88

def extract_tree_text
  first = @adapted.offset
  last = first + @adapted.length
  @adapted.eAllContents.each do |m|
    m_offset = m.offset
    first = m_offset if m_offset < first
    m_last = m_offset + m.length
    last = m_last if m_last > last
  end
  locator.string.slice(first, last-first)
end

#lengthObject



64
65
66
# File 'lib/puppet/pops/adapters.rb', line 64

def length
  @adapted.length
end

#lineObject

Note:

This is an expensive operation

Produces the line number for the given offset.



71
72
73
74
# 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

#offsetObject



60
61
62
# File 'lib/puppet/pops/adapters.rb', line 60

def offset
  @adapted.offset
end

#posObject

Note:

This is an expensive operation

Produces the position on the line of the given offset.



79
80
81
# File 'lib/puppet/pops/adapters.rb', line 79

def pos
  locator.pos_on_line(offset)
end

#to_uriObject

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



101
102
103
104
105
# File 'lib/puppet/pops/adapters.rb', line 101

def to_uri
  f = locator.file
  f = 'string:' if f.nil? || f.empty?
  URI("#{f}?line=#{line.to_s}&pos=#{pos.to_s}")
end