Created
February 26, 2016 17:19
-
-
Save timathom/6aba8a354368bf20344a to your computer and use it in GitHub Desktop.
XQuery Turtle parser (generated by Gunther Rademacher's REx)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "1.0" encoding "UTF-8"; | |
(: This file was generated on Sat Feb 13, 2016 20:32 (UTC-05) by REx v5.35 which is Copyright (c) 1979-2015 by Gunther Rademacher <[email protected]> :) | |
(: REx command line: turtle.ebnf -faster -xquery -tree :) | |
(:~ | |
: The parser that was generated for the turtle grammar. | |
:) | |
module namespace p="turtle"; | |
declare default function namespace "http://www.w3.org/2005/xpath-functions"; | |
(:~ | |
: The index of the parser state for accessing the combined | |
: (i.e. level > 1) lookahead code. | |
:) | |
declare variable $p:lk as xs:integer := 1; | |
(:~ | |
: The index of the parser state for accessing the position in the | |
: input string of the begin of the token that has been shifted. | |
:) | |
declare variable $p:b0 as xs:integer := 2; | |
(:~ | |
: The index of the parser state for accessing the position in the | |
: input string of the end of the token that has been shifted. | |
:) | |
declare variable $p:e0 as xs:integer := 3; | |
(:~ | |
: The index of the parser state for accessing the code of the | |
: level-1-lookahead token. | |
:) | |
declare variable $p:l1 as xs:integer := 4; | |
(:~ | |
: The index of the parser state for accessing the position in the | |
: input string of the begin of the level-1-lookahead token. | |
:) | |
declare variable $p:b1 as xs:integer := 5; | |
(:~ | |
: The index of the parser state for accessing the position in the | |
: input string of the end of the level-1-lookahead token. | |
:) | |
declare variable $p:e1 as xs:integer := 6; | |
(:~ | |
: The index of the parser state for accessing the token code that | |
: was expected when an error was found. | |
:) | |
declare variable $p:error as xs:integer := 7; | |
(:~ | |
: The index of the parser state that points to the first entry | |
: used for collecting action results. | |
:) | |
declare variable $p:result as xs:integer := 8; | |
(:~ | |
: The codepoint to charclass mapping for 7 bit codepoints. | |
:) | |
declare variable $p:MAP0 as xs:integer+ := | |
( | |
54, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 4, 5, 3, 6, 3, | |
7, 8, 9, 3, 10, 11, 12, 13, 3, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 16, 17, 3, 18, 3, 19, 20, 21, 22, 22, 23, | |
24, 25, 25, 26, 25, 25, 25, 25, 25, 25, 27, 25, 28, 29, 25, 30, 25, 25, 31, 25, 25, 32, 33, 34, 35, 36, 37, 38, 39, | |
22, 22, 40, 41, 25, 25, 42, 25, 25, 43, 25, 44, 25, 45, 25, 46, 47, 48, 49, 25, 25, 50, 25, 25, 37, 37, 37, 3, 51 | |
); | |
(:~ | |
: The codepoint to charclass mapping for codepoints below the surrogate block. | |
:) | |
declare variable $p:MAP1 as xs:integer+ := | |
( | |
108, 124, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 156, 181, 181, 181, 181, 181, 214, | |
215, 213, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, | |
214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, | |
214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, | |
214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 214, 247, 261, 277, 293, 309, 325, 341, | |
357, 394, 394, 394, 386, 442, 434, 442, 434, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, | |
442, 442, 411, 411, 411, 411, 411, 411, 411, 427, 442, 442, 442, 442, 442, 442, 442, 442, 372, 394, 394, 395, 393, | |
394, 394, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 394, 394, 394, | |
394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, | |
394, 394, 394, 394, 394, 394, 441, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, | |
442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 442, 394, 54, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, | |
0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 4, 5, 3, 6, 3, 7, 8, 9, 3, 10, 11, 12, 13, 3, 14, 14, | |
14, 14, 14, 14, 14, 14, 14, 14, 15, 16, 17, 3, 18, 3, 19, 20, 21, 22, 22, 23, 24, 25, 25, 26, 25, 25, 25, 25, 25, 25, | |
27, 25, 28, 29, 25, 30, 25, 25, 31, 25, 25, 32, 33, 34, 35, 36, 37, 38, 39, 22, 22, 40, 41, 25, 25, 42, 25, 25, 43, | |
25, 44, 25, 45, 25, 46, 47, 48, 49, 25, 25, 50, 25, 25, 37, 37, 37, 3, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, | |
53, 53, 51, 51, 51, 51, 51, 51, 51, 52, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 52, 52, 52, | |
52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 51, | |
53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53 | |
); | |
(:~ | |
: The codepoint to charclass mapping for codepoints above the surrogate block. | |
:) | |
declare variable $p:MAP2 as xs:integer+ := | |
( | |
57344, 63744, 64976, 65008, 65536, 983040, 63743, 64975, 65007, 65533, 983039, 1114111, 51, 53, 51, 53, 53, 51 | |
); | |
(:~ | |
: The token-set-id to DFA-initial-state mapping. | |
:) | |
declare variable $p:INITIAL as xs:integer+ := | |
( | |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 | |
); | |
(:~ | |
: The DFA transition table. | |
:) | |
declare variable $p:TRANSITION as xs:integer+ := | |
( | |
1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 880, 891, 1093, 1052, | |
1053, 961, 1053, 999, 911, 1000, 911, 1919, 912, 1053, 1053, 1053, 880, 883, 1021, 1053, 1053, 1053, 1053, 999, 911, | |
1000, 911, 1919, 912, 1053, 1053, 1053, 1053, 1384, 1054, 1052, 1265, 961, 1053, 999, 911, 1000, 911, 1919, 912, 1053, | |
1053, 1053, 1053, 921, 913, 1052, 1053, 1046, 1052, 1010, 911, 1988, 935, 1380, 912, 1053, 1053, 1053, 945, 949, 1054, | |
1052, 1265, 961, 1053, 999, 911, 1000, 911, 1919, 912, 1053, 1053, 1053, 1053, 1384, 957, 1052, 1673, 961, 1053, 999, | |
911, 1000, 911, 1919, 912, 1053, 1053, 1053, 1053, 969, 1054, 997, 1265, 1720, 1052, 999, 1008, 1757, 1018, 1919, | |
1029, 1053, 1053, 1053, 1053, 1039, 1054, 1052, 1265, 961, 1053, 999, 911, 1000, 911, 1919, 912, 1053, 1053, 1053, | |
1053, 1373, 1054, 1052, 1265, 961, 1053, 999, 911, 1000, 911, 1919, 912, 1053, 1053, 1053, 1053, 1062, 1054, 1052, | |
1265, 961, 1090, 999, 911, 1000, 911, 1919, 912, 1053, 1053, 1053, 897, 1069, 1054, 1052, 1265, 961, 1053, 999, 911, | |
1000, 911, 1919, 912, 1053, 1053, 1053, 1053, 1101, 1109, 1280, 1848, 1122, 1137, 1883, 1117, 987, 1130, 989, 912, | |
1053, 1053, 1053, 1351, 1151, 1159, 1167, 1181, 1197, 1213, 1631, 1211, 1187, 1221, 1189, 912, 1053, 1053, 1053, 1053, | |
1243, 1251, 1259, 1877, 1274, 1289, 1297, 1305, 1313, 1321, 1329, 1366, 1337, 1359, 1053, 1392, 1413, 1421, 1616, | |
1266, 1398, 1617, 1431, 1429, 1233, 1439, 1235, 912, 1053, 1053, 1053, 903, 1076, 1054, 1052, 1265, 961, 1053, 999, | |
911, 1000, 911, 1919, 912, 1053, 1053, 1053, 975, 981, 1054, 1052, 1053, 961, 1053, 999, 911, 1000, 911, 1919, 912, | |
1053, 1053, 1053, 1053, 1833, 1054, 1052, 1053, 961, 1053, 999, 911, 1000, 911, 1919, 912, 1053, 1053, 1053, 1053, | |
1453, 1054, 1052, 1265, 961, 1053, 999, 911, 1000, 911, 1919, 912, 1053, 1053, 1053, 1549, 1555, 1472, 1404, 1877, | |
1274, 1445, 1297, 1480, 1313, 1321, 1329, 1366, 1337, 1359, 1053, 1549, 1488, 1251, 1404, 1877, 1274, 1445, 1297, | |
1480, 1313, 1321, 1329, 1366, 1337, 1359, 1053, 1549, 1555, 1251, 1404, 1877, 1274, 1445, 1297, 1480, 1313, 1321, | |
1329, 1366, 1337, 1359, 1053, 1549, 1555, 1251, 1344, 1877, 1496, 1510, 1518, 1480, 1313, 1321, 1329, 1366, 1337, | |
1359, 1053, 1549, 1555, 1251, 1404, 1877, 1274, 1445, 1526, 1480, 1313, 1321, 1329, 1366, 1337, 1359, 1053, 1549, | |
1555, 1251, 1404, 1661, 1274, 1405, 1883, 1534, 987, 1542, 989, 912, 1053, 1053, 1053, 1549, 1555, 1251, 1404, 1661, | |
1274, 1405, 1883, 1534, 1227, 1542, 989, 912, 1053, 1053, 1053, 1549, 1563, 1251, 1404, 1661, 1274, 1405, 1883, 1534, | |
987, 1542, 989, 912, 1053, 1053, 1053, 1549, 1555, 1571, 1404, 1661, 1274, 1405, 1883, 1534, 987, 1542, 989, 912, | |
1053, 1053, 1053, 1549, 1555, 1251, 1404, 1661, 1579, 1405, 1883, 1534, 987, 1542, 989, 912, 1053, 1053, 1053, 1549, | |
1555, 1251, 1404, 1593, 1610, 1625, 1883, 1534, 1143, 1639, 989, 912, 1053, 1053, 1053, 1549, 1555, 1251, 1404, 1661, | |
1274, 1405, 1883, 1534, 987, 1542, 1460, 912, 1053, 1053, 1053, 1053, 1654, 1054, 1052, 1053, 961, 1053, 999, 911, | |
1000, 911, 1919, 912, 1053, 1053, 1053, 1053, 1955, 1602, 1754, 1599, 1748, 1052, 1031, 1686, 1698, 1695, 1951, 1687, | |
1053, 1053, 1053, 927, 1083, 1678, 1052, 1053, 961, 1053, 999, 911, 1000, 911, 1919, 912, 1053, 1053, 1053, 1053, | |
1667, 1054, 1203, 1053, 961, 1053, 999, 911, 1000, 911, 1919, 912, 1053, 1053, 1053, 1053, 1706, 1251, 1280, 1848, | |
1274, 1281, 1883, 1117, 987, 1714, 989, 912, 1053, 1053, 1053, 1053, 1923, 1054, 1052, 1053, 961, 1053, 999, 911, | |
1000, 911, 1919, 912, 1053, 1053, 1053, 1646, 1555, 1251, 1585, 1970, 1274, 1445, 1297, 1480, 1313, 1321, 1329, 1366, | |
1337, 1359, 1053, 1549, 1555, 1728, 1404, 1877, 1736, 1937, 1297, 1480, 1765, 1773, 1329, 1366, 1337, 1359, 1053, | |
1549, 1555, 1251, 1344, 1877, 1274, 1510, 1781, 1789, 1797, 1805, 1329, 1366, 1337, 1359, 1053, 1549, 1813, 1251, | |
1404, 1877, 1736, 1937, 1297, 1480, 1821, 1773, 1329, 1366, 1337, 1359, 1053, 1549, 1555, 1251, 1404, 1661, 1274, | |
1405, 1883, 1534, 987, 1542, 1829, 912, 1053, 1053, 1053, 1549, 1555, 1251, 1404, 1661, 1274, 1913, 1883, 1534, 987, | |
1542, 989, 912, 1053, 1053, 1053, 1549, 1555, 1251, 1404, 1661, 1736, 1742, 1883, 1534, 1854, 1841, 989, 912, 1053, | |
1053, 1053, 1549, 1555, 1862, 1404, 1661, 1274, 1405, 1883, 1534, 987, 1542, 989, 912, 1053, 1053, 1053, 1549, 1555, | |
1251, 1502, 1977, 1736, 1742, 1883, 1534, 1854, 1841, 989, 912, 1053, 1053, 1053, 1549, 1555, 1251, 1404, 1661, 1274, | |
1405, 1870, 1891, 987, 1542, 989, 912, 1053, 1053, 1053, 1549, 1899, 1251, 1404, 1661, 1736, 1742, 1883, 1534, 1854, | |
1841, 989, 912, 1053, 1053, 1053, 1549, 1555, 1251, 1404, 1907, 1931, 1945, 1883, 1534, 1173, 1963, 989, 912, 1053, | |
1053, 1053, 1549, 1555, 1251, 1404, 1661, 1274, 1405, 1883, 1534, 987, 1542, 989, 937, 1053, 1053, 1053, 1053, 1384, | |
1054, 1052, 1053, 961, 1053, 999, 911, 1000, 911, 1919, 912, 1053, 1053, 1053, 1053, 1464, 1109, 1280, 1661, 1122, | |
1281, 1883, 1117, 987, 1714, 989, 912, 1053, 1053, 1053, 1549, 1555, 1251, 1280, 1661, 1274, 1281, 1883, 1117, 987, | |
1714, 989, 912, 1053, 1053, 1053, 1053, 1985, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, 1053, | |
1053, 1053, 269, 269, 269, 269, 269, 269, 269, 269, 0, 0, 0, 269, 269, 269, 269, 269, 270, 0, 0, 0, 0, 0, 2432, 0, 0, | |
0, 0, 0, 2688, 0, 2688, 0, 66, 0, 0, 0, 0, 0, 0, 0, 1453, 0, 24, 24, 24, 0, 270, 0, 0, 0, 0, 0, 3456, 0, 3456, 0, 66, | |
66, 0, 0, 0, 0, 0, 0, 2944, 270, 270, 270, 270, 270, 270, 270, 270, 0, 270, 15, 0, 35, 0, 0, 0, 0, 0, 0, 44, 0, 0, 47, | |
0, 0, 25, 25, 25, 0, 270, 15, 0, 0, 15, 15, 0, 15, 15, 15, 15, 0, 270, 0, 0, 0, 0, 18, 63, 0, 0, 0, 0, 1584, 0, 0, 0, | |
0, 0, 0, 0, 63, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 78, 0, 0, 97, 66, 0, 0, 0, 0, 0, 22, 0, 0, 1664, 0, 0, 0, 0, 0, 0, 0, | |
79, 0, 2176, 2176, 2176, 2176, 0, 270, 15, 0, 0, 0, 1408, 63, 44, 47, 0, 0, 0, 0, 0, 0, 0, 0, 44, 0, 26, 26, 26, 0, | |
270, 15, 0, 0, 0, 2432, 0, 270, 15, 0, 0, 0, 2688, 0, 270, 15, 0, 0, 0, 3456, 0, 270, 15, 0, 0, 69, 0, 0, 0, 0, 0, 22, | |
0, 44, 0, 26, 26, 26, 0, 270, 15, 16, 0, 18, 0, 18, 18, 0, 0, 44, 0, 66, 0, 0, 0, 18, 18, 0, 44, 0, 0, 47, 0, 0, 66, | |
0, 0, 0, 18, 72, 0, 0, 69, 18, 18, 72, 0, 0, 0, 0, 18, 63, 93, 0, 0, 27, 27, 2587, 0, 270, 15, 34, 0, 38, 0, 38, 38, | |
0, 0, 44, 47, 27, 0, 50, 38, 38, 0, 0, 0, 0, 18, 63, 94, 0, 0, 34, 0, 36, 676, 38, 0, 0, 0, 0, 38, 63, 0, 0, 0, 0, 38, | |
38, 0, 44, 0, 0, 47, 0, 0, 0, 0, 0, 0, 3584, 0, 66, 0, 0, 0, 38, 38, 0, 0, 0, 0, 66, 0, 0, 0, 38, 0, 0, 0, 0, 91, 63, | |
0, 0, 0, 0, 529, 63, 0, 0, 0, 0, 0, 1052, 1052, 1052, 0, 270, 15, 16, 676, 18, 0, 18, 18, 0, 0, 44, 47, 1052, 1202, | |
1052, 18, 18, 0, 0, 0, 0, 676, 0, 0, 0, 0, 18, 18, 830, 44, 0, 0, 47, 0, 0, 0, 18, 18, 0, 0, 0, 0, 1202, 1349, 18, 18, | |
0, 73, 74, 676, 0, 0, 18, 18, 830, 63, 80, 81, 66, 84, 85, 1349, 18, 18, 983, 88, 89, 0, 0, 18, 63, 0, 95, 96, 66, 0, | |
100, 101, 18, 983, 102, 103, 0, 18, 63, 105, 106, 107, 108, 113, 114, 65, 44, 115, 116, 68, 47, 0, 0, 51, 18, 18, 950, | |
0, 0, 2560, 0, 0, 2560, 2560, 2560, 117, 118, 119, 120, 94, 63, 99, 66, 109, 110, 111, 112, 56, 15, 0, 0, 2304, 2304, | |
0, 270, 15, 0, 0, 0, 1792, 0, 0, 0, 0, 0, 270, 15, 0, 0, 512, 0, 529, 529, 0, 529, 529, 0, 44, 0, 0, 47, 0, 0, 0, 18, | |
18, 950, 0, 0, 529, 529, 529, 529, 0, 270, 15, 512, 676, 529, 0, 529, 529, 0, 43, 44, 0, 66, 0, 0, 0, 529, 529, 0, 63, | |
0, 0, 66, 0, 0, 0, 529, 0, 0, 0, 18, 18, 950, 73, 74, 19, 0, 0, 31, 0, 270, 15, 0, 0, 3218, 63, 0, 0, 0, 0, 0, 270, | |
15, 16, 676, 18, 0, 41, 18, 0, 0, 44, 81, 66, 84, 85, 0, 18, 18, 983, 20, 18, 18, 18, 0, 270, 15, 16, 18, 61, 830, 44, | |
0, 0, 47, 0, 0, 0, 18, 53, 950, 0, 0, 51, 0, 18, 18, 950, 73, 74, 676, 0, 0, 3090, 18, 830, 63, 80, 676, 0, 0, 18, 77, | |
830, 63, 80, 0, 66, 0, 0, 0, 18, 18, 983, 0, 66, 0, 0, 0, 18, 983, 0, 16, 0, 18, 18, 0, 18, 18, 18, 18, 0, 270, 15, | |
16, 21, 18, 18, 18, 0, 270, 15, 16, 676, 18, 0, 18, 42, 0, 0, 44, 60, 18, 830, 44, 0, 0, 47, 0, 0, 0, 52, 18, 950, 0, | |
55, 16, 0, 676, 0, 18, 0, 0, 0, 37, 0, 0, 0, 0, 0, 0, 46, 18, 18, 830, 44, 0, 64, 47, 0, 0, 0, 529, 529, 0, 0, 0, 67, | |
0, 0, 18, 18, 950, 0, 0, 0, 38, 38, 62, 63, 0, 0, 66, 98, 0, 0, 18, 983, 0, 16, 0, 18, 3730, 0, 3730, 3730, 3350, | |
3350, 3350, 3350, 0, 270, 15, 0, 16, 0, 676, 0, 18, 0, 0, 0, 32, 0, 270, 0, 0, 0, 35, 676, 0, 0, 0, 0, 0, 1920, 0, 44, | |
0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 83, 66, 0, 0, 0, 0, 0, 79, 63, 0, 23, 23, 23, 23, 0, 270, 15, 16, 0, 66, 0, 0, 0, 18, | |
0, 0, 0, 44, 0, 44, 1536, 66, 676, 18, 39, 18, 18, 0, 0, 44, 18, 18, 830, 44, 0, 44, 47, 0, 0, 18, 18, 950, 0, 0, 0, | |
46, 0, 44, 49, 0, 0, 0, 0, 0, 0, 0, 63, 63, 0, 88, 89, 0, 0, 18, 63, 63, 95, 96, 66, 66, 100, 101, 18, 983, 102, 676, | |
0, 76, 18, 18, 830, 63, 80, 81, 66, 84, 85, 0, 18, 3986, 983, 88, 89, 2816, 0, 18, 63, 0, 95, 96, 66, 0, 100, 101, | |
3858, 983, 102, 18, 29, 29, 29, 0, 270, 15, 16, 88, 89, 0, 90, 18, 63, 63, 95, 0, 104, 18, 63, 0, 0, 0, 0, 0, 270, | |
384, 0, 0, 66, 66, 0, 0, 18, 983, 0, 16, 0, 676, 676, 18, 0, 0, 0, 0, 18, 63, 63, 0, 676, 18, 40, 18, 18, 0, 0, 44, 0, | |
75, 0, 18, 18, 830, 63, 0, 16, 57, 676, 0, 18, 0, 0, 0, 18, 18, 830, 63, 0, 0, 66, 0, 0, 0, 86, 18, 983, 18, 30, 30, | |
30, 0, 270, 15, 16, 56, 16, 0, 676, 0, 18, 0, 0, 0, 70, 18, 950, 0, 0, 0, 63, 0, 0, 0, 0, 0, 270, 0, 0, 18, 18, 830, | |
44, 0, 65, 47, 0, 0, 18, 18, 950, 73, 74, 68, 0, 0, 18, 71, 950, 0, 0, 0, 79, 0, 0, 0, 0, 0, 270, 33, 0, 0, 66, 99, 0, | |
0, 18, 983, 0, 16, 57, 676, 0, 18, 58, 0, 16, 0, 676, 0, 18, 0, 59, 2048, 0, 0, 0, 0, 0, 0, 0, 92, 63, 0 | |
); | |
(:~ | |
: The DFA-state to expected-token-set mapping. | |
:) | |
declare variable $p:EXPECTED as xs:integer+ := | |
( | |
30, 34, 38, 42, 48, 66, 70, 81, 44, 74, 51, 97, 78, 85, 91, 95, 54, 80, 87, 61, 101, 106, 59, 62, 112, 57, 62, 102, | |
110, 110, 6, 10, 524290, 30, 268435486, 68943874, 268959774, 337117214, 65126462, 1644265406, 1644396478, 1847558142, | |
2, 2, 4, 8, 16, 16, 16, 24, 6291456, 8388632, 16777240, 32, 1024, 4096, 2048, 2048, 4, 4, 4194304, 16777240, 8192, | |
8192, 1024, 1024, 16777240, 16384, 32, 9216, 6144, 896, 768, 896, 16, 24, 2097152, 4194304, 2048, 768, 512, 536870936, | |
1073741848, 64, 134217728, 1073741848, 64, 4, 4, 2097152, 4194304, 16, 2097152, 4194304, 8388632, 16777240, 32, 8192, | |
1024, 2048, 4096, 1024, 4096, 4096, 2048, 2048, 2048, 536870936, 64, 4, 8192, 8192, 4096, 4096, 4096, 2048 | |
); | |
(:~ | |
: The token-string table. | |
:) | |
declare variable $p:TOKEN as xs:string+ := | |
( | |
"(0)", | |
"WhiteSpace", | |
"IRIREF", | |
"PNAME_NS", | |
"PNAME_LN", | |
"BLANK_NODE_LABEL", | |
"LANGTAG", | |
"INTEGER", | |
"DECIMAL", | |
"DOUBLE", | |
"STRING_LITERAL_QUOTE", | |
"STRING_LITERAL_SINGLE_QUOTE", | |
"STRING_LITERAL_LONG_SINGLE_QUOTE", | |
"STRING_LITERAL_LONG_QUOTE", | |
"'[]'", | |
"EOF", | |
"'('", | |
"')'", | |
"','", | |
"'.'", | |
"';'", | |
"'@base'", | |
"'@prefix'", | |
"'BASE'", | |
"'PREFIX'", | |
"'['", | |
"']'", | |
"'^^'", | |
"'a'", | |
"'false'", | |
"'true'" | |
); | |
(:~ | |
: Match next token in input string, starting at given index, using | |
: the DFA entry state for the set of tokens that are expected in | |
: the current context. | |
: | |
: @param $input the input string. | |
: @param $begin the index where to start in input string. | |
: @param $token-set the expected token set id. | |
: @return a sequence of three: the token code of the result token, | |
: with input string begin and end positions. If there is no valid | |
: token, return the negative id of the DFA state that failed, along | |
: with begin and end positions of the longest viable prefix. | |
:) | |
declare function p:match($input as xs:string, | |
$begin as xs:integer, | |
$token-set as xs:integer) as xs:integer+ | |
{ | |
let $result := $p:INITIAL[1 + $token-set] | |
return p:transition($input, | |
$begin, | |
$begin, | |
$begin, | |
$result, | |
$result mod 128, | |
0) | |
}; | |
(:~ | |
: The DFA state transition function. If we are in a valid DFA state, save | |
: it's result annotation, consume one input codepoint, calculate the next | |
: state, and use tail recursion to do the same again. Otherwise, return | |
: any valid result or a negative DFA state id in case of an error. | |
: | |
: @param $input the input string. | |
: @param $begin the begin index of the current token in the input string. | |
: @param $current the index of the current position in the input string. | |
: @param $end the end index of the result in the input string. | |
: @param $result the result code. | |
: @param $current-state the current DFA state. | |
: @param $previous-state the previous DFA state. | |
: @return a sequence of three: the token code of the result token, | |
: with input string begin and end positions. If there is no valid | |
: token, return the negative id of the DFA state that failed, along | |
: with begin and end positions of the longest viable prefix. | |
:) | |
declare function p:transition($input as xs:string, | |
$begin as xs:integer, | |
$current as xs:integer, | |
$end as xs:integer, | |
$result as xs:integer, | |
$current-state as xs:integer, | |
$previous-state as xs:integer) as xs:integer+ | |
{ | |
if ($current-state = 0) then | |
let $result := $result idiv 128 | |
return | |
if ($result != 0) then | |
( | |
$result - 1, | |
$begin, | |
$end | |
) | |
else | |
( | |
- $previous-state, | |
$begin, | |
$current - 1 | |
) | |
else | |
let $c0 := (string-to-codepoints(substring($input, $current, 1)), 0)[1] | |
let $c1 := | |
if ($c0 < 128) then | |
$p:MAP0[1 + $c0] | |
else if ($c0 < 55296) then | |
let $c1 := $c0 idiv 16 | |
let $c2 := $c1 idiv 32 | |
return $p:MAP1[1 + $c0 mod 16 + $p:MAP1[1 + $c1 mod 32 + $p:MAP1[1 + $c2]]] | |
else | |
p:map2($c0, 1, 6) | |
let $current := $current + 1 | |
let $i0 := 128 * $c1 + $current-state - 1 | |
let $i1 := $i0 idiv 8 | |
let $next-state := $p:TRANSITION[$i0 mod 8 + $p:TRANSITION[$i1 + 1] + 1] | |
return | |
if ($next-state > 127) then | |
p:transition($input, $begin, $current, $current, $next-state, $next-state mod 128, $current-state) | |
else | |
p:transition($input, $begin, $current, $end, $result, $next-state, $current-state) | |
}; | |
(:~ | |
: Recursively translate one 32-bit chunk of an expected token bitset | |
: to the corresponding sequence of token strings. | |
: | |
: @param $result the result of previous recursion levels. | |
: @param $chunk the 32-bit chunk of the expected token bitset. | |
: @param $base-token-code the token code of bit 0 in the current chunk. | |
: @return the set of token strings. | |
:) | |
declare function p:token($result as xs:string*, | |
$chunk as xs:integer, | |
$base-token-code as xs:integer) as xs:string* | |
{ | |
if ($chunk = 0) then | |
$result | |
else | |
p:token | |
( | |
($result, if ($chunk mod 2 != 0) then $p:TOKEN[$base-token-code] else ()), | |
if ($chunk < 0) then $chunk idiv 2 + 2147483648 else $chunk idiv 2, | |
$base-token-code + 1 | |
) | |
}; | |
(:~ | |
: Calculate expected token set for a given DFA state as a sequence | |
: of strings. | |
: | |
: @param $state the DFA state. | |
: @return the set of token strings | |
:) | |
declare function p:expected-token-set($state as xs:integer) as xs:string* | |
{ | |
if ($state > 0) then | |
for $t in 0 to 0 | |
let $i0 := $t * 120 + $state - 1 | |
let $i1 := $i0 idiv 4 | |
return p:token((), $p:EXPECTED[$i0 mod 4 + $p:EXPECTED[$i1 + 1] + 1], $t * 32 + 1) | |
else | |
() | |
}; | |
(:~ | |
: Classify codepoint by doing a tail recursive binary search for a | |
: matching codepoint range entry in MAP2, the codepoint to charclass | |
: map for codepoints above the surrogate block. | |
: | |
: @param $c the codepoint. | |
: @param $lo the binary search lower bound map index. | |
: @param $hi the binary search upper bound map index. | |
: @return the character class. | |
:) | |
declare function p:map2($c as xs:integer, $lo as xs:integer, $hi as xs:integer) as xs:integer | |
{ | |
if ($lo > $hi) then | |
0 | |
else | |
let $m := ($hi + $lo) idiv 2 | |
return | |
if ($p:MAP2[$m] > $c) then | |
p:map2($c, $lo, $m - 1) | |
else if ($p:MAP2[6 + $m] < $c) then | |
p:map2($c, $m + 1, $hi) | |
else | |
$p:MAP2[12 + $m] | |
}; | |
(:~ | |
: Parse BooleanLiteral. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-BooleanLiteral($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 30) then (: 'true' :) | |
let $state := p:shift(30, $input, $state) (: 'true' :) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:shift(29, $input, $state) (: 'false' :) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "BooleanLiteral", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse NumericLiteral. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-NumericLiteral($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 7) then (: INTEGER :) | |
let $state := p:shift(7, $input, $state) (: INTEGER :) | |
return $state | |
else if ($state[$p:l1] = 8) then (: DECIMAL :) | |
let $state := p:shift(8, $input, $state) (: DECIMAL :) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:shift(9, $input, $state) (: DOUBLE :) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "NumericLiteral", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse String. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-String($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 10) then (: STRING_LITERAL_QUOTE :) | |
let $state := p:shift(10, $input, $state) (: STRING_LITERAL_QUOTE :) | |
return $state | |
else if ($state[$p:l1] = 11) then (: STRING_LITERAL_SINGLE_QUOTE :) | |
let $state := p:shift(11, $input, $state) (: STRING_LITERAL_SINGLE_QUOTE :) | |
return $state | |
else if ($state[$p:l1] = 12) then (: STRING_LITERAL_LONG_SINGLE_QUOTE :) | |
let $state := p:shift(12, $input, $state) (: STRING_LITERAL_LONG_SINGLE_QUOTE :) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:shift(13, $input, $state) (: STRING_LITERAL_LONG_QUOTE :) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "String", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse RDFLiteral. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-RDFLiteral($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := p:parse-String($input, $state) | |
let $state := p:lookahead1W(11, $input, $state) (: WhiteSpace | IRIREF | PNAME_NS | PNAME_LN | | |
BLANK_NODE_LABEL | LANGTAG | INTEGER | DECIMAL | DOUBLE | | |
STRING_LITERAL_QUOTE | STRING_LITERAL_SINGLE_QUOTE | | |
STRING_LITERAL_LONG_SINGLE_QUOTE | | |
STRING_LITERAL_LONG_QUOTE | ANON | '(' | ')' | ',' | | |
'.' | ';' | '[' | ']' | '^^' | 'false' | 'true' :) | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 6 (: LANGTAG :) | |
or $state[$p:l1] = 27) then (: '^^' :) | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 6) then (: LANGTAG :) | |
let $state := p:shift(6, $input, $state) (: LANGTAG :) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:shift(27, $input, $state) (: '^^' :) | |
let $state := p:lookahead1W(3, $input, $state) (: WhiteSpace | IRIREF | PNAME_NS | PNAME_LN :) | |
let $state := p:whitespace($input, $state) | |
let $state := p:parse-iri($input, $state) | |
return $state | |
return $state | |
else | |
$state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "RDFLiteral", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse literal. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-literal($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 7 (: INTEGER :) | |
or $state[$p:l1] = 8 (: DECIMAL :) | |
or $state[$p:l1] = 9) then (: DOUBLE :) | |
let $state := p:parse-NumericLiteral($input, $state) | |
return $state | |
else if ($state[$p:l1] = 29 (: 'false' :) | |
or $state[$p:l1] = 30) then (: 'true' :) | |
let $state := p:parse-BooleanLiteral($input, $state) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:parse-RDFLiteral($input, $state) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "literal", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse the 1st loop of production objectList (zero or more). Use | |
: tail recursion for iteratively updating the parser state. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-objectList-1($input as xs:string, $state as item()+) as item()+ | |
{ | |
if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:lookahead1W(5, $input, $state) (: WhiteSpace | ',' | '.' | ';' | ']' :) | |
return | |
if ($state[$p:l1] != 18) then (: ',' :) | |
$state | |
else | |
let $state := p:shift(18, $input, $state) (: ',' :) | |
let $state := p:lookahead1W(9, $input, $state) (: WhiteSpace | IRIREF | PNAME_NS | PNAME_LN | | |
BLANK_NODE_LABEL | INTEGER | DECIMAL | DOUBLE | | |
STRING_LITERAL_QUOTE | STRING_LITERAL_SINGLE_QUOTE | | |
STRING_LITERAL_LONG_SINGLE_QUOTE | | |
STRING_LITERAL_LONG_QUOTE | ANON | '(' | '[' | 'false' | | |
'true' :) | |
let $state := p:whitespace($input, $state) | |
let $state := p:parse-object($input, $state) | |
return p:parse-objectList-1($input, $state) | |
}; | |
(:~ | |
: Parse objectList. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-objectList($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := p:parse-object($input, $state) | |
let $state := p:parse-objectList-1($input, $state) | |
let $end := $state[$p:e0] | |
return p:reduce($state, "objectList", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse predicate. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-predicate($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := p:parse-iri($input, $state) | |
let $end := $state[$p:e0] | |
return p:reduce($state, "predicate", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse verb. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-verb($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 28) then (: 'a' :) | |
let $state := p:shift(28, $input, $state) (: 'a' :) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:parse-predicate($input, $state) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "verb", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse the 1st loop of production predicateObjectList (zero or more). Use | |
: tail recursion for iteratively updating the parser state. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-predicateObjectList-1($input as xs:string, $state as item()+) as item()+ | |
{ | |
if ($state[$p:error]) then | |
$state | |
else | |
if ($state[$p:l1] != 20) then (: ';' :) | |
$state | |
else | |
let $state := p:shift(20, $input, $state) (: ';' :) | |
let $state := p:lookahead1W(7, $input, $state) (: WhiteSpace | IRIREF | PNAME_NS | PNAME_LN | '.' | ';' | | |
']' | 'a' :) | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] != 19 (: '.' :) | |
and $state[$p:l1] != 20 (: ';' :) | |
and $state[$p:l1] != 26) then (: ']' :) | |
let $state := p:whitespace($input, $state) | |
let $state := p:parse-verb($input, $state) | |
let $state := p:lookahead1W(9, $input, $state) (: WhiteSpace | IRIREF | PNAME_NS | PNAME_LN | | |
BLANK_NODE_LABEL | INTEGER | DECIMAL | DOUBLE | | |
STRING_LITERAL_QUOTE | STRING_LITERAL_SINGLE_QUOTE | | |
STRING_LITERAL_LONG_SINGLE_QUOTE | | |
STRING_LITERAL_LONG_QUOTE | ANON | '(' | '[' | 'false' | | |
'true' :) | |
let $state := p:whitespace($input, $state) | |
let $state := p:parse-objectList($input, $state) | |
return $state | |
else | |
$state | |
return p:parse-predicateObjectList-1($input, $state) | |
}; | |
(:~ | |
: Parse predicateObjectList. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-predicateObjectList($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := p:parse-verb($input, $state) | |
let $state := p:lookahead1W(9, $input, $state) (: WhiteSpace | IRIREF | PNAME_NS | PNAME_LN | | |
BLANK_NODE_LABEL | INTEGER | DECIMAL | DOUBLE | | |
STRING_LITERAL_QUOTE | STRING_LITERAL_SINGLE_QUOTE | | |
STRING_LITERAL_LONG_SINGLE_QUOTE | | |
STRING_LITERAL_LONG_QUOTE | ANON | '(' | '[' | 'false' | | |
'true' :) | |
let $state := p:whitespace($input, $state) | |
let $state := p:parse-objectList($input, $state) | |
let $state := p:parse-predicateObjectList-1($input, $state) | |
let $end := $state[$p:e0] | |
return p:reduce($state, "predicateObjectList", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse blankNodePropertyList. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-blankNodePropertyList($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := p:shift(25, $input, $state) (: '[' :) | |
let $state := p:lookahead1W(4, $input, $state) (: WhiteSpace | IRIREF | PNAME_NS | PNAME_LN | 'a' :) | |
let $state := p:whitespace($input, $state) | |
let $state := p:parse-predicateObjectList($input, $state) | |
let $state := p:shift(26, $input, $state) (: ']' :) | |
let $end := $state[$p:e0] | |
return p:reduce($state, "blankNodePropertyList", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse object. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-object($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 2 (: IRIREF :) | |
or $state[$p:l1] = 3 (: PNAME_NS :) | |
or $state[$p:l1] = 4) then (: PNAME_LN :) | |
let $state := p:parse-iri($input, $state) | |
return $state | |
else if ($state[$p:l1] = 5 (: BLANK_NODE_LABEL :) | |
or $state[$p:l1] = 14) then (: ANON :) | |
let $state := p:parse-BlankNode($input, $state) | |
return $state | |
else if ($state[$p:l1] = 16) then (: '(' :) | |
let $state := p:parse-collection($input, $state) | |
return $state | |
else if ($state[$p:l1] = 25) then (: '[' :) | |
let $state := p:parse-blankNodePropertyList($input, $state) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:parse-literal($input, $state) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "object", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse the 1st loop of production collection (zero or more). Use | |
: tail recursion for iteratively updating the parser state. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-collection-1($input as xs:string, $state as item()+) as item()+ | |
{ | |
if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:lookahead1W(10, $input, $state) (: WhiteSpace | IRIREF | PNAME_NS | PNAME_LN | | |
BLANK_NODE_LABEL | INTEGER | DECIMAL | DOUBLE | | |
STRING_LITERAL_QUOTE | STRING_LITERAL_SINGLE_QUOTE | | |
STRING_LITERAL_LONG_SINGLE_QUOTE | | |
STRING_LITERAL_LONG_QUOTE | ANON | '(' | ')' | '[' | | |
'false' | 'true' :) | |
return | |
if ($state[$p:l1] = 17) then (: ')' :) | |
$state | |
else | |
let $state := p:whitespace($input, $state) | |
let $state := p:parse-object($input, $state) | |
return p:parse-collection-1($input, $state) | |
}; | |
(:~ | |
: Parse collection. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-collection($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := p:shift(16, $input, $state) (: '(' :) | |
let $state := p:parse-collection-1($input, $state) | |
let $state := p:shift(17, $input, $state) (: ')' :) | |
let $end := $state[$p:e0] | |
return p:reduce($state, "collection", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse BlankNode. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-BlankNode($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 5) then (: BLANK_NODE_LABEL :) | |
let $state := p:shift(5, $input, $state) (: BLANK_NODE_LABEL :) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:shift(14, $input, $state) (: ANON :) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "BlankNode", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse PrefixedName. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-PrefixedName($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 4) then (: PNAME_LN :) | |
let $state := p:shift(4, $input, $state) (: PNAME_LN :) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:shift(3, $input, $state) (: PNAME_NS :) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "PrefixedName", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse iri. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-iri($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 2) then (: IRIREF :) | |
let $state := p:shift(2, $input, $state) (: IRIREF :) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:parse-PrefixedName($input, $state) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "iri", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse subject. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-subject($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 5 (: BLANK_NODE_LABEL :) | |
or $state[$p:l1] = 14) then (: ANON :) | |
let $state := p:parse-BlankNode($input, $state) | |
return $state | |
else if ($state[$p:l1] = 16) then (: '(' :) | |
let $state := p:parse-collection($input, $state) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:parse-iri($input, $state) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "subject", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse triples. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-triples($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 25) then (: '[' :) | |
let $state := p:parse-blankNodePropertyList($input, $state) | |
let $state := p:lookahead1W(6, $input, $state) (: WhiteSpace | IRIREF | PNAME_NS | PNAME_LN | '.' | 'a' :) | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] != 19) then (: '.' :) | |
let $state := p:whitespace($input, $state) | |
let $state := p:parse-predicateObjectList($input, $state) | |
return $state | |
else | |
$state | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:parse-subject($input, $state) | |
let $state := p:lookahead1W(4, $input, $state) (: WhiteSpace | IRIREF | PNAME_NS | PNAME_LN | 'a' :) | |
let $state := p:whitespace($input, $state) | |
let $state := p:parse-predicateObjectList($input, $state) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "triples", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse sparqlBase. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-sparqlBase($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := p:shift(23, $input, $state) (: 'BASE' :) | |
let $state := p:lookahead1W(0, $input, $state) (: WhiteSpace | IRIREF :) | |
let $state := p:shift(2, $input, $state) (: IRIREF :) | |
let $end := $state[$p:e0] | |
return p:reduce($state, "sparqlBase", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse sparqlPrefix. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-sparqlPrefix($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := p:shift(24, $input, $state) (: 'PREFIX' :) | |
let $state := p:lookahead1W(1, $input, $state) (: WhiteSpace | PNAME_NS :) | |
let $state := p:shift(3, $input, $state) (: PNAME_NS :) | |
let $state := p:lookahead1W(0, $input, $state) (: WhiteSpace | IRIREF :) | |
let $state := p:shift(2, $input, $state) (: IRIREF :) | |
let $end := $state[$p:e0] | |
return p:reduce($state, "sparqlPrefix", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse base. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-base($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := p:shift(21, $input, $state) (: '@base' :) | |
let $state := p:lookahead1W(0, $input, $state) (: WhiteSpace | IRIREF :) | |
let $state := p:shift(2, $input, $state) (: IRIREF :) | |
let $state := p:lookahead1W(2, $input, $state) (: WhiteSpace | '.' :) | |
let $state := p:shift(19, $input, $state) (: '.' :) | |
let $end := $state[$p:e0] | |
return p:reduce($state, "base", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse prefixID. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-prefixID($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := p:shift(22, $input, $state) (: '@prefix' :) | |
let $state := p:lookahead1W(1, $input, $state) (: WhiteSpace | PNAME_NS :) | |
let $state := p:shift(3, $input, $state) (: PNAME_NS :) | |
let $state := p:lookahead1W(0, $input, $state) (: WhiteSpace | IRIREF :) | |
let $state := p:shift(2, $input, $state) (: IRIREF :) | |
let $state := p:lookahead1W(2, $input, $state) (: WhiteSpace | '.' :) | |
let $state := p:shift(19, $input, $state) (: '.' :) | |
let $end := $state[$p:e0] | |
return p:reduce($state, "prefixID", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse directive. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-directive($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 22) then (: '@prefix' :) | |
let $state := p:parse-prefixID($input, $state) | |
return $state | |
else if ($state[$p:l1] = 21) then (: '@base' :) | |
let $state := p:parse-base($input, $state) | |
return $state | |
else if ($state[$p:l1] = 24) then (: 'PREFIX' :) | |
let $state := p:parse-sparqlPrefix($input, $state) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:parse-sparqlBase($input, $state) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "directive", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse statement. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-statement($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = 21 (: '@base' :) | |
or $state[$p:l1] = 22 (: '@prefix' :) | |
or $state[$p:l1] = 23 (: 'BASE' :) | |
or $state[$p:l1] = 24) then (: 'PREFIX' :) | |
let $state := p:parse-directive($input, $state) | |
return $state | |
else if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:parse-triples($input, $state) | |
let $state := p:shift(19, $input, $state) (: '.' :) | |
return $state | |
let $end := $state[$p:e0] | |
return p:reduce($state, "statement", $count, $begin, $end) | |
}; | |
(:~ | |
: Parse the 1st loop of production turtleDoc (zero or more). Use | |
: tail recursion for iteratively updating the parser state. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-turtleDoc-1($input as xs:string, $state as item()+) as item()+ | |
{ | |
if ($state[$p:error]) then | |
$state | |
else | |
let $state := p:lookahead1W(8, $input, $state) (: WhiteSpace | IRIREF | PNAME_NS | PNAME_LN | | |
BLANK_NODE_LABEL | ANON | EOF | '(' | '@base' | | |
'@prefix' | 'BASE' | 'PREFIX' | '[' :) | |
return | |
if ($state[$p:l1] = 15) then (: EOF :) | |
$state | |
else | |
let $state := p:whitespace($input, $state) | |
let $state := p:parse-statement($input, $state) | |
return p:parse-turtleDoc-1($input, $state) | |
}; | |
(:~ | |
: Parse turtleDoc. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:parse-turtleDoc($input as xs:string, $state as item()+) as item()+ | |
{ | |
let $count := count($state) | |
let $begin := $state[$p:e0] | |
let $state := p:parse-turtleDoc-1($input, $state) | |
let $state := p:shift(15, $input, $state) (: EOF :) | |
let $end := $state[$p:e0] | |
return p:reduce($state, "turtleDoc", $count, $begin, $end) | |
}; | |
(:~ | |
: Create a textual error message from a parsing error. | |
: | |
: @param $input the input string. | |
: @param $error the parsing error descriptor. | |
: @return the error message. | |
:) | |
declare function p:error-message($input as xs:string, $error as element(error)) as xs:string | |
{ | |
let $begin := xs:integer($error/@b) | |
let $context := string-to-codepoints(substring($input, 1, $begin - 1)) | |
let $linefeeds := index-of($context, 10) | |
let $line := count($linefeeds) + 1 | |
let $column := ($begin - $linefeeds[last()], $begin)[1] | |
return | |
if ($error/@o) then | |
concat | |
( | |
"syntax error, found ", $p:TOKEN[$error/@o + 1], " ", | |
"while expecting ", $p:TOKEN[$error/@x + 1], " ", | |
"at line ", string($line), ", column ", string($column), " ", | |
"...", substring($input, $begin, 32), "..." | |
) | |
else | |
let $expected := p:expected-token-set($error/@s) | |
return | |
concat | |
( | |
"lexical analysis failed ", | |
"while expecting ", | |
"["[exists($expected[2])], | |
string-join($expected, ", "), | |
"]"[exists($expected[2])], | |
" ", | |
if ($error/@e = $begin) then | |
"" | |
else | |
concat("after successfully scanning ", string($error/@e - $begin), " characters "), | |
"at line ", string($line), ", column ", string($column), " ", | |
"...", substring($input, $begin, 32), "..." | |
) | |
}; | |
(:~ | |
: Shift one token, i.e. compare lookahead token 1 with expected | |
: token and in case of a match, shift lookahead tokens down such that | |
: l1 becomes the current token, and higher lookahead tokens move down. | |
: When lookahead token 1 does not match the expected token, raise an | |
: error by saving the expected token code in the error field of the | |
: parser state. | |
: | |
: @param $code the expected token. | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:shift($code as xs:integer, $input as xs:string, $state as item()+) as item()+ | |
{ | |
if ($state[$p:error]) then | |
$state | |
else if ($state[$p:l1] = $code) then | |
( | |
subsequence($state, $p:l1, $p:e1 - $p:l1 + 1), | |
0, | |
$state[$p:e1], | |
subsequence($state, $p:e1), | |
let $begin := $state[$p:e0] | |
let $end := $state[$p:b1] | |
where $begin ne $end | |
return | |
text | |
{ | |
substring($input, $begin, $end - $begin) | |
}, | |
let $token := $p:TOKEN[1 + $state[$p:l1]] | |
let $name := if (starts-with($token, "'")) then "TOKEN" else $token | |
let $begin := $state[$p:b1] | |
let $end := $state[$p:e1] | |
return | |
element {$name} | |
{ | |
substring($input, $begin, $end - $begin) | |
} | |
) | |
else | |
( | |
subsequence($state, 1, $p:error - 1), | |
element error | |
{ | |
attribute b {$state[$p:b1]}, | |
attribute e {$state[$p:e1]}, | |
if ($state[$p:l1] < 0) then | |
attribute s {- $state[$p:l1]} | |
else | |
(attribute o {$state[$p:l1]}, attribute x {$code}) | |
}, | |
subsequence($state, $p:error + 1) | |
) | |
}; | |
(:~ | |
: Consume whitespace. | |
: | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:whitespace($input as xs:string, | |
$state as item()+) as item()+ | |
{ | |
if ($state[$p:e0] = $state[$p:b1]) then | |
$state | |
else | |
let $begin := $state[$p:e0] | |
let $end := $state[$p:b1] | |
return | |
( | |
0, | |
$state[$p:b0], | |
$end, | |
subsequence($state, $p:e0 + 1), | |
text | |
{ | |
substring($input, $begin, $end - $begin) | |
} | |
) | |
}; | |
(:~ | |
: Use p:match to fetch the next token, but skip any leading | |
: whitespace. | |
: | |
: @param $input the input string. | |
: @param $begin the index where to start. | |
: @param $token-set the valid token set id. | |
: @return a sequence of three values: the token code of the result | |
: token, with input string positions of token begin and end. | |
:) | |
declare function p:matchW($input as xs:string, | |
$begin as xs:integer, | |
$token-set as xs:integer) as xs:integer+ | |
{ | |
let $match := p:match($input, $begin, $token-set) | |
return | |
if ($match[1] = 1) then (: WhiteSpace :) | |
p:matchW($input, $match[3], $token-set) | |
else | |
$match | |
}; | |
(:~ | |
: Lookahead one token on level 1 with whitespace skipping. | |
: | |
: @param $set the code of the DFA entry state for the set of valid tokens. | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:lookahead1W($set as xs:integer, $input as xs:string, $state as item()+) as item()+ | |
{ | |
if ($state[$p:l1] != 0) then | |
$state | |
else | |
let $match := p:matchW($input, $state[$p:b1], $set) | |
return | |
( | |
$match[1], | |
subsequence($state, $p:lk + 1, $p:l1 - $p:lk - 1), | |
$match, | |
subsequence($state, $p:e1 + 1) | |
) | |
}; | |
(:~ | |
: Lookahead one token on level 1. | |
: | |
: @param $set the code of the DFA entry state for the set of valid tokens. | |
: @param $input the input string. | |
: @param $state the parser state. | |
: @return the updated parser state. | |
:) | |
declare function p:lookahead1($set as xs:integer, $input as xs:string, $state as item()+) as item()+ | |
{ | |
if ($state[$p:l1] != 0) then | |
$state | |
else | |
let $match := p:match($input, $state[$p:b1], $set) | |
return | |
( | |
$match[1], | |
subsequence($state, $p:lk + 1, $p:l1 - $p:lk - 1), | |
$match, | |
subsequence($state, $p:e1 + 1) | |
) | |
}; | |
(:~ | |
: Reduce the result stack, creating a nonterminal element. Pop | |
: $count elements off the stack, wrap them in a new element | |
: named $name, and push the new element. | |
: | |
: @param $state the parser state. | |
: @param $name the name of the result node. | |
: @param $count the number of child nodes. | |
: @param $begin the input index where the nonterminal begins. | |
: @param $end the input index where the nonterminal ends. | |
: @return the updated parser state. | |
:) | |
declare function p:reduce($state as item()+, $name as xs:string, $count as xs:integer, $begin as xs:integer, $end as xs:integer) as item()+ | |
{ | |
subsequence($state, 1, $count), | |
element {$name} | |
{ | |
subsequence($state, $count + 1) | |
} | |
}; | |
(:~ | |
: Parse start symbol turtleDoc from given string. | |
: | |
: @param $s the string to be parsed. | |
: @return the result as generated by parser actions. | |
:) | |
declare function p:parse-turtleDoc($s as xs:string) as item()* | |
{ | |
let $state := p:parse-turtleDoc($s, (0, 1, 1, 0, 1, 0, false())) | |
let $error := $state[$p:error] | |
return | |
if ($error) then | |
element ERROR {$error/@*, p:error-message($s, $error)} | |
else | |
subsequence($state, $p:result) | |
}; | |
(: End :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment