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


75
76
77
78
79
80
81
82
# File 'lib/puppet/pops/parser/locator.rb', line 75

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.


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

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


47
48
# File 'lib/puppet/pops/parser/locator.rb', line 47

def char_length(offset, end_offset)
end

#char_offset(byte_offset) ⇒ Object

Returns the character offset for a given reported offset


43
44
# File 'lib/puppet/pops/parser/locator.rb', line 43

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)


52
53
# File 'lib/puppet/pops/parser/locator.rb', line 52

def extract_text(offset, length)
end

#extract_tree_text(ast) ⇒ Object


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

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


18
19
# File 'lib/puppet/pops/parser/locator.rb', line 18

def file
end

#line_for_offset(offset) ⇒ Object

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


34
35
# File 'lib/puppet/pops/parser/locator.rb', line 34

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.


71
72
# File 'lib/puppet/pops/parser/locator.rb', line 71

def line_index()
end

#offset_on_line(offset) ⇒ Object

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


39
40
# File 'lib/puppet/pops/parser/locator.rb', line 39

def offset_on_line(offset)
end

#pos_on_line(offset) ⇒ Object

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


30
31
# File 'lib/puppet/pops/parser/locator.rb', line 30

def pos_on_line(offset)
end

#stringObject

Returns the string content


22
23
# File 'lib/puppet/pops/parser/locator.rb', line 22

def string
end

#to_sObject


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

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


85
86
87
88
89
90
91
92
93
94
# File 'lib/puppet/pops/parser/locator.rb', line 85

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