Class: Oga::XPath::Lexer Private

Inherits:
Object
  • Object
show all
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:

  1. The token type as a symbol
  2. 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.

Returns:

  • (Hash)
{
  '@'  => '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.

Returns:

  • (Array)
%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.

Returns:

  • (Array)
%w{descendant-or-self}

Class Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • data (String)

    The data to lex.



1375
1376
1377
# File 'lib/oga/xpath/lexer.rb', line 1375

def initialize(data)
  @data = data
end

Class Attribute Details

._xpath_lexer_eof_transObject

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.



1298
1299
1300
# File 'lib/oga/xpath/lexer.rb', line 1298

def _xpath_lexer_eof_trans
  @_xpath_lexer_eof_trans
end

._xpath_lexer_from_state_actionsObject

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.



1273
1274
1275
# File 'lib/oga/xpath/lexer.rb', line 1273

def _xpath_lexer_from_state_actions
  @_xpath_lexer_from_state_actions
end

._xpath_lexer_index_offsetsObject

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.



128
129
130
# File 'lib/oga/xpath/lexer.rb', line 128

def _xpath_lexer_index_offsets
  @_xpath_lexer_index_offsets
end

._xpath_lexer_indiciesObject

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.



153
154
155
# File 'lib/oga/xpath/lexer.rb', line 153

def _xpath_lexer_indicies
  @_xpath_lexer_indicies
end

._xpath_lexer_key_spansObject

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.



103
104
105
# File 'lib/oga/xpath/lexer.rb', line 103

def _xpath_lexer_key_spans
  @_xpath_lexer_key_spans
end

._xpath_lexer_to_state_actionsObject

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.



1248
1249
1250
# File 'lib/oga/xpath/lexer.rb', line 1248

def _xpath_lexer_to_state_actions
  @_xpath_lexer_to_state_actions
end

._xpath_lexer_trans_actionsObject

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.



1218
1219
1220
# File 'lib/oga/xpath/lexer.rb', line 1218

def _xpath_lexer_trans_actions
  @_xpath_lexer_trans_actions
end

._xpath_lexer_trans_keysObject

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.



43
44
45
# File 'lib/oga/xpath/lexer.rb', line 43

def _xpath_lexer_trans_keys
  @_xpath_lexer_trans_keys
end

._xpath_lexer_trans_targsObject

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.



1188
1189
1190
# File 'lib/oga/xpath/lexer.rb', line 1188

def _xpath_lexer_trans_targs
  @_xpath_lexer_trans_targs
end

.xpath_lexer_en_mainObject

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.



1336
1337
1338
# File 'lib/oga/xpath/lexer.rb', line 1336

def xpath_lexer_en_main
  @xpath_lexer_en_main
end

.xpath_lexer_errorObject

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.



1331
1332
1333
# File 'lib/oga/xpath/lexer.rb', line 1331

def xpath_lexer_error
  @xpath_lexer_error
end

.xpath_lexer_first_finalObject

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.



1327
1328
1329
# File 'lib/oga/xpath/lexer.rb', line 1327

def xpath_lexer_first_final
  @xpath_lexer_first_final
end

.xpath_lexer_startObject

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.



1323
1324
1325
# File 'lib/oga/xpath/lexer.rb', line 1323

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.

See Also:

  • Oga::XPath::Lexer.[[#add_token]


1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
# File 'lib/oga/xpath/lexer.rb', line 1410

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 1436 "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 1464 "lib/oga/xpath/lexer.rb"
	end
	_keys = cs << 1
	_inds = _xpath_lexer_index_offsets[cs]
	_slen = _xpath_lexer_key_spans[cs]
	_trans = if (   _slen > 0 && 
			_xpath_lexer_trans_keys[_keys] <= ( (data.getbyte(p) || 0)) && 
			( (data.getbyte(p) || 0)) <= _xpath_lexer_trans_keys[_keys + 1] 
) then
			_xpath_lexer_indicies[ _inds + ( (data.getbyte(p) || 0)) - _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 186 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_SLASH) 		end
	when 34 then
# line 298 "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 370 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
		end
	when 9 then
# line 351 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
 begin 
    emit(:T_TYPE_TEST, ts, te - 2)
   end
		end
	when 3 then
# line 364 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
 begin 
    emit(:T_VAR, ts + 1, te)
   end
		end
	when 2 then
# line 240 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
 begin 
    emit(:T_STRING, ts + 1, te - 1)
   end
		end
	when 8 then
# line 262 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
 begin 
    emit(:T_AXIS, ts, te - 2)
   end
		end
	when 21 then
# line 274 "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 201 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
 begin 
    emit(:T_IDENT, ts, te)
   end
		end
	when 24 then
# line 370 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 32 then
# line 364 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1; begin 
    emit(:T_VAR, ts + 1, te)
   end
		end
	when 36 then
# line 215 "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 37 then
# line 221 "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 38 then
# line 274 "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 44 then
# line 201 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1; begin 
    emit(:T_IDENT, ts, te)
   end
		end
	when 1 then
# line 370 "lib/oga/xpath/lexer.rl"
		begin
 begin p = ((te))-1; end
		end
	when 7 then
# line 201 "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 187 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_LPAREN) 		end
# line 370 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
		end
	when 15 then
# line 188 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_RPAREN) 		end
# line 370 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
		end
	when 18 then
# line 189 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_COMMA) 		end
# line 370 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
		end
	when 20 then
# line 190 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_COLON) 		end
# line 370 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
		end
	when 22 then
# line 191 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_LBRACK) 		end
# line 370 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
		end
	when 23 then
# line 192 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_RBRACK) 		end
# line 370 "lib/oga/xpath/lexer.rl"
		begin
te = p+1
		end
	when 45 then
# line 297 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_PIPE) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 33 then
# line 298 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_ADD) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 41 then
# line 299 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_EQ) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 31 then
# line 300 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_NEQ) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 39 then
# line 301 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_LT) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 42 then
# line 302 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_GT) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 40 then
# line 303 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_LTE) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 43 then
# line 304 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_GTE) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 27 then
# line 314 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_AND) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 30 then
# line 315 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_OR) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 28 then
# line 316 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_DIV) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 29 then
# line 317 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_MOD) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 25 then
# line 318 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_MUL) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 26 then
# line 319 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_SUB) 		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 17 then
# line 1 "NONE"
		begin
te = p+1
		end
# line 369 "lib/oga/xpath/lexer.rl"
		begin
act = 1;		end
	when 5 then
# line 1 "NONE"
		begin
te = p+1
		end
# line 215 "lib/oga/xpath/lexer.rl"
		begin
act = 6;		end
	when 6 then
# line 1 "NONE"
		begin
te = p+1
		end
# line 221 "lib/oga/xpath/lexer.rl"
		begin
act = 7;		end
	when 35 then
# line 1 "NONE"
		begin
te = p+1
		end
# line 298 "lib/oga/xpath/lexer.rl"
		begin
 add_token(:T_ADD) 		end
# line 215 "lib/oga/xpath/lexer.rl"
		begin
act = 6;		end
# line 1861 "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 1875 "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 132 "lib/oga/xpath/lexer.rl"

  # % fix highlight
ensure
  @block = nil
end

#lexArray

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.

Returns:

  • (Array)

See Also:

  • Oga::XPath::Lexer.[[#advance]


1385
1386
1387
1388
1389
1390
1391
1392
1393
# File 'lib/oga/xpath/lexer.rb', line 1385

def lex
  tokens = []

  advance do |type, value|
    tokens << [type, value]
  end

  return tokens
end