Class: Puppet::Pops::Parser::Locator::AbstractLocator
- Inherits:
-
Puppet::Pops::Parser::Locator
- Object
- Puppet::Pops::Parser::Locator
- Puppet::Pops::Parser::Locator::AbstractLocator
- Defined in:
- lib/puppet/pops/parser/locator.rb
Direct Known Subclasses
Instance Attribute Summary collapse
- #file ⇒ Object readonly
- #line_index ⇒ Object
- #string ⇒ Object readonly
Instance Method Summary collapse
-
#ary_bsearch_i(ary, value) ⇒ Object
Returns the index of the smallest item for which the item > the given value This is a min binary search.
-
#eql?(o) ⇒ Boolean
Equal method needed by serializer to perform tabulation.
- #hash ⇒ Object
-
#initialize(string, file, line_index = nil) ⇒ AbstractLocator
constructor
Create a locator based on a content string, and a boolean indicating if ruby version support multi-byte strings or not.
-
#line_for_offset(offset) ⇒ Object
Returns the line number (first line is 1) for the given offset.
-
#pos_on_line(offset) ⇒ Object
Returns the position on line (first position on a line is 1).
- #to_location_hash(reported_offset, end_offset) ⇒ Object
Methods inherited from Puppet::Pops::Parser::Locator
#char_length, #char_offset, compute_line_index, #extract_text, #extract_tree_text, locator, #offset_on_line, #to_s, #to_uri
Constructor Details
#initialize(string, file, line_index = nil) ⇒ AbstractLocator
Create a locator based on a content string, and a boolean indicating if ruby version support multi-byte strings or not.
107 108 109 110 111 112 113 |
# File 'lib/puppet/pops/parser/locator.rb', line 107 def initialize(string, file, line_index = nil) @string = string.freeze @file = file.freeze @prev_offset = nil @prev_line = nil @line_index = line_index.nil? ? Locator.compute_line_index(@string) : line_index end |
Instance Attribute Details
#file ⇒ Object (readonly)
102 103 104 |
# File 'lib/puppet/pops/parser/locator.rb', line 102 def file @file end |
#line_index ⇒ Object
100 101 102 |
# File 'lib/puppet/pops/parser/locator.rb', line 100 def line_index @line_index end |
#string ⇒ Object (readonly)
101 102 103 |
# File 'lib/puppet/pops/parser/locator.rb', line 101 def string @string end |
Instance Method Details
#ary_bsearch_i(ary, value) ⇒ Object
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.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/puppet/pops/parser/locator.rb', line 136 def ary_bsearch_i(ary, value) low = 0 high = ary.length mid = nil smaller = false satisfied = false v = nil while low < high mid = low + ((high - low) / 2) v = (ary[mid] > value) if v == true satisfied = true smaller = true elsif !v smaller = false else raise TypeError, "wrong argument, must be boolean or nil, got '#{v.class}'" end if smaller high = mid else low = mid + 1; end end return nil if low == ary.length return nil unless satisfied low end |
#eql?(o) ⇒ Boolean
Equal method needed by serializer to perform tabulation
174 175 176 |
# File 'lib/puppet/pops/parser/locator.rb', line 174 def eql?(o) self.class == o.class && string == o.string && file == o.file && line_index == o.line_index end |
#hash ⇒ Object
169 170 171 |
# File 'lib/puppet/pops/parser/locator.rb', line 169 def hash [string, file, line_index].hash end |
#line_for_offset(offset) ⇒ Object
Returns the line number (first line is 1) for the given offset
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/puppet/pops/parser/locator.rb', line 179 def line_for_offset(offset) if @prev_offset == offset # use cache return @prev_line end line_nbr = ary_bsearch_i(line_index, offset) if line_nbr # cache @prev_offset = offset @prev_line = line_nbr return line_nbr end # If not found it is after last # clear cache @prev_offset = @prev_line = nil line_index.size end |
#pos_on_line(offset) ⇒ Object
Returns the position on line (first position on a line is 1)
116 117 118 |
# File 'lib/puppet/pops/parser/locator.rb', line 116 def pos_on_line(offset) offset_on_line(offset) + 1 end |
#to_location_hash(reported_offset, end_offset) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/puppet/pops/parser/locator.rb', line 120 def to_location_hash(reported_offset, end_offset) pos = pos_on_line(reported_offset) offset = char_offset(reported_offset) length = char_length(reported_offset, end_offset) start_line = line_for_offset(reported_offset) { :line => start_line, :pos => pos, :offset => offset, :length => length } end |