about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/fsobject/loader.py2
1 files changed, 1 insertions, 1 deletions
diff --git a/ranger/fsobject/loader.py b/ranger/fsobject/loader.py
index 674af1f3..8eb1e1a7 100644
--- a/ranger/fsobject/loader.py
+++ b/ranger/fsobject/loader.py
@@ -103,7 +103,7 @@ class Loader(FileManagerAware):
 				next(item.load_generator)
 		except StopIteration:
 			item.load_generator = None
-			self.queue.popleft()
+			self.queue.remove(item)
 		except Exception as err:
 			self.fm.notify(str(err), bad=True)
 	
' href='#n80'>80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 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 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 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
#
#
#            Nim's Runtime Library
#        (c) Copyright 2015 Andreas Rumpf, Dominik Picheta
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module implements a simple high performance `JSON`:idx:
## parser. JSON (JavaScript Object Notation) is a lightweight
## data-interchange format that is easy for humans to read and write
## (unlike XML). It is easy for machines to parse and generate.
## JSON is based on a subset of the JavaScript Programming Language,
## Standard ECMA-262 3rd Edition - December 1999.
##
## Dynamically retrieving fields from JSON
## =======================================
##
## This module allows you to access fields in a parsed JSON object in two
## different ways, one of them is described in this section.
##
## The ``parseJson`` procedure takes a string containing JSON and returns a
## ``JsonNode`` object. This is an object variant and it is either a
## ``JObject``, ``JArray``, ``JString``, ``JInt``, ``JFloat``, ``JBool`` or
## ``JNull``. You
## check the kind of this object variant by using the ``kind`` accessor.
##
## For a ``JsonNode`` who's kind is ``JObject``, you can acess its fields using
## the ``[]`` operator. The following example shows how to do this:
##
## .. code-block:: Nim
##   let jsonNode = parseJson("""{"key": 3.14}""")
##   doAssert jsonNode.kind == JObject
##   doAssert jsonNode["key"].kind == JFloat
##
## Retrieving the value of a JSON node can then be achieved using one of the
## helper procedures, which include:
##
## * ``getInt``
## * ``getFloat``
## * ``getStr``
## * ``getBool``
##
## To retrieve the value of ``"key"`` you can do the following:
##
## .. code-block:: Nim
##   doAssert jsonNode["key"].getFloat() == 3.14
##
## The ``[]`` operator will raise an exception when the specified field does
## not exist. If you wish to avoid this behaviour you can use the ``{}``
## operator instead, it will simply return ``nil`` when the field is not found.
## The ``get``-family of procedures will return a default value when called on
## ``nil``.
##
## Unmarshalling JSON into a type
## ==============================
##
## This module allows you to access fields in a parsed JSON object in two
## different ways, one of them is described in this section.
##
## This is done using the ``to`` macro. Take a look at
## `its documentation <#to.m,JsonNode,typedesc>`_ to see an example of its use.
##
## Creating JSON
## =============
##
## This module can also be used to comfortably create JSON using the `%*`
## operator:
##
## .. code-block:: nim
##
##   var hisName = "John"
##   let herAge = 31
##   var j = %*
##     [
##       {
##         "name": hisName,
##         "age": 30
##       },
##       {
##         "name": "Susan",
##         "age": herAge
##       }
##     ]
##
##    var j2 = %* {"name": "Isaac", "books": ["Robot Dreams"]}
##    j2["details"] = %* {"age":35, "pi":3.1415}
##    echo j2

import
  hashes, tables, strutils, lexbase, streams, unicode, macros

export
  tables.`$`

when defined(nimJsonGet):
  {.pragma: deprecatedGet, deprecated.}
else:
  {.pragma: deprecatedGet.}

type
  JsonEventKind* = enum  ## enumeration of all events that may occur when parsing
    jsonError,           ## an error occurred during parsing
    jsonEof,             ## end of file reached
    jsonString,          ## a string literal
    jsonInt,             ## an integer literal
    jsonFloat,           ## a float literal
    jsonTrue,            ## the value ``true``
    jsonFalse,           ## the value ``false``
    jsonNull,            ## the value ``null``
    jsonObjectStart,     ## start of an object: the ``{`` token
    jsonObjectEnd,       ## end of an object: the ``}`` token
    jsonArrayStart,      ## start of an array: the ``[`` token
    jsonArrayEnd         ## start of an array: the ``]`` token

  TokKind = enum         # must be synchronized with TJsonEventKind!
    tkError,
    tkEof,
    tkString,
    tkInt,
    tkFloat,
    tkTrue,
    tkFalse,
    tkNull,
    tkCurlyLe,
    tkCurlyRi,
    tkBracketLe,
    tkBracketRi,
    tkColon,
    tkComma

  JsonError* = enum        ## enumeration that lists all errors that can occur
    errNone,               ## no error
    errInvalidToken,       ## invalid token
    errStringExpected,     ## string expected
    errColonExpected,      ## ``:`` expected
    errCommaExpected,      ## ``,`` expected
    errBracketRiExpected,  ## ``]`` expected
    errCurlyRiExpected,    ## ``}`` expected
    errQuoteExpected,      ## ``"`` or ``'`` expected
    errEOC_Expected,       ## ``*/`` expected
    errEofExpected,        ## EOF expected
    errExprExpected        ## expr expected

  ParserState = enum
    stateEof, stateStart, stateObject, stateArray, stateExpectArrayComma,
    stateExpectObjectComma, stateExpectColon, stateExpectValue

  JsonParser* = object of BaseLexer ## the parser object.
    a: string
    tok: TokKind
    kind: JsonEventKind
    err: JsonError
    state: seq[ParserState]
    filename: string

  JsonKindError* = object of ValueError ## raised by the ``to`` macro if the
                                        ## JSON kind is incorrect.

const
  errorMessages: array[JsonError, string] = [
    "no error",
    "invalid token",
    "string expected",
    "':' expected",
    "',' expected",
    "']' expected",
    "'}' expected",
    "'\"' or \"'\" expected",
    "'*/' expected",
    "EOF expected",
    "expression expected"
  ]
  tokToStr: array[TokKind, string] = [
    "invalid token",
    "EOF",
    "string literal",
    "int literal",
    "float literal",
    "true",
    "false",
    "null",
    "{", "}", "[", "]", ":", ","
  ]

proc open*(my: var JsonParser, input: Stream, filename: string) =
  ## initializes the parser with an input stream. `Filename` is only used
  ## for nice error messages.
  lexbase.open(my, input)
  my.filename = filename
  my.state = @[stateStart]
  my.kind = jsonError
  my.a = ""

proc close*(my: var JsonParser) {.inline.} =
  ## closes the parser `my` and its associated input stream.
  lexbase.close(my)

proc str*(my: JsonParser): string {.inline.} =
  ## returns the character data for the events: ``jsonInt``, ``jsonFloat``,
  ## ``jsonString``
  assert(my.kind in {jsonInt, jsonFloat, jsonString})
  return my.a

proc getInt*(my: JsonParser): BiggestInt {.inline.} =
  ## returns the number for the event: ``jsonInt``
  assert(my.kind == jsonInt)
  return parseBiggestInt(my.a)

proc getFloat*(my: JsonParser): float {.inline.} =
  ## returns the number for the event: ``jsonFloat``
  assert(my.kind == jsonFloat)
  return parseFloat(my.a)

proc kind*(my: JsonParser): JsonEventKind {.inline.} =
  ## returns the current event type for the JSON parser
  return my.kind

proc getColumn*(my: JsonParser): int {.inline.} =
  ## get the current column the parser has arrived at.
  result = getColNumber(my, my.bufpos)

proc getLine*(my: JsonParser): int {.inline.} =
  ## get the current line the parser has arrived at.
  result = my.lineNumber

proc getFilename*(my: JsonParser): string {.inline.} =
  ## get the filename of the file that the parser processes.
  result = my.filename

proc errorMsg*(my: JsonParser): string =
  ## returns a helpful error message for the event ``jsonError``
  assert(my.kind == jsonError)
  result = "$1($2, $3) Error: $4" % [
    my.filename, $getLine(my), $getColumn(my), errorMessages[my.err]]

proc errorMsgExpected*(my: JsonParser, e: string): string =
  ## returns an error message "`e` expected" in the same format as the
  ## other error messages
  result = "$1($2, $3) Error: $4" % [
    my.filename, $getLine(my), $getColumn(my), e & " expected"]

proc handleHexChar(c: char, x: var int): bool =
  result = true # Success
  case c
  of '0'..'9': x = (x shl 4) or (ord(c) - ord('0'))
  of 'a'..'f': x = (x shl 4) or (ord(c) - ord('a') + 10)
  of 'A'..'F': x = (x shl 4) or (ord(c) - ord('A') + 10)
  else: result = false # error

