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
75 def self.compute_line_index(string)
76   scanner = StringScanner.new(string)
77   result = [0] # first line starts at 0
78   while scanner.scan_until(/\n/)
79     result << scanner.pos
80   end
81   result.freeze
82 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
 9 def self.locator(string, file, index = nil, char_offsets = false)
10   if char_offsets
11     LocatorForChars.new(string, file, index)
12   else
13     Locator19.new(string, file, index)
14   end
15 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
47 def char_length(offset, end_offset)
48 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
43 def char_offset(byte_offset)
44 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
52 def extract_text(offset, length)
53 end
extract_tree_text(ast) click to toggle source
   # File lib/puppet/pops/parser/locator.rb
55 def extract_tree_text(ast)
56   first = ast.offset
57   last = first + ast.length
58   ast._pcore_all_contents([]) do |m|
59     next unless m.is_a?(Model::Positioned)
60     m_offset = m.offset
61     m_last = m_offset + m.length
62     first = m_offset if m_offset < first
63     last = m_last if m_last > last
64   end
65   extract_text(first, last - first)
66 end
file() click to toggle source

Returns the file name associated with the string content

   # File lib/puppet/pops/parser/locator.rb
18 def file
19 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
34 def line_for_offset(offset)
35 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
71 def line_index()
72 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
39 def offset_on_line(offset)
40 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
30 def pos_on_line(offset)
31 end
string() click to toggle source

Returns the string content

   # File lib/puppet/pops/parser/locator.rb
22 def string
23 end
to_s() click to toggle source
   # File lib/puppet/pops/parser/locator.rb
25 def to_s
26   "Locator for file #{file}"
27 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
85 def to_uri(ast)
86   f = file
87   if f.nil? || f.empty?
88     f = 'string:'
89   else
90     f = Puppet::Util.path_to_uri(f).to_s
91   end
92   offset = ast.offset
93   URI("#{f}?line=#{line_for_offset(offset)}&pos=#{pos_on_line(offset)}")
94 end