class Puppet::Pops::Parser::Locator

Helper class that keeps track of where line breaks are located and can answer questions about positions.

Public Class Methods

compute_line_index(string) click to toggle source

Common byte based impl that works for all rubies (stringscanner is byte based

   # File lib/puppet/pops/parser/locator.rb
76 def self.compute_line_index(string)
77   scanner = StringScanner.new(string)
78   result = [0] # first line starts at 0
79   while scanner.scan_until(/\n/)
80     result << scanner.pos
81   end
82   result.freeze
83 end
locator(string, file, index = nil, char_offsets = false) click to toggle source

Creates, or recreates a Locator. A Locator is created if index is not given (a scan is then performed of the given source string.

   # File lib/puppet/pops/parser/locator.rb
10 def self.locator(string, file, index = nil, char_offsets = false)
11   if char_offsets
12     LocatorForChars.new(string, file, index)
13   else
14     Locator19.new(string, file, index)
15   end
16 end

Public Instance Methods

char_length(offset, end_offset) click to toggle source

Returns the length measured in number of characters from the given start and end byte offset

   # File lib/puppet/pops/parser/locator.rb
48 def char_length(offset, end_offset)
49 end
char_offset(byte_offset) click to toggle source

Returns the character offset for a given reported offset

   # File lib/puppet/pops/parser/locator.rb
44 def char_offset(byte_offset)
45 end
extract_text(offset, length) click to toggle source

Extracts the text from offset with given length (measured in what the locator uses for offset) @returns String - the extracted text

   # File lib/puppet/pops/parser/locator.rb
53 def extract_text(offset, length)
54 end
extract_tree_text(ast) click to toggle source
   # File lib/puppet/pops/parser/locator.rb
56 def extract_tree_text(ast)
57   first = ast.offset
58   last = first + ast.length
59   ast._pcore_all_contents([]) do |m|
60     next unless m.is_a?(Model::Positioned)
61     m_offset = m.offset
62     m_last = m_offset + m.length
63     first = m_offset if m_offset < first
64     last = m_last if m_last > last
65   end
66   extract_text(first, last - first)
67 end
file() click to toggle source

Returns the file name associated with the string content

   # File lib/puppet/pops/parser/locator.rb
19 def file
20 end
line_for_offset(offset) click to toggle source

Returns the line number (first line is 1) for the given offset

   # File lib/puppet/pops/parser/locator.rb
35 def line_for_offset(offset)
36 end
line_index() click to toggle source

Returns the line index - an array of line offsets for the start position of each line, starting at 0 for the first line.

   # File lib/puppet/pops/parser/locator.rb
72 def line_index()
73 end
offset_on_line(offset) click to toggle source

Returns the offset on line (first offset on a line is 0).

   # File lib/puppet/pops/parser/locator.rb
40 def offset_on_line(offset)
41 end
pos_on_line(offset) click to toggle source

Returns the position on line (first position on a line is 1)

   # File lib/puppet/pops/parser/locator.rb
31 def pos_on_line(offset)
32 end
string() click to toggle source

Returns the string content

   # File lib/puppet/pops/parser/locator.rb
23 def string
24 end
to_s() click to toggle source
   # File lib/puppet/pops/parser/locator.rb
26 def to_s
27   "Locator for file #{file}"
28 end
to_uri(ast) click to toggle source

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/parser/locator.rb
86 def to_uri(ast)
87   f = file
88   if f.nil? || f.empty?
89     f = 'string:'
90   else
91     f = Puppet::Util.path_to_uri(f).to_s
92   end
93   offset = ast.offset
94   URI("#{f}?line=#{line_for_offset(offset)}&pos=#{pos_on_line(offset)}")
95 end