proc parseEscapedUTF16(buf: cstring, pos: var int): int =
  result = 0
  #UTF-16 escape is always 4 bytes.
  for _ in 0..3:
    if handleHexChar(buf[pos], result):
      inc(pos)
    else:
      return -1

proc parseString(my: var JsonParser): TokKind =
  result = tkString
  var pos = my.bufpos + 1
  var buf = my.buf
  while true:
    case buf[pos]
    of '\0':
      my.err = errQuoteExpected
      result = tkError
      break
    of '"':
      inc(pos)
      break
    of '\\':
      case buf[pos+1]
      of '\\', '"', '\'', '/':
        add(my.a, buf[pos+1])
        inc(pos, 2)
      of 'b':
        add(my.a, '\b')
        inc(pos, 2)
      of 'f':
        add(my.a, '\f')
        inc(pos, 2)
      of 'n':
        add(my.a, '\L')
        inc(pos, 2)
      of 'r':
        add(my.a, '\C')
        inc(pos, 2)
      of 't':
        add(my.a, '\t')
        inc(pos, 2)
      of 'u':
        inc(pos, 2)
        var r = parseEscapedUTF16(buf, pos)
        if r < 0:
          my.err = errInvalidToken
          break
        # Deal with surrogates
        if (r and 0xfc00) == 0xd800:
          if buf[pos] & buf[pos+1] != "\\u":
            my.err = errInvalidToken
            break
          inc(pos, 2)
          var s = parseEscapedUTF16(buf, pos)
          if (s and 0xfc00) == 0xdc00 and s > 0:
            r = 0x10000 + (((r - 0xd800) shl 10) or (s - 0xdc00))
          else:
            my.err = errInvalidToken
            break
        add(my.a, toUTF8(Rune(r)))
      else:
        # don't bother with the error
        add(my.a, buf[pos])
        inc(pos)
    of '\c':
      pos = lexbase.handleCR(my, pos)
      buf = my.buf
      add(my.a, '\c')
    of '\L':
      pos = lexbase.handleLF(my, pos)
      buf = my.buf
      add(my.a, '\L')
    else:
      add(my.a, buf[pos])
      inc(pos)
  my.bufpos = pos # store back

proc skip(my: var JsonParser) =
  var pos = my.bufpos
  var buf = my.buf
  while true:
    case buf[pos]
    of '/':
      if buf[pos+1] == '/':
        # skip line comment:
        inc(pos, 2)
        while true:
          case buf[pos]
          of '\0':
            break
          of '\c':
            pos = lexbase.handleCR(my, pos)
            buf = my.buf
            break
          of '\L':
            pos = lexbase.handleLF(my, pos)
            buf = my.buf
            break
          else:
            inc(pos)
      elif buf[pos+1] == '*':
        # skip long comment:
        inc(pos, 2)
        while true:
          case buf[pos]
          of '\0':
            my.err = errEOC_Expected
            break
          of '\c':
            pos = lexbase.handleCR(my, pos)
            buf = my.buf
          of '\L':
            pos = lexbase.handleLF(my, pos)
            buf = my.buf
          of '*':
            inc(pos)
            if buf[pos] == '/':
              inc(pos)
              break
          else:
            inc(pos)
      else:
        break
    of ' ', '\t':
      inc(pos)
    of '\c':
      pos = lexbase.handleCR(my, pos)
      buf = my.buf
    of '\L':
      pos = lexbase.handleLF(my, pos)
      buf = my.buf
    else:
      break
  my.bufpos = pos

proc parseNumber(my: var JsonParser) =
  var pos = my.bufpos
  var buf = my.buf
  if buf[pos] == '-':
    add(my.a, '-')
    inc(pos)
  if buf[pos] == '.':
    add(my.a, "0.")
    inc(pos)
  else:
    while buf[pos] in Digits:
      add(my.a, buf[pos])
      inc(pos)
    if buf[pos] == '.':
      add(my.a, '.')
      inc(pos)
  # digits after the dot:
  while buf[pos] in Digits:
    add(my.a, buf[pos])
    inc(pos)
  if buf[pos] in {'E', 'e'}:
    add(my.a, buf[pos])
    inc(pos)
    if buf[pos] in {'+', '-'}:
      add(my.a, buf[pos])
      inc(pos)
    while buf[pos] in Digits:
      add(my.a, buf[pos])
      inc(pos)
  my.bufpos = pos

proc parseName(my: var JsonParser) =
  var pos = my.bufpos
  var buf = my.buf
  if buf[pos] in IdentStartChars:
    while buf[pos] in IdentChars:
      add(my.a, buf[pos])
      inc(pos)
  my.bufpos = pos

proc getTok(my: var JsonParser): TokKind =
  setLen(my.a, 0)
  skip(my) # skip whitespace, comments
  case my.buf[my.bufpos]
  of '-', '.', '0'..'9':
    parseNumber(my)
    if {'.', 'e', 'E'} in my.a:
      result = tkFloat
    else:
      result = tkInt
  of '"':
    result = parseString(my)
  of '[':
    inc(my.bufpos)
    result = tkBracketLe
  of '{':
    inc(my.bufpos)
    result = tkCurlyLe
  of ']':
    inc(my.bufpos)
    result = tkBracketRi
  of '}':
    inc(my.bufpos)
    result = tkCurlyRi
  of ',':
    inc(my.bufpos)
    result = tkComma
  of ':':
    inc(my.bufpos)
    result = tkColon
  of '\0':
    result = tkEof
  of 'a'..'z', 'A'..'Z', '_':
    parseName(my)
    case my.a
    of "null": result = tkNull
    of "true": result = tkTrue
    of "false": result = tkFalse
    else: result = tkError
  else:
    inc(my.bufpos)
    result = tkError
  my.tok = result

proc next*(my: var JsonParser) =
  ## retrieves the first/next event. This controls the parser.
  var tk = getTok(my)
  var i = my.state.len-1
  # the following code is a state machine. If we had proper coroutines,
  # the code could be much simpler.
  case my.state[i]
  of stateEof:
    if tk == tkEof:
      my.kind = jsonEof
    else:
      my.kind = jsonError
      my.err = errEofExpected
  of stateStart:
    # tokens allowed?
    case tk
    of tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull:
      my.state[i] = stateEof # expect EOF next!
      my.kind = JsonEventKind(ord(tk))
    of tkBracketLe:
      my.state.add(stateArray) # we expect any
      my.kind = jsonArrayStart
    of tkCurlyLe:
      my.state.add(stateObject)
      my.kind = jsonObjectStart
    of tkEof:
      my.kind = jsonEof
    else:
      my.kind = jsonError
      my.err = errEofExpected
  of stateObject:
    case tk
    of tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull:
      my.state.add(stateExpectColon)
      my.kind = JsonEventKind(ord(tk))
    of tkBracketLe:
      my.state.add(stateExpectColon)
      my.state.add(stateArray)
      my.kind = jsonArrayStart
    of tkCurlyLe:
      my.state.add(stateExpectColon)
      my.state.add(stateObject)
      my.kind = jsonObjectStart
    of tkCurlyRi:
      my.kind = jsonObjectEnd
      discard my.state.pop()
    else:
      my.kind = jsonError
      my.err = errCurlyRiExpected
  of stateArray:
    case tk
    of tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull:
      my.state.add(stateExpectArrayComma) # expect value next!
      my.kind = JsonEventKind(ord(tk))
    of tkBracketLe:
      my.state.add(stateExpectArrayComma)
      my.state.add(stateArray)
      my.kind = jsonArrayStart
    of tkCurlyLe:
      my.state.add(stateExpectArrayComma)
      my.state.add(stateObject)
      my.kind = jsonObjectStart
    of tkBracketRi:
      my.kind = jsonArrayEnd
      discard my.state.pop()
    else:
      my.kind = jsonError
      my.err = errBracketRiExpected
  of stateExpectArrayComma:
    case tk
    of tkComma:
      discard my.state.pop()
      next(my)
    of tkBracketRi:
      my.kind = jsonArrayEnd
      discard my.state.pop() # pop stateExpectArrayComma
      discard my.state.pop() # pop stateArray
    else:
      my.kind = jsonError
      my.err = errBracketRiExpected
  of stateExpectObjectComma:
    case tk
    of tkComma:
      discard my.state.pop()
      next(my)
    of tkCurlyRi:
      my.kind = jsonObjectEnd
      discard my.state.pop() # pop stateExpectObjectComma
      discard my.state.pop() # pop stateObject
    else:
      my.kind = jsonError
      my.err = errCurlyRiExpected
  of stateExpectColon:
    case tk
    of tkColon:
      my.state[i] = stateExpectValue
      next(my)
    else:
      my.kind = jsonError
      my.err = errColonExpected
  of stateExpectValue:
    case tk
    of tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull:
      my.state[i] = stateExpectObjectComma
      my.kind = JsonEventKind(ord(tk))
    of tkBracketLe:
      my.state[i] = stateExpectObjectComma
      my.state.add(stateArray)
      my.kind = jsonArrayStart
    of tkCurlyLe:
      my.state[i] = stateExpectObjectComma
      my.state.add(stateObject)
      my.kind = jsonObjectStart
    else:
      my.kind = jsonError
      my.err = errExprExpected


