class Puppet::Pops::Parser::Locator::AbstractLocator
Attributes
Public Class Methods
Create a locator based on a content string, and a boolean indicating if ruby version support multi-byte strings or not.
# File lib/puppet/pops/parser/locator.rb 104 def initialize(string, file, line_index = nil) 105 @string = string.freeze 106 @file = file.freeze 107 @prev_offset = nil 108 @prev_line = nil 109 @line_index = line_index.nil? ? Locator.compute_line_index(@string) : line_index 110 end
Public Instance Methods
Returns the index of the smallest item for which the item > the given value This is a min binary search. Although written in Ruby it is only slightly slower than the corresponding method in C in Ruby 2.0.0 - the main benefit to use this method over the Ruby C version is that it returns the index (not the value) which means there is not need to have an additional structure to get the index (or record the index in the structure). This saves both memory and CPU. It also does not require passing a block that is called since this method is specialized to search the line index.
# File lib/puppet/pops/parser/locator.rb 133 def ary_bsearch_i(ary, value) 134 low = 0 135 high = ary.length 136 mid = nil 137 smaller = false 138 satisfied = false 139 v = nil 140 141 while low < high do 142 mid = low + ((high - low) / 2) 143 v = (ary[mid] > value) 144 if v == true 145 satisfied = true 146 smaller = true 147 elsif !v 148 smaller = false 149 else 150 raise TypeError, "wrong argument, must be boolean or nil, got '#{v.class}'" 151 end 152 153 if smaller 154 high = mid 155 else 156 low = mid + 1; 157 end 158 end 159 160 return nil if low == ary.length 161 return nil if !satisfied 162 return low 163 end
Equal method needed by serializer to perform tabulation
# File lib/puppet/pops/parser/locator.rb 170 def eql?(o) 171 self.class == o.class && string == o.string && file == o.file && line_index == o.line_index 172 end
# File lib/puppet/pops/parser/locator.rb 165 def hash 166 [string, file, line_index].hash 167 end
Returns the line number (first line is 1) for the given offset
# File lib/puppet/pops/parser/locator.rb 175 def line_for_offset(offset) 176 if @prev_offset == offset 177 # use cache 178 return @prev_line 179 end 180 line_nbr = ary_bsearch_i(line_index, offset) 181 if line_nbr 182 # cache 183 @prev_offset = offset 184 @prev_line = line_nbr 185 return line_nbr 186 end 187 # If not found it is after last 188 # clear cache 189 @prev_offset = @prev_line = nil 190 return line_index.size 191 end
Returns the position on line (first position on a line is 1)
# File lib/puppet/pops/parser/locator.rb 113 def pos_on_line(offset) 114 offset_on_line(offset) +1 115 end
# File lib/puppet/pops/parser/locator.rb 117 def to_location_hash(reported_offset, end_offset) 118 pos = pos_on_line(reported_offset) 119 offset = char_offset(reported_offset) 120 length = char_length(reported_offset, end_offset) 121 start_line = line_for_offset(reported_offset) 122 { :line => start_line, :pos => pos, :offset => offset, :length => length} 123 end