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:

  • 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.



33
34
35
# File 'lib/puppet/pops/adapters.rb', line 33

def initialize(o)
  @adapted = o
end

Instance Attribute Details

#adaptedObject (readonly)



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

def adapted
  @adapted
end

#locatorObject



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

def locator
  @locator
end

Class Method Details

.create_adapter(o) ⇒ Object



29
30
31
# File 'lib/puppet/pops/adapters.rb', line 29

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

.find_locator(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
# File 'lib/puppet/pops/adapters.rb', line 45

def self.find_locator(o)
  raise ArgumentError, 'InternalError: SourcePosAdapter for something that has no locator among parents' if o.nil?
  found_locator = o.respond_to?(:locator) ? o.locator : nil
  return found_locator unless found_locator.nil?
  adapter = get(o)
  return adapter.locator unless adapter.nil?
  container = o.eContainer
  container.nil? ? nil : find_locator(container)
end

Instance Method Details

#extract_textObject

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



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

def extract_text
  locator.extract_text(offset, length)
end

#extract_tree_textObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet/pops/adapters.rb', line 82

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

#lengthObject



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

def length
  @adapted.length
end

#lineObject

Note:

This is an expensive operation

Produces the line number for the given offset.



66
67
68
# File 'lib/puppet/pops/adapters.rb', line 66

def line
  locator.line_for_offset(offset)
end

#offsetObject



55
56
57
# File 'lib/puppet/pops/adapters.rb', line 55

def offset
  @adapted.offset
end

#posObject

Note:

This is an expensive operation

Produces the position on the line of the given offset.



73
74
75
# File 'lib/puppet/pops/adapters.rb', line 73

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



96
97
98
99
100
101
102
103
104
# File 'lib/puppet/pops/adapters.rb', line 96

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