# ------------- higher level interface ---------------------------------------

type
  JsonNodeKind* = enum ## possible JSON node types
    JNull,
    JBool,
    JInt,
    JFloat,
    JString,
    JObject,
    JArray

  JsonNode* = ref JsonNodeObj ## JSON node
  JsonNodeObj* {.acyclic.} = object
    case kind*: JsonNodeKind
    of JString:
      str*: string
    of JInt:
      num*: BiggestInt
    of JFloat:
      fnum*: float
    of JBool:
      bval*: bool
    of JNull:
      nil
    of JObject:
      fields*: OrderedTable[string, JsonNode]
    of JArray:
      elems*: seq[JsonNode]

  JsonParsingError* = object of ValueError ## is raised for a JSON error

proc raiseParseErr*(p: JsonParser, msg: string) {.noinline, noreturn.} =
  ## raises an `EJsonParsingError` exception.
  raise newException(JsonParsingError, errorMsgExpected(p, msg))

proc newJString*(s: string): JsonNode =
  ## Creates a new `JString JsonNode`.
  new(result)
  result.kind = JString
  result.str = s

proc newJStringMove(s: string): JsonNode =
  new(result)
  result.kind = JString
  shallowCopy(result.str, s)

proc newJInt*(n: BiggestInt): JsonNode =
  ## Creates a new `JInt JsonNode`.
  new(result)
  result.kind = JInt
  result.num  = n

proc newJFloat*(n: float): JsonNode =
  ## Creates a new `JFloat JsonNode`.
  new(result)
  result.kind = JFloat
  result.fnum  = n

proc newJBool*(b: bool): JsonNode =
  ## Creates a new `JBool JsonNode`.
  new(result)
  result.kind = JBool
  result.bval = b

proc newJNull*(): JsonNode =
  ## Creates a new `JNull JsonNode`.
  new(result)

proc newJObject*(): JsonNode =
  ## Creates a new `JObject JsonNode`
  new(result)
  result.kind = JObject
  result.fields = initOrderedTable[string, JsonNode](4)

proc newJArray*(): JsonNode =
  ## Creates a new `JArray JsonNode`
  new(result)
  result.kind = JArray
  result.elems = @[]

proc getStr*(n: JsonNode, default: string = ""): string =
  ## Retrieves the string value of a `JString JsonNode`.
  ##
  ## Returns ``default`` if ``n`` is not a ``JString``, or if ``n`` is nil.
  if n.isNil or n.kind != JString: return default
  else: return n.str

proc getInt*(n: JsonNode, default: int = 0): int =
  ## Retrieves the int value of a `JInt JsonNode`.
  ##
  ## Returns ``default`` if ``n`` is not a ``JInt``, or if ``n`` is nil.
  if n.isNil or n.kind != JInt: return default
  else: return int(n.num)

proc getBiggestInt*(n: JsonNode, default: BiggestInt = 0): BiggestInt =
  ## Retrieves the BiggestInt value of a `JInt JsonNode`.
  ##
  ## Returns ``default`` if ``n`` is not a ``JInt``, or if ``n`` is nil.
  if n.isNil or n.kind != JInt: return default
  else: return n.num

proc getNum*(n: JsonNode, default: BiggestInt = 0): BiggestInt {.deprecated: "use getInt or getBiggestInt instead".} =
  ## **Deprecated since v0.18.2:** use ``getInt`` or ``getBiggestInt`` instead.
  getBiggestInt(n, default)

proc getFloat*(n: JsonNode, default: float = 0.0): float =
  ## Retrieves the float value of a `JFloat JsonNode`.
  ##
  ## Returns ``default`` if ``n`` is not a ``JFloat`` or ``JInt``, or if ``n`` is nil.
  if n.isNil: return default
  case n.kind
  of JFloat: return n.fnum
  of JInt: return float(n.num)
  else: return default

proc getFNum*(n: JsonNode, default: float = 0.0): float {.deprecated: "use getFloat instead".} =
  ## **Deprecated since v0.18.2:** use ``getFloat`` instead.
  getFloat(n, default)

proc getBool*(n: JsonNode, default: bool = false): bool =
  ## Retrieves the bool value of a `JBool JsonNode`.
  ##
  ## Returns ``default`` if ``n`` is not a ``JBool``, or if ``n`` is nil.
  if n.isNil or n.kind != JBool: return default
  else: return n.bval

proc getBVal*(n: JsonNode, default: bool = false): bool {.deprecated: "use getBool instead".} =
  ## **Deprecated since v0.18.2:** use ``getBool`` instead.
  getBool(n, default)

proc getFields*(n: JsonNode,
    default = initOrderedTable[string, JsonNode](4)):
        OrderedTable[string, JsonNode] =
  ## Retrieves the key, value pairs of a `JObject JsonNode`.
  ##
  ## Returns ``default`` if ``n`` is not a ``JObject``, or if ``n`` is nil.
  if n.isNil or n.kind != JObject: return default
  else: return n.fields

proc getElems*(n: JsonNode, default: seq[JsonNode] = @[]): seq[JsonNode] =
  ## Retrieves the array of a `JArray JsonNode`.
  ##
  ## Returns ``default`` if ``n`` is not a ``JArray``, or if ``n`` is nil.
  if n.isNil or n.kind != JArray: return default
  else: return n.elems

proc add*(father, child: JsonNode) =
  ## Adds `child` to a JArray node `father`.
  assert father.kind == JArray
  father.elems.add(child)

proc add*(obj: JsonNode, key: string, val: JsonNode) =
  ## Sets a field from a `JObject`.
  assert obj.kind == JObject
  obj.fields[key] = val

proc `%`*(s: string): JsonNode =
  ## Generic constructor for JSON data. Creates a new `JString JsonNode`.
  new(result)
  if s.isNil: return
  result.kind = JString
  result.str = s

proc `%`*(n: BiggestInt): JsonNode =
  ## Generic constructor for JSON data. Creates a new `JInt JsonNode`.
  new(result)
  result.kind = JInt
  result.num  = n

proc `%`*(n: float): JsonNode =
  ## Generic constructor for JSON data. Creates a new `JFloat JsonNode`.
  new(result)
  result.kind = JFloat
  result.fnum  = n

proc `%`*(b: bool): JsonNode =
  ## Generic constructor for JSON data. Creates a new `JBool JsonNode`.
  new(result)
  result.kind = JBool
  result.bval = b

