Class: Puppet::Pops::Parser::Locator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/parser/locator.rb

Overview

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

Direct Known Subclasses

AbstractLocator

Defined Under Namespace

Classes: AbstractLocator, Locator19, LocatorForChars, SubLocator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compute_line_index(string) ⇒ Object

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



78
79
80
81
82
83
84
85
# File 'lib/puppet/pops/parser/locator.rb', line 78

def self.compute_line_index(string)
  scanner = StringScanner.new(string)
  result = [0] # first line starts at 0
  while scanner.scan_until(/\n/)
    result << scanner.pos
  end
  result.freeze
end

.locator(string, file, index = nil, char_offsets = false) ⇒ Object

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



11
12
13
14
15
16
17
# File 'lib/puppet/pops/parser/locator.rb', line 11

def self.locator(string, file, index = nil, char_offsets = false)
  if char_offsets
    LocatorForChars.new(string, file, index)
  else
    Locator19.new(string, file, index)
  end
end

Instance Method Details

#char_length(offset, end_offset) ⇒ Object

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



49
50
# File 'lib/puppet/pops/parser/locator.rb', line 49

def char_length(offset, end_offset)
end

#char_offset(byte_offset) ⇒ Object

Returns the character offset for a given reported offset



45
46
# File 'lib/puppet/pops/parser/locator.rb', line 45

def char_offset(byte_offset)
end

#extract_text(offset, length) ⇒ Object

Extracts the text from offset with given length (measured in what the locator uses for offset)



54
55
# File 'lib/puppet/pops/parser/locator.rb', line 54

def extract_text(offset, length)
end

#extract_tree_text(ast) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/puppet/pops/parser/locator.rb', line 57

def extract_tree_text(ast)
  first = ast.offset
  last = first + ast.length
  ast._pcore_all_contents([]) do |m|
    next unless m.is_a?(Model::Positioned)

    m_offset = m.offset
    m_last = m_offset + m.length
    first = m_offset if m_offset < first
    last = m_last if m_last > last
  end
  extract_text(first, last - first)
end

#fileObject

Returns the file name associated with the string content



20
21
# File 'lib/puppet/pops/parser/locator.rb', line 20

def file
end

#line_for_offset(offset) ⇒ Object

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



36
37
# File 'lib/puppet/pops/parser/locator.rb', line 36

def line_for_offset(offset)
end

#line_indexObject

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



74
75
# File 'lib/puppet/pops/parser/locator.rb', line 74

def line_index
end

#offset_on_line(offset) ⇒ Object

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



41
42
# File 'lib/puppet/pops/parser/locator.rb', line 41

def offset_on_line(offset)
end

#pos_on_line(offset) ⇒ Object

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



32
33
# File 'lib/puppet/pops/parser/locator.rb', line 32

def pos_on_line(offset)
end

#stringObject

Returns the string content



24
25
# File 'lib/puppet/pops/parser/locator.rb', line 24

def string
end

#to_sObject



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

def to_s
  "Locator for file #{file}"
end

#to_uri(ast) ⇒ Object

Produces an URI with path?line=n&pos=n. If origin is unknown the URI is string:?line=n&pos=n



88
89
90
91
92
93
94
95
96
97
# File 'lib/puppet/pops/parser/locator.rb', line 88

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