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

Instance Method Details

#extract_textObject

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



86
87
88
# File 'lib/puppet/pops/adapters.rb', line 86

def extract_text
  locator.extract_text(offset, length)
end

#extract_tree_textObject



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

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



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

def length
  @adapted.length
end

#lineObject

Note:

This is an expensive operation

Produces the line number for the given offset.



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

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



62
63
64
# File 'lib/puppet/pops/adapters.rb', line 62

def offset
  @adapted.offset
end

#posObject

Note:

This is an expensive operation

Produces the position on the line of the given offset.



81
82
83
# File 'lib/puppet/pops/adapters.rb', line 81

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



104
105
106
107
108
# File 'lib/puppet/pops/adapters.rb', line 104

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