proc `%`*(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode =
  ## Generic constructor for JSON data. Creates a new `JObject JsonNode`
  if keyvals.len == 0: return newJArray()
  result = newJObject()
  for key, val in items(keyVals): result.fields[key] = val

template `%`*(j: JsonNode): JsonNode = j

proc `%`*[T](elements: openArray[T]): JsonNode =
  ## Generic constructor for JSON data. Creates a new `JArray JsonNode`
  result = newJArray()
  for elem in elements: result.add(%elem)

when false:
  # For 'consistency' we could do this, but that only pushes people further
  # into that evil comfort zone where they can use Nim without understanding it
  # causing problems later on.
  proc `%`*(elements: set[bool]): JsonNode =
    ## Generic constructor for JSON data. Creates a new `JObject JsonNode`.
    ## This can only be used with the empty set ``{}`` and is supported
    ## to prevent the gotcha ``%*{}`` which used to produce an empty
    ## JSON array.
    result = newJObject()
    assert false notin elements, "usage error: only empty sets allowed"
    assert true notin elements, "usage error: only empty sets allowed"

proc `%`*(o: object): JsonNode =
  ## Generic constructor for JSON data. Creates a new `JObject JsonNode`
  result = newJObject()
  for k, v in o.fieldPairs: result[k] = %v

proc `%`*(o: ref object): JsonNode =
  ## Generic constructor for JSON data. Creates a new `JObject JsonNode`
  if o.isNil:
    result = newJNull()
  else:
    result = %(o[])

proc `%`*(o: enum): JsonNode =
  ## Construct a JsonNode that represents the specified enum value as a
  ## string. Creates a new ``JString JsonNode``.
  result = %($o)

proc toJson(x: NimNode): NimNode {.compiletime.} =
  case x.kind
  of nnkBracket: # array
    if x.len == 0: return newCall(bindSym"newJArray")
    result = newNimNode(nnkBracket)
    for i in 0 ..< x.len:
      result.add(toJson(x[i]))
    result = newCall(bindSym"%", result)
  of nnkTableConstr: # object
    if x.len == 0: return newCall(bindSym"newJObject")
    result = newNimNode(nnkTableConstr)
    for i in 0 ..< x.len:
      x[i].expectKind nnkExprColonExpr
      result.add newTree(nnkExprColonExpr, x[i][0], toJson(x[i][1]))
    result = newCall(bindSym"%", result)
  of nnkCurly: # empty object
    x.expectLen(0)
    result = newCall(bindSym"newJObject")
  of nnkNilLit:
    result = newCall(bindSym"newJNull")
  else:
    result = newCall(bindSym"%", x)

macro `%*`*(x: untyped): untyped =
  ## Convert an expression to a JsonNode directly, without having to specify
  ## `%` for every element.
  result = toJson(x)

proc `==`* (a, b: JsonNode): bool =
  ## Check two nodes for equality
  if a.isNil:
    if b.isNil: return true
    return false
  elif b.isNil or a.kind != b.kind:
    return false
  else:
    case a.kind
    of JString:
      result = a.str == b.str
    of JInt:
      result = a.num == b.num
    of JFloat:
      result = a.fnum == b.fnum
    of JBool:
      result = a.bval == b.bval
    of JNull:
      result = true
    of JArray:
      result = a.elems == b.elems
    of JObject:
     # we cannot use OrderedTable's equality here as
     # the order does not matter for equality here.
     if a.fields.len != b.fields.len: return false
     for key, val in a.fields:
       if not b.fields.hasKey(key): return false
       if b.fields[key] != val: return false
     result = true

proc hash*(n: OrderedTable[string, JsonNode]): Hash {.noSideEffect.}

proc hash*(n: JsonNode): Hash =
  ## Compute the hash for a JSON node
  case n.kind
  of JArray:
    result = hash(n.elems)
  of JObject:
    result = hash(n.fields)
  of JInt:
    result = hash(n.num)
  of JFloat:
    result = hash(n.fnum)
  of JBool:
    result = hash(n.bval.int)
  of JString:
    result = hash(n.str)
  of JNull:
    result = Hash(0)

proc hash*(n: OrderedTable[string, JsonNode]): Hash =
  for key, val in n:
    result = result xor (hash(key) !& hash(val))
  result = !$result

proc len*(n: JsonNode): int =
  ## If `n` is a `JArray`, it returns the number of elements.
  ## If `n` is a `JObject`, it returns the number of pairs.
  ## Else it returns 0.
  case n.kind
  of JArray: result = n.elems.len
  of JObject: result = n.fields.len
  else: discard

proc `[]`*(node: JsonNode, name: string): JsonNode {.inline, deprecatedGet.} =
  ## Gets a field from a `JObject`, which must not be nil.
  ## If the value at `name` does not exist, raises KeyError.
  ##
  ## **Note:** The behaviour of this procedure changed in version 0.14.0. To
  ## get a list of usages and to restore the old behaviour of this procedure,
  ## compile with the ``-d:nimJsonGet`` flag.
  assert(not isNil(node))
  assert(node.kind == JObject)
  when defined(nimJsonGet):
    if not node.fields.hasKey(name): return nil
  result = node.fields[name]

proc `[]`*(node: JsonNode, index: int): JsonNode {.inline.} =
  ## Gets the node at `index` in an Array. Result is undefined if `index`
  ## is out of bounds, but as long as array bound checks are enabled it will
  ## result in an exception.
  assert(not isNil(node))
  assert(node.kind == JArray)
  return node.elems[index]

proc hasKey*(node: JsonNode, key: string): bool =
  ## Checks if `key` exists in `node`.
  assert(node.kind == JObject)
  result = node.fields.hasKey(key)

proc contains*(node: JsonNode, key: string): bool =
  ## Checks if `key` exists in `node`.
  assert(node.kind == JObject)
  node.fields.hasKey(key)

proc contains*(node: JsonNode, val: JsonNode): bool =
  ## Checks if `val` exists in array `node`.
  assert(node.kind == JArray)
  find(node.elems, val) >= 0

proc existsKey*(node: JsonNode, key: string): bool {.deprecated: "use hasKey instead".} = node.hasKey(key)
  ## **Deprecated:** use `hasKey` instead.

proc `[]=`*(obj: JsonNode, key: string, val: JsonNode) {.inline.} =
  ## Sets a field from a `JObject`.
  assert(obj.kind == JObject)
  obj.fields[key] = val

proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode =
  ## Traverses the node and gets the given value. If any of the
  ## keys do not exist, returns ``nil``. Also returns ``nil`` if one of the
  ## intermediate data structures is not an object.
  ##
  ## This proc can be used to create tree structures on the
  ## fly (sometimes called `autovivification`:idx:):
  ##
  ## .. code-block:: nim
  ##   myjson{"parent", "child", "grandchild"} = newJInt(1)
  ##
  result = node
  for key in keys:
    if isNil(result) or result.kind != JObject:
      return nil
    result = result.fields.getOrDefault(key)

proc `{}`*(node: JsonNode, index: varargs[int]): JsonNode =
  ## Traverses the node and gets the given value. If any of the
  ## indexes do not exist, returns ``nil``. Also returns ``nil`` if one of the
  ## intermediate data structures is not an array.
  result = node
  for i in index:
    if isNil(result) or result.kind != JArray or i >= node.len:
      return nil
    result = result.elems[i]

proc getOrDefault*(node: JsonNode, key: string): JsonNode =
  ## Gets a field from a `node`. If `node` is nil or not an object or
  ## value at `key` does not exist, returns nil
  if not isNil(node) and node.kind == JObject:
    result = node.fields.getOrDefault(key)

template simpleGetOrDefault*{`{}`(node, [key])}(node: JsonNode, key: string): JsonNode = node.getOrDefault(key)

proc `{}=`*(node: JsonNode, keys: varargs[string], value: JsonNode) =
  ## Traverses the node and tries to set the value at the given location
  ## to ``value``. If any of the keys are missing, they are added.
  var node = node
  for i in 0..(keys.len-2):
    if not node.hasKey(keys[i]):
      node[keys[i]] = newJObject()
    node = node[keys[i]]
  node[keys[keys.len-1]] = value

proc delete*(obj: JsonNode, key: string) =
  ## Deletes ``obj[key]``.
  assert(obj.kind == JObject)
  if not obj.fields.hasKey(key):
    raise newException(IndexError, "key not in object")
  obj.fields.del(key)

proc copy*(p: JsonNode): JsonNode =
  ## Performs a deep copy of `a`.
  case p.kind
  of JString:
    result = newJString(p.str)
  of JInt:
    result = newJInt(p.num)
  of JFloat:
    result = newJFloat(p.fnum)
  of JBool:
    result = newJBool(p.bval)
  of JNull:
    result = newJNull()
  of JObject:
    result = newJObject()
    for key, val in pairs(p.fields):
      result.fields[key] = copy(val)
  of JArray:
    result = newJArray()
    for i in items(p.elems):
      result.elems.add(copy(i))

# ------------- pretty printing ----------------------------------------------

proc indent(s: var string, i: int) =
  s.add(spaces(i))

proc newIndent(curr, indent: int, ml: bool): int =
  if ml: return curr + indent
  else: return indent

proc nl(s: var string, ml: bool) =
  s.add(if ml: "\n" else: " ")

proc escapeJson*(s: string; result: var string) =
  ## Converts a string `s` to its JSON representation.
  ## Appends to ``result``.
  result.add("\"")
  for c in s:
    case c
    of '\L': result.add("\\n")
    of '\b': result.add("\\b")
    of '\f': result.add("\\f")
    of '\t': result.add("\\t")
    of '\r': result.add("\\r")
    of '"': result.add("\\\"")
    of '\\': result.add("\\\\")
    else: result.add(c)
  result.add("\"")

proc escapeJson*(s: string): string =
  ## Converts a string `s` to its JSON representation.
  result = newStringOfCap(s.len + s.len shr 3)
  escapeJson(s, result)

proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true,
              lstArr = false, currIndent = 0) =
  case node.kind
  of JObject:
    if lstArr: result.indent(currIndent) # Indentation
    if node.fields.len > 0:
      result.add("{")
      result.nl(ml) # New line
      var i = 0
      for key, val in pairs(node.fields):
        if i > 0:
          result.add(",")
          result.nl(ml) # New Line
        inc i
        # Need to indent more than {
        result.indent(newIndent(currIndent, indent, ml))
        escapeJson(key, result)
        result.add(": ")
        toPretty(result, val, indent, ml, false,
                 newIndent(currIndent, indent, ml))
      result.nl(ml)
      result.indent(currIndent) # indent the same as {
      result.add("}")
    else:
      result.add("{}")
  of JString:
    if lstArr: result.indent(currIndent)
    escapeJson(node.str, result)
  of JInt:
    if lstArr: result.indent(currIndent)
    when defined(js): result.add($node.num)
    else: result.add(node.num)
  of JFloat:
    if lstArr: result.indent(currIndent)
    # Fixme: implement new system.add ops for the JS target
    when defined(js): result.add($node.fnum)
    else: result.add(node.fnum)
  of JBool:
    if lstArr: result.indent(currIndent)
    result.add(if node.bval: "true" else: "false")
  of JArray:
    if lstArr: result.indent(currIndent)
    if len(node.elems) != 0:
      result.add("[")
      result.nl(ml)
      for i in 0..len(node.elems)-1:
        if i > 0:
          result.add(",")
          result.nl(ml) # New Line
        toPretty(result, node.elems[i], indent, ml,
            true, newIndent(currIndent, indent, ml))
      result.nl(ml)
      result.indent(currIndent)
      result.add("]")
    else: result.add("[]")
  of JNull:
    if lstArr: result.indent(currIndent)
    result.add("null")

