1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
|
---
layout: base
title: Specification
---
<style>
/* Non-terminals */
i {
font-family: Arial, sans-serif;
font-style: italic;
color: #008
}
/* Value sets */
em {
font-family: Arial, sans-serif;
font-style: italic;
color: #800
}
code {
font-family: Courier New, monospace;
background-color: #efefef;
font-size: 15px;
}
div.desugar-rule {
font-size:75%;
}
div.desugar-rule-group {
padding: 3px 0;
}
div.sequent-rule {
margin:10px;
float:left;
font-size:75%;
}
div.rules {
overflow: auto;
width:100%
}
</style>
<div style="display:none">
\[
\newcommand{\assert}[1]{\texttt{assert }#1}
\newcommand{\array}[1]{[ #1 ]}
\newcommand{\assign}[2]{ #1\texttt{ = }#2}
\newcommand{\binary}[3]{ #1\texttt{ }#2\texttt{ }#3}
\newcommand{\error}[1]{\texttt{error }#1}
\newcommand{\false}{\texttt{false}}
\newcommand{\function}[2]{\texttt{function}\texttt{(} #1\texttt{)}{\ #2}}
\newcommand{\if}[3]{\texttt{if }#1\texttt{ then }#2\texttt{ else }#3}
\newcommand{\ifnoelse}[2]{\texttt{if }#1\texttt{ then }#2}
\newcommand{\import}[1]{\texttt{import }#1}
\newcommand{\importstr}[1]{\texttt{importstr }#1}
\newcommand{\index}[2]{#1\texttt{[}#2\texttt{]}}
\newcommand{\local}[2]{\texttt{local }#1\texttt{ ; }#2}
\newcommand{\null}{\texttt{null}}
\newcommand{\object}[1]{\{ #1 \}}
\newcommand{\objlocal}[1]{\texttt{local }#1}
\newcommand{\ocomp}[4]{\object{[#1]:#2\texttt{ for }#3\texttt{ in }#4}}
\newcommand{\self}{\texttt{self}}
\newcommand{\super}{\texttt{super}}
\newcommand{\true}{\texttt{true}}
\newcommand{\unary}[2]{\texttt{ }#1\texttt{ }#2}
\newcommand{\rule}[3]{\frac{#2}{#3}\textrm{(#1)}}
\]
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h1 id="specification">Jsonnet Specification</h1>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
This page is the authority on what Jsonnet programs should do. It defines Jsonnet lexing
and syntax. It describes which programs should be rejected statically (i.e. before
execution). Finally, it specifies the manner in which the program is executed, i.e. the
JSON that is output, or the runtime error if there is one.
</p>
<p>
The specification is intended to be terse, precise, and illuminate all the subtleties and
edge cases in order to allow fully-compatible language reimplementations and tools. The
specification employs some standard theoretical computer science techniques, namely <a
href="http://en.wikipedia.org/wiki/Type_systems">type systems</a> and <a
href="http://en.wikipedia.org/wiki/Operational_semantics">big step operational
semantics</a>. If you just want to write Jsonnet code (not build a Jsonnet interpreter or
tool), you don't need to read this. You should read the <a
href="/learning/tutorial.html">tutorial</a> and <a href="/ref/language.html">reference</a> .
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h2 id="lexing">Lexing</h2>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
A Jsonnet program is UTF-8 encoded text. The file is a sequence of tokens, separated by
optional whitespace and comments. Whitespace consists of space, tab, newline and carriage
return. Tokens are lexed greedily. Comments are either single line comments, beginning
with a <code>#</code> or a <code>//</code>, or block comments beginning with <code>/*</code>
and terminating at the first <code>*/</code> encountered within the comment.
</p>
<ul>
<li>
<p>
<i>id</i>: Matched by <tt>[_a-zA-Z][_a-zA-Z0-9]*</tt>.
</p>
<p>
Some identifiers are reserved as keywords, thus are not in the set <i>id</i>:
<code>assert</code> <code>else</code> <code>error</code> <code>false</code>
<code>for</code> <code>function</code> <code>if</code> <code>import</code>
<code>importstr</code> <code>in</code> <code>local</code> <code>null</code>
<code>tailstrict</code> <code>then</code> <code>self</code> <code>super</code>
<code>true</code>.
</p>
</li>
<li>
<p>
<i>number</i>: As defined by <a href="http://json.org/">JSON</a> but without the leading
minus.
</p>
</li>
<li>
<p>
<i>string</i>: Which can have five quoting forms:
</p>
<ul>
<li>
Double-quoted, beginning with <code>"</code> and ending with the first subsequent
non-quoted <code>"</code>
</li>
<li>
Single-quoted, beginning with <code>'</code> and ending with the first subsequent
non-quoted <code>'</code>
</li>
<li>
Double-quoted verbatim, beginning with <code>@"</code> and ending with the first
subsequent non-quoted <code>"</code>
</li>
<li>
Single-quoted verbatim, beginning with <code>@'</code> and ending with the first
subsequent non-quoted <code>'</code>
</li>
<li>
Text block, beginning with <code>|||</code>, followed by optional whitespace and a
new-line. The next non-blank line must be prefixed with some non-zero length whitespace
<i>W</i>. The block ends at the first subsequent line that does not begin with
<i>W</i>, and it is an error if this line does not contain some optional whitespace
followed by <code>|||</code>. The content of the string is the concatenation of all
the lines that began with <i>W</i> but with that prefix stripped. The line ending
style in the file is preserved in the string. This form cannot be used in
<code>import</code> statements.
</li>
</ul>
<p>
Double- and single-quoted strings are allowed to span multiple lines, in which case
whatever dos/unix end-of-line character your editor inserts will be put in the string.
They both understand the following escape characters: <code>"'\/bfnrt</code> which have
their standard meanings, as well as <code>\uXXXX</code> for hexadecimal unicode escapes.
</p>
<p>
Verbatim strings eschew all of the normal string escaping, including hexidecimal unicode
escapes. Every character in a verbatim string is processed literally, with the
exception of doubled end-quotes. Within a verbatim single-quoted string,
<code>''</code> is processed as <code>'</code>, and a verbatim double-quoted string,
<code>""</code> is processed as <code>"</code>.
</p>
<p>
In the rest of this specification, the string is assumed to be canonicalized into a
sequence of unicode codepoints with no record of the original quoting form as well and
any escape characters removed.
</p>
</li>
<li>
<p>
<i>symbol</i>: The following single-character symbols:
</p>
<p><code>{}[],.();</code></p>
</li>
<li>
<p>
<i>operator</i>: A sequence of at least one of the following single-character symbols:
<code>!$:~+-&|^=<>*/%</code>.
</p>
<p>
Additionally it is subject to the following rules, which may cause the lexing to terminate with a shorter
token:
</p>
<ul>
<li>
The sequence <code>//</code> is not allowed in an operator.
</li>
<li>
The sequence <code>/*</code> is not allowed in an operator.
</li>
<li>
The sequence <code>|||</code> is not allowed in an operator.
</li>
<li>
If the sequence has more than one character, it is not allowed to end in any of
<code>+</code>, <code>-</code>, <code>~</code>, <code>!</code>, <code>$</code>.
</li>
</ul>
</li>
</ul>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h2 id="abstract_syntax">Abstract Syntax</h2>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
The notation used here is as follows: { } denotes zero or more repetitions of a sequence of
tokens, and [ ] represents an optional sequence of tokens. This is not to be confused with
<code>{ }</code> and <code>[ ]</code> which represent tokens in Jsonnet itself.
</p>
<p>
Note that although the lexer will generate tokens for a wide range of operators, only a
finite set are currently parseable, the rest being reserved for possible future use.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="inverse hgroup">
<div class="hgroup-inline">
<div class="panel wide">
<table>
<tr>
<td><i>expr ∈ Expr</i></td>
<td>::=</td>
<td>
<code>null</code> |
<code>true</code> |
<code>false</code> |
<code>self</code> |
<code>$</code> |
<i>string</i> |
<i>number</i>
</td>
</tr>
<!-- object -->
<tr>
<td></td>
<td> | </td>
<td>
<code>{</code>
<i>objinside</i>
<code>}</code>
</td>
</tr>
<!-- array -->
<tr>
<td></td>
<td>|</td>
<td>
<code>[</code>
[ <i>expr</i> { <code>,</code> <i>expr</i> } [ <code>,</code> ] ]
<code>]</code>
</td>
</tr>
<!-- array comprehension -->
<tr>
<td></td>
<td> | </td>
<td>
<code>[</code>
<i>expr</i>
[ <code>,</code> ]
<i>forspec</i> <i>compspec</i>
<code>]</code>
</td>
</tr>
<!-- object index (id) -->
<tr>
<td></td>
<td> | </td>
<td>
<i>expr</i>
<code>.</code>
<i>id</i>
</td>
</tr>
<!-- object index (general) -->
<tr>
<td></td>
<td> | </td>
<td>
<i>expr</i>
<code>[</code>
[ <i>expr</i> ]
[ <code>:</code> [ <i> expr</i> ]
[ <code>:</code> [ <i>expr</i> ] ] ]
<code>]</code>
</td>
</tr>
<!-- super index (id) -->
<tr>
<td></td>
<td> | </td>
<td>
<code>super</code>
<code>.</code>
<i>id</i>
</td>
</tr>
<!-- super index (general) -->
<tr>
<td></td>
<td> | </td>
<td>
<code>super</code>
<code>[</code>
<i>expr</i>
<code>]</code>
</td>
</tr>
<!-- apply -->
<tr>
<td></td>
<td> | </td>
<td>
<i>expr</i>
<code>(</code>
[ <i>args</i> ]
<code>)</code>
</td>
</tr>
<!-- var -->
<tr>
<td></td>
<td> | </td>
<td>
<i>id</i>
</td>
</tr>
<!-- local expression -->
<tr>
<td></td>
<td> | </td>
<td>
<code>local</code>
<i>bind</i>
{
<code>,</code>
<i>bind</i>
}
<code>;</code>
<i>expr</i>
</td>
</tr>
<!-- conditional -->
<tr>
<td></td>
<td> | </td>
<td>
<code>if</code>
<i>expr</i>
<code>then</code>
<i>expr</i>
[ <code>else</code>
<i>expr</i> ]
</td>
</tr>
<!-- binary -->
<tr>
<td></td>
<td> | </td>
<td>
<i>expr</i>
<i>binaryop</i>
<i>expr</i>
</td>
</tr>
<!-- unary -->
<tr>
<td></td>
<td> | </td>
<td>
<i>unaryop</i>
<i>expr</i>
</td>
</tr>
<!-- object apply -->
<tr>
<td></td>
<td> | </td>
<td>
<i>expr</i>
<code>{</code>
<i>objinside</i>
<code>}</code>
</td>
</tr>
<!-- function -->
<tr>
<td></td>
<td> | </td>
<td>
<code>function</code>
<code>(</code>
[ <i>params</i> ]
<code>)</code>
<i>expr</i>
</td>
</tr>
<!-- assert -->
<tr>
<td></td>
<td> | </td>
<td>
<i>assert</i>
<code>;</code>
<i>expr</i>
</td>
</tr>
<!-- import -->
<tr>
<td></td>
<td> | </td>
<td>
<code>import</code>
<i>string</i>
</td>
</tr>
<!-- importstr -->
<tr>
<td></td>
<td> | </td>
<td>
<code>importstr</code>
<i>string</i>
</td>
</tr>
<!-- error -->
<tr>
<td></td>
<td> | </td>
<td>
<code>error</code>
<i>expr</i>
</td>
</tr>
<!-- super in e -->
<tr>
<td></td>
<td> | </td>
<td>
<i>expr</i> <code>in</code> <code>super</code>
</td>
</tr>
</table>
<p></p>
<!-- AUX STUFF -->
<table>
<tr>
<td><i>objinside</i></td>
<td>::=</td>
<td>
<!-- object -->
<i>member</i> { <code>,</code> <i>member</i> } [ <code>,</code> ]
</td>
</tr>
<!-- object comprehension -->
<tr>
<td></td>
<td> | </td>
<td>
{ <i>objlocal</i> <code>,</code> }
<code>[</code>
<i>expr</i>
<code>]</code>
<code>:</code>
<i>expr</i>
[ { <code>,</code> <i>objlocal</i> } ] [ <code>,</code> ]
<i>forspec</i> <i>compspec</i>
</td>
</tr>
<!-- member -->
<tr>
<td><i>member</i></td>
<td>::=</td>
<td>
<i>objlocal</i> | <i>assert</i> | <i>field</i>
</td>
</tr>
<!-- field -->
<tr>
<td><i>field ∈ Field</i></td>
<td>::=</td>
<td>
<i>fieldname</i>
[ <code>+</code> ] <i>h</i>
<i>expr</i>
</td>
</tr>
<!-- method -->
<tr>
<td></td>
<td> | </td>
<td>
<i>fieldname</i>
<code>(</code>
[ <i>params</i> ]
<code>)</code>
<i>h</i>
<i>expr</i>
</td>
</tr>
<!-- colons -->
<tr>
<td><i>h ∈ Hidden</i></td>
<td>::=</td>
<td>
<code>:</code> | <code>::</code> | <code>:::</code>
</td>
</tr>
<!-- obj local -->
<tr>
<td><i>objlocal</i></td>
<td>::=</td>
<td>
<code>local</code> <i>bind</i>
</td>
</tr>
<!-- compspec (object comps) -->
<tr>
<td><i>compspec ∈ CompSpec</i></td>
<td>::=</td>
<td>
{ <i>forspec</i> | <i>ifspec</i> }
</td>
</tr>
<!-- for spec (object comps) -->
<tr>
<td><i>forspec</i></td>
<td>::=</td>
<td>
<code>for</code>
<i>id</i>
<code>in</code>
<i>expr</i>
</td>
</tr>
<!-- if spec (object comps) -->
<tr>
<td><i>ifspec</i></td>
<td>::=</td>
<td>
<code>if</code>
<i>expr</i>
</td>
</tr>
<!-- fieldname -->
<tr>
<td><i>fieldname</i></td>
<td>::=</td>
<td>
<i>id</i> |
<i>string</i> |
<code>[</code>
<i>expr</i>
<code>]</code>
</td>
</tr>
</table>
<p></p>
<!-- obj assert -->
<table>
<tr>
<td><i>assert</i></td>
<td>::=</td>
<td>
<code>assert</code> <i>expr</i> [ <code>:</code> <i>expr</i> ]
</td>
</tr>
</table>
<!-- bind -->
<table>
<tr>
<td><i>bind ∈ Bind</i></td>
<td>::=</td>
<td>
<i>id</i>
<code>=</code>
<i>expr</i>
</td>
</tr>
<tr>
<td></td>
<td> | </td>
<td>
<i>id</i>
<code>(</code>
[ <i>params</i> ]
<code>)</code>
<code>=</code>
<i>expr</i>
</td>
</tr>
</table>
<p></p>
<!-- args -->
<table>
<tr>
<td><i>args</i></td>
<td>::=</td>
<td>
<i>expr</i> { <code>,</code> <i>expr</i> } { <code>,</code> <i>id</i> <code>=</code>
<i>expr</i> } [ <code>,</code> ]
</td>
</tr>
<tr>
<td></td>
<td>|</td>
<td>
<i>id</i> <code>=</code> <i>expr</i> { <code>,</code> <i>id</i> <code>=</code>
<i>expr</i> } [ <code>,</code> ]
</td>
</tr>
</table>
<p></p>
<!-- params -->
<table>
<tr>
<td><i>params</i></td>
<td>::=</td>
<td>
<i>param</i> { <code>,</code> <i>param</i> } [ <code>,</code> ]
</td>
</tr>
</table>
<!-- param -->
<table>
<tr>
<td><i>param</i></td>
<td>::=</td>
<td>
<i>id</i> [ <code>=</code> <i>expr</i> ]
</td>
</tr>
</table>
<p></p>
<!-- binaryop -->
<table>
<tr>
<td><i>binaryop</i></td>
<td>::=</td>
<td>
<code>*</code> |
<code>/</code> |
<code>%</code> |
<code>+</code> |
<code>-</code> |
<code><<</code> |
<code>>></code> |
<code><</code> |
<code><=</code> |
<code>></code> |
<code>>=</code> |
<code>==</code> |
<code>!=</code> |
<code>in</code> |
<code>&</code> |
<code>^</code> |
<code>|</code> |
<code>&&</code> |
<code>||</code>
</td>
</tr>
<!-- unaryop -->
<tr>
<td><i>unaryop</i></td>
<td>::=</td>
<td> <code>-</code> | <code>+</code> | <code>!</code> | <code>~</code> </td>
</tr>
</table>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h2 id="associativity_precedence">Associativity and Operator Precedence</h2>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
The abstract syntax by itself cannot unambiguously parse a sequence of tokens. Ambiguities
are resolved according to the following rules, which can also be overridden by adding
parenthesis symbols <code>()</code>.
</p>
<p>
Everything is left associative. In the case of <code>assert</code>, <code>error</code>,
<code>function</code>, <code>if</code>, <code>import</code>, <code>importstr</code>, and
<code>local</code>, ambiguity is resolved by consuming as many tokens as possible on the
right hand side. For example the parentheses are redundant in <code>local x = 1; (x +
x)</code>. All remaining ambiguities are resolved according to the following decreasing
order of precedence:
</p>
<ol>
<li><code>e(...)</code> <code>e[...]</code> <code>e.f</code>
(application and indexing)</li>
<li><code>+</code> <code>-</code> <code>!</code> <code>~</code>
(the unary operators)</li>
<li><code>*</code> <code>/</code> <code>%</code>
(these, and the remainder below, are binary operators)</li>
<li><code>+</code> <code>-</code></li>
<li><code><<</code> <code>>></code></li>
<li>
<code><</code> <code>></code> <code><=</code> <code>>=</code> <code>in</code>
</li>
<li><code>==</code> <code>!=</code></li>
<li><code>&</code></li>
<li><code>^</code></li>
<li><code>|</code></li>
<li><code>&&</code></li>
<li><code>||</code></li>
</ol>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h2 id="core">Core Language Subset</h2>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
To make the specification of Jsonnet as simple as possible, many of the language features
are represented as syntax sugar. Below is defined the core syntax and the desugaring
function from the abstract syntax to the core syntax. Both the static checking rules and
the operational semantics are defined at the level of the core language, so it is possible
to desugar immediately after parsing.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h3 id="core_syntax">Core Syntax</h3>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
The core language has the following simplifications:
</p>
<ul>
<li>
The set of identifiers now includes <code>$</code>, which is no-longer a special keyword.
</li>
<li>
The following binary operators are removed: <code>!=</code> <code>==</code> <code>%</code>
<code>in</code>
</li>
<li>Array slices <code>[::]</code> are removed.</li>
<li>
Array and object comprehensions are replaced with a simple object comprehension construct.
</li>
<li>Expression-level asserts are removed.</li>
<li>Object-level level assert messages are removed.</li>
<li>
Object-level level assert values are ignored, but their evaluation may still raise an
error.
</li>
<li>Object methods and local functions are removed.</li>
<li>Object-level locals are removed.</li>
<li>Object field name definitions can only be expressions.</li>
<li>The <code>+:</code>, <code>+::</code>, and <code>+:::</code> sugars are removed.</li>
<li>Field lookup is only possible through <code>e[e]</code>.</li>
<li>All conditionals must have an else branch.</li>
<li>The keyword <code>super</code> can exist on its own.</li>
</ul>
<p>
Commas are no-longer part of this abstract syntax but we may still write them in our
notation to make the presentation more clear.
</p>
<p>
Also removed in the core language are <code>import</code> and <code>importstr</code>. The
semantics of these constructs is that they are replaced with either the contents of the
file, or an error construct if importing failed (e.g. due to I/O errors). In the first
case, the file is parsed, desugared, and subject to static checking before it can be
substituted. In the latter case, the file is substituted in the form of a string, so it
merely needs to contain valid UTF-8.
</p>
<p>
A given Jsonnet file can be recursively imported via <code>import</code>. Thus, the
implementation loads files lazily (i.e. during execution) as opposed to via static
desugaring. The imported Jsonnet file is parsed and statically checked in isolation.
Therefore, the behavior of the import is not affected by the environment into which it is
imported. The files are cached by filename, so that even if the file changes on disk during
Jsonnet execution, referential transparency is maintained.
</p>
<table>
<tr>
<td><i>e ∈ Core</i></td>
<td>::=</td>
<td>
<code>null</code> |
<code>true</code> |
<code>false</code> |
<code>self</code> |
<code>super</code> |
<i>string</i> |
<i>number</i>
</td>
</tr>
<!-- core objects -->
<tr>
<td></td>
<td> | </td>
<td>
<code>{</code>
{
<code>assert</code> <i>e</i>
}
{
<code>[</code> <i>e</i> <code>]</code> <i>h</i> <i>e</i>
}
<code>}</code>
</td>
</tr>
<!-- core object comprehension -->
<tr>
<td></td>
<td> | </td>
<td>
<code>{</code>
<code>[</code> <i>e</i> <code>]</code> <code>:</code> <i>e</i>
<code>for</code> <i>id</i> <code>in</code> <i>e</i>
<code>}</code>
</td>
</tr>
<!-- core array -->
<tr>
<td></td>
<td> | </td>
<td>
<code>[</code>
{ <i>e</i> }
<code>]</code>
</td>
</tr>
<!-- core index -->
<tr>
<td></td>
<td> | </td>
<td>
<i>e</i>
<code>[</code>
<i>e</i>
<code>]</code>
</td>
</tr>
<!-- core apply -->
<tr>
<td></td>
<td> | </td>
<td>
<i>e</i>
<code>(</code>
{ <i>e</i> }
{ <i>id</i> <code>=</code> <i>e</i> }
<code>)</code>
</td>
</tr>
<!-- core var -->
<tr>
<td></td>
<td> | </td>
<td>
<i>id</i>
</td>
</tr>
<!-- core local -->
<tr>
<td></td>
<td> | </td>
<td>
<code>local</code>
<i>id</i> <code>=</code> <i>e</i>
{
<i>id</i> <code>=</code> <i>e</i>
}
<code>;</code>
<i>e</i>
</td>
</tr>
<!-- core conditional -->
<tr>
<td></td>
<td> | </td>
<td>
<code>if</code>
<i>e</i>
<code>then</code>
<i>e</i>
<code>else</code>
<i>e</i>
</td>
</tr>
<!-- core unary op -->
<tr>
<td></td>
<td> | </td>
<td>
<i>e</i>
<i>binaryop</i>
<i>e</i>
</td>
</tr>
<!-- core binary op -->
<tr>
<td></td>
<td> | </td>
<td>
<i>unaryop</i>
<i>e</i>
</td>
</tr>
<!-- core closure -->
<tr>
<td></td>
<td> | </td>
<td>
<code>function</code>
<code>(</code>
{ <i>id</i> <code>=</code> <i>e</i> }
<code>)</code>
<i>e</i>
</td>
</tr>
<!-- core error -->
<tr>
<td></td>
<td> | </td>
<td>
<code>error</code>
<i>e</i>
</td>
</tr>
</table>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h3 id="desugaring">Desugaring</h3>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
Desugaring removes constructs that are not in the core language by replacing them with
constructs that are. It is defined via the following functions, which proceed by
syntax-directed recursion. If a function is not defined on a construct then it simply
recurses into the sub-expressions of that construct. Note that we import the standard
library at the top of every file, and some of the desugarings call functions defined in the
standard library. Their behavior is specified by implementation. However not all standard
library functions are written in Jsonnet. The ones that are built into the interpreter
(e.g. reflection) will be given special operational semantics rules with the rest of the
core language constructs.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="inverse hgroup">
<div class="hgroup-inline">
<div class="panel wide">
<div class="desugar-rule-group">
<p>
<i>desugar</i>: Expr → Core. This desugars a Jsonnet file. Let \(e_{std}\) be the parsed
content of <a href="https://github.com/google/jsonnet/blob/master/stdlib/std.jsonnet">
<tt>std.jsonnet</tt></a>.
</p>
<div class="desugar-rule">
\[
desugar(e) = desugar_{expr}(\local{\texttt{std} = e_{std}}{e}, false)
\]
</div>
</div>
<div class="desugar-rule-group">
<p>
<i>desugar<sub>expr</sub></i>: (Expr × Boolean) → Core: This desugars an expression. The
second parameter of the function tracks whether we are within an object.
</p>
<div class="desugar-rule">
\[
desugar_{expr}(\{
\objlocal{bind_1} \ldots \objlocal{bind_n},
assert_1 \ldots assert_m, field_1 \ldots field_p
\}, b) = \\
\hspace{10mm}
\textrm{let }binds = \left\{\begin{array}{ll}
bind_1 \ldots bind_n & \textrm{if }b \\
bind_1 \ldots bind_n, \objlocal{$ = \self} & \textrm{otherwise} \\
\end{array}\right. \\
\hspace{10mm}
\textrm{let } obj = \{ \\
\hspace{20mm} desugar_{assert}(assert_1, binds)
\ldots desugar_{assert}(assert_m, binds), \\
\hspace{20mm} desugar_{field}(field_1, binds)
\ldots desugar_{field}(field_p, binds, b) \\
\hspace{10mm} \} \\
\hspace{10mm} \textrm{in } \left\{\begin{array}{ll}
\local{\texttt{\$outerself} = \self, \texttt{\$outersuper} = \super} {obj}
& \textrm{if }b \\
obj & \textrm{otherwise} \\
\end{array}\right. \\
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(\object{
\objlocal{bind_1} \ldots \objlocal{bind_m},
[e_f] : e_{body},
\objlocal{bind_m+1} \ldots \objlocal{bind_n}
forspec\ compspec
}, b) = \\
\hspace{10mm} \textrm{Let } arr \textrm{ fresh and } x_1 \ldots x_n
\textrm{ be the sequence of variables defined in }forspec\ compspec \\
\hspace{10mm} \object{ \\
\hspace{20mm} [desugar_{expr}(\local{x_1=arr[0] \ldots x_n=arr[n-1]}{e_f}, b)]: \\
\hspace{30mm} desugar_{expr}(
\local{x_1=arr[0] \ldots x_n=arr[n-1]}{
\local{bind_1 \ldots bind_n}{e_{body}}
}, true) \\
\hspace{20mm} \textrm{ for } arr \textrm{ in }
desugar_{expr}([ [x_1 \ldots x_n] forspec\ compspec, b): \\
\hspace{10mm}}
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}([e\ forspec\ compspec], b) = desugar_{arrcomp}(e, forspec\ compspec, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(\local {bind_1 \ldots bind_n} e, b) =
\local {desugar_{bind}(bind_1, b) \ldots
desugar_{bind}(bind_n, b)} {desugar_{expr}(e, b)}
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e \{ objinside \}, b) = desugar_{expr}(e + \{ objinside \}, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(\function{p_1 \ldots p_n}e, b) = \function{desugar_{param}(p_1, b)\ldots desugar_{param}(p_n, b)} desugar_{expr}(e, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(\assert e ; e', b) =
desugar_{expr}(\assert e : \texttt{"Assertion failed"} ; e', b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(\assert e : e' ; e'', b) = desugar_{expr}(\if {e} {e''} {\error{e'}}, b)
\]
</div>
<!-- 1 slice default -->
<div class="desugar-rule">
\[
desugar_{expr}(e[e':e'':], b) = desugar_{expr}(e[e':e'':\null], b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e[e':e''], b) = desugar_{expr}(e[e':e'':\null], b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e[e'::e'''], b) = desugar_{expr}(e[e':\null:e'''], b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e[:e'':e'''], b) = desugar_{expr}(e[\null:e'':e'''], b)
\]
</div>
<!-- 2 slice defaults -->
<div class="desugar-rule">
\[
desugar_{expr}(e[e':], b) = desugar_{expr}(e[e':\null:\null], b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e[:e'':], b) = desugar_{expr}(e[\null:e'':\null], b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e[:e''], b) = desugar_{expr}(e[\null:e'':\null], b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e[::e'''], b) = desugar_{expr}(e[\null:\null:e'''], b)
\]
</div>
<!-- 3 slice defaults -->
<div class="desugar-rule">
\[
desugar_{expr}(e[::], b) = desugar_{expr}(e[\null:\null:\null], b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e[:], b) = desugar_{expr}(e[\null:\null:\null], b)
\]
</div>
<!-- slice -->
<div class="desugar-rule">
\[
desugar_{expr}(e[e':e'':e'''], b) =
desugar_{expr}(\texttt{std.slice}(e, e', e'', e'''), b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(\ifnoelse e e'), b) =
\if {desugar_{expr}(e, b)} {desugar_{expr}(e', b)} {\null}
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e.id, b) = desugar_{expr}(e, b)[\texttt{"}id\texttt{"}]
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(\super.id, b) = \super[\texttt{"}id\texttt{"}]
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e \mathop{\textrm{!=}} e', b) = desugar_{expr}(!(e \mathop{==} e'), b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e \mathop{==} e', b) = desugar_{expr}(\texttt{std.equals}(e, e'), b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e \mathop{\%} e', b) = desugar_{expr}(\texttt{std.mod}(e, e'), b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e \mathop{\texttt{in}} e', b) =
desugar_{expr}(\texttt{std.objectHasEx}(e', e, \texttt{true}), b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{expr}(e \mathop{\texttt{in}} \texttt{super}, b) =
desugar_{expr}(\texttt{std.objectHasEx}(\texttt{super}, e, \texttt{true}), b)
\]
</div>
</div>
<div class="desugar-rule-group">
<p>
<i>desugar<sub>assert</sub></i>: (Field × [Bind]) → Field. This desugars object
assertions.
</p>
<div class="desugar-rule">
\[
desugar_{assert}(\assert e, binds) =
desugar_{assert}(\assert e : \texttt{"Assertion failed"}, binds)
\]
</div>
<div class="desugar-rule">
\[
desugar_{assert}(\assert e : e', binds) =
\assert{desugar_{expr}(\local{binds}{\if{e}{\null}{\error{e'}}}, \true)}
\]
</div>
</div>
<div class="desugar-rule-group">
<p>
<i>desugar<sub>field</sub></i>: (Field × [Bind] × Boolean) → Field. This desugars object fields.
Recall that <i>h</i> ranges over <code>:</code>, <code>::</code>, <code>:::</code>. The
boolean records whether the object containing this field is itself in another object. The
notation <i>string(id)</i> means converting the identifier token to a string literal.
</p>
<div class="desugar-rule">
\[
desugar_{field}(id \mathrel{h} e, binds, b) =
desugar_{field}([string(id)] \mathrel{h} e), binds, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{field}(id \mathrel{+h} e, binds, b) =
desugar_{field}([string(id)] \mathrel{+h} e), binds, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{field}(id(params) \mathrel{h} e, binds, b) =
desugar_{field}([string(id)](params) \mathrel{h} e), binds, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{field}(string \mathrel{h} e, binds, b) =
desugar_{field}([string] \mathrel{h} e), binds, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{field}(string \mathrel{+h} e, binds, b) =
desugar_{field}([string] \mathrel{+h} e), binds, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{field}(string(params) \mathrel{h} e, binds, b) =
desugar_{field}([string](params) \mathrel{h} e), binds, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{field}([e] \mathrel{h} e', binds, b) =
[desugar_{expr}(e, b)] \mathrel{h} desugar_{expr}(\local{binds}{e}, \true)
\]
</div>
<div class="desugar-rule">
\[
desugar_{field}([e] \mathrel{+h} e', binds, b) = \\
\hspace{10mm}
\textrm{let } e'' = e[\texttt{\$outerself} / \self, \texttt{\$outersuper} / \super] \\
\hspace{10mm}
\textrm{let } e''' = \if{e''\mathrel{\texttt{in}} \super}{\super[e''] + {e'}}{e'} \\
\hspace{10mm}
\textrm{in } desugar_{field}([e] \mathrel{h} e''', binds, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{field}([e](params) \mathrel{h} e', binds, b) =
desugar_{field}([e] \mathrel{h} \function{params}{e'}, binds, b)
\]
</div>
</div>
<div class="desugar-rule-group">
<p>
<i>desugar<sub>bind</sub></i>: (Bind × Boolean) → Field. This desugars local bindings.
</p>
<div class="desugar-rule">
\[
desugar_{bind}(id \texttt{=} e, b) =
id \texttt{=} desugar_{expr}(e, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{bind}(id(params) \texttt{=} e, b) =
id \texttt{=} desugar_{expr}(\function{params}e, b)
\]
</div>
</div>
<div class="desugar-rule-group">
<p>
<i>desugar<sub>param</sub></i>: (Param × Boolean) → Param. This desugars function parameters.
</p>
<div class="desugar-rule">
\[
desugar_{param}(id, b) =
id \texttt{=} \error{\texttt{"Parameter not bound"}}
\]
</div>
<div class="desugar-rule">
\[
desugar_{param}(id \texttt{=} e, b) =
id \texttt{=} desugar_{expr}(e, b)
\]
</div>
</div>
<div class="desugar-rule-group">
<p>
<i>desugar<sub>arrcomp</sub></i>: (Expr × CompSpec × Boolean) → Field. This desugars
array comprehensions.
</p>
<div class="desugar-rule">
\[
desugar_{arrcomp}(e, \textrm{if }e'\ compspec, b) =
desugar_{expr}(\if{e'}{desugar_{arrcomp}(e, compspec, b)}{[\ ]}, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{arrcomp}(e, \textrm{if }e', b) =
desugar_{expr}(\if{e'}{[e]}{[\ ]}, b)
\]
</div>
<div class="desugar-rule">
\[
desugar_{arrcomp}(e, \textrm{for }x\textrm{ in }e'\ compspec, b) = \\
\hspace{10mm}\textrm{Let }arr, i\textrm{ fresh} \\
\hspace{10mm}desugar_{expr}(
\local{arr = e'}{
\texttt{std.join}(\\\hspace{20mm}[\ ], \texttt{std.makeArray}(
\texttt{std.length}(arr),
\function{i}{\local{x = arr[i]}{desugar_{arrcomp}(e, compspec, b)}}
))
},
b
)
\]
</div>
<div class="desugar-rule">
\[
desugar_{arrcomp}(e, \textrm{for }x\textrm{ in }e', b) = \\
\hspace{10mm}\textrm{Let }arr, i\textrm{ fresh} \\
\hspace{10mm}desugar_{expr}(
\local{arr = e'}{
\texttt{std.join}(\\\hspace{20mm}[\ ], \texttt{std.makeArray}(
\texttt{std.length}(arr),
\function{i}{\local{x = arr[i]}{[e]}}
))
},
b
)
\]
</div>
</div>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h3 id="static_checking">Static Checking</h3>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
After the Jsonnet program is parsed and desugared, a syntax-directed algorithm is employed
to reject programs that contain certain classes of errors. This is presented like a static
type system, except that there are no static types. Programs are only rejected if they use
undefined variables, or if <code>self</code>, <code>super</code> or <code>$</code> are used
outside the bounds of an object. In the core language, <code>$</code> has been desugared to
a variable, so its checking is implicit in the checking of bound variables.
</p>
<p>
The static checking is described below as a judgement \(Γ ⊢ e\), where \(Γ\) is the set of
variables in scope of \(e\). The set \(Γ\) initially contains only <code>std</code>, the
implicit standard library. In the case of imported files, each jsonnet file is checked
independently of the other files.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="inverse hgroup">
<div class="hgroup-inline">
<div class="panel wide">
<div class="rules">
<div class="sequent-rule">
\[
\rule{chk-lit} {
} {
\_ ⊢ \null, \true, \false, string, number
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-self} {
\self ∈ Γ
} {
Γ ⊢ \self
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-super} {
\super ∈ Γ
} {
Γ ⊢ \super
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-object} {
Γ ⊢ e_1 \ldots e_m
\\
Γ ∪ \{\self,\super\} ⊢ e'_1 \ldots e'_n
\\
∀ i,j: e_i ∈ string ∧ e_j = e_i ⇒ i = j
} {
Γ ⊢ \object{[e_1] h_1 e'_1 \ldots [e_m] h_m e'_m,
\assert e'_{m+1} \ldots \assert e'_n}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-object-comp} {
Γ ∪ \{x\} ⊢ e_1
\\
Γ ∪ \{x,\self,\super\} ⊢ e_2
\\
Γ ⊢ e_3
} {
Γ ⊢ \ocomp{e_1}{e_2}{x}{e_3}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-array} {
Γ ⊢ e_1 \ldots e_n
} {
Γ ⊢ \array{e_1 \ldots e_n}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-array-index} {
Γ ⊢ e
\\
Γ ⊢ e'
} {
Γ ⊢ e[e']
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-apply} {
Γ ⊢ e
\\
∀ i∈\{1\ldots n\}: Γ ⊢ e_i
\\
∀ i,j∈\{1\ldots n\}: x_i = x_j ⇒ i = j
} {
Γ ⊢ e(x_1 = e_1 \ldots x_n = e_n)
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-var} {
x ∈ Γ
} {
Γ ⊢ x
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-local} {
Γ ∪ \{x_1 \ldots x_n\} ⊢ e_1 \ldots e_n, e
\\
∀ i,j: x_i = x_j ⇒ i = j
} {
Γ ⊢ \local{\assign{x_1}{e_1} \ldots \assign{x_n}{e_n}}e
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-if} {
Γ ⊢ e_1, e_2, e_3
} {
Γ ⊢ \if{e_1}{e_2}{e_3}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-binary} {
Γ ⊢ e_L, e_R
} {
Γ ⊢ \binary{e_L}{sym}{e_R}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-unary} {
Γ ⊢ e
} {
Γ ⊢ \unary{sym}{e}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-function} {
∀ i ∈ \{m+1 \ldots n\}: Γ ∪ \{x_1 \ldots x_n\} ⊢ e_i
\\
Γ ∪ \{x_1 \ldots x_n\} ⊢ e'
\\
∀ i,j: x_i = x_j ⇒ i = j
} {
Γ ⊢ \function{x_1\ldots x_m, x_{m+1}=e_{m+1}\ldots x_n=e_n}{e'}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-import} {
} {
Γ ⊢ \import{s}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-importstr} {
} {
Γ ⊢ \importstr{s}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{chk-error} {
Γ ⊢ e
} {
Γ ⊢ \error{e}
}
\]
</div>
</div>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h3 id="semantics">Operational Semantics</h3>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
We present two sets of operational semantics rules. The first defines the judgement \(e ↓
v\) which represents the execution of Jsonnet expressions into Jsonnet values. The other
defines the judgement \(v ⇓ j\) which represents <dfn>manifestation</dfn>, the process by
which Jsonnet values are converted into JSON values.
</p>
<p>
We model both explicit runtime errors (raised by the error construct) and implicit runtime
errors (e.g. array bounds errors) as stuck execution. Errors can occur both in the \(e ↓
v\) judgement and in the \(v ⇓ j\) judgement (because it is defined in terms of \(e ↓ v\)).
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h4 id="jsonnet_values">Jsonnet Values</h4>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
When executed, Jsonnet expressions yield Jsonnet values. These need to be manifested, an
additional step, to get JSON values. The differences between Jsonnet values and JSON values
are: 1) Jsonnet values contain functions (which are not representable in JSON). 2) Due to
the lazy semantics, both object fields and array elements have yet to be executed to yield
values. 3) Object assertions still need to be checked.
</p>
<p>
Execution of a statically-checked expression will never yield an object with duplicate field
names. By abuse of notation, we consider two objects to be equivalent even if their fields
and assertions are re-ordered. However this is not true of array elements or function
parameters.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="inverse hgroup">
<div class="hgroup-inline">
<div class="panel wide">
<table>
<tr>
<td><em>v</em></td>
<td>∈</td>
<td><em>Value</em></td>
<td>=</td>
<td>
<em>Primitive</em> ∪
<em>Object</em> ∪
<em>Function</em> ∪
<em>Array</em>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td><em>Primitive</em></td>
<td>::=</td>
<td>
<code>null</code> | <code>true</code> | <code>false</code> | <i>string</i>
| <i>double</i>
</td>
</tr>
<tr>
<td><em>o</em></td>
<td>∈</td>
<td><em>Object</em></td>
<td>::=</td>
<td>
<code>{</code>
{ <code>assert</code> <i>e</i> } { <i>string</i> <i>h</i> <i>e</i> }
<code>}</code>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td><em>Function</em></td>
<td>::=</td>
<td>
<code>function (</code> { <i>id</i>=<i>e</i> } <code>)</code> <i>e</i>
</td>
</tr>
<tr>
<td><em>a</em></td>
<td>∈</td>
<td><em>Array</em></td>
<td>::=</td>
<td>
<code>[</code> { <i>e</i> } <code>]</code>
</td>
</tr>
</table>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h4 id="hidden_inheritance">Hidden status inheritance</h4>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
The hidden status of fields is preserved over inheritance if the right hand side uses the
<code>:</code> form. This is codified with the following function:
</p>
<div class="rules">
<div class="sequent-rule">
\[
h_L + h_R = \left\{\begin{array}{ll}
h_L & \textrm{if }h_R = \texttt{:} \\
h_R & \textrm{otherwise} \\
\end{array}\right.
\]
</div>
</div>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h4 id="substitution">Capture-Avoiding Substitution</h4>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
The rules for capture-avoiding variable substitution [<i>e</i>/<i>id</i>] are an
extension of those in the <a href="http://en.wikipedia.org/wiki/Lambda_calculus">lambda
calculus</a>.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="inverse hgroup">
<div class="hgroup-inline">
<div class="panel wide">
<p>Let y ≠ x.</p>
<table>
<tr>
<td>
<code>self</code>[<i>e</i>/<i>x</i>] = <code>self</code>
</td>
</tr>
<tr>
<td>
<code>super</code>[<i>e</i>/<i>x</i>] = <code>super</code>
</td>
</tr>
<tr>
<td>
<i>x</i>[<i>e</i>/<i>x</i>] = <i>e</i>
</td>
</tr>
<tr>
<td>
<i>y</i>[<i>e</i>/<i>x</i>] = <i>y</i>
</td>
</tr>
<tr>
<td colspan=2>
<code>{</code>
...
<code>assert</code> <i>e'</i>
...
<code>[</code><i>e''</i><code>]</code> <i>h</i> <i>e'''</i>
...
<code>}</code>[<i>e</i>/<i>x</i>] =
<code>{</code>
...
<code>assert</code> <i>e'</i>[<i>e</i>/<i>x</i>]
...
<code>[</code><i>e'</i>[<i>e</i>/<i>x</i>]<code>]</code> <i>h</i>
<i>e''</i>[<i>e</i>/<i>x</i>]
...
<code>}</code>
</td>
</tr>
<tr>
<td>
<code>{</code>
<code>[</code><i>e'</i><code>]</code><code>:</code> <i>e''</i>
<code>for</code> <i>x</i> <code>in</code> <i>e'''</i>
<code>}</code>[<i>e</i>/<i>x</i>] =
<code>{</code>
<code>[</code><i>e'</i><code>]</code><code>:</code> <i>e''</i>
<code>for</code> <i>x</i> <code>in</code> <i>e'''</i>[<i>e</i>/<i>x</i>]
<code>}</code>
</td>
</tr>
<tr>
<td>
<code>{</code>
<code>[</code><i>e'</i><code>]</code><code>:</code> <i>e''</i>
<code>for</code> <i>y</i> <code>in</code> <i>e'''</i>
<code>}</code>[<i>e</i>/<i>x</i>] =
<code>{</code>
<code>[</code><i>e'</i>[<i>e</i>/<i>x</i>]<code>]:</code>
<i>e''</i>[<i>e</i>/<i>x</i>]
<code>for</code> <i>y</i> <code>in</code> <i>e'''</i> [<i>e</i>/<i>x</i>]
<code>}</code>
</td>
</tr>
<tr>
<td>
(<code>local</code> ... <i>y</i><code>=</code><i>e'</i> ... <code>;</code> <i>e''</i>)
[<i>e</i>/<i>x</i>] =
<code>local</code> ... <i>y</i><code>=</code><i>e'</i> ... <code>;</code> <i>e''</i>
</td>
<td>(If any variable matches.)</td>
</tr>
<tr>
<td>
(<code>local</code> ... <i>y</i><code>=</code><i>e'</i> ... <code>;</code> <i>e''</i>)
[<i>e</i>/<i>x</i>] =
<code>local</code> ... <i>y</i><code>=</code><i>e'</i>[<i>e</i>/<i>x</i>] ...
<code>;</code><i>e''</i>[<i>e</i>/<i>x</i>]
</td>
<td>(If no variable matches.)</td>
</tr>
<tr>
<td>
(<code>function</code> <code>(</code>
... <i>y</i>=<i>e'</i> ...
<code>)</code> <i>e''</i>)[<i>e</i>/<i>x</i>] =
<code>function</code> <code>(</code>
... <i>y</i>=<i>e'</i> ...
<code>)</code><i>e''</i>
</td>
<td>(If any param matches.)</td>
</tr>
<tr>
<td>
(<code>function</code> <code>(</code>
... <i>y</i>=<i>e'</i> ...
<code>)</code> <i>e''</i>)[<i>e</i>/<i>x</i>] =
<code>function</code> <code>(</code>
... <i>y</i>=<i>e'</i>[<i>e</i>/<i>x</i>] ...
<code>)</code> <i>e''</i>[<i>e</i>/<i>x</i>]
</td>
<td>(If no param matches.)</td>
</tr>
<tr>
<td>
Otherwise, <i>e'</i> [<i>e</i>/<i>x</i>] proceeds via syntax-directed recursion into
subterms of <i>e'</i>.
</td>
</tr>
</table>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
The rules for keyword substitution ⟦<i>e</i>/<i>kw</i>⟧ for <i>kw</i> ∈ { <code>self</code>,
<code>super</code> } avoid substituting keywords that are captured by nested objects:
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="inverse hgroup">
<div class="hgroup-inline">
<div class="panel wide">
<table>
<tr>
<td>
<code>self</code>
⟦<i>e</i>/<code>self</code>⟧ = <i>e</i>
</td>
</tr>
<tr>
<td>
<code>super</code>
⟦<i>e</i>/<code>super</code>⟧ = <i>e</i>
</td>
</tr>
<tr>
<td>
<code>self</code>
⟦<i>e</i>/<code>super</code>⟧ = <code>self</code>
</td>
</tr>
<tr>
<td>
<code>super</code>
⟦<i>e</i>/<code>self</code>⟧ = <code>super</code>
</td>
</tr>
<tr>
<td>
<code>{</code>
...
<code>assert</code> <i>e'</i>
...
<code>[</code><i>e''</i><code>]</code><i>h</i> <i>e'''</i>
...
<code>}</code>
⟦<i>e</i>/<i>kw</i>⟧ =
<code>{</code>
...
<code>assert</code> <i>e'</i>
...
<code>[</code><i>e''</i>⟦<i>e</i>/<i>kw</i>⟧<code>]</code><i>h</i> <i>e'''</i>
...
<code>}</code>
</td>
</tr>
<tr>
<td>
<code>{</code>
<code>[</code><i>e'</i><code>]</code><code>:</code> <i>e''</i>
<code>for</code> <i>x</i> <code>in</code> <i>e'''</i>
<code>}</code>⟦<i>e</i>/<i>kw</i>⟧ =
<code>{</code>
<code>[</code><i>e'</i>⟦<i>e'</i>/<i>kw</i>⟧<code>]</code><code>:</code> <i>e''</i>
<code>for</code> <i>x</i> <code>in</code> <i>e'''</i>⟦<i>e</i>/<i>kw</i>⟧
<code>}</code>
</td>
</tr>
<tr>
<td>
Otherwise, <i>e'</i>⟦<i>e'</i>/<i>kw</i>⟧ proceeds via syntax-directed recursion into
the subterms of <i>e'</i>.
</td>
</tr>
</table>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h4 id="execution">Execution</h4>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
The following big step operational semantics rules define the execution of Jsonnet programs,
i.e. the reduction of a Jsonnet program <i>e</i> into its Jsonnet value <em>v</em> via the
judgement \(e ↓ v\).
</p>
<div style="clear: both"></div>
</div>
</div>
<div class="inverse hgroup">
<div class="hgroup-inline">
<div class="panel wide">
<p>
Let <em>f</em> range over strings, as used in object field names.
</p>
<div class="rules">
<div class="sequent-rule">
\[
\rule{value} {
v ∈ \{\null, \true, \false\} ∪ String ∪ Number ∪ Function ∪ Array
} {
v ↓ v
}
\]
</div>
<div class="sequent-rule">
\[
\rule{object} {
∀i∈\{1\ldots p\}: e_i ↓ f_i
\\
∀i∈\{p+1 \ldots n\}: e_i ↓ \null
\\
∀i,j∈\{1\ldots p\}: f_i = f_j ⇒ i = j
\\
o = \object{
\assert{e_1} \ldots \assert{e_m}, f_1\mathop{h_1}e''_1 \ldots f_p\mathop{h_p}e''_p
}
} {
\object{\assert{e_1} \ldots \assert{e_m},
[e'_1]\mathop{h_1}e''_1 \ldots [e'_n]\mathop{h_n}e''_n } ↓ o
}
\]
</div>
<div class="sequent-rule">
\[
\rule{object-comp} {
e_{arr} ↓ [ e_1 \ldots e_n ]
\\
∀i∈\{1 \ldots n\}: e_{field}[e_i/x] ↓ f_i
\\
∀i,j∈\{1\ldots n\}: f_i = f_j ≠ \null ⇒ i = j
\\
\{ (f'_1, e'_1) \ldots (f'_m, e'_m) \} =
\{ (f_i, e_{body}[e_i/x]) \ | \ i ∈\{1\ldots n\} ∧ f_i ≠ \null \}
\\
o = \object{f'_1: e'_1 \ldots f'_m: e'_m}
} {
\ocomp{e_{field}}{e_{body}}{x}{e_{arr}} ↓ o
}
\]
</div>
<div class="sequent-rule">
\[
\rule{array-index} {
e ↓ \array{e_0 \ldots e_n}
\\
e' ↓ i ∈ \{ 0 \ldots n \}
\\
e_i ↓ v
} {
\index{e}{e'} ↓ v
}
\]
</div>
<div class="sequent-rule">
\[
\rule{object-index} {
e ↓ o = \object{\assert{e'''_1} \ldots \assert{e'''_m}, f_1 h_1 e''_1 \ldots f_n h_n e''_n}
\\
∀j ∈ \{1 \ldots m \}: e'''_j⟦ o / \self, \{\} / \super ⟧↓\_
\\
e' ↓ f_i
\\
e''_i ⟦ o / \self, \{\} / \super ⟧ ↓ v
} {
\index{e}{e'} ↓ v
}
\]
</div>
<div class="sequent-rule">
\[
\rule{apply} {
e_0 ↓ \function{y_1=e'_1 \ldots y_p=e'_p}{e_b}
\\
∀i∈\{m+1 \ldots n\}: x_i ∈ \{y_1 \ldots y_p\}
\\
∀i∈\{1 \ldots p\}: e''_i = \left\{\begin{array}{ll}
e_i & \textrm{if } i ≤ m \\
e_j & \textrm{if } y_i=x_j \textrm{ for some } j \\
e'_i & \textrm{otherwise}\\
\end{array}\right.
\\
(\local{y_1=e''_1 \ldots y_p=e''_p}{e_b}) ↓ v
} {
e_0(e_1 \ldots e_m, x_{m+1}=e_{m+1} \ldots x_n = e_n) ↓ v
}
\]
</div>
<div class="sequent-rule">
\[
\rule{local} {
e ↓ v
} {
\local{\_}e ↓ v
}
\]
</div>
<div class="sequent-rule">
\[
\rule{local-var} {
binds = \assign{x_1}{e_1} \ldots \assign{x_n}{e_n}
\\
\local{binds}{e[\local{binds}{e_1} / x_1 \ldots \local{binds}{e_n} / x_n ]} ↓ v
} {
\local{binds}e ↓ v
}
\]
</div>
<div class="sequent-rule">
\[
\rule{if-true} {
e_1 ↓ \true \hspace{15pt} e_2 ↓ v
} {
\if{e_1}{e_2}{e_3} ↓ v
}
\]
</div>
<div class="sequent-rule">
\[
\rule{if-false} {
e_1 ↓ \false \hspace{15pt} e_3 ↓ v
} {
\if{e_1}{e_2}{e_3} ↓ v
}
\]
</div>
<div class="sequent-rule">
\[
\rule{object-inherit} {
e^L ↓ \object{
\assert e''^L_1 \ldots \assert e''^L_n,\
f_1 h^L_1 e^L_1 \ldots f_m h^L_m e^L_m,\
f'^L_1 h'^L_1 e'^L_1 \ldots f'^L_p h'^L_p e'^L_p
}
\\
e^R ↓ \object{
\assert e''^R_1 \ldots \assert e''^R_q,\
f_1 h^R_1 e^R_1 \ldots f_m h^R_m e^R_m,\
f'^R_1 h'^R_1 e'^R_1 \ldots f'^R_r h'^R_r e'^R_r
}
\\
\{ f'^L_1 \ldots f'^L_p \} ∩ \{ f'^R_1 \ldots f'^R_r \} = ∅
\\
x, y \textrm{ fresh} \hspace{15pt} \textrm{let } S = λe . e⟦x/\self, y/\super⟧
\\
e_s = \super + \object{
\assert S(e''^L_1) \ldots \assert S(e''^L_n),\\
\hspace{20mm}
f_1 h^L_1 S(e^L_1) \ldots f_m h^L_m S(e^L_m),\
f'^L_1 h'^L_1 S(e'^L_1) \ldots f'^L_p h'^L_p S(e'^L_p)
}
\\
∀i∈\{1 \ldots m\}: h'''_i = h^L_i + h^R_i ∧
e'''_i = (\local{x = \self, y = \super}{e^R_i ⟦e_s / \super⟧})
\\
o = \{ \\
\hspace{5mm} \assert e''^L_1 \ldots \assert e''^L_n, \
\assert e''^R_1 \ldots \assert e''^R_q, \\
\hspace{5mm} f'^L_1 h'^L_1 e'^L_1 \ldots f'^L_p h'^L_p e'^L_p, \
f'^R_1 h'^R_1 e'^R_1 \ldots f'^R_r h'^R_r e'^R_r, \
f_1 h'''_1 e'''_m \ldots f_m h'''_m e'''_m \\
\}
} {
e^L \texttt{ + } e^R ↓ o
}
\]
</div>
<div class="sequent-rule">
\[
\rule{array-concat} {
e ↓ \array{e_0 \ldots e_m}
\\
e' ↓ \array{e_{m+1} \ldots e_n}
} {
e + e' ↓ \array{e_1 \ldots e_n}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{string-concat} {
e_L ↓ v_L \hspace{15pt} e_R ↓ v_R
\\
v_L ∈ String \vee v_R ∈ String
} {
e_L \texttt{ + } e_R ↓ stringconcat(tostring(v_L), tostring(v_R))
}
\]
</div>
<!-- TODO(sbarzowski) Consider explicitly introducing std.cmp (three way comparison) -->
<div class="sequent-rule">
\[
\rule{less-than-true} {
\texttt{std.cmp}(e_L, e_R) ↓ -1
} {
e_L < e_R ↓ \texttt{true}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{less-than-false} {
\texttt{std.cmp}(e_L, e_R) ↓ r
\\
r ∈ \{0, 1\}
} {
e_L < e_R ↓ \texttt{false}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{greater-than-true} {
\texttt{std.cmp}(e_L, e_R) ↓ 1
} {
e_L < e_R ↓ \texttt{true}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{greater-than-false} {
\texttt{std.cmp}(e_L, e_R) ↓ r
\\
r ∈ \{-1, 0\}
} {
e_L < e_R ↓ \texttt{false}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{less-or-equal-true} {
\texttt{std.cmp}(e_L, e_R) ↓ r
\\
r ∈ \{-1, 0\}
} {
e_L <= e_R ↓ \texttt{true}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{less-or-equal-false} {
\texttt{std.cmp}(e_L, e_R) ↓ 1
} {
e_L <= e_R ↓ \texttt{false}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{greater-or-equal-true} {
\texttt{std.cmp}(e_L, e_R) ↓ r
\\
r ∈ \{0, 1\}
} {
e_L >= e_R ↓ \texttt{true}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{greater-or-equal-false} {
\texttt{std.cmp}(e_L, e_R) ↓ -1
} {
e_L >= e_R ↓ \texttt{false}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{cmp-array-left-empty} {
e_L ↓ \array{} \hspace{15pt} e_R ↓ \array{a_0 \ldots a_{n - 1}}
\\
n > 0
} {
\texttt{std.cmp}(e_L, e_R) ↓ -1
}
\]
</div>
<div class="sequent-rule">
\[
\rule{cmp-array-right-empty} {
e_L ↓ \array{a_0 \ldots a_{n - 1}} \hspace{15pt} e_R ↓ \array{}
\\
n > 0
} {
\texttt{std.cmp}(e_L, e_R) ↓ 1
}
\]
</div>
<div class="sequent-rule">
\[
\rule{cmp-array-both-empty} {
e_L ↓ \array{} \hspace{15pt} e_R ↓ \array{}
} {
\texttt{std.cmp}(e_L, e_R) ↓ 0
}
\]
</div>
<div class="sequent-rule">
\[
\rule{cmp-array-first-different} {
e_L ↓ \array{a_0 \ldots a_{n - 1}} \hspace{15pt} e_R ↓ \array{b_0 \ldots b_{m - 1}}
\\
n > 0 \hspace{15pt} m > 0
\\
\texttt{std.cmp}(a_0, b_0) ↓ r
\\
r ≠ 0
} {
\texttt{std.cmp}(e_L, e_R) ↓ r
}
\]
</div>
<div class="sequent-rule">
\[
\rule{cmp-array-first-equal} {
e_L ↓ \array{a_0 \ldots a_{n - 1}} \hspace{15pt} e_R ↓ \array{b_0 \ldots b_{m - 1}}
\\
n > 0 \hspace{15pt} m > 0
\\
\texttt{std.cmp}(a_0, b_0) ↓ 0
\\
\texttt{std.cmp}(\array{a_1 \ldots a_{n - 1}}, \array{b_1 \ldots b_{m - 1}}) ↓ r'
} {
\texttt{std.cmp}(e_L, e_R) ↓ r'
}
\]
</div>
<div class="sequent-rule">
\[
\rule{cmp-string} {
e_L ↓ v_L \hspace{15pt} e_R ↓ v_R
\\
v_L ∈ String
\\
v_R ∈ String
} {
\texttt{std.cmp}(e_L, e_R) ↓ stringcmp(v_L, v_R)
}
\]
</div>
<div class="sequent-rule">
\[
\rule{cmp-number} {
e_L ↓ v_L \hspace{15pt} e_R ↓ v_R
\\
v_L ∈ Number
\\
v_R ∈ Number
} {
\texttt{std.cmp}(e_L, e_R) ↓ numbercmp(v_L, v_R)
}
\]
</div>
<div class="sequent-rule">
\[
\rule{boolean-and-shortcut} {
e_L ↓ \false
} {
e_L \texttt{ && } e_R ↓ \false
}
\]
</div>
<div class="sequent-rule">
\[
\rule{boolean-and-longcut} {
e_L ↓ \true
\\
e_R ↓ b
} {
e_L \texttt{ && } e_R ↓ b
}
\]
</div>
<div class="sequent-rule">
\[
\rule{boolean-or-shortcut} {
e_L ↓ \true
} {
e_L \texttt{ || } e_R ↓ \true
}
\]
</div>
<div class="sequent-rule">
\[
\rule{boolean-or-longcut} {
e_L ↓ \false
\\
e_R ↓ b
} {
e_L \texttt{ || } e_R ↓ b
}
\]
</div>
<div class="sequent-rule">
\[
\rule{not-true} {
e ↓ \true
} {
\texttt{!} e ↓ \false
}
\]
</div>
<div class="sequent-rule">
\[
\rule{not-false} {
e ↓ \false
} {
\texttt{!} e ↓ \true
}
\]
</div>
<div class="sequent-rule">
\[
\rule{primitiveEquals} {
e ↓ v
\\
e' ↓ v'
\\
b = (v ∈ String ∨ v ∈ Boolean ∨ v ∈ Number ∨ v = \null) ∧ v = v'
} {
\texttt{std.primitiveEquals}(e, e') ↓ b
}
\]
</div>
<div class="sequent-rule">
\[
\rule{length-array} {
e ↓ \array{e_0 \ldots e_{n - 1}}
} {
\texttt{std.length}(e) ↓ n
}
\]
</div>
<div class="sequent-rule">
\[
\rule{length-object} {
\texttt{std.length}(\texttt{std.objectFieldsEx}(e, false) ↓ n
} {
\texttt{std.length}(e) ↓ n
}
\]
</div>
<div class="sequent-rule">
\[
\rule{length-string} {
e ↓ v ∈ String
} {
\texttt{std.length}(e) ↓ strlen(v)
}
\]
</div>
<div class="sequent-rule">
\[
\rule{makeArray} {
e ↓ n
\\
e' ↓ f ∈ Function
} {
\texttt{std.makeArray}(e, e') ↓ \array{f(0) \ldots f(n - 1)}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{filter} {
e ↓ f ∈ Function
\\
e' ↓ \array{e_0 \ldots e_{n - 1}}
\\
j_1 \ldots j_m = \{ i \ |\ f(e_i) ↓ \true \}
} {
\texttt{std.filter}(e, e') ↓ \array{e_{j_1} \ldots e_{j_m}}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{type-null} {
e ↓ \texttt{null}
} {
\texttt{std.type}(e) ↓ \texttt{"null"}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{type-boolean} {
e ↓ v ∈ Boolean
} {
\texttt{std.type}(e) ↓ \texttt{"boolean"}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{type-number} {
e ↓ v ∈ Number
} {
\texttt{std.type}(e) ↓ \texttt{"number"}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{type-string} {
e ↓ v ∈ String
} {
\texttt{std.type}(e) ↓ \texttt{"string"}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{type-object} {
e ↓ v ∈ Object
} {
\texttt{std.type}(e) ↓ \texttt{"object"}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{type-function} {
e ↓ v ∈ Function
} {
\texttt{std.type}(e) ↓ \texttt{"function"}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{type-array} {
e ↓ v ∈ Array
} {
\texttt{std.type}(e) ↓ \texttt{"array"}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{object-has-ex} {
e' ↓ f
\\
e'' ↓ b'
\\
e ↓ \object{\assert{e'_1} \ldots \assert{e'_m}, f_1 h_1 e_1 \ldots f_n h_n e_n }
\\
b = ∃i: f = f_i ∧ (h_i \mathop{≠} :: \mathop{∨} b')
} {
\texttt{std.objectHasEx}(e, e', e'') ↓ b
}
\]
</div>
<div class="sequent-rule">
\[
\rule{object-fields-ex} {
e' ↓ b'
\\
e ↓ \object{\assert{e'_1} \ldots \assert{e'_m}, f_1 h_1 e_1 \ldots f_n h_n e_n }
\\
\{ f'_1 \ldots f'_p \} = \{ f\ |\ ∃i: f = f_i ∧ (h_i \mathop{≠} :: \mathop{∨} b') \}
\\
∀i,j∈\{1 \ldots p\}: i≤j ⇒ f'_i≤f'_j
} {
\texttt{std.objectFieldsEx}(e, e') ↓ \array{f'_1 \ldots f'_p}
}
\]
</div>
</div>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
String concatenation will implicitly convert one of the values to a string if necessary.
This is similar to Java. The referred function \(tostring\) returns its argument unchanged
if it is a string. Otherwise it will manifest its argument as a JSON value \(j\) and
unparse it as a single line of text. The referred function \(strlen\) returns the number of
unicode characters in the string.
</p>
<p>
The numeric semantics are as follows:
</p>
<ul>
<li>
<b>Arithmetic:</b> Binary <code>*</code>, <code>/</code>, <code>+</code>, <code>-</code>,
<code><</code>, <code><=</code>, <code>></code>, <code>>=</code>, and unary
<code>+</code> and <code>-</code> operate on numbers and have IEEE double precision
floating point semantics, except that the special states NaN, Infinity raise errors. Note
that <code>+</code> is also overloaded on objects, arrays, and when either argument is a
string. Also, <code><</code>, <code><=</code>, <code>></code>, <code>>=</code>
are overloaded on strings and on arrays. In both cases the comparison is performed
lexicographically (in case of strings, by unicode codepoint).
</li>
<li>
<b>Bitwise:</b> Operators
<code><<</code>,
<code>>></code>,
<code>&</code>,
<code>^</code>,
<code>|</code> and
<code>~</code>
first convert their operands to signed 64 bit integers, then perform the operations in a
standard way, then convert back to IEEE double precision floating point. In shift
operations <code><<</code>, <code>>></code>, the right hand value modulo 64 is
interpreted as the shift count. Shifting with a negative shift count raises an error.
</li>
<li>
<b>Standard functions</b>: The following functions have standard mathematical behavior
and operate on IEEE double precision floating point:
<code>std.pow(a, b)</code>,
<code>std.floor(x)</code>,
<code>std.ceil(x)</code>,
<code>std.sqrt(x)</code>,
<code>std.sin(x)</code>,
<code>std.cos(x)</code>,
<code>std.tan(x)</code>,
<code>std.asin(x)</code>,
<code>std.acos(x)</code>,
<code>std.atan(x)</code>,
<code>std.log(x)</code>,
<code>std.exp(x)</code>,
<code>std.mantissa(x)</code>,
<code>std.exponent(x)</code> and
<code>std.modulo(a, b)</code>.
Also, <code>std.codepoint(x)</code> take a single character string,
returning the unicode codepoint as a number, and <code>std.char(x)</code> is its inverse.
</li>
</ul>
<p>
The <code>error</code> operator has no rule because we model errors (both from the language
and user-defined) as stuck execution. The semantics of <code>error</code> are that its
subterm is evaluated to a Jsonnet value. If this is a string, then that is the error that
is raised. Otherwise, it is converted to a string using \(tostring\) like during string
concatenation. The specification does not specify how the error is presented to the user,
and whether or not there is a stack trace. Error messages are meant for human inspection,
and there is therefore no need to standardize them.
</p>
<p>
Finally, the function <code>std.native(x)</code> takes a string and returns a function
configured by the user in a custom execution environment, thus its semantics cannot be
formally described here. The function <code>std.extVar(x)</code> also takes a string and
returns the value bound to that external variable (always a string) at the time the Jsonnet
environment was created.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h4 id="json_values">JSON Values</h4>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
After execution, the resulting Jsonnet value is manifested into a JSON value whose
serialized form is the ultimate output. The Manifestation process removes hidden fields,
checks assertions, and forces array elements and non-hidden object fields. Attempting to
manifest a function raises an error since they do not exist in JSON. JSON values are
formalized below.
</p>
<p>
By abuse of notation, we consider two objects to be equivalent even if their fields are
re-ordered. However this is not true of array elements whose ordering is strict.
</p>
<table>
<tr>
<td><em>j</em></td>
<td>∈</td>
<td><em>JValue</em></td>
<td>=</td>
<td>
<em>Primitive</em> ∪
<em>JObject</em> ∪
<em>JArray</em>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td><em>Primitive</em></td>
<td>::=</td>
<td>
<code>null</code> | <code>true</code> | <code>false</code> | <i>string</i> |
<i>double</i>
</td>
</tr>
<tr>
<td><em>o</em></td>
<td>∈</td>
<td><em>JObject</em></td>
<td>::=</td>
<td>
<code>{</code> { <i>string</i> : <em>j</em> } <code>}</code>
</td>
</tr>
<tr>
<td><em>a</em></td>
<td>∈</td>
<td><em>Array</em></td>
<td>::=</td>
<td>
<code>[</code> { <em>j</em> } <code>]</code>
</td>
</tr>
</table>
<p>Note that <em>JValue</em> ⊂ <em>Value</em>.</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h4 id="manifestation">Manifestation</h4>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
Manifestation is the conversion of a Jsonnet value into a JSON value. It is represented
with the judgement \(v⇓j\). The process requires executing arbitrary Jsonnet code
fragments, so the two semantic judgements represented by \(↓\) and \(⇓\) are mutually
recursive. Hidden fields are ignored during manifestation. Functions cannot be manifested,
so an error is raised in that case (formalized as stuck execution).
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="inverse hgroup">
<div class="hgroup-inline">
<div class="panel wide">
<div class="rules">
<div class="sequent-rule">
\[
\rule{manifest-value} {
j ∈ (\{\null, \true, \false\} ∪ String ∪ Number)
} {
j ⇓ j
}
\]
</div>
<div class="sequent-rule">
\[
\rule{manifest-object} {
o = \object{
\assert e''_1 \ldots \assert e''_m,\
f_1 h_1 e_1 \ldots f_n h_n e_n,\
f'_1 :: e'_1 \ldots f'_p :: e'_p,\
}
\\
∀i∈\{1\ldots m\}: e''_i⟦o/\self,\{\}/\super⟧↓\_
\\
∀i∈\{1\ldots n\}: e_i⟦o/\self,\{\}/\super⟧↓v_i⇓j_i\ ∧\ h_i ≠ ::
} {
o ⇓ \object{f_1 : j_1 \ldots f_n : j_n}
}
\]
</div>
<div class="sequent-rule">
\[
\rule{manifest-array} {
∀i∈\{1\ldots n\}: e_i↓v_i⇓j_i
} {
\array{e_1 \ldots e_n} ⇓ \array{j_1 \ldots j_n}
}
\]
</div>
</div>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
</div>
<div style="clear: both"></div>
</div>
</div>
|