Class: Oga::XPath::Lexer Private
- Inherits:
-
Object
- Object
- Oga::XPath::Lexer
- Defined in:
- lib/oga/xpath/lexer.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Lexer for turning XPath expressions into a set of tokens. Tokens are returned as arrays with every array having two values:
- The token type as a symbol
- The token value or nil if there is no value
Basic usage of this lexer is as following:
lexer = Oga::XPath::Lexer.new('//foo/bar')
tokens = lexer.lex
Alternatively you can stream tokens instead of returning them as a whole:
lexer = Oga::XPath::Lexer.new('//foo/bar')
lexer.advance do |type, value|
end
Unlike the XML lexer the XPath lexer does not support IO instances, it can only lex strings.
Thread Safety
This class keeps track of an internal state. As a result it’s not safe to share a single instance between multiple threads. However, you’re free to use separate instances per thread as there is no global (= class level) shared state.
Constant Summary collapse
- AXIS_MAPPING =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Maps certain XPath axes written in their short form to their long form equivalents.
{ '@' => 'attribute', '//' => 'descendant-or-self', '..' => 'parent', '.' => 'self' }
- AXIS_EMIT_NODE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Axes that require a separate
node()
call to be emitted. %w{descendant-or-self parent self}
- AXIS_EMIT_EXTRA_SLASH =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Axes that require an extra T_SLASH token to be emitted.
%w{descendant-or-self}
Class Attribute Summary collapse
-
._xpath_lexer_eof_trans ⇒ Object
private
-
._xpath_lexer_from_state_actions ⇒ Object
private
-
._xpath_lexer_index_offsets ⇒ Object
private
-
._xpath_lexer_indicies ⇒ Object
private
-
._xpath_lexer_key_spans ⇒ Object
private
-
._xpath_lexer_to_state_actions ⇒ Object
private
-
._xpath_lexer_trans_actions ⇒ Object
private
-
._xpath_lexer_trans_keys ⇒ Object
private
-
._xpath_lexer_trans_targs ⇒ Object
private
-
.xpath_lexer_en_main ⇒ Object
private
-
.xpath_lexer_error ⇒ Object
private
-
.xpath_lexer_first_final ⇒ Object
private
-
.xpath_lexer_start ⇒ Object
private
Instance Method Summary collapse
-
#advance(&block) ⇒ Object
private
Advances through the input and generates the corresponding tokens.
-
#initialize(data) ⇒ Lexer
constructor
private
A new instance of Lexer.
-
#lex ⇒ Array
private
Gathers all the tokens for the input and returns them as an Array.
Constructor Details
#initialize(data) ⇒ Lexer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Lexer
1991 1992 1993 |
# File 'lib/oga/xpath/lexer.rb', line 1991 def initialize(data) @data = data end |
Class Attribute Details
._xpath_lexer_eof_trans ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
1922 1923 1924 |
# File 'lib/oga/xpath/lexer.rb', line 1922 def _xpath_lexer_eof_trans @_xpath_lexer_eof_trans end |
._xpath_lexer_from_state_actions ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
1897 1898 1899 |
# File 'lib/oga/xpath/lexer.rb', line 1897 def _xpath_lexer_from_state_actions @_xpath_lexer_from_state_actions end |
._xpath_lexer_index_offsets ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
126 127 128 |
# File 'lib/oga/xpath/lexer.rb', line 126 def _xpath_lexer_index_offsets @_xpath_lexer_index_offsets end |
._xpath_lexer_indicies ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
151 152 153 |
# File 'lib/oga/xpath/lexer.rb', line 151 def _xpath_lexer_indicies @_xpath_lexer_indicies end |
._xpath_lexer_key_spans ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
101 102 103 |
# File 'lib/oga/xpath/lexer.rb', line 101 def _xpath_lexer_key_spans @_xpath_lexer_key_spans end |
._xpath_lexer_to_state_actions ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
1872 1873 1874 |
# File 'lib/oga/xpath/lexer.rb', line 1872 def _xpath_lexer_to_state_actions @_xpath_lexer_to_state_actions end |
._xpath_lexer_trans_actions ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
1842 1843 1844 |
# File 'lib/oga/xpath/lexer.rb', line 1842 def _xpath_lexer_trans_actions @_xpath_lexer_trans_actions end |
._xpath_lexer_trans_keys ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
41 42 43 |
# File 'lib/oga/xpath/lexer.rb', line 41 def _xpath_lexer_trans_keys @_xpath_lexer_trans_keys end |
._xpath_lexer_trans_targs ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
1812 1813 1814 |
# File 'lib/oga/xpath/lexer.rb', line 1812 def _xpath_lexer_trans_targs @_xpath_lexer_trans_targs end |
.xpath_lexer_en_main ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
1960 1961 1962 |
# File 'lib/oga/xpath/lexer.rb', line 1960 def xpath_lexer_en_main @xpath_lexer_en_main end |
.xpath_lexer_error ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
1955 1956 1957 |
# File 'lib/oga/xpath/lexer.rb', line 1955 def xpath_lexer_error @xpath_lexer_error end |
.xpath_lexer_first_final ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
1951 1952 1953 |
# File 'lib/oga/xpath/lexer.rb', line 1951 def xpath_lexer_first_final @xpath_lexer_first_final end |
.xpath_lexer_start ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
1947 1948 1949 |
# File 'lib/oga/xpath/lexer.rb', line 1947 def xpath_lexer_start @xpath_lexer_start end |
Instance Method Details
#advance(&block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Advances through the input and generates the corresponding tokens. Each token is yielded to the supplied block.
Each token is an Array in the following format:
[TYPE, VALUE]
The type is a symbol, the value is either nil or a String.
This method stores the supplied block in @block
and resets it after
the lexer loop has finished.
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 |
# File 'lib/oga/xpath/lexer.rb', line 2022 def advance(&block) @block = block data = @data # saves ivar lookups while lexing. ts = nil te = nil stack = [] top = 0 cs = self.class.xpath_lexer_start act = 0 eof = @data.bytesize p = 0 pe = eof _xpath_lexer_eof_trans = self.class.send(:_xpath_lexer_eof_trans) _xpath_lexer_from_state_actions = self.class.send(:_xpath_lexer_from_state_actions) _xpath_lexer_index_offsets = self.class.send(:_xpath_lexer_index_offsets) _xpath_lexer_indicies = self.class.send(:_xpath_lexer_indicies) _xpath_lexer_key_spans = self.class.send(:_xpath_lexer_key_spans) _xpath_lexer_to_state_actions = self.class.send(:_xpath_lexer_to_state_actions) _xpath_lexer_trans_actions = self.class.send(:_xpath_lexer_trans_actions) _xpath_lexer_trans_keys = self.class.send(:_xpath_lexer_trans_keys) _xpath_lexer_trans_targs = self.class.send(:_xpath_lexer_trans_targs) # line 2048 "lib/oga/xpath/lexer.rb" begin testEof = false _slen, _trans, _keys, _inds, _acts, _nacts = nil _goto_level = 0 _resume = 10 _eof_trans = 15 _again = 20 _test_eof = 30 _out = 40 while true if _goto_level <= 0 if p == pe _goto_level = _test_eof next end if cs == 0 _goto_level = _out next end end if _goto_level <= _resume case _xpath_lexer_from_state_actions[cs] when 11 then # line 1 "NONE" begin ts = p end # line 2076 "lib/oga/xpath/lexer.rb" end _keys = cs << 1 _inds = _xpath_lexer_index_offsets[cs] _slen = _xpath_lexer_key_spans[cs] _wide = ( (data.getbyte(p) || 0)) _trans = if ( _slen > 0 && _xpath_lexer_trans_keys[_keys] <= _wide && _wide <= _xpath_lexer_trans_keys[_keys + 1] ) then _xpath_lexer_indicies[ _inds + _wide - _xpath_lexer_trans_keys[_keys] ] else _xpath_lexer_indicies[ _inds + _slen ] end end if _goto_level <= _eof_trans cs = _xpath_lexer_trans_targs[_trans] if _xpath_lexer_trans_actions[_trans] != 0 case _xpath_lexer_trans_actions[_trans] when 19 then # line 166 "lib/oga/xpath/lexer.rl" begin add_token(:T_SLASH) end when 35 then # line 279 "lib/oga/xpath/lexer.rl" begin add_token(:T_ADD) end when 13 then # line 1 "NONE" begin te = p+1 end when 12 then # line 350 "lib/oga/xpath/lexer.rl" begin te = p+1 end when 9 then # line 332 "lib/oga/xpath/lexer.rl" begin te = p+1 begin emit(:T_TYPE_TEST, ts, te - 2) end end when 3 then # line 344 "lib/oga/xpath/lexer.rl" begin te = p+1 begin emit(:T_VAR, ts + 1, te) end end when 2 then # line 223 "lib/oga/xpath/lexer.rl" begin te = p+1 begin emit(:T_STRING, ts + 1, te - 1) end end when 8 then # line 244 "lib/oga/xpath/lexer.rl" begin te = p+1 begin emit(:T_AXIS, ts, te - 2) end end when 21 then # line 255 "lib/oga/xpath/lexer.rl" begin te = p+1 begin value = AXIS_MAPPING[slice_input(ts, te)] add_token(:T_AXIS, value) # Short axes that use node() as their default, implicit test. This is # added on lexer level to make it easier to handle these cases on # parser/evaluator level. if AXIS_EMIT_NODE.include?(value) add_token(:T_TYPE_TEST, 'node') if AXIS_EMIT_EXTRA_SLASH.include?(value) and te != eof add_token(:T_SLASH) end end end end when 16 then # line 185 "lib/oga/xpath/lexer.rl" begin te = p+1 begin emit(:T_IDENT, ts, te) end end when 25 then # line 350 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 33 then # line 344 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; begin emit(:T_VAR, ts + 1, te) end end when 37 then # line 199 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; begin value = slice_input(ts, te).to_i add_token(:T_INT, value) end end when 38 then # line 205 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; begin value = slice_input(ts, te).to_f add_token(:T_FLOAT, value) end end when 39 then # line 255 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; begin value = AXIS_MAPPING[slice_input(ts, te)] add_token(:T_AXIS, value) # Short axes that use node() as their default, implicit test. This is # added on lexer level to make it easier to handle these cases on # parser/evaluator level. if AXIS_EMIT_NODE.include?(value) add_token(:T_TYPE_TEST, 'node') if AXIS_EMIT_EXTRA_SLASH.include?(value) and te != eof add_token(:T_SLASH) end end end end when 24 then # line 185 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; begin emit(:T_IDENT, ts, te) end end when 1 then # line 350 "lib/oga/xpath/lexer.rl" begin begin p = ((te))-1; end end when 7 then # line 185 "lib/oga/xpath/lexer.rl" begin begin p = ((te))-1; end begin emit(:T_IDENT, ts, te) end end when 4 then # line 1 "NONE" begin case act when 0 then begin begin cs = 0 _goto_level = _again next end end when 6 then begin begin p = ((te))-1; end value = slice_input(ts, te).to_i add_token(:T_INT, value) end when 7 then begin begin p = ((te))-1; end value = slice_input(ts, te).to_f add_token(:T_FLOAT, value) end else begin begin p = ((te))-1; end end end end when 14 then # line 167 "lib/oga/xpath/lexer.rl" begin add_token(:T_LPAREN) end # line 350 "lib/oga/xpath/lexer.rl" begin te = p+1 end when 15 then # line 168 "lib/oga/xpath/lexer.rl" begin add_token(:T_RPAREN) end # line 350 "lib/oga/xpath/lexer.rl" begin te = p+1 end when 18 then # line 169 "lib/oga/xpath/lexer.rl" begin add_token(:T_COMMA) end # line 350 "lib/oga/xpath/lexer.rl" begin te = p+1 end when 20 then # line 170 "lib/oga/xpath/lexer.rl" begin add_token(:T_COLON) end # line 350 "lib/oga/xpath/lexer.rl" begin te = p+1 end when 22 then # line 171 "lib/oga/xpath/lexer.rl" begin add_token(:T_LBRACK) end # line 350 "lib/oga/xpath/lexer.rl" begin te = p+1 end when 23 then # line 172 "lib/oga/xpath/lexer.rl" begin add_token(:T_RBRACK) end # line 350 "lib/oga/xpath/lexer.rl" begin te = p+1 end when 45 then # line 278 "lib/oga/xpath/lexer.rl" begin add_token(:T_PIPE) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 34 then # line 279 "lib/oga/xpath/lexer.rl" begin add_token(:T_ADD) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 42 then # line 280 "lib/oga/xpath/lexer.rl" begin add_token(:T_EQ) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 32 then # line 281 "lib/oga/xpath/lexer.rl" begin add_token(:T_NEQ) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 40 then # line 282 "lib/oga/xpath/lexer.rl" begin add_token(:T_LT) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 43 then # line 283 "lib/oga/xpath/lexer.rl" begin add_token(:T_GT) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 41 then # line 284 "lib/oga/xpath/lexer.rl" begin add_token(:T_LTE) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 44 then # line 285 "lib/oga/xpath/lexer.rl" begin add_token(:T_GTE) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 28 then # line 295 "lib/oga/xpath/lexer.rl" begin add_token(:T_AND) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 31 then # line 296 "lib/oga/xpath/lexer.rl" begin add_token(:T_OR) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 29 then # line 297 "lib/oga/xpath/lexer.rl" begin add_token(:T_DIV) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 30 then # line 298 "lib/oga/xpath/lexer.rl" begin add_token(:T_MOD) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 26 then # line 299 "lib/oga/xpath/lexer.rl" begin add_token(:T_MUL) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 27 then # line 300 "lib/oga/xpath/lexer.rl" begin add_token(:T_SUB) end # line 349 "lib/oga/xpath/lexer.rl" begin te = p p = p - 1; end when 17 then # line 1 "NONE" begin te = p+1 end # line 349 "lib/oga/xpath/lexer.rl" begin act = 1; end when 5 then # line 1 "NONE" begin te = p+1 end # line 199 "lib/oga/xpath/lexer.rl" begin act = 6; end when 6 then # line 1 "NONE" begin te = p+1 end # line 205 "lib/oga/xpath/lexer.rl" begin act = 7; end when 36 then # line 1 "NONE" begin te = p+1 end # line 279 "lib/oga/xpath/lexer.rl" begin add_token(:T_ADD) end # line 199 "lib/oga/xpath/lexer.rl" begin act = 6; end # line 2474 "lib/oga/xpath/lexer.rb" end end end if _goto_level <= _again case _xpath_lexer_to_state_actions[cs] when 10 then # line 1 "NONE" begin ts = nil; end # line 1 "NONE" begin act = 0 end # line 2488 "lib/oga/xpath/lexer.rb" end if cs == 0 _goto_level = _out next end p += 1 if p != pe _goto_level = _resume next end end if _goto_level <= _test_eof if p == eof if _xpath_lexer_eof_trans[cs] > 0 _trans = _xpath_lexer_eof_trans[cs] - 1; _goto_level = _eof_trans next; end end end if _goto_level <= _out break end end end # line 118 "lib/oga/xpath/lexer.rl" # % fix highlight ensure @block = nil end |
#lex ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Gathers all the tokens for the input and returns them as an Array.
1999 2000 2001 2002 2003 2004 2005 2006 2007 |
# File 'lib/oga/xpath/lexer.rb', line 1999 def lex tokens = [] advance do |type, value| tokens << [type, value] end return tokens end |