proc pretty*(node: JsonNode, indent = 2): string =
  ## Returns a JSON Representation of `node`, with indentation and
  ## on multiple lines.
  result = ""
  toPretty(result, node, indent)

proc toUgly*(result: var string, node: JsonNode) =
  ## Converts `node` to its JSON Representation, without
  ## regard for human readability. Meant to improve ``$`` string
  ## conversion performance.
  ##
  ## JSON representation is stored in the passed `result`
  ##
  ## This provides higher efficiency than the ``pretty`` procedure as it
  ## does **not** attempt to format the resulting JSON to make it human readable.
  var comma = false
  case node.kind:
  of JArray:
    result.add "["
    for child in node.elems:
      if comma: result.add ","
      else:     comma = true
      result.toUgly child
    result.add "]"
  of JObject:
    result.add "{"
    for key, value in pairs(node.fields):
      if comma: result.add ","
      else:     comma = true
      key.escapeJson(result)
      result.add ":"
      result.toUgly value
    result.add "}"
  of JString:
    node.str.escapeJson(result)
  of JInt:
    when defined(js): result.add($node.num)
    else: result.add(node.num)
  of JFloat:
    when defined(js): result.add($node.fnum)
    else: result.add(node.fnum)
  of JBool:
    result.add(if node.bval: "true" else: "false")
  of JNull:
    result.add "null"

proc `$`*(node: JsonNode): string =
  ## Converts `node` to its JSON Representation on one line.
  result = newStringOfCap(node.len shl 1)
  toUgly(result, node)

iterator items*(node: JsonNode): JsonNode =
  ## Iterator for the items of `node`. `node` has to be a JArray.
  assert node.kind == JArray
  for i in items(node.elems):
    yield i

iterator mitems*(node: var JsonNode): var JsonNode =
  ## Iterator for the items of `node`. `node` has to be a JArray. Items can be
  ## modified.
  assert node.kind == JArray
  for i in mitems(node.elems):
    yield i

iterator pairs*(node: JsonNode): tuple[key: string, val: JsonNode] =
  ## Iterator for the child elements of `node`. `node` has to be a JObject.
  assert node.kind == JObject
  for key, val in pairs(node.fields):
    yield (key, val)

iterator mpairs*(node: var JsonNode): tuple[key: string, val: var JsonNode] =
  ## Iterator for the child elements of `node`. `node` has to be a JObject.
  ## Values can be modified
  assert node.kind == JObject
  for key, val in mpairs(node.fields):
    yield (key, val)

proc eat(p: var JsonParser, tok: TokKind) =
  if p.tok == tok: discard getTok(p)
  else: raiseParseErr(p, tokToStr[tok])

proc parseJson(p: var JsonParser): JsonNode =
  ## Parses JSON from a JSON Parser `p`.
  case p.tok
  of tkString:
    # we capture 'p.a' here, so we need to give it a fresh buffer afterwards:
    result = newJStringMove(p.a)
    p.a = ""
    discard getTok(p)
  of tkInt:
    result = newJInt(parseBiggestInt(p.a))
    discard getTok(p)
  of tkFloat:
    result = newJFloat(parseFloat(p.a))
    discard getTok(p)
  of tkTrue:
    result = newJBool(true)
    discard getTok(p)
  of tkFalse:
    result = newJBool(false)
    discard getTok(p)
  of tkNull:
    result = newJNull()
    discard getTok(p)
  of tkCurlyLe:
    result = newJObject()
    discard getTok(p)
    while p.tok != tkCurlyRi:
      if p.tok != tkString:
        raiseParseErr(p, "string literal as key")
      var key = p.a
      discard getTok(p)
      eat(p, tkColon)
      var val = parseJson(p)
      result[key] = val
      if p.tok != tkComma: break
      discard getTok(p)
    eat(p, tkCurlyRi)
  of tkBracketLe:
    result = newJArray()
    discard getTok(p)
    while p.tok != tkBracketRi:
      result.add(parseJson(p))
      if p.tok != tkComma: break
      discard getTok(p)
    eat(p, tkBracketRi)
  of tkError, tkCurlyRi, tkBracketRi, tkColon, tkComma, tkEof:
    raiseParseErr(p, "{")

when not defined(js):
  proc parseJson*(s: Stream, filename: string = ""): JsonNode =
    ## Parses from a stream `s` into a `JsonNode`. `filename` is only needed
    ## for nice error messages.
    ## If `s` contains extra data, it will raise `JsonParsingError`.
    var p: JsonParser
    p.open(s, filename)
    defer: p.close()
    discard getTok(p) # read first token
    result = p.parseJson()
    eat(p, tkEof) # check if there is no extra data

  proc parseJson*(buffer: string): JsonNode =
    ## Parses JSON from `buffer`.
    ## If `buffer` contains extra data, it will raise `JsonParsingError`.
    result = parseJson(newStringStream(buffer), "input")

  proc parseFile*(filename: string): JsonNode =
    ## Parses `file` into a `JsonNode`.
    ## If `file` contains extra data, it will raise `JsonParsingError`.
    var stream = newFileStream(filename, fmRead)
    if stream == nil:
      raise newException(IOError, "cannot read from file: " & filename)
    result = parseJson(stream, filename)
else:
  from math import `mod`
  type
    JSObject = object

  proc parseNativeJson(x: cstring): JSObject {.importc: "JSON.parse".}

  proc getVarType(x: JSObject): JsonNodeKind =
    result = JNull
    proc getProtoName(y: JSObject): cstring
      {.importc: "Object.prototype.toString.call".}
    case $getProtoName(x) # TODO: Implicit returns fail here.
    of "[object Array]": return JArray
    of "[object Object]": return JObject
    of "[object Number]":
      if cast[float](x) mod 1.0 == 0:
        return JInt
      else:
        return JFloat
    of "[object Boolean]": return JBool
    of "[object Null]": return JNull
    of "[object String]": return JString
    else: assert false

  proc len(x: JSObject): int =
    assert x.getVarType == JArray
    asm """
      `result` = `x`.length;
    """

  proc `[]`(x: JSObject, y: string): JSObject =
    assert x.getVarType == JObject
    asm """
      `result` = `x`[`y`];
    """

  proc `[]`(x: JSObject, y: int): JSObject =
    assert x.getVarType == JArray
    asm """
      `result` = `x`[`y`];
    """

  proc convertObject(x: JSObject): JsonNode =
    case getVarType(x)
    of JArray:
      result = newJArray()
      for i in 0 ..< x.len:
        result.add(x[i].convertObject())
    of JObject:
      result = newJObject()
      asm """for (var property in `x`) {
        if (`x`.hasOwnProperty(property)) {
      """
      var nimProperty: cstring
      var nimValue: JSObject
      asm "`nimProperty` = property; `nimValue` = `x`[property];"
      result[$nimProperty] = nimValue.convertObject()
      asm "}}"
    of JInt:
      result = newJInt(cast[int](x))
    of JFloat:
      result = newJFloat(cast[float](x))
    of JString:
      result = newJString($cast[cstring](x))
    of JBool:
      result = newJBool(cast[bool](x))
    of JNull:
      result = newJNull()

  proc parseJson*(buffer: string): JsonNode =
    return parseNativeJson(buffer).convertObject()

