Class: Puppet::Pops::Adapters::SourcePosAdapter
- Inherits:
-
Puppet::Pops::Adaptable::Adapter
- Object
- Puppet::Pops::Adaptable::Adapter
- Puppet::Pops::Adapters::SourcePosAdapter
- Defined in:
- lib/puppet/pops/adapters.rb
Overview
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.
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
-
#extract_text ⇒ Object
Extracts the text represented by this source position (the string is obtained from the locator).
- #extract_tree_text ⇒ Object
-
#initialize(o) ⇒ SourcePosAdapter
constructor
A new instance of SourcePosAdapter.
- #length ⇒ Object
-
#line ⇒ Object
Produces the line number for the given offset.
- #offset ⇒ Object
-
#pos ⇒ Object
Produces the position on the line of the given offset.
-
#to_uri ⇒ Object
Produces an URI with path?line=n&pos=n.
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
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_text ⇒ Object
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_text ⇒ Object
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 |
#length ⇒ Object
64 65 66 |
# File 'lib/puppet/pops/adapters.rb', line 64 def length @adapted.length end |
#line ⇒ Object
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 |
#offset ⇒ Object
60 61 62 |
# File 'lib/puppet/pops/adapters.rb', line 60 def offset @adapted.offset end |
#pos ⇒ Object
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_uri ⇒ Object
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 |