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 105 def initialize(string, file, line_index = nil) 106 @string = string.freeze 107 @file = file.freeze 108 @prev_offset = nil 109 @prev_line = nil 110 @line_index = line_index.nil? ? Locator.compute_line_index(@string) : line_index 111 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 134 def ary_bsearch_i(ary, value) 135 low = 0 136 high = ary.length 137 mid = nil 138 smaller = false 139 satisfied = false 140 v = nil 141 142 while low < high do 143 mid = low + ((high - low) / 2) 144 v = (ary[mid] > value) 145 if v == true 146 satisfied = true 147 smaller = true 148 elsif !v 149 smaller = false 150 else 151 raise TypeError, "wrong argument, must be boolean or nil, got '#{v.class}'" 152 end 153 154 if smaller 155 high = mid 156 else 157 low = mid + 1; 158 end 159 end 160 161 return nil if low == ary.length 162 return nil if !satisfied 163 return low 164 end
Equal method needed by serializer to perform tabulation
# File lib/puppet/pops/parser/locator.rb 171 def eql?(o) 172 self.class == o.class && string == o.string && file == o.file && line_index == o.line_index 173 end
# File lib/puppet/pops/parser/locator.rb 166 def hash 167 [string, file, line_index].hash 168 end
Returns the line number (first line is 1) for the given offset
# File lib/puppet/pops/parser/locator.rb 176 def line_for_offset(offset) 177 if @prev_offset == offset 178 # use cache 179 return @prev_line 180 end 181 line_nbr = ary_bsearch_i(line_index, offset) 182 if line_nbr 183 # cache 184 @prev_offset = offset 185 @prev_line = line_nbr 186 return line_nbr 187 end 188 # If not found it is after last 189 # clear cache 190 @prev_offset = @prev_line = nil 191 return line_index.size 192 end
Returns the position on line (first position on a line is 1)
# File lib/puppet/pops/parser/locator.rb 114 def pos_on_line(offset) 115 offset_on_line(offset) +1 116 end
# File lib/puppet/pops/parser/locator.rb 118 def to_location_hash(reported_offset, end_offset) 119 pos = pos_on_line(reported_offset) 120 offset = char_offset(reported_offset) 121 length = char_length(reported_offset, end_offset) 122 start_line = line_for_offset(reported_offset) 123 { :line => start_line, :pos => pos, :offset => offset, :length => length} 124 end