# -- Json deserialiser macro. --

proc createJsonIndexer(jsonNode: NimNode,
                       index: string | int | NimNode): NimNode
    {.compileTime.} =
  when index is string:
    let indexNode = newStrLitNode(index)
  elif index is int:
    let indexNode = newIntLitNode(index)
  elif index is NimNode:
    let indexNode = index

  result = newNimNode(nnkBracketExpr).add(
    jsonNode,
    indexNode
  )

proc transformJsonIndexer(jsonNode: NimNode): NimNode =
  case jsonNode.kind
  of nnkBracketExpr:
    result = newNimNode(nnkCurlyExpr)
  else:
    result = jsonNode.copy()

  for child in jsonNode:
    result.add(transformJsonIndexer(child))

template verifyJsonKind(node: JsonNode, kinds: set[JsonNodeKind],
                        ast: string) =
  if node.kind notin kinds:
    let msg = "Incorrect JSON kind. Wanted '$1' in '$2' but got '$3'." % [
      $kinds,
      ast,
      $node.kind
    ]
    raise newException(JsonKindError, msg)

proc getEnum(node: JsonNode, ast: string, T: typedesc): T =
  when T is SomeInteger:
    # TODO: I shouldn't need this proc.
    proc convert[T](x: BiggestInt): T = T(x)
    verifyJsonKind(node, {JInt}, ast)
    return convert[T](node.getBiggestInt())
  else:
    verifyJsonKind(node, {JString}, ast)
    return parseEnum[T](node.getStr())

proc toIdentNode(typeNode: NimNode): NimNode =
  ## Converts a Sym type node (returned by getType et al.) into an
  ## Ident node. Placing Sym type nodes inside the resulting code AST is
  ## unsound (according to @Araq) so this is necessary.
  case typeNode.kind
  of nnkSym:
    return newIdentNode($typeNode)
  of nnkBracketExpr:
    result = typeNode
    for i in 0..<len(result):
      result[i] = newIdentNode($result[i])
  of nnkIdent:
    return typeNode
  else:
    doAssert false, "Cannot convert typeNode to an ident node: " & $typeNode.kind

proc createGetEnumCall(jsonNode, kindType: NimNode): NimNode =
  # -> getEnum(`jsonNode`, `kindType`)
  let getEnumSym = bindSym("getEnum")
  let astStrLit = toStrLit(jsonNode)
  let getEnumCall = newCall(getEnumSym, jsonNode, astStrLit, kindType)
  return getEnumCall

proc createOfBranchCond(ofBranch, getEnumCall: NimNode): NimNode =
  ## Creates an expression that acts as the condition for an ``of`` branch.
  var cond = newIdentNode("false")
  for ofCond in ofBranch:
    if ofCond.kind == nnkRecList:
      break

    let comparison = infix(getEnumCall, "==", ofCond)
    cond = infix(cond, "or", comparison)

  return cond

proc processObjField(field, jsonNode: NimNode): seq[NimNode] {.compileTime.}
proc processOfBranch(ofBranch, jsonNode, kindType,
                     kindJsonNode: NimNode): seq[NimNode] {.compileTime.} =
  ## Processes each field inside of an object's ``of`` branch.
  ## For each field a new ExprColonExpr node is created and put in the
  ## resulting list.
  ##
  ## Sample ``ofBranch`` AST:
  ##
  ## .. code-block::plain
  ##     OfBranch                      of 0, 1:
  ##       IntLit 0                      foodPos: float
  ##       IntLit 1                      enemyPos: float
  ##       RecList
  ##         Sym "foodPos"
  ##         Sym "enemyPos"
  result = @[]
  let getEnumCall = createGetEnumCall(kindJsonNode, kindType)

  for branchField in ofBranch[^1]:
    let objFields = processObjField(branchField, jsonNode)

    for objField in objFields:
      let exprColonExpr = newNimNode(nnkExprColonExpr)
      result.add(exprColonExpr)
      # Add the name of the field.
      exprColonExpr.add(toIdentNode(objField[0]))

      # Add the value of the field.
      let cond = createOfBranchCond(ofBranch, getEnumCall)
      exprColonExpr.add(newIfStmt(
        (cond, objField[1])
      ))

proc processElseBranch(recCaseNode, elseBranch, jsonNode, kindType,
                       kindJsonNode: NimNode): seq[NimNode] {.compileTime.} =
  ## Processes each field inside of a variant object's ``else`` branch.
  ##
  ## ..code-block::plain
  ##   Else
  ##     RecList
  ##       Sym "other"
  result = @[]
  let getEnumCall = createGetEnumCall(kindJsonNode, kindType)

  # We need to build up a list of conditions from each ``of`` branch so that
  # we can then negate it to get ``else``.
  var cond = newIdentNode("false")
  for i in 1 ..< len(recCaseNode):
    if recCaseNode[i].kind == nnkElse:
      break

    cond = infix(cond, "or", createOfBranchCond(recCaseNode[i], getEnumCall))

  # Negate the condition.
  cond = prefix(cond, "not")

  for branchField in elseBranch[^1]:
    let objFields = processObjField(branchField, jsonNode)

    for objField in objFields:
      let exprColonExpr = newNimNode(nnkExprColonExpr)
      result.add(exprColonExpr)
      # Add the name of the field.
      exprColonExpr.add(toIdentNode(objField[0]))

      # Add the value of the field.
      let ifStmt = newIfStmt((cond, objField[1]))
      exprColonExpr.add(ifStmt)

proc createConstructor(typeSym, jsonNode: NimNode): NimNode {.compileTime.}
proc processObjField(field, jsonNode: NimNode): seq[NimNode] =
  ## Process a field from a ``RecList``.
  ##
  ## The field will typically be a simple ``Sym`` node, but for object variants
  ## it may also be a ``RecCase`` in which case things become complicated.
  result = @[]
  case field.kind
  of nnkSym:
    # Ordinary field. For example, `name: string`.
    let exprColonExpr = newNimNode(nnkExprColonExpr)
    result.add(exprColonExpr)

    # Add the field name.
    exprColonExpr.add(toIdentNode(field))

    # Add the field value.
    # -> jsonNode["`field`"]
    let indexedJsonNode = createJsonIndexer(jsonNode, $field)
    exprColonExpr.add(createConstructor(getTypeInst(field), indexedJsonNode))

  of nnkRecCase:
    # A "case" field that introduces a variant.
    let exprColonExpr = newNimNode(nnkExprColonExpr)
    result.add(exprColonExpr)

    # Add the "case" field name (usually "kind").
    exprColonExpr.add(toIdentNode(field[0]))

    # -> jsonNode["`field[0]`"]
    let kindJsonNode = createJsonIndexer(jsonNode, $field[0])

    # Add the "case" field's value.
    let kindType = toIdentNode(getTypeInst(field[0]))
    let getEnumSym = bindSym("getEnum")
    let astStrLit = toStrLit(kindJsonNode)
    let getEnumCall = newCall(getEnumSym, kindJsonNode, astStrLit, kindType)
    exprColonExpr.add(getEnumCall)

    # Iterate through each `of` branch.
    for i in 1 ..< field.len:
      case field[i].kind
      of nnkOfBranch:
        result.add processOfBranch(field[i], jsonNode, kindType, kindJsonNode)
      of nnkElse:
        result.add processElseBranch(field, field[i], jsonNode, kindType, kindJsonNode)
      else:
        doAssert false, "Expected OfBranch or Else node kinds, got: " & $field[i].kind
  else:
    doAssert false, "Unable to process object field: " & $field.kind

  doAssert result.len > 0

proc processFields(obj: NimNode,
                   jsonNode: NimNode): seq[NimNode] {.compileTime.} =
  ## Process all the fields of an ``ObjectTy`` and any of its
  ## parent type's fields (via inheritance).
  result = @[]
  case obj.kind
  of nnkObjectTy:
    expectKind(obj[2], nnkRecList)
    for field in obj[2]:
      let nodes = processObjField(field, jsonNode)
      result.add(nodes)

    # process parent type fields
    case obj[1].kind
    of nnkBracketExpr:
      assert $obj[1][0] == "ref"
      result.add(processFields(getType(obj[1][1]), jsonNode))
    of nnkSym:
      result.add(processFields(getType(obj[1]), jsonNode))
    else:
      discard
  of nnkTupleTy:
    for identDefs in obj:
      expectKind(identDefs, nnkIdentDefs)
      let nodes = processObjField(identDefs[0], jsonNode)
      result.add(nodes)
  else:
    doAssert false, "Unable to process field type: " & $obj.kind

proc processType(typeName: NimNode, obj: NimNode,
                 jsonNode: NimNode, isRef: bool): NimNode {.compileTime.} =
  ## Process a type such as ``Sym "float"`` or ``ObjectTy ...``.
  ##
  ## Sample ``ObjectTy``:
  ##
  ## .. code-block::plain
  ##     ObjectTy
  ##       Empty
  ##       InheritanceInformation
  ##       RecList
  ##         Sym "events"
  case obj.kind
  of nnkObjectTy, nnkTupleTy:
    # Create object constructor.
    result =
      if obj.kind == nnkObjectTy: newNimNode(nnkObjConstr)
      else: newNimNode(nnkPar)

    if obj.kind == nnkObjectTy:
      result.add(typeName) # Name of the type to construct.

    # Process each object/tuple field and add it as an exprColonExpr
    result.add(processFields(obj, jsonNode))

    # Object might be null. So we need to check for that.
    if isRef:
      result = quote do:
        verifyJsonKind(`jsonNode`, {JObject, JNull}, astToStr(`jsonNode`))
        if `jsonNode`.kind == JNull:
          nil
        else:
          `result`
    else:
      result = quote do:
        verifyJsonKind(`jsonNode`, {JObject}, astToStr(`jsonNode`));
        `result`

  of nnkEnumTy:
    let instType = toIdentNode(getTypeInst(typeName))
    let getEnumCall = createGetEnumCall(jsonNode, instType)
    result = quote do:
      (
        `getEnumCall`
      )
  of nnkSym:
    let name = ($typeName).normalize
    case name
    of "string":
      result = quote do:
        (
          verifyJsonKind(`jsonNode`, {JString, JNull}, astToStr(`jsonNode`));
          if `jsonNode`.kind == JNull: nil else: `jsonNode`.str
        )
    of "biggestint":
      result = quote do:
        (
          verifyJsonKind(`jsonNode`, {JInt}, astToStr(`jsonNode`));
          `jsonNode`.num
        )
    of "bool":
      result = quote do:
        (
          verifyJsonKind(`jsonNode`, {JBool}, astToStr(`jsonNode`));
          `jsonNode`.bval
        )
    else:
      if name.startsWith("int") or name.startsWith("uint"):
        result = quote do:
          (
            verifyJsonKind(`jsonNode`, {JInt}, astToStr(`jsonNode`));
            `jsonNode`.num.`obj`
          )
      elif name.startsWith("float"):
        result = quote do:
          (
            verifyJsonKind(`jsonNode`, {JInt, JFloat}, astToStr(`jsonNode`));
            if `jsonNode`.kind == JFloat: `jsonNode`.fnum.`obj` else: `jsonNode`.num.`obj`
          )
      else:
        doAssert false, "Unable to process nnkSym " & $typeName
  else:
    doAssert false, "Unable to process type: " & $obj.kind

  doAssert(not result.isNil(), "processType not initialised.")

import options
proc workaroundMacroNone[T](): Option[T] =
  none(T)

proc depth(n: NimNode, current = 0): int =
  result = 1
  for child in n:
    let d = 1 + child.depth(current + 1)
    if d > result:
      result = d

proc createConstructor(typeSym, jsonNode: NimNode): NimNode =
  ## Accepts a type description, i.e. "ref Type", "seq[Type]", "Type" etc.
  ##
  ## The ``jsonNode`` refers to the node variable that we are deserialising.
  ##
  ## Returns an object constructor node.
  # echo("--createConsuctor-- \n", treeRepr(typeSym))
  # echo()

  if depth(jsonNode) > 150:
    error("The `to` macro does not support ref objects with cycles.", jsonNode)

  case typeSym.kind
  of nnkBracketExpr:
    var bracketName = ($typeSym[0]).normalize
    case bracketName
    of "option":
      # TODO: Would be good to verify that this is Option[T] from
      # options module I suppose.
      let lenientJsonNode = transformJsonIndexer(jsonNode)

      let optionGeneric = typeSym[1]
      let value = createConstructor(typeSym[1], jsonNode)
      let workaround = bindSym("workaroundMacroNone") # TODO: Nim Bug: This shouldn't be necessary.

      result = quote do:
        (
          if `lenientJsonNode`.isNil or `jsonNode`.kind == JNull: `workaround`[`optionGeneric`]() else: some[`optionGeneric`](`value`)
        )
    of "table", "orderedtable":
      let tableKeyType = typeSym[1]
      if ($tableKeyType).cmpIgnoreStyle("string") != 0:
        error("JSON doesn't support keys of type " & $tableKeyType)
      let tableValueType = typeSym[2]

      let forLoopKey = genSym(nskForVar, "key")
      let indexerNode = createJsonIndexer(jsonNode, forLoopKey)
      let constructorNode = createConstructor(tableValueType, indexerNode)

      let tableInit =
        if bracketName == "table":
          bindSym("initTable")
        else:
          bindSym("initOrderedTable")

      # Create a statement expression containing a for loop.
      result = quote do:
        (
          var map = `tableInit`[`tableKeyType`, `tableValueType`]();
          verifyJsonKind(`jsonNode`, {JObject}, astToStr(`jsonNode`));
          for `forLoopKey` in keys(`jsonNode`.fields): map[`forLoopKey`] = `constructorNode`;
          map
        )
    of "ref":
      # Ref type.
      var typeName = $typeSym[1]
      # Remove the `:ObjectType` suffix.
      if typeName.endsWith(":ObjectType"):
        typeName = typeName[0 .. ^12]

      let obj = getType(typeSym[1])
      result = processType(newIdentNode(typeName), obj, jsonNode, true)
    of "seq":
      let seqT = typeSym[1]
      let forLoopI = genSym(nskForVar, "i")
      let indexerNode = createJsonIndexer(jsonNode, forLoopI)
      let constructorNode = createConstructor(seqT, indexerNode)

      # Create a statement expression containing a for loop.
      result = quote do:
        (
          var list: `typeSym` = @[];
          verifyJsonKind(`jsonNode`, {JArray}, astToStr(`jsonNode`));
          for `forLoopI` in 0 ..< `jsonNode`.len: list.add(`constructorNode`);
          list
        )
    of "array":
      let arrayT = typeSym[2]
      let forLoopI = genSym(nskForVar, "i")
      let indexerNode = createJsonIndexer(jsonNode, forLoopI)
      let constructorNode = createConstructor(arrayT, indexerNode)

      # Create a statement expression containing a for loop.
      result = quote do:
        (
          var list: `typeSym`;
          verifyJsonKind(`jsonNode`, {JArray}, astToStr(`jsonNode`));
          for `forLoopI` in 0 ..< `jsonNode`.len: list[`forLoopI`] =`constructorNode`;
          list
        )

    else:
      # Generic type.
      let obj = getType(typeSym)
      result = processType(typeSym, obj, jsonNode, false)
  of nnkSym:
    # Handle JsonNode.
    if ($typeSym).cmpIgnoreStyle("jsonnode") == 0:
      return jsonNode

    # Handle all other types.
    let obj = getType(typeSym)
    if obj.kind == nnkBracketExpr:
      # When `Sym "Foo"` turns out to be a `ref object`.
      result = createConstructor(obj, jsonNode)
    else:
      result = processType(typeSym, obj, jsonNode, false)
  of nnkTupleTy:
    result = processType(typeSym, typeSym, jsonNode, false)
  of nnkPar, nnkTupleConstr:
    # TODO: The fact that `jsonNode` here works to give a good line number
    # is weird. Specifying typeSym should work but doesn't.
    error("Use a named tuple instead of: " & $toStrLit(typeSym), jsonNode)
  else:
    doAssert false, "Unable to create constructor for: " & $typeSym.kind

  doAssert(not result.isNil(), "Constructor not initialised.")

proc postProcess(node: NimNode): NimNode
proc postProcessValue(value: NimNode): NimNode =
  ## Looks for object constructors and calls the ``postProcess`` procedure
  ## on them. Otherwise it just returns the node as-is.
  case value.kind
  of nnkObjConstr:
    result = postProcess(value)
  else:
    result = value
    for i in 0 ..< len(result):
      result[i] = postProcessValue(result[i])

proc postProcessExprColonExpr(exprColonExpr, resIdent: NimNode): NimNode =
  ## Transform each field mapping in the ExprColonExpr into a simple
  ## field assignment. Special processing is performed if the field mapping
  ## has an if statement.
  ##
  ## ..code-block::plain
  ##    field: (if true: 12)  ->  if true: `resIdent`.field = 12
  expectKind(exprColonExpr, nnkExprColonExpr)
  let fieldName = exprColonExpr[0]
  let fieldValue = exprColonExpr[1]
  case fieldValue.kind
  of nnkIfStmt:
    doAssert fieldValue.len == 1, "Cannot postProcess two ElifBranches."
    expectKind(fieldValue[0], nnkElifBranch)

    let cond = fieldValue[0][0]
    let bodyValue = postProcessValue(fieldValue[0][1])
    doAssert(bodyValue.kind != nnkNilLit)
    result =
      quote do:
        if `cond`:
          `resIdent`.`fieldName` = `bodyValue`
  else:
    let fieldValue = postProcessValue(fieldValue)
    doAssert(fieldValue.kind != nnkNilLit)
    result =
      quote do:
        `resIdent`.`fieldName` = `fieldValue`


proc postProcess(node: NimNode): NimNode =
  ## The ``createConstructor`` proc creates a ObjConstr node which contains
  ## if statements for fields that may not be assignable (due to an object
  ## variant). Nim doesn't handle this, but may do in the future.
  ##
  ## For simplicity, we post process the object constructor into multiple
  ## assignments.
  ##
  ## For example:
  ##
  ## ..code-block::plain
  ##    Object(                           (var res = Object();
  ##      field: if true: 12      ->       if true: res.field = 12;
  ##    )                                  res)
  result = newNimNode(nnkStmtListExpr)

  expectKind(node, nnkObjConstr)

  # Create the type.
  # -> var res = Object()
  var resIdent = genSym(nskVar, "res")
  # TODO: Placing `node[0]` inside quote is buggy
  var resType = toIdentNode(node[0])

  result.add(
    quote do:
      var `resIdent` = `resType`();
  )

  # Process each ExprColonExpr.
  for i in 1..<len(node):
    result.add postProcessExprColonExpr(node[i], resIdent)

  # Return the `res` variable.
  result.add(
    quote do:
      `resIdent`
  )


macro to*(node: JsonNode, T: typedesc): untyped =
  ## `Unmarshals`:idx: the specified node into the object type specified.
  ##
  ## Known limitations:
  ##
  ##   * Heterogeneous arrays are not supported.
  ##   * Sets in object variants are not supported.
  ##   * Not nil annotations are not supported.
  ##
  ## Example:
  ##
  ## .. code-block:: Nim
  ##     let jsonNode = parseJson("""
  ##        {
  ##          "person": {
  ##            "name": "Nimmer",
  ##            "age": 21
  ##          },
  ##          "list": [1, 2, 3, 4]
  ##        }
  ##     """)
  ##
  ##     type
  ##       Person = object
  ##         name: string
  ##         age: int
  ##
  ##       Data = object
  ##         person: Person
  ##         list: seq[int]
  ##
  ##     var data = to(jsonNode, Data)
  ##     doAssert data.person.name == "Nimmer"
  ##     doAssert data.person.age == 21
  ##     doAssert data.list == @[1, 2, 3, 4]

  let typeNode = getTypeInst(T)
  expectKind(typeNode, nnkBracketExpr)
  doAssert(($typeNode[0]).normalize == "typedesc")

  # Create `temp` variable to store the result in case the user calls this
  # on `parseJson` (see bug #6604).
  result = newNimNode(nnkStmtListExpr)
  let temp = genSym(nskLet, "temp")
  result.add quote do:
    let `temp` = `node`

  let constructor = createConstructor(typeNode[1], temp)
  # TODO: Rename postProcessValue and move it (?)
  result.add(postProcessValue(constructor))

  # echo(treeRepr(result))
  # echo(toStrLit(result))

when false:
  import os
  var s = newFileStream(paramStr(1), fmRead)
  if s == nil: quit("cannot open the file" & paramStr(1))
  var x: JsonParser
  open(x, s, paramStr(1))
  while true:
    next(x)
    case x.kind
    of jsonError:
      Echo(x.errorMsg())
      break
    of jsonEof: break
    of jsonString, jsonInt, jsonFloat: echo(x.str)
    of jsonTrue: echo("!TRUE")
    of jsonFalse: echo("!FALSE")
    of jsonNull: echo("!NULL")
    of jsonObjectStart: echo("{")
    of jsonObjectEnd: echo("}")
    of jsonArrayStart: echo("[")
    of jsonArrayEnd: echo("]")

  close(x)

# { "json": 5 }
# To get that we shall use, obj["json"]

when isMainModule:
  # Note: Macro tests are in tests/stdlib/tjsonmacro.nim

  let testJson = parseJson"""{ "a": [1, 2, 3, 4], "b": "asd", "c": "\ud83c\udf83", "d": "\u00E6"}"""
  # nil passthrough
  doAssert(testJson{"doesnt_exist"}{"anything"}.isNil)
  testJson{["e", "f"]} = %true
  doAssert(testJson["e"]["f"].bval)

  # make sure UTF-16 decoding works.
  doAssert(testJson["c"].str == "🎃")
  doAssert(testJson["d"].str == "æ")

  # make sure no memory leek when parsing invalid string
  let startMemory = getOccupiedMem()
  for i in 0 .. 10000:
    try:
      discard parseJson"""{ invalid"""
    except:
      discard
  # memory diff should less than 4M
  doAssert(abs(getOccupiedMem() - startMemory) < 4 * 1024 * 1024)


  # test `$`
  let stringified = $testJson
  let parsedAgain = parseJson(stringified)
  doAssert(parsedAgain["b"].str == "asd")

  parsedAgain["abc"] = %5
  doAssert parsedAgain["abc"].num == 5

  # Bounds checking
  try:
    let a = testJson["a"][9]
    doAssert(false, "EInvalidIndex not thrown")
  except IndexError:
    discard
  try:
    let a = testJson["a"][-1]
    doAssert(false, "EInvalidIndex not thrown")
  except IndexError:
    discard
  try:
    doAssert(testJson["a"][0].num == 1, "Index doesn't correspond to its value")
  except:
    doAssert(false, "EInvalidIndex thrown for valid index")

  doAssert(testJson{"b"}.str=="asd", "Couldn't fetch a singly nested key with {}")
  doAssert(isNil(testJson{"nonexistent"}), "Non-existent keys should return nil")
  doAssert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil")
  doAssert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil")
  doAssert(testJson{"a"}==parseJson"[1, 2, 3, 4]", "Didn't return a non-JObject when there was one to be found")
  doAssert(isNil(parseJson("[1, 2, 3]"){"foo"}), "Indexing directly into a list should return nil")

  # Generator:
  var j = %* [{"name": "John", "age": 30}, {"name": "Susan", "age": 31}]
  doAssert j == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]

  var j2 = %*
    [
      {
        "name": "John",
        "age": 30
      },
      {
        "name": "Susan",
        "age": 31
      }
    ]
  doAssert j2 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]

  var name = "John"
  let herAge = 30
  const hisAge = 31

  var j3 = %*
    [ { "name": "John"
      , "age": herAge
      }
    , { "name": "Susan"
      , "age": hisAge
      }
    ]
  doAssert j3 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]

  var j4 = %*{"test": nil}
  doAssert j4 == %{"test": newJNull()}

  let seqOfNodes = @[%1, %2]
  let jSeqOfNodes = %seqOfNodes
  doAssert(jSeqOfNodes[1].num == 2)

  type MyObj = object
    a, b: int
    s: string
    f32: float32
    f64: float64
    next: ref MyObj
  var m: MyObj
  m.s = "hi"
  m.a = 5
  let jMyObj = %m
  doAssert(jMyObj["a"].num == 5)
  doAssert(jMyObj["s"].str == "hi")

  # Test loading of file.
  when not defined(js):
    echo("99% of tests finished. Going to try loading file.")
    var parsed = parseFile("tests/testdata/jsontest.json")

    try:
      discard parsed["key2"][12123]
      doAssert(false)
    except IndexError: doAssert(true)

    var parsed2 = parseFile("tests/testdata/jsontest2.json")
    doAssert(parsed2{"repository", "description"}.str=="IRC Library for Haskell", "Couldn't fetch via multiply nested key using {}")

  doAssert escapeJson("\10Foo🎃barÄ") == "\"\\nFoo🎃barÄ\""

  # Test with extra data
  when not defined(js):
    try:
      discard parseJson("123 456")
      doAssert(false)
    except JsonParsingError:
      doAssert getCurrentExceptionMsg().contains(errorMessages[errEofExpected])

    try:
      discard parseFile("tests/testdata/jsonwithextradata.json")
      doAssert(false)
    except JsonParsingError:
      doAssert getCurrentExceptionMsg().contains(errorMessages[errEofExpected])

  # bug #6438
  doAssert($ %*[] == "[]")
  doAssert($ %*{} == "{}")

  echo("Tests succeeded!")