jintp_0.2.0_f1c20f36/src/jintp.adb

   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
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
with Ada.Containers.Vectors;
with Ada.Streams;
with Ada.Streams.Stream_IO;
with Ada.Directories;
with Ada.Calendar;
with Ada.Unchecked_Deallocation;
with Ada.Strings.Fixed;
with Ada.Characters.Handling;
with Ada.Exceptions;
with Ada.Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;

with Jintp.Input;
with Jintp.Scanner;

package body Jintp is

   use Ada.Streams;
   use Ada.Streams.Stream_IO;
   use Ada.Calendar;
   use Ada.Containers;
   use Ada.Strings;
   use Template_Access_Maps;

   type Value_Kind is (String_Expression_Value, Boolean_Expression_Value,
                       Integer_Expression_Value, Float_Expression_Value,
                       Dictionary_Expression_Value, List_Expression_Value);

   type Expression_Value (Kind : Value_Kind := String_Expression_Value)
   is record
      case Kind is
         when String_Expression_Value =>
            S : Unbounded_String;
         when Integer_Expression_Value =>
            I : Integer;
         when Boolean_Expression_Value =>
            B : Boolean;
         when Float_Expression_Value =>
            F : Long_Float;
         when Dictionary_Expression_Value =>
            Dictionary_Value : Dictionary;
         when List_Expression_Value =>
            List_Value : List;
      end case;
   end record;

   package Expression_Value_Vectors is new
     Ada.Containers.Vectors (Index_Type => Natural,
                             Element_Type => Expression_Value);

   use Expression_Value_Vectors;

   type List_Elements is record
      Ref_Count : Natural;
      Values : Expression_Value_Vectors.Vector;
   end record;

   function Hash (Value : Expression_Value) return Hash_Type is
   begin
      case Value.Kind is
         when String_Expression_Value =>
            return Ada.Strings.Unbounded.Hash (Value.S);
         when Integer_Expression_Value =>
            return Hash_Type (Value.I);
         when others =>
            raise Template_Error
              with "only string and integer values can be keys";
      end case;
   end Hash;

   package Association_Maps is new
     Ada.Containers.Hashed_Maps (Key_Type => Expression_Value,
                                 Element_Type => Expression_Value,
                                 Hash => Hash,
                                 Equivalent_Keys => "=");

   use Association_Maps;

   type Dictionary_Assocs is record
      Ref_Count : Natural;
      Value_Assocs : Association_Maps.Map;
   end record;

   Empty_String_Value : constant Expression_Value
     := (Kind => String_Expression_Value,
         S => Null_Unbounded_String);

   type Expression;

   type Expression_Access is access Expression;

   type Statement_Kind is (If_Statement, Elif_Statement, Else_Statement,
                           Endif_Statement, For_Statement, Endfor_Statement,
                           Include_Statement, Macro_Statement,
                           Endmacro_Statement, Raw_Statement, Endraw_Statement,
                           Extends_Statement, Block_Statement,
                           Endblock_Statement, Import_Statement,
                           From_Import_Statement);

   type Parameter (Has_Default_Value : Boolean := False) is record
      Name : Unbounded_String;
      case Has_Default_Value is
         when True =>
            Default_Value : Expression_Value;
         when False =>
            null;
      end case;
   end record;

   type Parameters is array (Positive range <>) of Parameter;

   package Parameter_Vectors is new
     Ada.Containers.Vectors (Index_Type => Positive,
                             Element_Type => Parameter);

   type Name_Mapping is record
      Source : Unbounded_String;
      Target : Unbounded_String;
   end record;

   package Name_Mapping_Vectors is new
     Ada.Containers.Vectors (Index_Type => Positive,
                             Element_Type => Name_Mapping);

   type Statement (Kind : Statement_Kind := If_Statement) is record
      case Kind is
         when If_Statement | Elif_Statement =>
            If_Condition : Expression_Access;
         when For_Statement =>
            For_Variable_1_Name : Unbounded_String;
            For_Variable_2_Name : Unbounded_String;
            For_Expression : Expression_Access;
            For_Condition : Expression_Access;
         when Include_Statement =>
            File_Name : Unbounded_String;
         when Macro_Statement =>
            Macro_Name : Unbounded_String;
            Macro_Parameters : Parameter_Vectors.Vector;
         when Extends_Statement =>
            Parent_Name : Unbounded_String;
         when Block_Statement =>
            Block_Name : Unbounded_String;
         when Import_Statement =>
            Import_File_Name : Unbounded_String;
            Import_Variable_Name : Unbounded_String;
         when From_Import_Statement =>
            From_File_Name : Unbounded_String;
            Import_Variable_Names : Name_Mapping_Vectors.Vector;
         when others =>
            null;
      end case;
   end record;

   type Tag_Kind is (Expression_Element, Statement_Element, Comment_Element);

   subtype Element_Kind is Tag_Kind
     range Expression_Element .. Statement_Element;

   type Template_Element (Kind : Element_Kind := Expression_Element) is record
      Line : Positive;
      case Kind is
         when Expression_Element =>
            Expr : Expression_Access;
         when Statement_Element =>
            Stmt : Statement;
      end case;
   end record;

   package Template_Element_Vectors is new
     Ada.Containers.Vectors (Index_Type => Positive,
                             Element_Type => Template_Element);

   use Template_Element_Vectors;

   type Macro is record
      Parameters : Parameter_Vectors.Vector;
      Elements : Template_Element_Vectors.Vector;
   end record;

   type Macro_Access is access Macro;

   procedure Free_Macro is new Ada.Unchecked_Deallocation
     (Macro, Macro_Access);

   package Macro_Maps is new
     Ada.Containers.Hashed_Maps (Key_Type => Unbounded_String,
                                 Element_Type => Macro_Access,
                                 Hash => Hash,
                                 Equivalent_Keys => "=");

   package Block_Maps is new
     Ada.Containers.Hashed_Maps (Key_Type => Unbounded_String,
                                 Element_Type => Positive,
                                 Hash => Hash,
                                 Equivalent_Keys => "=");

   use Block_Maps;

   type Template is new Ada.Finalization.Limited_Controlled with record
      Timestamp : Time;
      File_Name : Unbounded_String;
      Elements : Template_Element_Vectors.Vector;
      Block_Map : Block_Maps.Map;
      Cached : Boolean;
   end record;

   overriding procedure Finalize (Self : in out Template);

   type Named_Argument is record
      Name : Unbounded_String;
      Argument : Expression_Access;
   end record;

   package Named_Argument_Vectors is new
     Ada.Containers.Vectors (Index_Type => Positive,
                             Element_Type => Named_Argument);

   use Named_Argument_Vectors;

   type Expression_Kind is (Literal, Operator_Super, Operator_Not, Operator_Or,
                            Operator_And, Operator_Eq, Operator_Neq,
                            Operator_Lt, Operator_Le, Operator_Gt, Operator_Ge,
                            Operator_Tilde, Operator_Plus, Operator_Minus,
                            Operator_Mul, Operator_Div, Operator_Integer_Div,
                            Operator_Power, Operator_Dot, Operator_Brackets,
                            Operator_Items, Operator_Macro,
                            Variable, Filter, Test);

   type Expression_Access_Array is array (Positive range <>)
     of Expression_Access;

   Argument_Capacity : constant Positive := 6;

   type Expression (Kind : Expression_Kind) is record
      case Kind is
         when Literal =>
            Value : Expression_Value;
         when Variable =>
            Variable_Name : Unbounded_String;
         when Filter | Test =>
            Name : Unbounded_String;
            Arguments : Expression_Access_Array (1 .. Argument_Capacity);
         when Operator_Super .. Operator_Items =>
            Named_Arguments : Named_Argument_Vectors.Vector;
         when Operator_Macro =>
            Macro_Variable_Name : Unbounded_String;
            Macro_Name : Unbounded_String;
            Macro_Arguments : Named_Argument_Vectors.Vector;
      end case;
   end record;

   procedure Free_Expression is new Ada.Unchecked_Deallocation
     (Expression, Expression_Access);

   procedure Delete_Expression (Expr : in out Expression_Access) is
   begin
      if Expr = null then
         return;
      end if;
      case Expr.Kind is
         when Filter | Test =>
            for I in Expr.Arguments'First ..  Expr.Arguments'Last
            loop
               Delete_Expression (Expr.Arguments (I));
            end loop;
         when Operator_Super .. Operator_Items =>
            for E of Expr.Named_Arguments loop
               Delete_Expression (E.Argument);
            end loop;
         when Operator_Macro =>
            for E of Expr.Macro_Arguments loop
               Delete_Expression (E.Argument);
            end loop;
         when others =>
            null;
      end case;
      Free_Expression (Expr);
   end Delete_Expression;

   procedure Init (Container : in out List) is
   begin
      if Container.Elements = null then
         Container.Elements := new List_Elements;
         Container.Elements.Ref_Count := 1;
      end if;
   end Init;

   procedure Init (Container : in out Dictionary) is
   begin
      if Container.Assocs = null then
         Container.Assocs := new Dictionary_Assocs;
         Container.Assocs.Ref_Count := 1;
      end if;
   end Init;

   function Element (Source : Dictionary;
                     Key : Unbounded_String)
                     return Expression_Value is
   begin
      return Source.Assocs.Value_Assocs
        ((Kind => String_Expression_Value,
          S => Key));
   end Element;

   function Element (Source : Dictionary;
                     Key : Unbounded_String;
                     Value : out Expression_Value)
                     return Boolean is
      Position : constant Association_Maps.Cursor := Find
        (Source.Assocs.Value_Assocs,
         (Kind => String_Expression_Value,
          S => Key));
   begin
      if Position = Association_Maps.No_Element then
         return False;
      end if;
      Value := Association_Maps.Element (Position);
      return True;
   end Element;

   function Find_Named_Argument (Source : Named_Argument_Vectors.Vector;
                                 Name : Unbounded_String)
                                 return Named_Argument_Vectors.Cursor
   is
      Position : Named_Argument_Vectors.Cursor := Source.First;
   begin
      while Position /= Named_Argument_Vectors.No_Element loop
         if Named_Argument_Vectors.Element (Position).Name = Name then
            return Position;
         end if;
         Position := Named_Argument_Vectors.Next (Position);
      end loop;
      return Named_Argument_Vectors.No_Element;
   end Find_Named_Argument;

   package Expression_Parser is

      function Parse
        (Scanner : in out Jintp.Scanner.Scanner_State;
         Input : in out Jintp.Input.Character_Iterator'Class;
         Settings : Environment'Class)
         return Jintp.Expression_Access;

      function Parse_With_End
        (Input : in out Jintp.Input.Character_Iterator'Class;
         Settings : Environment'Class)
         return Jintp.Expression_Access;

   end Expression_Parser;

   package body Expression_Parser is separate;

   package Template_Access_Vectors is new
     Ada.Containers.Vectors (Index_Type => Positive,
                             Element_Type => Template_Access);

   type Environment_Access is access all Environment'Class;

   function To_String (Value : Expression_Value)
                       return String;

   type Context;

   type Context_Access is access all Context;

   type Context is new Ada.Finalization.Controlled with record
      Settings : Environment_Access;
      Parent_Resolver : Context_Access;
      Values : Dictionary;
      Template_Refs : Template_Access_Vectors.Vector; -- For template inheritance
      Template_Index : Positive;
      Block_Name : Unbounded_String;
      Included_Templates : Template_Access_Vectors.Vector;
      Macros : Macro_Maps.Map;
      Imported_Templates : Template_Access_Vectors.Vector;
   end record;

   overriding procedure Finalize (Self : in out Context);

   Loop_Index0_Name : constant Unbounded_String
     := To_Unbounded_String ("loop.index0");
   Loop_Length_Name : constant Unbounded_String
     := To_Unbounded_String ("loop.length");

   function Loop_Index (Resolver : Context)
                        return Natural is
      Index0_Value : Expression_Value;
      Found : constant Boolean := Element (Resolver.Values,
                                  Loop_Index0_Name,
                                  Index0_Value);
   begin
      if Found and then Index0_Value.Kind = Integer_Expression_Value then
         return Index0_Value.I + 1;
      end if;
      raise Template_Error with "'loop.index' is undefined";
   end Loop_Index;

   function Loop_Revindex (Resolver : Context)
                        return Natural is
      Index0_Value, Length_Value : Expression_Value;
      Found : Boolean := Element (Resolver.Values,
                                  Loop_Index0_Name,
                                  Index0_Value);
   begin
      if Found and then Index0_Value.Kind = Integer_Expression_Value then
         Found := Element (Resolver.Values, Loop_Length_Name, Length_Value);
         if Found
           and then Length_Value.Kind = Integer_Expression_Value
         then
            return Length_Value.I - Index0_Value.I;
         end if;
      end if;
      raise Template_Error with "'loop.revindex' is undefined";
   end Loop_Revindex;

   function Loop_First (Resolver : Context)
                        return Boolean is
      Index0_Value : Expression_Value;
      Found : constant Boolean := Element (Resolver.Values,
                                  Loop_Index0_Name,
                                  Index0_Value);
   begin
      if Found and then Index0_Value.Kind = Integer_Expression_Value then
         return Index0_Value.I = 0;
      end if;
      raise Template_Error with "'loop.first' is undefined";
   end Loop_First;

   function Loop_Last (Resolver : Context)
                       return Boolean is
      Index0_Value, Length_Value : Expression_Value;
      Found : Boolean := Element (Resolver.Values,
                                  Loop_Index0_Name,
                                  Index0_Value);
   begin
      if Found and then Index0_Value.Kind = Integer_Expression_Value then
         Found := Element (Resolver.Values, Loop_Length_Name, Length_Value);
         if Found
           and then Length_Value.Kind = Integer_Expression_Value
         then
            return Index0_Value.I = Length_Value.I - 1;
         end if;
      end if;
      raise Template_Error with "'loop.last' is undefined";
   end Loop_Last;

   function Resolve (Resolver : Context;
                     Name : Unbounded_String)
                     return Expression_Value is
      Value : Expression_Value;
      Found : constant Boolean := Element (Resolver.Values, Name, Value);
   begin
      if Found then
         return Value;
      end if;
      if Name = "loop.index" then
         return (Kind => Integer_Expression_Value,
                 I => Loop_Index (Resolver));
      end if;
      if Name = "loop.revindex" then
         return (Kind => Integer_Expression_Value,
                 I => Loop_Revindex (Resolver));
      end if;
      if Name = "loop.revindex0" then
         return (Kind => Integer_Expression_Value,
                 I => Loop_Revindex (Resolver) - 1);
      end if;
      if Name = "loop.first" then
         return (Kind => Boolean_Expression_Value,
                 B => Loop_First (Resolver));
      end if;
      if Name = "loop.last" then
         return (Kind => Boolean_Expression_Value,
                 B => Loop_Last (Resolver));
      end if;
      if Resolver.Parent_Resolver /= null then
         return Resolve (Resolver.Parent_Resolver.all, Name);
      end if;
      raise Template_Error with "'" & To_String (Name) & "' is undefined";
   end Resolve;

   use Macro_Maps;

   function To_String (N : Integer)
                       return String is
   begin
      return Ada.Strings.Fixed.Trim (Integer'Image (N), Both);
   end To_String;

   procedure Append (Resolver : Context;
                     Target : in out Unbounded_String;
                     Name : Unbounded_String) is
      Key : constant Expression_Value := (Kind => String_Expression_Value,
                                 S => Name);
   begin
      if Contains (Resolver.Values.Assocs.Value_Assocs, Key) then
         Append (Target,
                 To_String (Association_Maps.Constant_Reference
                   (Resolver.Values.Assocs.Value_Assocs, Key)));
      elsif Name = "loop.index" then
         Append (Target, To_String (Loop_Index (Resolver)));
      elsif Name = "loop.revindex" then
         Append (Target, To_String (Loop_Revindex (Resolver)));
      elsif Name = "loop.revindex0" then
         Append (Target, To_String (Loop_Revindex (Resolver) - 1));
      elsif Name = "loop.first" then
         Append (Target, Boolean'Image (Loop_First (Resolver)));
      elsif Name = "loop.last" then
         Append (Target, Boolean'Image (Loop_Last (Resolver)));
      elsif Resolver.Parent_Resolver /= null then
         Append (Resolver.Parent_Resolver.all, Target, Name);
      end if;
   exception
      when Template_Error =>
         null; -- ignore
   end Append;

   function Current_Template_Index (Resolver : Context)
                                    return Natural is
   begin
      if not Resolver.Template_Refs.Is_Empty then
         return Resolver.Template_Index;
      end if;
      if Resolver.Parent_Resolver /= null then
         return Current_Template_Index (Resolver.Parent_Resolver.all);
      end if;
      return 0;
   end Current_Template_Index;

   function Get_Macro (Resolver : Context;
                       Name : Unbounded_String)
                       return Macro_Access is
      Position : Macro_Maps.Cursor;
   begin
      if Resolver.Parent_Resolver /= null then
         return Get_Macro (Resolver.Parent_Resolver.all,
                           Name);
      end if;
      Position := Macro_Maps.Find (Resolver.Macros, Name);
      if Position = Macro_Maps.No_Element then
         return null;
      end if;
      return Element (Position);
   end Get_Macro;

   function Get_Environment (Resolver : Context)
                             return Environment_Access is
   begin
      if Resolver.Settings /= null then
         return Resolver.Settings;
      end if;
      if Resolver.Parent_Resolver /= null then
         return Get_Environment (Resolver.Parent_Resolver.all);
      end if;
      return null;
   end Get_Environment;

   function Current_Template (Resolver : Context)
                              return Template_Access is
   begin
      if not Resolver.Template_Refs.Is_Empty then
         return Resolver.Template_Refs (Current_Template_Index (Resolver));
      end if;
      if Resolver.Parent_Resolver /= null then
         return Current_Template (Resolver.Parent_Resolver.all);
      end if;
      raise Template_Error with "internal error: no current template found";
   end Current_Template;

   procedure Set_Current_Template_Index (Resolver : in out Context;
                                                    Index : Positive)
   is
   begin
      if Resolver.Template_Refs.Is_Empty
        and then Resolver.Parent_Resolver /= null
      then
         Set_Current_Template_Index (Resolver.Parent_Resolver.all, Index);
      else
         Resolver.Template_Index := Index;
      end if;
   end Set_Current_Template_Index;

   function Template_Count (Resolver : Context)
                                       return Natural is
   begin
      if Resolver.Template_Refs.Is_Empty
        and then Resolver.Parent_Resolver /= null
      then
         return Template_Count (Resolver.Parent_Resolver.all);
      end if;
      return Natural (Resolver.Template_Refs.Length);
   end Template_Count;

   function Get_Template (Resolver : Context;
                          Index : Positive)
                          return Template_Access is
   begin
      if Resolver.Template_Refs.Is_Empty
        and then Resolver.Parent_Resolver /= null
      then
         return Get_Template (Resolver.Parent_Resolver.all, Index);
      end if;
      return Resolver.Template_Refs (Index);
   end Get_Template;

   procedure Add_Parent_Template (Resolver : in out Context;
                                  Item : Template_Access) is
   begin
      if Resolver.Template_Refs.Is_Empty
        and then Resolver.Parent_Resolver /= null
      then
         Add_Parent_Template (Resolver.Parent_Resolver.all, Item);
      else
         Resolver.Template_Refs.Prepend (Item);
      end if;
   end Add_Parent_Template;

   function Current_Block_Name (Resolver : Context)
                                return Unbounded_String is
   begin
      return Resolver.Block_Name;
   end Current_Block_Name;

   procedure Set_Current_Block_Name
     (Resolver : in out Context;
      Block_Name : Unbounded_String) is
   begin
      Resolver.Block_Name := Block_Name;
   end Set_Current_Block_Name;

   function Evaluate (Source : Expression;
                      Resolver : aliased in out Context)
                      return Expression_Value;

   package Statement_Parser is

      use Jintp.Input;

      procedure Parse (Input : in out Character_Iterator'Class;
                       Settings : Environment'Class;
                       Result : out Statement;
                       End_Modifier : out Character);

      function Parse_Endraw  (Input : in out Character_Iterator'Class;
                              Settings : Environment'Class)
                              return Boolean;

   end Statement_Parser;

   package body Statement_Parser is separate;

   procedure Free_Template is new Ada.Unchecked_Deallocation (Template,
                                                              Template_Access);

   overriding procedure Finalize (Self : in out Context) is
      Macro : Macro_Access;
   begin
      for C in Self.Macros.Iterate loop
         Macro := Element (C);
         Free_Macro (Macro);
      end loop;
      Self.Macros.Clear;

      for E of Self.Included_Templates loop
         Free_Template (E);
      end loop;
      Self.Included_Templates.Clear;

      for E of Self.Imported_Templates loop
         if not E.Cached then
            Free_Template (E);
         end if;
      end loop;
      Self.Imported_Templates.Clear;
   end Finalize;

   protected body Template_Cache is
      function Get (Path : String) return Template_Access is
         C : constant Template_Access_Maps.Cursor
           := Templates_Map.Find (To_Unbounded_String (Path));
      begin
         if C = Template_Access_Maps.No_Element then
            return null;
         end if;
         return Templates_Map (C);
      end Get;

      procedure Put (Path : String;
                     Template : Template_Access;
                     Max_Size : Natural;
                     Inserted : out Boolean) is
         Position : Template_Access_Maps.Cursor;
         Insert_Succeeded : Boolean;
      begin
         if Natural (Templates_Map.Length) >= Max_Size then
            Inserted := False;
            return;
         end if;
         Templates_Map.Insert (To_Unbounded_String (Path), Template,
                               Position, Insert_Succeeded);
         if not Insert_Succeeded then
            Templates_Map.Replace_Element (Position, Template);
         end if;
         Inserted := True;
      end Put;

      function Size return Natural is
      begin
         return Natural (Templates_Map.Length);
      end Size;

      procedure Cleanup is
      begin
         for C in Templates_Map.Iterate loop
            Free_Template (Templates_Map (C));
         end loop;
      end Cleanup;

   end Template_Cache;

   Default_Environment : Environment;

   overriding procedure Finalize (Self : in out Environment) is
   begin
      Self.Cached_Templates.Cleanup;
   end Finalize;

   procedure Cleanup (Target : in out Statement) is
   begin
      case Target.Kind is
         when If_Statement | Elif_Statement =>
            Delete_Expression (Target.If_Condition);
         when For_Statement =>
            Delete_Expression (Target.For_Expression);
            Delete_Expression (Target.For_Condition);
         when others =>
            null;
      end case;
   end Cleanup;

   procedure Cleanup (Element : in out Template_Element) is
   begin
      case Element.Kind is
         when Expression_Element =>
            Delete_Expression (Element.Expr);
         when Statement_Element =>
            Cleanup (Element.Stmt);
      end case;
   end Cleanup;

   overriding procedure Finalize (Self : in out Template) is
   begin
      for E of Self.Elements loop
         Cleanup (E);
      end loop;
      Self.Elements.Clear;
   end Finalize;

   type Stream_Element_Array_Access is access Stream_Element_Array;

   type File_Parser_Input is new Jintp.Input.Character_Iterator with record
      Buffer : Stream_Element_Array_Access;
      Pos : Stream_Element_Offset;
   end record;

   procedure Free_Stream_Element_Array is new Ada.Unchecked_Deallocation
     (Stream_Element_Array, Stream_Element_Array_Access);

   overriding function Next (Source : in out File_Parser_Input)
                             return Character;

   overriding procedure Back (Source : in out File_Parser_Input);

   overriding procedure Match (Source : in out File_Parser_Input;
                    Pattern : String;
                    Matches : out Boolean);

   overriding function Next (Source : in out File_Parser_Input)
                             return Character is
   begin
      Source.Pos := Source.Pos + 1;
      return Character'Val (Source.Buffer (Source.Pos - 1));
   end Next;

   overriding procedure Back (Source : in out File_Parser_Input) is
   begin
      Source.Pos := Source.Pos - 1;
   end Back;

   overriding procedure Match (Source : in out File_Parser_Input;
                               Pattern : String;
                               Matches : out Boolean) is
   begin
      if Natural (Source.Buffer'Last - Source.Pos) + 1 < Pattern'Length then
         Matches := False;
         return;
      end if;
      for I in Pattern'First .. Pattern'Last loop
         if Character'Val (Source.Buffer (Source.Pos
                           + Stream_Element_Offset (I - Pattern'First)))
           /= Pattern (I)
         then
            Matches := False;
            return;
         end if;
      end loop;
      Matches := True;
      Source.Pos := Source.Pos + Stream_Element_Offset (Pattern'Length);
   end Match;

   function Start_String_Length (Kind : Tag_Kind;
                                 Settings : Environment'Class)
                                 return Stream_Element_Offset is
   begin
      case Kind is
         when Expression_Element =>
            return Stream_Element_Offset (Length (Settings.Expression_Start));
         when Statement_Element =>
            return Stream_Element_Offset (Length (Settings.Statement_Start));
         when Comment_Element =>
            return Stream_Element_Offset (Length (Settings.Comment_Start));
      end case;
   end Start_String_Length;

   procedure Raise_With_Location (Message : String;
                                  File_Name : String;
                                  Line : Positive);

   pragma No_Return (Raise_With_Location);

   procedure Raise_With_Location (Message : String;
                                  File_Name : String;
                                  Line : Positive) is
   begin
      if Ada.Strings.Fixed.Index (Message, "File ") = 0 then
         raise Template_Error with "File """ & File_Name & """, line"
           & Line'Image & ": " & Message;
      end if;
      raise Template_Error with Message;
   end Raise_With_Location;

   procedure Get_Template (File_Name : String;
                           Target : out Template;
                           Settings : Environment'Class := Default_Environment) is
      Input : File_Parser_Input;

      function Buffer_Matches (Str : Unbounded_String;
                               Index : Stream_Element_Offset)
                               return Boolean is
      begin
         if Natural (Index) + Length (Str) - 1
           > Natural (Input.Buffer'Last)
         then
            return False;
         end if;
         for I in 1 .. Length (Str) loop
            if Character'Val (Input.Buffer (Index + Stream_Element_Offset (I)
                              - 1))
              /= Element (Str, I)
            then
               return False;
            end if;
         end loop;
         return True;
      end Buffer_Matches;

      procedure Find_Start (Result : out Tag_Kind;
                            Found_Pos : out Stream_Element_Offset;
                            Modifier : out Character) is
         Modifier_Candidate : Character;
      begin
         Modifier := ' ';
         for I in Input.Pos .. Input.Buffer'Last loop
            if Buffer_Matches (Settings.Expression_Start, I) then
               Found_Pos := I;
               Result := Expression_Element;
               return;
            end if;
            if Buffer_Matches (Settings.Statement_Start, I) then
               if Natural (I) + Length (Settings.Statement_Start) + 1
                 <= Natural (Input.Buffer'Last)
               then
                  Modifier_Candidate := Character'Val
                    (Input.Buffer (I + Stream_Element_Offset (
                     Length (Settings.Statement_Start))));
                  if Modifier_Candidate = '+'
                    or else Modifier_Candidate = '-'
                  then
                     Modifier := Modifier_Candidate;
                  end if;
               end if;
               Found_Pos := I;
               Result := Statement_Element;
               return;
            end if;
            if Buffer_Matches (Settings.Comment_Start, I) then
               Found_Pos := I;
               Result := Comment_Element;
               return;
            end if;
         end loop;
         Found_Pos := Input.Buffer'Last + 1;
      end Find_Start;

      procedure Find_Comment_End (Found_Pos : out Stream_Element_Offset) is
      begin
         for I in Input.Pos .. Input.Buffer'Last loop
            if Buffer_Matches (Settings.Comment_End, I) then
               Found_Pos := I;
               return;
            end if;
         end loop;
         Found_Pos := Input.Buffer'Last + 1;
      end Find_Comment_End;

      function Buffer_Slice (Low : Stream_Element_Offset;
                             High : Stream_Element_Offset)
                             return Unbounded_String is
         Slice : Unbounded_String;
      begin
         for I in Low .. High loop
            Append (Slice, Character'Val (Input.Buffer (I)));
         end loop;
         return Slice;
      end Buffer_Slice;

      procedure Skip_Linebreak is
      begin
         if Input.Pos <= Input.Buffer'Last
           and then Character'Val (Input.Buffer (Input.Pos)) = ASCII.LF
         then
            Input.Pos := Input.Pos + 1;
         elsif Input.Pos + 1 <= Input.Buffer'Last
           and then Character'Val (Input.Buffer (Input.Pos)) = ASCII.CR
           and then Character'Val (Input.Buffer (Input.Pos + 1)) = ASCII.LF
         then
            Input.Pos := Input.Pos + 2;
         end if;
      end Skip_Linebreak;

      procedure Skip_Comment is
         New_Pos : Stream_Element_Offset;
      begin
         Find_Comment_End (New_Pos);
         if New_Pos <= Input.Buffer'Last then
            Input.Pos := New_Pos
              + Stream_Element_Offset (Length (Settings.Comment_End));
            if Settings.Trim_Blocks then
               Skip_Linebreak;
            end if;
         end if;
      end Skip_Comment;

      function Line_Count (From : Stream_Element_Offset := 1;
                           To : Stream_Element_Offset := Input.Pos)
                           return Natural is
         Result : Natural := 0;
         Last : Stream_Element_Offset := To;
      begin
         begin
            if Character'Val (Input.Buffer (Last)) = ASCII.LF then
               Last := Last - 1;
            end if;
         exception
            when Constraint_Error =>
               Last := Input.Buffer'Last;
         end;
         for I in From .. Last loop
            if Character'Val (Input.Buffer (I)) = ASCII.LF then
               Result := Result + 1;
            end if;
         end loop;
         return Result;
      end Line_Count;

      function Get_Raw (Modifier : Character) return Expression_Access is
         Kind : Tag_Kind;
         Initial_Pos : Stream_Element_Offset := Input.Pos;
         Text_End_Pos : Stream_Element_Offset;
         New_Pos : Stream_Element_Offset;
         Closing_Modifier : Character;
      begin
         loop
            Find_Start (Kind, New_Pos, Closing_Modifier);
            if New_Pos > Input.Buffer'Last then
               raise Template_Error with "missing end of raw statement";
            end if;
            Text_End_Pos := New_Pos - 1;
            Input.Pos := New_Pos + Start_String_Length (Kind, Settings);
            if Modifier = '-' then
               while Jintp.Scanner.Is_Whitespace
                 (Character'Val (Input.Buffer (Initial_Pos))) loop
                  Initial_Pos := Initial_Pos + 1;
               end loop;
            end if;
            if Jintp.Statement_Parser.Parse_Endraw (Input, Settings) then
               return new Expression'
                 (Kind => Literal,
                  Value => (Kind => String_Expression_Value,
                            S => Buffer_Slice (Initial_Pos, Text_End_Pos)));
            end if;
         end loop;
      end Get_Raw;

      File : File_Type;
      Kind : Tag_Kind;
      New_Expression : Expression_Access;
      New_Statement : Statement;
      New_Pos : Stream_Element_Offset;
      Input_Size : Ada.Directories.File_Size
        := Ada.Directories.Size (File_Name);
      Current_Line : Positive := 1;
      Last_Pos : Stream_Element_Offset := 1;
      Modifier : Character;
   begin
      Input.Pos := 1;
      Input.Buffer := new Stream_Element_Array
        (1 .. Stream_Element_Offset (Input_Size));
      Open (File, In_File, File_Name);
      Read (File, Input.Buffer.all,
            Stream_Element_Offset (Input_Size));
      Close (File);
      Target.File_Name := To_Unbounded_String (File_Name);
      loop
         Find_Start (Kind, New_Pos, Modifier);
         if New_Pos <= Input.Buffer'Last then
            if New_Pos > Input.Pos then
               declare
                  Last_Char_Pos : Stream_Element_Offset := New_Pos - 1;
               begin
                  if (Settings.Lstrip_Blocks and then Modifier /= '+') and then
                    (Kind = Statement_Element or else Kind = Comment_Element)
                  then
                     while Last_Char_Pos > 0
                       and then (Character'Val (Input.Buffer (Last_Char_Pos))
                                 = ' '
                                 or else Character'Val (Input.Buffer (Last_Char_Pos))
                                 = ASCII.VT) loop
                        Last_Char_Pos := Last_Char_Pos - 1;
                     end loop;
                  end if;
                  if Modifier = '-' then
                     while Last_Char_Pos > 0
                       and then Jintp.Scanner.Is_Whitespace
                         (Character'Val (Input.Buffer (Last_Char_Pos))) loop
                        Last_Char_Pos := Last_Char_Pos - 1;
                     end loop;
                  end if;
                  New_Expression := new Expression'
                    (Kind => Literal,
                     Value => (Kind => String_Expression_Value,
                               S => Buffer_Slice (Input.Pos, Last_Char_Pos)));
               end;
               Target.Elements.Append ((Line => Current_Line,
                                        Kind => Expression_Element,
                                        Expr => New_Expression));
            end if;
            Input.Pos := New_Pos + Start_String_Length (Kind, Settings);
            if Modifier = '+' or else Modifier = '-' then
               Input.Pos := Input.Pos + 1;
            end if;
            begin
               case Kind is
               when Expression_Element =>
                  New_Expression := Jintp.Expression_Parser.
                    Parse_With_End (Input, Settings);
                  Current_Line := Current_Line + Line_Count (Last_Pos,
                                                             Input.Pos - 1);
                  Last_Pos := Input.Pos;
                  Target.Elements.Append ((Line => Current_Line,
                                           Kind => Expression_Element,
                                           Expr => New_Expression));
               when Statement_Element =>
                  Jintp.Statement_Parser.Parse (Input,
                                                Settings,
                                                New_Statement,
                                                Modifier);
                  Current_Line := Current_Line + Line_Count (Last_Pos,
                                                             Input.Pos - 1);
                  if Settings.Trim_Blocks and then Modifier /= '+'
                  then
                     Skip_Linebreak;
                  end if;
                  Last_Pos := Input.Pos;
                  if New_Statement.Kind = Raw_Statement then
                     Target.Elements.Append ((Line => Current_Line,
                                              Kind => Expression_Element,
                                              Expr => Get_Raw (Modifier)));
                  else
                     Target.Elements.Append ((Line => Current_Line,
                                              Kind => Statement_Element,
                                              Stmt => New_Statement));
                     if New_Statement.Kind = Block_Statement then
                        Target.Block_Map.Include
                          (New_Statement.Block_Name,
                           Positive (Target.Elements.Length));
                     end if;
                  end if;
                  if Modifier = '-'
                  then
                     while Input.Pos <= Input.Buffer'Last and then
                       Jintp.Scanner.Is_Whitespace
                         (Character'Val (Input.Buffer (Input.Pos)))
                     loop
                        Input.Pos := Input.Pos + 1;
                     end loop;
                  end if;
               when Comment_Element =>
                  Skip_Comment;
               end case;
            exception
               when E : Template_Error =>
                  Raise_With_Location (Message =>
                                          Ada.Exceptions.Exception_Message (E),
                                       File_Name => File_Name,
                                       Line => Line_Count + 1);
            end;
         else
            Last_Pos := Input.Buffer'Last;
            if Last_Pos > Input.Buffer'First
              and then Character'Val (Input.Buffer (Last_Pos)) = ASCII.LF
            then
               Last_Pos := Last_Pos - 1;
               if Last_Pos > Input.Buffer'First
                 and then Character'Val (Input.Buffer (Last_Pos)) = ASCII.CR
               then
                  Last_Pos := Last_Pos - 1;
               end if;
            end if;

            New_Expression := new Expression'
              (Kind => Literal,
               Value => (Kind => String_Expression_Value,
                         S => Buffer_Slice (
                           Input.Pos,
                           Last_Pos)
                        )
              );
            Target.Elements.Append ((Line => Current_Line,
                                     Kind => Expression_Element,
                                     Expr => New_Expression));
            exit;
         end if;
      end loop;
      Free_Stream_Element_Array (Input.Buffer);
   exception
      when others =>
         Free_Stream_Element_Array (Input.Buffer);
         if Is_Open (File) then
            Close (File);
         end if;
         raise;
   end Get_Template;

   function Remove_Trailing_Zeroes (Source : Unbounded_String)
                                    return String
   is
      Last : Positive := Length (Source);
   begin
      while Last > 2
        and then Element (Source, Last) = '0'
        and then Element (Source, Last - 1) /= '.' loop
         Last := Last - 1;
      end loop;
      return Slice (Source, 1, Last);
   end Remove_Trailing_Zeroes;

   package Long_Float_IO is new
     Ada.Text_IO.Float_IO (Num => Long_Float);

   function To_String (Value : Expression_Value)
                       return String is
      Buffer : Unbounded_String;
      First_Element : Boolean := True;
      Float_Buffer : String (1 .. 32);
      Dec_Exponent : Integer;
   begin
      case Value.Kind is
         when String_Expression_Value =>
            return To_String (Value.S);
         when Boolean_Expression_Value =>
            return Value.B'Image;
         when Integer_Expression_Value =>
            return To_String (Value.I);
         when Float_Expression_Value =>
            Dec_Exponent := Long_Float'Exponent (Value.F) * 3 / 10;
            if Dec_Exponent
              <= Float_Buffer'Last - Long_Float_IO.Default_Aft - 3
              and then Dec_Exponent >= -3
            then
               Long_Float_IO.Put (Float_Buffer, Value.F,
                                  Long_Float_IO.Default_Aft, 0);
               return Remove_Trailing_Zeroes (Trim (
                    To_Unbounded_String (Float_Buffer),
                  Both));
            end if;
            Long_Float_IO.Put (Float_Buffer, Value.F);
            return Ada.Strings.Fixed.Trim (Float_Buffer, Both);
         when List_Expression_Value =>
            Append (Buffer, '[');
            for V of Value.List_Value.Elements.Values loop
               if not First_Element then
                  Append (Buffer, ", ");
               end if;
               First_Element := False;
               Append (Buffer, "'");
               Append (Buffer, To_String (V));
               Append (Buffer, "'");
            end loop;
            Append (Buffer, ']');
            return To_String (Buffer);
         when Dictionary_Expression_Value =>
            Append (Buffer, '{');
            for C in Value.Dictionary_Value.Assocs.Value_Assocs.Iterate loop
               if not First_Element then
                  Append (Buffer, ", ");
               end if;
               First_Element := False;
               Append (Buffer, "'");
               Append (Buffer, To_String (Key (C)));
               Append (Buffer, "'");
               Append (Buffer, ": ");
               Append (Buffer, "'");
               Append (Buffer, To_String (Element (C)));
               Append (Buffer, "'");
            end loop;
            Append (Buffer, '}');
            return To_String (Buffer);
      end case;
   end To_String;

   function "<" (Left, Right : Expression_Value) return Boolean is
   begin
      if Left.Kind /= Right.Kind then
         raise Template_Error
           with "comparison not supported for values of different type";
      end if;
      case Left.Kind is
         when String_Expression_Value =>
            return Left.S < Right.S;
         when Boolean_Expression_Value =>
            return Left.B < Right.B;
         when Integer_Expression_Value =>
            return Left.I < Right.I;
         when Float_Expression_Value =>
            return Left.F < Right.F;
         when others =>
            raise Template_Error
              with "comparison not supported for this type";
      end case;
   end "<";

   function Evaluate (Source : Expression;
                      Resolver : aliased in out Context)
                      return String;

   function To_Float (V : Expression_Value) return Long_Float is
   begin
      case V.Kind is
         when Integer_Expression_Value => return Long_Float (V.I);
         when Float_Expression_Value => return V.F;
         when others => raise Template_Error  with "numeric value expected";
      end case;
   end To_Float;

   package Filters is

      function Evaluate_Filter
        (Source : Expression;
         Resolver : aliased in out Context)
      return Jintp.Expression_Value;

   end Filters;

   package body Filters is separate;

   procedure Put (File : Ada.Text_IO.File_Type;
                  Item : Expression_Value) is
   begin
      Ada.Text_IO.Put (File, Item.Kind'Image);
      Ada.Text_IO.Put (File, ": ");
      Ada.Text_IO.Put (File, To_String (Item));
   end Put;

   procedure Put (File : Ada.Text_IO.File_Type;
                  Item : Expression) is
   begin
      Ada.Text_IO.Put (File, Item.Kind'Image);
      Ada.Text_IO.Put (File, ':');
      case Item.Kind is
         when Literal =>
            Put (File, Item.Value);
         when Variable =>
            Ada.Text_IO.Put (File, To_String (Item.Variable_Name));
         when Operator_Super .. Operator_Macro =>
            Ada.Text_IO.Put (File, To_String (Item.Name));
            Ada.Text_IO.Put (File, '(');
            for I in Item.Arguments'First .. Item.Arguments'Last loop
               if Item.Arguments (I) = null then
                  exit;
               end if;
               Put (File, Item.Arguments (I).all);
               Ada.Text_IO.Put (File, ',');
            end loop;
         when Filter =>
            Put (File, Item.Arguments (1).all);
            Ada.Text_IO.Put ('|');
            Ada.Text_IO.Put (To_String (Item.Name));
         when Test =>
            Put (File, Item.Arguments (1).all);
            Ada.Text_IO.Put (File, " is ");
            Ada.Text_IO.Put (To_String (Item.Name));
      end case;
   end Put;

   procedure Put (File : Ada.Text_IO.File_Type;
                  Item : Statement) is
   begin
      Ada.Text_IO.Put (File, Item.Kind'Image);
      Ada.Text_IO.Put (File, ':');
      case Item.Kind is
         when If_Statement =>
            Put (File, Item.If_Condition.all);
         when others =>
            null;
      end case;
   end Put;
   pragma Unreferenced (Put);

   function Is_Numeric (Kind : Value_Kind) return Boolean is
   begin
      return Kind = Integer_Expression_Value
        or else Kind = Float_Expression_Value;
   end Is_Numeric;

   function Evaluate_Add (Source : Expression;
                          Resolver : aliased in out Context)
                          return Expression_Value
   is
      Left_Arg : constant Expression_Value
        := Evaluate (Source.Named_Arguments (1).Argument.all, Resolver);
      Right_Arg : Expression_Value;
   begin
      if Length (Source.Named_Arguments) = 1 then
         return Left_Arg;
      end if;
      Right_Arg := Evaluate (Source.Named_Arguments (2).Argument.all, Resolver);
      if Left_Arg.Kind = String_Expression_Value
        and then Right_Arg.Kind = String_Expression_Value
      then
         return (Kind => String_Expression_Value,
                 S => Left_Arg.S & Right_Arg.S);
      end if;
      if not Is_Numeric (Left_Arg.Kind)
        or else not Is_Numeric (Right_Arg.Kind)
      then
         raise Template_Error with
           "arguments of + operator must both be either string or numeric";
      end if;
      if Left_Arg.Kind = Integer_Expression_Value
        and then Right_Arg.Kind = Integer_Expression_Value
      then
         return (Kind => Integer_Expression_Value,
                 I => Left_Arg.I + Right_Arg.I);
      end if;
      return (Kind => Float_Expression_Value,
              F => To_Float (Left_Arg) + To_Float (Right_Arg));
   end Evaluate_Add;

   function Evaluate_Subtract (Source : Expression;
                               Resolver : aliased in out Context)
                               return Expression_Value
   is
      Left_Arg : constant Expression_Value := Evaluate
        (Source.Named_Arguments (1).Argument.all, Resolver);
      Right_Arg : Expression_Value;

   begin
      if not Is_Numeric (Left_Arg.Kind) then
         raise Template_Error with
           "arguments of - operator must be numeric";
      end if;
      if Length (Source.Named_Arguments) = 1 then
         if Left_Arg.Kind = Integer_Expression_Value
         then
            return (Kind => Integer_Expression_Value,
                    I => -Left_Arg.I);
         end if;
         return (Kind => Float_Expression_Value,
                 F => -Left_Arg.F);
      end if;
      Right_Arg := Evaluate (Source.Named_Arguments (2).Argument.all,
                                Resolver);
      if not Is_Numeric (Right_Arg.Kind) then
         raise Template_Error with
           "arguments of - operator must be numeric";
      end if;
      if Left_Arg.Kind = Integer_Expression_Value
        and then Right_Arg.Kind = Integer_Expression_Value
      then
         return (Kind => Integer_Expression_Value,
                 I => Left_Arg.I - Right_Arg.I);
      end if;
      return (Kind => Float_Expression_Value,
              F => To_Float (Left_Arg) - To_Float (Right_Arg));
   end Evaluate_Subtract;

   function Evaluate_Mul (Source : Expression;
                          Resolver : aliased in out Context)
                          return Expression_Value
   is
      Left_Arg : constant Expression_Value
        := Evaluate (Source.Named_Arguments (1).Argument.all, Resolver);
      Right_Arg : constant Expression_Value
        := Evaluate (Source.Named_Arguments (2).Argument.all, Resolver);
   begin
      if not Is_Numeric (Left_Arg.Kind) or else not Is_Numeric (Right_Arg.Kind)
      then
         raise Template_Error with
           "arguments of * operator must both be numeric";
      end if;
      if Left_Arg.Kind = Integer_Expression_Value
        and then Right_Arg.Kind = Integer_Expression_Value
      then
         return (Kind => Integer_Expression_Value,
                 I => Left_Arg.I * Right_Arg.I);
      end if;
      return (Kind => Float_Expression_Value,
              F => To_Float (Left_Arg) * To_Float (Right_Arg));
   end Evaluate_Mul;

   function Evaluate_Div (Source : Expression;
                          Resolver : aliased in out Context)
                          return Expression_Value
   is
      Left_Arg : constant Expression_Value
        := Evaluate (Source.Named_Arguments (1).Argument.all, Resolver);
      Right_Arg : constant Expression_Value
        := Evaluate (Source.Named_Arguments (2).Argument.all, Resolver);
   begin
      if not Is_Numeric (Left_Arg.Kind)
        or else not Is_Numeric (Right_Arg.Kind)
      then
         raise Template_Error with
           "arguments of / operator must both be numeric";
      end if;
      return (Kind => Float_Expression_Value,
              F => To_Float (Left_Arg) / To_Float (Right_Arg));
   end Evaluate_Div;

   function Evaluate_Integer_Div (Source : Expression;
                                  Resolver : aliased in out Context)
                                  return Expression_Value
   is
      Left_Arg : constant Expression_Value
        := Evaluate (Source.Named_Arguments (1).Argument.all, Resolver);
      Right_Arg : constant Expression_Value
        := Evaluate (Source.Named_Arguments (2).Argument.all, Resolver);
   begin
      if Left_Arg.Kind /= Integer_Expression_Value
        or else Right_Arg.Kind /= Integer_Expression_Value
      then
         raise Template_Error with
           "arguments of // operator must both be integer";
      end if;
      return (Kind => Integer_Expression_Value,
              I => Left_Arg.I / Right_Arg.I);
   end Evaluate_Integer_Div;

   package Long_Float_Elementary_Functions is new
     Ada.Numerics.Generic_Elementary_Functions (Float_Type => Long_Float);

   use Long_Float_Elementary_Functions;

   function Evaluate_Power (Source : Expression;
                            Resolver : aliased in out Context)
                            return Expression_Value
   is
      Left_Arg : constant Expression_Value
        := Evaluate (Source.Named_Arguments (1).Argument.all, Resolver);
      Right_Arg : constant Expression_Value
        := Evaluate (Source.Named_Arguments (2).Argument.all, Resolver);
   begin
      if not Is_Numeric (Left_Arg.Kind) or else not Is_Numeric (Right_Arg.Kind)
      then
         raise Template_Error with
           "arguments of * operator must both be numeric";
      end if;
      return (Kind => Float_Expression_Value,
              F => To_Float (Left_Arg) ** To_Float (Right_Arg));
   end Evaluate_Power;

   procedure Execute_Statement
     (Stmt : Statement;
      Current : in out Template_Element_Vectors.Cursor;
      Out_Buffer : in out Unbounded_String;
      Resolver : aliased in out Context);

   procedure Append_Value (Target : in out Unbounded_String;
                           Source : Expression;
                           Resolver : aliased in out Context) is
   begin
      case Source.Kind is
         when Literal =>
            Append (Target, To_String (Source.Value));
         when Variable =>
            Append (Resolver, Target, Source.Variable_Name);
         when Operator_Dot =>
            if Length (Source.Named_Arguments) = 2
              and then Source.Named_Arguments (1).Argument.Kind = Variable
              and then Source.Named_Arguments (1).Argument.Variable_Name = "loop"
              and then Source.Named_Arguments (2).Argument.Kind = Variable
            then
               Append
                 (Resolver,
                  Target,
                  To_Unbounded_String ("loop.")
                  & Source.Named_Arguments (2).Argument.Variable_Name);
            else
               Append (Target, Evaluate (Source, Resolver));
            end if;
         when others =>
            Append (Target, Evaluate (Source, Resolver));
      end case;
   end Append_Value;

   function Render (File_Name : String;
                    Resolver : aliased in out Context)
                    return Unbounded_String;

   function Render (Source : Template_Element_Vectors.Vector;
                    File_Name : String;
                    Resolver : aliased in out Context)
                    return Unbounded_String is
      Out_Buffer : Unbounded_String;
      Current : Template_Element_Vectors.Cursor := First (Source);
      Element : Template_Element;
   begin
      while Current /= Template_Element_Vectors.No_Element loop
         Element := Template_Element_Vectors.Element (Current);
         begin
            case Element.Kind is
            when Expression_Element =>
               Append_Value (Out_Buffer, Element.Expr.all, Resolver);
            when Statement_Element =>
               case Element.Stmt.Kind is
               when Extends_Statement =>
                  Append (Out_Buffer,
                          Render (To_String (Element.Stmt.Parent_Name),
                           Resolver));
                  return Out_Buffer;
               when others =>
                  Execute_Statement (Element.Stmt,
                                     Current,
                                     Out_Buffer,
                                     Resolver);
               end case;
            end case;
         exception
            when E : Template_Error =>
                  Raise_With_Location (Message =>
                                          Ada.Exceptions.Exception_Message (E),
                                       File_Name => File_Name,
                                       Line => Element.Line);
         end;
         Next (Current);
      end loop;
      return Out_Buffer;
   end Render;

   procedure Execute_Block (Start_Index : Positive;
                            T : Template;
                            Out_Buffer : in out Unbounded_String;
                            Resolver : aliased in out Context) is
      Position : Template_Element_Vectors.Cursor
        := T.Elements.To_Cursor (Start_Index + 1);
      Current_Element : Template_Element;
   begin
      while Position /= Template_Element_Vectors.No_Element loop
         Current_Element := Template_Element_Vectors.Element (Position);
         case Current_Element.Kind is
            when Expression_Element =>
               Append (Out_Buffer,
                       Evaluate (Current_Element.Expr.all,
                         Resolver));
            when Statement_Element =>
               if Current_Element.Stmt.Kind = Endblock_Statement then
                  exit;
               end if;
               Execute_Statement (Current_Element.Stmt,
                                  Position,
                                  Out_Buffer,
                                  Resolver);
         end case;
         Position := Template_Element_Vectors.Next (Position);
      end loop;
   exception
      when E : Template_Error =>
         Raise_With_Location (Message =>
                                 Ada.Exceptions.Exception_Message (E),
                              File_Name => To_String (T.File_Name),
                              Line => Current_Element.Line);
   end Execute_Block;

   function Evaluate_Super (Resolver : aliased in out Context;
                            Level : Positive)
                            return Unbounded_String
   is
      Old_Template_Index : constant Positive := Current_Template_Index (Resolver);
      Out_Buffer : Unbounded_String;
   begin
      if Current_Template_Index (Resolver) < Level + 1 then
         raise Template_Error with "parent block not found";
      end if;
      Set_Current_Template_Index (Resolver, Current_Template_Index (Resolver) - Level);
      declare
         Element_Index : constant Positive := Current_Template (Resolver)
           .Block_Map (Current_Block_Name (Resolver));
      begin
         Execute_Block (Element_Index,
                        Current_Template (Resolver).all,
                        Out_Buffer,
                        Resolver);
      exception
         when Constraint_Error =>
            raise Template_Error with "parent block not found";
      end;
      Set_Current_Template_Index (Resolver, Old_Template_Index);
      return Out_Buffer;
   end Evaluate_Super;

   function Evaluate_Operator
     (Source : Expression;
      Resolver : aliased in out Context)
      return Expression_Value
     with Pre => Source.Kind in Operator_Super .. Operator_Macro
   is
      Left_Arg : Expression_Value;
      Right_Arg : Expression_Value;
      Macro : Macro_Access;
      Nested_Expression : Expression_Access;
      Level : Positive;
   begin
      case Source.Kind is
         when Operator_Super =>
            if Source.Named_Arguments.Length <= 1 then
               Level := 1;
               if not Source.Named_Arguments.Is_Empty then
                  Level := 2;
                  Nested_Expression := Source.Named_Arguments.Element (1).Argument;
                  while Nested_Expression.Kind = Operator_Dot
                    and then Nested_Expression.Named_Arguments.Length = 2
                    and then Nested_Expression.Named_Arguments.Element (2).Argument.Kind = Variable
                    and then Nested_Expression.Named_Arguments.Element (2).Argument.Variable_Name = "super"
                  loop
                     Level := Level + 1;
                     Nested_Expression := Nested_Expression.Named_Arguments.Element (1).Argument;
                  end loop;
                  if Nested_Expression.Kind /= Variable then
                     raise Template_Error with "invalid use of 'super'";
                  end if;
                  if Nested_Expression.Variable_Name /= "super" then
                     raise Template_Error with To_String (Nested_Expression.Variable_Name)
                       & " is undefined";
                  end if;
               end if;
               return (Kind => String_Expression_Value,
                       S => Evaluate_Super (Resolver, Level));
            end if;
         when Operator_Not =>
            Left_Arg := Evaluate (Source.Named_Arguments (1).Argument.all,
                                  Resolver);
            if Left_Arg.Kind /= Boolean_Expression_Value then
               raise Template_Error with "boolean expression expected";
            end if;
            return (Kind => Boolean_Expression_Value,
                    B => not Left_Arg.B);
         when Operator_Or =>
            Left_Arg := Evaluate (Source.Named_Arguments (1).Argument.all,
                                  Resolver);
            if Left_Arg.Kind /= Boolean_Expression_Value then
               raise Template_Error with "boolean expression expected";
            end if;
            if Left_Arg.B then
               return (Kind => Boolean_Expression_Value,
                       B => True);
            end if;
            Right_Arg := Evaluate (Source.Named_Arguments (2).Argument.all,
                                   Resolver);
            if Right_Arg.Kind /= Boolean_Expression_Value then
               raise Template_Error with "boolean expression expected";
            end if;
            return (Kind => Boolean_Expression_Value,
                    B => Right_Arg.B);
         when Operator_And =>
            Left_Arg := Evaluate (Source.Named_Arguments (1).Argument.all,
                                  Resolver);
            if Left_Arg.Kind /= Boolean_Expression_Value then
               raise Template_Error with "boolean expression expected";
            end if;
            if not Left_Arg.B then
               return (Kind => Boolean_Expression_Value,
                       B => False);
            end if;
            Right_Arg := Evaluate (Source.Named_Arguments (2).Argument.all,
                                   Resolver);
            if Right_Arg.Kind /= Boolean_Expression_Value then
               raise Template_Error with "boolean expression expected";
            end if;
            return (Kind => Boolean_Expression_Value,
                    B => Right_Arg.B);
         when Operator_Eq =>
            Left_Arg := Evaluate (Source.Named_Arguments (1).Argument.all,
                                  Resolver);
            Right_Arg := Evaluate (Source.Named_Arguments (2).Argument.all,
                                   Resolver);
            return (Kind => Boolean_Expression_Value,
                    B => Left_Arg = Right_Arg);
         when Operator_Neq =>
            Left_Arg := Evaluate (Source.Named_Arguments (1).Argument.all,
                                  Resolver);
            Right_Arg := Evaluate (Source.Named_Arguments (2).Argument.all,
                                   Resolver);
            return (Kind => Boolean_Expression_Value,
                    B => Left_Arg /= Right_Arg);
         when Operator_Lt =>
            Left_Arg := Evaluate (Source.Named_Arguments (1).Argument.all,
                                  Resolver);
            Right_Arg := Evaluate (Source.Named_Arguments (2).Argument.all,
                                   Resolver);
            return (Kind => Boolean_Expression_Value,
                    B => Left_Arg < Right_Arg);
         when Operator_Ge =>
            Left_Arg := Evaluate (Source.Named_Arguments (1).Argument.all,
                                  Resolver);
            Right_Arg := Evaluate (Source.Named_Arguments (2).Argument.all,
                                   Resolver);
            return (Kind => Boolean_Expression_Value,
                    B => not (Right_Arg < Left_Arg));
         when Operator_Gt =>
            Left_Arg := Evaluate (Source.Named_Arguments (1).Argument.all,
                                  Resolver);
            Right_Arg := Evaluate (Source.Named_Arguments (2).Argument.all,
                                   Resolver);
            return (Kind => Boolean_Expression_Value,
                    B => Right_Arg < Left_Arg);
         when Operator_Le =>
            Left_Arg := Evaluate (Source.Named_Arguments (1).Argument.all,
                                  Resolver);
            Right_Arg := Evaluate (Source.Named_Arguments (2).Argument.all,
                                   Resolver);
            return (Kind => Boolean_Expression_Value,
                    B => not (Left_Arg < Right_Arg));
         when Operator_Tilde =>
            declare
               Left_String : constant String
                 := Evaluate (Source.Named_Arguments (1).Argument.all,
                              Resolver);
               Right_String : constant String
                 := Evaluate (Source.Named_Arguments (2).Argument.all,
                              Resolver);
            begin
               return (Kind => String_Expression_Value,
                       S => To_Unbounded_String (Left_String & Right_String));
            end;
         when Operator_Plus =>
            return Evaluate_Add (Source, Resolver);
         when Operator_Minus =>
            return Evaluate_Subtract (Source, Resolver);
         when Operator_Mul =>
            return Evaluate_Mul (Source, Resolver);
         when Operator_Div =>
            return Evaluate_Div (Source, Resolver);
         when Operator_Integer_Div =>
            return Evaluate_Integer_Div (Source, Resolver);
         when Operator_Power =>
            return Evaluate_Power (Source, Resolver);
         when Operator_Dot =>
            if Source.Named_Arguments (1).Argument.Kind = Variable
              and then Source.Named_Arguments (1).Argument.Variable_Name = "loop"
              and then Source.Named_Arguments (1).Argument.Kind = Variable
            then
               return Resolve
                 (Resolver,
                  "loop." & Source.Named_Arguments (2).Argument.Variable_Name);
            end if;
            Left_Arg := Evaluate (Source.Named_Arguments (1).Argument.all,
                                  Resolver);
            if Left_Arg.Kind /= Dictionary_Expression_Value then
               raise Template_Error with "dictionary expected before '.'";
            end if;
            if Source.Named_Arguments (2).Argument.Kind /= Variable then
               raise Template_Error with "identifier expected after '.'";
            end if;
            begin
               return Element (Left_Arg.Dictionary_Value,
                               Source.Named_Arguments (2).Argument.Variable_Name);
            exception
               when Constraint_Error =>
                  raise Template_Error with "dictionary has no attribute '"
                    & To_String (Source.Named_Arguments (2).Argument.Variable_Name)
                    & ''';
            end;
         when Operator_Brackets =>
            Left_Arg := Evaluate (Source.Named_Arguments (1).Argument.all,
                                  Resolver);
            Right_Arg := Evaluate (Source.Named_Arguments (2).Argument.all,
                                   Resolver);
            if Left_Arg.Kind = Dictionary_Expression_Value then
               if Right_Arg.Kind /= String_Expression_Value then
                  raise Template_Error with "dictionary index must be a string";
               end if;
               begin
                  return Left_Arg.Dictionary_Value.Assocs.Value_Assocs
                    (Right_Arg);
               exception
                  when Constraint_Error =>
                     raise Template_Error with "dictionary has no attribute '"
                       & To_String (Right_Arg.S) & ''';
               end;
            elsif Left_Arg.Kind = List_Expression_Value then
               if Right_Arg.Kind /= Integer_Expression_Value then
                  raise Template_Error with "list index must be integer";
               end if;
               begin
                  return Left_Arg.List_Value.Elements.Values (Right_Arg.I);
               exception
                  when Constraint_Error =>
                     raise Template_Error with "list has no element "
                       & Right_Arg.I'Image;
               end;
            else
               raise Template_Error with "dictionary or list expected before '['";
            end if;
         when Operator_Macro =>
            Macro := (if Source.Macro_Variable_Name = Null_Unbounded_String
                      then Get_Macro (Resolver, Source.Macro_Name)
                      else Get_Macro (Resolver, Source.Macro_Variable_Name
                        & "." & Source.Macro_Name));
            if Macro = null then
               raise Template_Error with "macro '"
                 & To_String (Source.Macro_Name) & "' not found";
            end if;
            declare
               Macro_Resolver : aliased Context;
               Position : Named_Argument_Vectors.Cursor;
            begin
               Macro_Resolver.Parent_Resolver := Resolver'Unchecked_Access;
               for I in 1 .. Macro.Parameters.Length loop
                  if I <= Source.Macro_Arguments.Length
                    and then Source.Macro_Arguments (Positive (I)).Name
                      = Null_Unbounded_String
                  then
                     --  Positional parameter
                     Macro_Resolver.Values.Insert
                       (Macro.Parameters (Positive (I)).Name,
                        Evaluate
                          (Source.Macro_Arguments (Positive (I)).Argument.all,
                           Resolver));
                  else
                     Position := Find_Named_Argument
                       (Source.Macro_Arguments,
                        Macro.Parameters (Positive (I)).Name);
                     if Position /= Named_Argument_Vectors.No_Element then
                        --  Named parameter
                        Macro_Resolver.Values.Insert
                          (Macro.Parameters (Positive (I)).Name,
                           Evaluate
                             (Named_Argument_Vectors.Element (Position).Argument.all,
                              Resolver));
                     elsif Macro.Parameters (Positive (I)).Has_Default_Value then
                        --  Default parameter value
                        Init (Macro_Resolver.Values);
                        Include (Macro_Resolver.Values.Assocs.Value_Assocs,
                                 (Kind => String_Expression_Value,
                                  S => Macro.Parameters (Positive (I)).Name),
                                 Macro.Parameters (Positive (I)).Default_Value);
                     end if;
                  end if;
               end loop;
               return (Kind => String_Expression_Value,
                       S => Render (Macro.Elements, "", Macro_Resolver));
            end;
         when others =>
            null;
      end case;
      raise Template_Error with "invalid usage of " & Source.Kind'Image;
   end Evaluate_Operator;

   function Evaluate_Test
     (Source : Expression;
      Resolver : aliased in out Context)
      return Expression_Value
   is
      Source_Value : Expression_Value;
   begin
      if Source.Name = "defined" then
         if Source.Arguments (1).Kind /= Variable then
            raise Template_Error with "variable expected";
         end if;
         begin
            Source_Value := Resolve (Resolver,
                                     Source.Arguments (1).Variable_Name);
            return (Kind => Boolean_Expression_Value,
                    B => True
                   );
         exception
            when Template_Error =>
               return (Kind => Boolean_Expression_Value,
                       B => False
                      );
         end;
      end if;
      if Source.Name = "undefined" then
         if Source.Arguments (1).Kind /= Variable then
            raise Template_Error with "variable expected";
         end if;
         begin
            Source_Value := Resolve (Resolver,
                                     Source.Arguments (1).Variable_Name);
            return (Kind => Boolean_Expression_Value,
                    B => False
                   );
         exception
            when Template_Error =>
               return (Kind => Boolean_Expression_Value,
                       B => True
                      );
         end;
      end if;
      Source_Value := Evaluate (Source.Arguments (1).all,
                                Resolver);
      if Source.Name = "boolean" then
         return (Kind => Boolean_Expression_Value,
                 B => Source_Value.Kind = Boolean_Expression_Value);
      end if;
      if Source.Name = "false" then
         return (Kind => Boolean_Expression_Value,
                 B => Source_Value.Kind = Boolean_Expression_Value
                      and then not Source_Value.B);
      end if;
      if Source.Name = "true" then
         return (Kind => Boolean_Expression_Value,
                 B => Source_Value.Kind = Boolean_Expression_Value
                      and then Source_Value.B);
      end if;
      if Source.Name = "integer" then
         return (Kind => Boolean_Expression_Value,
                 B => Source_Value.Kind = Integer_Expression_Value);
      end if;
      if Source.Name = "even" then
         return (Kind => Boolean_Expression_Value,
                 B => Source_Value.I mod 2 = 0);
      end if;
      if Source.Name = "odd" then
         return (Kind => Boolean_Expression_Value,
                 B => Source_Value.I mod 2 = 1);
      end if;
      if Source.Name = "float" then
         return (Kind => Boolean_Expression_Value,
                 B => Source_Value.Kind = Float_Expression_Value);
      end if;
      if Source.Name = "string" then
         return (Kind => Boolean_Expression_Value,
                 B => Source_Value.Kind = String_Expression_Value);
      end if;
      if Source.Name = "sequence" then
         return (Kind => Boolean_Expression_Value,
                 B => Source_Value.Kind = List_Expression_Value
                      or else Source_Value.Kind = Dictionary_Expression_Value
                );
      end if;
      if Source.Name = "divisibleby" then
         if Source.Arguments (2) = null then
            raise Template_Error
              with "invalid number of arguments to 'divisibleby'";
         end if;
         declare
            Source_Value_2 : constant Expression_Value
              := Evaluate (Source.Arguments (2).all,
                           Resolver);
         begin
            if Source_Value.Kind /= Integer_Expression_Value
              or else Source_Value_2.Kind /= Integer_Expression_Value
            then
               raise Template_Error with "integer arguments expected";
            end if;
            return (Kind => Boolean_Expression_Value,
                    B => Source_Value.I mod Source_Value_2.I = 0);
         end;
      end if;
      if Source.Name = "in" then
         if Source.Arguments (2) = null then
            raise Template_Error
              with "invalid number of arguments to 'in'";
         end if;
         declare
            Source_Value_2 : constant Expression_Value
              := Evaluate (Source.Arguments (2).all,
                           Resolver);
         begin
            if Source_Value_2.Kind /= List_Expression_Value
            then
               raise Template_Error with "'in' is only supported for lists";
            end if;
            return (Kind => Boolean_Expression_Value,
                    B => Contains (Source_Value_2.List_Value.Elements.Values,
                     Source_Value));
         end;
      end if;
      raise Template_Error with "no test named '"
        & To_String (Source.Name) & "'";
   end Evaluate_Test;

   function Evaluate (Source : Expression;
                      Resolver : aliased in out Context)
                      return Expression_Value is
   begin
      case Source.Kind is
         when Literal =>
            return Source.Value;
         when Variable =>
            return Resolve (Resolver, Source.Variable_Name);
         when Operator_Super .. Operator_Macro =>
            return Evaluate_Operator (Source, Resolver);
         when Filter =>
            return Filters.Evaluate_Filter (Source, Resolver);
         when Test =>
            return Evaluate_Test (Source, Resolver);
      end case;
   end Evaluate;

   function Evaluate (Source : Expression;
                      Resolver : aliased in out Context)
                      return String
   is
   begin
      return To_String (Evaluate (Source, Resolver));
   exception
      when Template_Error =>
         case Source.Kind is
            when Variable =>
               return "";
            when Operator_Brackets | Operator_Dot =>
               return "";
            when others =>
               null;
         end case;
         raise;
   end Evaluate;

   procedure Insert_Value (Container : in out Dictionary;
                     Key : Unbounded_String;
                     New_Item : Expression_Value) is
   begin
      Init (Container);
      Include (Container.Assocs.Value_Assocs,
              (Kind => String_Expression_Value,
               S => Key),
              New_Item);
   end Insert_Value;

   procedure Process_Control_Block_Elements
     (Current : in out Template_Element_Vectors.Cursor;
      Out_Buffer : in out Unbounded_String;
      Resolver : aliased in out Context)
   is
      Current_Element : Template_Element;
   begin
      while Current /= Template_Element_Vectors.No_Element loop
         Current_Element := Template_Element_Vectors.Element (Current);
         case Current_Element.Kind is
            when Expression_Element =>
               Append_Value (Out_Buffer,
                             Current_Element.Expr.all,
                             Resolver);
            when Statement_Element =>
               case Current_Element.Stmt.Kind is
                  when Endif_Statement | Elif_Statement | Else_Statement
                       | Endfor_Statement =>
                     exit;
                  when others =>
                     null;
               end case;
               Execute_Statement (Current_Element.Stmt,
                                  Current,
                                  Out_Buffer,
                                  Resolver);
         end case;
         Next (Current);
      end loop;
   end Process_Control_Block_Elements;

   procedure Skip_Control_Block_Elements
     (Current : in out Template_Element_Vectors.Cursor)
   is
      Current_Element : Template_Element;
      Level : Natural := 0;
   begin
      Next (Current);
      while Current /= Template_Element_Vectors.No_Element loop
         Current_Element := Template_Element_Vectors.Element (Current);
         case Current_Element.Kind is
            when Statement_Element =>
               case Current_Element.Stmt.Kind is
                  when Endif_Statement | Elif_Statement | Else_Statement
                       | Endfor_Statement =>
                     if Level = 0 then
                        exit;
                     end if;
                     Level := Level - 1;
                  when If_Statement | For_Statement =>
                     Level := Level + 1;
                  when others =>
                     null;
               end case;
            when others =>
               null;
         end case;
         Next (Current);
      end loop;
   end Skip_Control_Block_Elements;

   function Evaluate_Boolean (Source : Expression;
                              Resolver : aliased in out Context)
                              return Boolean is
      Result_Value : constant Expression_Value := Evaluate (Source, Resolver);
   begin
      if Result_Value.Kind /= Boolean_Expression_Value then
         raise Template_Error with "boolean expression expected";
      end if;
      return Result_Value.B;
   end Evaluate_Boolean;

   procedure Execute_If
     (Condition : Expression;
      Current : in out Template_Element_Vectors.Cursor;
      Out_Buffer : in out Unbounded_String;
      Resolver : aliased in out Context)
   is
      Condition_Value : constant Boolean := Evaluate_Boolean
        (Condition, Resolver);
      Element : Template_Element;
   begin
      if Condition_Value then
         Next (Current);
         Process_Control_Block_Elements (Current, Out_Buffer, Resolver);
         Element := Template_Element_Vectors.Element (Current);
         if Element.Kind = Statement_Element
           and then Element.Stmt.Kind = Else_Statement
         then
            Skip_Control_Block_Elements (Current);
         end if;
      else
         Skip_Control_Block_Elements (Current);
         Element := Template_Element_Vectors.Element (Current);
         if Element.Kind = Statement_Element
           and then Element.Stmt.Kind = Else_Statement
         then
            Next (Current);
            Process_Control_Block_Elements (Current, Out_Buffer, Resolver);
         end if;
      end if;
   exception
      when Constraint_Error =>
         raise Template_Error with "unbalanced 'if'";
   end Execute_If;

   package Value_Sorting is new
     Expression_Value_Vectors.Generic_Sorting ("<" => "<");

   function Less_Case_Insensitive (Left, Right : Expression_Value)
     return Boolean is
   begin
      if Left.Kind /= Right.Kind then
         raise Template_Error
           with "comparison not supported for values of different type";
      end if;
      if Left.Kind = String_Expression_Value then
         return Ada.Characters.Handling.To_Upper (To_String (Left.S))
           < Ada.Characters.Handling.To_Upper (To_String (Right.S));
      end if;
      return Left < Right;
   end Less_Case_Insensitive;

   package Value_Sorting_Case_Insensitive is new
     Expression_Value_Vectors.Generic_Sorting
       ("<" => Less_Case_Insensitive);

   type Key_And_Value is record
      Key : Expression_Value;
      Value : Expression_Value;
   end record;

   function Value_Less (Left, Right : Key_And_Value) return Boolean is
   begin
      return Left.Value < Right.Value;
   end Value_Less;

   package Key_And_Value_Vectors is new
     Ada.Containers.Vectors (Index_Type => Natural,
                             Element_Type => Key_And_Value);

   package Key_And_Value_Sorting_By_Value is new
     Key_And_Value_Vectors.Generic_Sorting ("<" => Value_Less);

   function Value_Less_Case_Insensitive (Left, Right : Key_And_Value)
                                         return Boolean is
   begin
      if Left.Value.Kind /= Right.Value.Kind then
         raise Template_Error
           with "comparison not supported for values of different type";
      end if;
      if Left.Value.Kind = String_Expression_Value then
         return Ada.Characters.Handling.To_Upper (To_String (Left.Value.S))
           < Ada.Characters.Handling.To_Upper (To_String (Right.Value.S));
      end if;
      return Left.Value < Right.Value;
   end Value_Less_Case_Insensitive;

   package Key_And_Value_Sorting_Case_Insensitive_By_Value is new
     Key_And_Value_Vectors.Generic_Sorting ("<" => Value_Less_Case_Insensitive);

   procedure Set_Loop_Index0 (Resolver : in out Context;
                              Index0 : Natural) is
   begin
      Insert (Resolver.Values, Loop_Index0_Name, Index0);
   end Set_Loop_Index0;

   procedure Set_Loop_Length (Resolver : in out Context;
                              Length : Natural) is
   begin
      Insert (Resolver.Values, Loop_Length_Name, Length);
   end Set_Loop_Length;

   procedure Execute_For
     (Collection : Expression;
      Variable_1_Name : Unbounded_String;
      Variable_2_Name : Unbounded_String;
      Condition : Expression_Access;
      Current : in out Template_Element_Vectors.Cursor;
      Out_Buffer : in out Unbounded_String;
      Resolver : aliased in out Context)
   is
      Start_Cursor : constant Template_Element_Vectors.Cursor := Current;
      Loop_Resolver : aliased Context;
      Empty_Loop : Boolean := True;
      Loop_Length : Natural := 0;
      Loop_Index0 : Natural := 0;

      procedure Execute_For_Items is
         Collection_Value : constant Expression_Value
           := Evaluate (Collection.Named_Arguments (1).Argument.all,
                        Resolver);
      begin
         if Collection_Value.Kind /= Dictionary_Expression_Value then
            raise Template_Error with "dictionary expected";
         end if;
         if Condition = null then
            Set_Loop_Length (Loop_Resolver,
                             Natural (Collection_Value.Dictionary_Value.Assocs
                             .Value_Assocs.Length));
         else
            Loop_Length := 0;
            for C in Collection_Value.Dictionary_Value.Assocs.Value_Assocs.Iterate
            loop
               Insert_Value (Loop_Resolver.Values, Variable_1_Name, Key (C));
               Insert_Value
                 (Loop_Resolver.Values,
                  Variable_2_Name,
                  Collection_Value.Dictionary_Value.Assocs.Value_Assocs (C));
               if Evaluate_Boolean (Condition.all,
                                    Loop_Resolver)
               then
                  Loop_Length := Loop_Length + 1;
               end if;
            end loop;
            Set_Loop_Length (Loop_Resolver,
                             Loop_Length);
         end if;
         for C in Collection_Value.Dictionary_Value.Assocs.Value_Assocs.Iterate
         loop
            Insert_Value (Loop_Resolver.Values, Variable_1_Name, Key (C));
               Insert_Value
                 (Loop_Resolver.Values,
                  Variable_2_Name,
                  Collection_Value.Dictionary_Value.Assocs.Value_Assocs (C));
            if Condition = null or else Evaluate_Boolean (Condition.all,
                                                          Loop_Resolver)
            then
               Current := Start_Cursor;
               Next (Current);
               Set_Loop_Index0 (Loop_Resolver, Loop_Index0);
               Process_Control_Block_Elements (Current, Out_Buffer, Loop_Resolver);
               Loop_Index0 := Loop_Index0 + 1;
               Empty_Loop := False;
            end if;
         end loop;
         if Empty_Loop then
            Skip_Control_Block_Elements (Current);
         end if;
      end Execute_For_Items;

      procedure Process_Control_Block_Elements
        (Resolver : aliased in out Context)
      is
      begin
         Current := Start_Cursor;
         Next (Current);
         Process_Control_Block_Elements (Current, Out_Buffer, Resolver);
         Empty_Loop := False;
      end Process_Control_Block_Elements;

      use Key_And_Value_Vectors;

      procedure Execute_Sorted_By_Key
        (Value_Assocs : Association_Maps.Map;
         Case_Sensitive : Boolean;
         Reverse_Sort : Boolean)
      is
         Keys : Expression_Value_Vectors.Vector;
         Loop_Length : Natural := 0;
         Loop_Index0 : Natural := 0;
      begin
         for C in Value_Assocs.Iterate loop
            Append (Keys, Key (C));
         end loop;
         if Case_Sensitive then
            Value_Sorting.Sort (Keys);
         else
            Value_Sorting_Case_Insensitive.Sort (Keys);
         end if;
         if Condition = null then
            Loop_Length := Natural (Length (Keys));
         else
            for C in Value_Assocs.Iterate loop
               Insert_Value (Loop_Resolver.Values, Variable_1_Name, Key (C));
               Insert_Value (Loop_Resolver.Values,
                             Variable_2_Name,
                             Value_Assocs (Key (C)));
               if Evaluate_Boolean (Condition.all, Loop_Resolver) then
                  Loop_Length := Loop_Length + 1;
               end if;
            end loop;
         end if;
         Set_Loop_Length (Loop_Resolver, Loop_Length);
         if Reverse_Sort then
            for I in reverse 0 .. Natural (Length (Keys) - 1) loop
               Insert_Value (Loop_Resolver.Values, Variable_1_Name, Keys (I));
               Insert_Value (Loop_Resolver.Values,
                             Variable_2_Name,
                             Value_Assocs (Keys (I)));
               Set_Loop_Index0 (Loop_Resolver,
                                Loop_Length - 1 - I);
               if Condition = null
                 or else Evaluate_Boolean (Condition.all, Loop_Resolver)
               then
                  Process_Control_Block_Elements (Loop_Resolver);
                  Empty_Loop := False;
               end if;
            end loop;
         else
            Loop_Index0 := 0;
            for C in Value_Assocs.Iterate loop
               Insert_Value (Loop_Resolver.Values, Variable_1_Name, Key (C));
               Insert_Value (Loop_Resolver.Values,
                             Variable_2_Name,
                             Value_Assocs (Key (C)));
               if Condition = null
                 or else Evaluate_Boolean (Condition.all, Loop_Resolver)
               then
                  Set_Loop_Index0 (Loop_Resolver, Loop_Index0);
                  Process_Control_Block_Elements (Loop_Resolver);
                  Empty_Loop := False;
                  Loop_Index0 := Loop_Index0 + 1;
               end if;
            end loop;
         end if;
         if Empty_Loop then
            Skip_Control_Block_Elements (Current);
         end if;
      end Execute_Sorted_By_Key;

      procedure Execute_Sorted_By_Value (Value_Assocs : Association_Maps.Map;
                               Case_Sensitive : Boolean;
                               Reverse_Sort : Boolean) is
         Items : Key_And_Value_Vectors.Vector;
         Loop_Length : Natural := 0;
      begin
         for C in Value_Assocs.Iterate loop
            Append (Items,
                    (Key (C), Element (C))
                   );
         end loop;
         if Case_Sensitive then
            Key_And_Value_Sorting_By_Value.Sort (Items);
         else
            Key_And_Value_Sorting_Case_Insensitive_By_Value.Sort (Items);
         end if;
         if Condition = null then
            Loop_Length := Natural (Length (Items));
         else
            for I in 0 .. Natural (Length (Items)) - 1 loop
               Insert_Value (Loop_Resolver.Values, Variable_1_Name, Items (I).Key);
               Insert_Value (Loop_Resolver.Values, Variable_2_Name, Items (I).Value);
               if Evaluate_Boolean (Condition.all, Loop_Resolver) then
                  Loop_Length := Loop_Length + 1;
               end if;
            end loop;
         end if;
         Set_Loop_Length (Loop_Resolver, Loop_Length);
         if Reverse_Sort then
            for I in reverse 0 .. Natural (Length (Items)) - 1 loop
               Insert_Value (Loop_Resolver.Values, Variable_1_Name, Items (I).Key);
               Insert_Value (Loop_Resolver.Values, Variable_2_Name, Items (I).Value);
               Set_Loop_Index0 (Loop_Resolver,
                                Loop_Length - 1 - I);
               if Condition = null
                 or else Evaluate_Boolean (Condition.all, Loop_Resolver)
               then
                  Process_Control_Block_Elements (Loop_Resolver);
               end if;
            end loop;
         else
            for I in 0 .. Natural (Length (Items)) - 1 loop
               Insert_Value (Loop_Resolver.Values, Variable_1_Name, Items (I).Key);
               Insert_Value (Loop_Resolver.Values, Variable_2_Name, Items (I).Value);
               Set_Loop_Index0 (Loop_Resolver, I);
               Process_Control_Block_Elements (Loop_Resolver);
            end loop;
         end if;
      end Execute_Sorted_By_Value;

      procedure Execute_For_Dictsort is
         Collection_Value : constant Expression_Value
           := Evaluate (Collection.Arguments (1).all,
                        Resolver);
         Case_Sensitive_Argument : constant Expression_Access
           := Collection.Arguments (2);
         Case_Sensitive : Boolean := False;
         By_Argument : constant Expression_Access
           := Collection.Arguments (3);
         By_Value : Expression_Value;
         By_Key : Boolean := True;
         Reverse_Argument : constant Expression_Access
           := Collection.Arguments (4);
         Reverse_Sort : Boolean := False;
      begin
         if Collection_Value.Kind /= Dictionary_Expression_Value then
            raise Template_Error with "dictionary expected";
         end if;
         if Case_Sensitive_Argument /= null then
            Case_Sensitive := Evaluate_Boolean (Case_Sensitive_Argument.all,
                                                Resolver);
         end if;
         if By_Argument /= null then
            By_Value := Evaluate (By_Argument.all, Resolver);
            if By_Value.Kind /= String_Expression_Value then
               raise Template_Error
                 with "you can sort either by 'key' or by 'value'";
            end if;
            if By_Value.S = "key" then
               By_Key := True;
            elsif By_Value.S = "value" then
               By_Key := False;
            else
               raise Template_Error
                 with "you can sort either by 'key' or by 'value'";
            end if;
         end if;
         if Reverse_Argument /= null then
            Reverse_Sort := Evaluate_Boolean (Reverse_Argument.all,
                                              Resolver);
         end if;
         if By_Key then
            Execute_Sorted_By_Key
              (Collection_Value.Dictionary_Value.Assocs.Value_Assocs,
               Case_Sensitive,
               Reverse_Sort);
         else
            Execute_Sorted_By_Value
              (Collection_Value.Dictionary_Value.Assocs.Value_Assocs,
               Case_Sensitive,
               Reverse_Sort);
         end if;
      end Execute_For_Dictsort;

      procedure Execute_For_Default is
         Collection_Value : Expression_Value := Evaluate (Collection, Resolver);
         Loop_Length : Natural := 0;
         Loop_Index0 : Natural := 0;
      begin
         case Collection_Value.Kind is
            when List_Expression_Value =>
               Init (Collection_Value.List_Value);
               if Condition = null then
                  Loop_Length := Natural
                    (Length (Collection_Value.List_Value.Elements.Values));
               else
                  for E of Collection_Value.List_Value.Elements.Values loop
                     Insert_Value (Loop_Resolver.Values, Variable_1_Name, E);
                     if Evaluate_Boolean (Condition.all,
                                          Loop_Resolver)
                     then
                        Loop_Length := Loop_Length + 1;
                     end if;
                  end loop;
               end if;
               Set_Loop_Length (Loop_Resolver, Loop_Length);
               for E of Collection_Value.List_Value.Elements.Values loop
                  Insert_Value (Loop_Resolver.Values, Variable_1_Name, E);
                  if Condition = null or else Evaluate_Boolean (Condition.all,
                                                                Loop_Resolver)
                  then
                     Current := Start_Cursor;
                     Next (Current);
                     Set_Loop_Index0 (Loop_Resolver, Loop_Index0);
                     Process_Control_Block_Elements (Current, Out_Buffer, Loop_Resolver);
                     Empty_Loop := False;
                     Loop_Index0 := Loop_Index0 + 1;
                  end if;
               end loop;
               if Empty_Loop then
                  Skip_Control_Block_Elements (Current);
               end if;
            when Dictionary_Expression_Value =>
               if Variable_2_Name /= Null_Unbounded_String then
                  raise Template_Error
                    with "too many loop variables";
               end if;
               if Condition = null then
                  Loop_Length := Natural
                    (Length (Collection_Value.Dictionary_Value.Assocs.Value_Assocs));
               else
                  Loop_Length := 0;
                  for C in Collection_Value.Dictionary_Value.Assocs.Value_Assocs
                    .Iterate loop
                     Insert_Value (Loop_Resolver.Values, Variable_1_Name, Key (C));
                     if Evaluate_Boolean (Condition.all,
                                          Loop_Resolver)
                     then
                        Loop_Length := Loop_Length + 1;
                     end if;
                  end loop;
               end if;
               Set_Loop_Length (Loop_Resolver, Loop_Length);
               for C in Collection_Value.Dictionary_Value.Assocs.Value_Assocs
                 .Iterate loop
                     Insert_Value (Loop_Resolver.Values, Variable_1_Name, Key (C));
                  if Condition = null or else Evaluate_Boolean (Condition.all,
                                                                Loop_Resolver)
                  then
                     Current := Start_Cursor;
                     Next (Current);
                     Set_Loop_Index0 (Loop_Resolver, Loop_Index0);
                     Process_Control_Block_Elements (Current,
                                                     Out_Buffer,
                                                     Loop_Resolver);
                     Empty_Loop := False;
                     Loop_Index0 := Loop_Index0 + 1;
                  end if;
               end loop;
               if Empty_Loop then
                  Skip_Control_Block_Elements (Current);
               end if;
            when others =>
               raise Template_Error with "list or dictionary expected, got "
                 & Collection_Value.Kind'Image;
         end case;
      end Execute_For_Default;

   begin
      Loop_Resolver.Parent_Resolver := Resolver'Unchecked_Access;
      if Collection.Kind = Operator_Items
      then
         Execute_For_Items;
      elsif Collection.Kind = Filter
        and then Collection.Name = "dictsort"
      then
         Execute_For_Dictsort;
      else
         Execute_For_Default;
      end if;
      declare
         Element : constant Template_Element
           := Template_Element_Vectors.Element (Current);
      begin
         if Element.Kind = Statement_Element
           and then Element.Stmt.Kind = Else_Statement
         then
            if Empty_Loop then
               Next (Current);
               Process_Control_Block_Elements (Current, Out_Buffer, Resolver);
            else
               Skip_Control_Block_Elements (Current);
            end if;
         end if;
      end;
   exception
      when Constraint_Error =>
         raise Template_Error with "unbalanced 'for'";
   end Execute_For;

   procedure Execute_Include (File_Name : String;
                              Out_Buffer : in out Unbounded_String;
                              Resolver : aliased in out Context)
   is
      Included_Template : Template_Access;
      Current : Template_Element_Vectors.Cursor;
   begin
      if Resolver.Parent_Resolver /= null then
         raise Template_Error with "including templates is only permitted at top level";
      end if;
      begin
         Included_Template := new Template;
         Get_Template (File_Name, Included_Template.all, Get_Environment (Resolver).all);
      exception
         when Name_Error =>
            Free_Template (Included_Template);
            raise Template_Error with "template not found: " & File_Name;
         when Constraint_Error =>
            Free_Template (Included_Template);
            raise Template_Error with "including a template twice is not supported";
         when others =>
            Free_Template (Included_Template);
            raise;
      end;
      Resolver.Included_Templates.Append (Included_Template);
      Current := First (Included_Template.Elements);
      Process_Control_Block_Elements (Current, Out_Buffer, Resolver);
   end Execute_Include;

   procedure Execute_Macro (Name : Unbounded_String;
                            Parameters : Parameter_Vectors.Vector;
                            Current : in out Template_Element_Vectors.Cursor;
                            Resolver : aliased in out Context)
   is
      Position : Macro_Maps.Cursor;
      M : Macro_Access;
      E : Template_Element;
      Elements : Template_Element_Vectors.Vector;
      Root_Context : Context_Access := Resolver'Unchecked_Access;
   begin
      while Root_Context.Parent_Resolver /= null loop
         Root_Context := Root_Context.Parent_Resolver;
      end loop;
      Position := Macro_Maps.Find (Root_Context.Macros, Name);
      if Position /= Macro_Maps.No_Element then
         --  Delete old macro
         M := Element (Position);
         Free_Macro (M);
         Root_Context.Macros.Delete (Position);
      end if;
      Current := Template_Element_Vectors.Next (Current);
      while Current /= Template_Element_Vectors.No_Element loop
         E := Element (Current);
         if E.Kind = Statement_Element
           and then E.Stmt.Kind = Endmacro_Statement
         then
            exit;
         end if;
         Elements.Append (E);
         Current := Template_Element_Vectors.Next (Current);
      end loop;
      M := new Macro'
           (Parameters => Parameters,
            Elements => Elements);
      Root_Context.Macros.Insert (Name, M);
   end Execute_Macro;

   function Get_Template (File_Name : String;
                          Resolver : aliased in out Context)
                          return Template_Access is
      New_Template : Template_Access :=
        Get_Environment (Resolver).Cached_Templates.Get (File_Name);
      File_Time : Time;
   begin
      File_Time := Ada.Directories.Modification_Time (File_Name);
      if New_Template = null or else File_Time > New_Template.Timestamp then
         begin
            New_Template := new Template;
            New_Template.Timestamp := File_Time;
            Get_Template (File_Name, New_Template.all, Get_Environment (Resolver).all);
            Get_Environment (Resolver).Cached_Templates.Put
              (Path     => File_Name,
               Template => New_Template,
               Max_Size => Get_Environment (Resolver).Max_Cache_Size,
               Inserted => New_Template.Cached);
         exception
            when others =>
               Free_Template (New_Template);
               raise;
         end;
      end if;
      return New_Template;
   end Get_Template;

   function Find (Source : Name_Mapping_Vectors.Vector;
                  Name : Unbounded_String)
                  return Natural is
   begin
      for I in 1 .. Name_Mapping_Vectors.Length (Source) loop
         if Name_Mapping_Vectors.Element (Source, Positive (I)).Source = Name then
            return Natural (I);
         end if;
      end loop;
      return 0;
   end Find;

   procedure Execute_Import (File_Name : String;
                             Variable_Name : Unbounded_String;
                             Resolver : aliased in out Context) is
      New_Template : Template_Access;
      Current : Template_Element_Vectors.Cursor;
      E : Template_Element;
   begin
      New_Template := Get_Template (File_Name, Resolver);
      Resolver.Imported_Templates.Append (New_Template);
      Current := First (New_Template.Elements);
      while Current /= Template_Element_Vectors.No_Element loop
         E := Template_Element_Vectors.Element (Current);
         if E.Kind = Statement_Element
           and then E.Stmt.Kind = Macro_Statement
         then
            Execute_Macro (Variable_Name & "." & To_String (E.Stmt.Macro_Name),
                        E.Stmt.Macro_Parameters,
                        Current,
                        Resolver);
         end if;
         if Current = Template_Element_Vectors.No_Element then
            exit;
         end if;
         Next (Current);
      end loop;
   exception
      when Constraint_Error =>
         if not New_Template.Cached then
            Free_Template (New_Template);
         end if;
         raise Template_Error with "importing a template twice is not supported";
      when Name_Error =>
         raise Template_Error with "template not found: " & File_Name;
   end Execute_Import;

   procedure Execute_Import (File_Name : String;
                             Variable_Names : Name_Mapping_Vectors.Vector;
                             Resolver : aliased in out Context) is
      New_Template : Template_Access;
      Current : Template_Element_Vectors.Cursor;
      E : Template_Element;
      Name_Index : Natural;
      Mapping : Name_Mapping;
   begin
      New_Template := Get_Template (File_Name, Resolver);
      Resolver.Imported_Templates.Append (New_Template);
      Current := First (New_Template.Elements);
      while Current /= Template_Element_Vectors.No_Element loop
         E := Template_Element_Vectors.Element (Current);
         if E.Kind = Statement_Element
           and then E.Stmt.Kind = Macro_Statement
         then
            Name_Index := Find (Variable_Names, E.Stmt.Macro_Name);
            if Name_Index /= 0 then
               Mapping := Name_Mapping_Vectors.Element (Variable_Names,
                                                          Name_Index);
               Execute_Macro
                 ((if Mapping.Target = Null_Unbounded_String
                   then E.Stmt.Macro_Name else Mapping.Target),
                  E.Stmt.Macro_Parameters,
                  Current,
                  Resolver);
            end if;
         end if;
         if Current = Template_Element_Vectors.No_Element then
            exit;
         end if;
         Next (Current);
      end loop;
   exception
      when Constraint_Error =>
         if not New_Template.Cached then
            Free_Template (New_Template);
         end if;
         raise Template_Error with "importing a template twice is not supported";
      when Name_Error =>
         raise Template_Error with "template not found: " & File_Name;
   end Execute_Import;

   procedure Find_Child_Block
     (Resolver : Context;
      Name : Unbounded_String;
      Template_Index : out Natural;
      Element_Index : out Positive)
   is
      Map_Position : Block_Maps.Cursor;
   begin
      if Current_Template_Index (Resolver) + 1 > Template_Count (Resolver)
      then
         Template_Index := 0;
         Element_Index := 1; -- not a valid index, only to initialize the field
         return;
      end if;
      for I in reverse Current_Template_Index (Resolver) + 1 .. Template_Count (Resolver)
      loop
         Map_Position := Get_Template (Resolver, I).Block_Map.Find (Name);
         if Map_Position /= Block_Maps.No_Element then
            Template_Index := I;
            Element_Index := Block_Maps.Element (Map_Position);
            return;
         end if;
      end loop;
      Template_Index := 0;
      Element_Index := 1;
   end Find_Child_Block;

   procedure Execute_Statement
     (Stmt : Statement;
      Current : in out Template_Element_Vectors.Cursor;
      Out_Buffer : in out Unbounded_String;
      Resolver : aliased in out Context) is

      procedure Replace_Block is
         Current_Element : Template_Element;
         Level : Natural;
         Template_Index : Natural;
         Element_Index : Positive;
         Old_Template_Index : constant Positive := Current_Template_Index (Resolver);
      begin
         Find_Child_Block (Resolver, Stmt.Block_Name,
                           Template_Index, Element_Index);
         if Template_Index = 0 then
            return;
         end if;
         Set_Current_Template_Index (Resolver, Template_Index);
         Set_Current_Block_Name (Resolver, Stmt.Block_Name);
         Execute_Block (Element_Index,
                        Get_Template (Resolver, Template_Index).all,
                        Out_Buffer,
                        Resolver);
         Set_Current_Template_Index (Resolver, Old_Template_Index);

         --  Skip replaced block
         Current := Template_Element_Vectors.Next (Current);
         Level := 0;
         while Current /= Template_Element_Vectors.No_Element loop
            Current_Element := Template_Element_Vectors.Element (Current);
            if Current_Element.Kind = Statement_Element then
               case Current_Element.Stmt.Kind is
               when Block_Statement =>
                  Level := Level + 1;
               when Endblock_Statement =>
                  if Level = 0 then
                     exit;
                  end if;
                  Level := Level - 1;
               when others =>
                  null;
               end case;
            end if;
            Current := Template_Element_Vectors.Next (Current);
         end loop;
      end Replace_Block;

   begin
      case Stmt.Kind is
         when If_Statement =>
            Execute_If (Stmt.If_Condition.all,
                        Current,
                        Out_Buffer,
                        Resolver);
         when For_Statement =>
            Execute_For (Stmt.For_Expression.all,
                         Stmt.For_Variable_1_Name,
                         Stmt.For_Variable_2_Name,
                         Stmt.For_Condition,
                         Current,
                         Out_Buffer,
                         Resolver);
         when Include_Statement =>
            Execute_Include (To_String (Stmt.File_Name),
                             Out_Buffer,
                             Resolver);
         when Block_Statement =>
            if Template_Count (Resolver) > 0 then
               Replace_Block;
            end if;
         when Macro_Statement =>
            Execute_Macro (Stmt.Macro_Name,
                           Stmt.Macro_Parameters,
                           Current,
                           Resolver);
         when Import_Statement =>
            Execute_Import (To_String (Stmt.Import_File_Name),
                            Stmt.Import_Variable_Name,
                            Resolver);
         when From_Import_Statement =>
            Execute_Import (To_String (Stmt.From_File_Name),
                            Stmt.Import_Variable_Names,
                            Resolver);
         when others =>
            raise Template_Error with "internal error: invalid statement";
      end case;
   end Execute_Statement;

   procedure Configure (Settings : in out Environment;
                        Expression_Start : String := Default_Expression_Start;
                        Expression_End : String := Default_Expression_End;
                        Statement_Start : String := Default_Statement_Start;
                        Statement_End : String := Default_Statement_End;
                        Comment_Start : String := Default_Comment_Start;
                        Comment_End : String := Default_Comment_End;
                        Max_Cache_Size : Natural := 200;
                        Trim_Blocks : Boolean := False;
                        Lstrip_Blocks : Boolean := False)
   is
   begin
      Settings.Expression_Start := To_Unbounded_String (Expression_Start);
      Settings.Expression_End := To_Unbounded_String (Expression_End);
      Settings.Statement_Start := To_Unbounded_String (Statement_Start);
      Settings.Statement_End := To_Unbounded_String (Statement_End);
      Settings.Comment_Start := To_Unbounded_String (Comment_Start);
      Settings.Comment_End := To_Unbounded_String (Comment_End);
      Settings.Max_Cache_Size := Max_Cache_Size;
      Settings.Trim_Blocks := Trim_Blocks;
      Settings.Lstrip_Blocks := Lstrip_Blocks;
   end Configure;

   overriding function "=" (Left, Right : Dictionary) return Boolean is
   begin
      if Left.Assocs = null or else Right.Assocs = null then
         return Left.Assocs = Right.Assocs;
      end if;
      return Left.Assocs.Value_Assocs = Right.Assocs.Value_Assocs;
   end "=";

   overriding function "=" (Left, Right : List) return Boolean is
   begin
      if Left.Elements = null or else Right.Elements = null then
         return Left.Elements = Right.Elements;
      end if;
      return Left.Elements.Values = Right.Elements.Values;
   end "=";

   function Render (File_Name : String;
                    Resolver : aliased in out Context)
                    return Unbounded_String is
      New_Template : Template_Access := Get_Template (File_Name, Resolver);
   begin
      Add_Parent_Template (Resolver, New_Template);
      if not New_Template.Cached then
         declare
            Result : Unbounded_String;
         begin
            Result := Render (New_Template.Elements, File_Name, Resolver);
            Resolver.Template_Refs.Clear;
            Free_Template (New_Template);
            return Result;
         exception
            when others =>
               Free_Template (New_Template);
               Resolver.Template_Refs.Clear;
               raise;
         end;
      end if;
      return Render (New_Template.Elements, File_Name, Resolver);
   exception
      when Name_Error =>
         raise Template_Error with "template not found: " & File_Name;
   end Render;

   function Render (File_Name : String;
                    Values : Dictionary;
                    Settings : in out Environment'Class)
                    return Unbounded_String is
      Resolver : aliased Context :=
        (Ada.Finalization.Controlled with
         Settings => Settings'Unchecked_Access,
         Template_Refs => Template_Access_Vectors.Empty_Vector,
         Template_Index => 1,
         Block_Name => Null_Unbounded_String,
         Values => Values,
         Parent_Resolver => null,
         Included_Templates => Template_Access_Vectors.Empty_Vector,
         Macros => Macro_Maps.Empty_Map,
         Imported_Templates => Template_Access_Vectors.Empty_Vector);
   begin
      return Render (File_Name, Resolver);
   exception
      when others =>
         for E of Resolver.Included_Templates loop
            Free_Template (E);
         end loop;
         raise;
   end Render;

   function Render (File_Name : String;
                    Values : Dictionary;
                    Settings : in out Environment'Class)
                    return String is
   begin
      return To_String (Render (File_Name, Values, Settings));
   end Render;

   function Render (File_Name : String;
                    Values : Dictionary)
                    return String is
   begin
      return Render (File_Name, Values, Default_Environment);
   end Render;

   function Render (File_Name : String;
                    Values : Dictionary)
                    return Unbounded_String is
   begin
      return Render (File_Name, Values, Default_Environment);
   end Render;

   procedure Insert (Container : in out Dictionary;
                     Key : String;
                     New_Item : Unbounded_String) is
   begin
      Insert (Container, To_Unbounded_String (Key), New_Item);
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : Unbounded_String;
                     New_Item : Unbounded_String) is
   begin
      Init (Container);
      Include (Container.Assocs.Value_Assocs,
              (Kind => String_Expression_Value,
               S => Key),
              (Kind => String_Expression_Value,
               S => New_Item));
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : String;
                     New_Item : String) is
   begin
      Insert (Container, Key, To_Unbounded_String (New_Item));
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : Unbounded_String;
                     New_Item : String) is
   begin
      Insert (Container, Key, To_Unbounded_String (New_Item));
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : Unbounded_String;
                     New_Item : Integer) is
   begin
      Init (Container);
      Include (Container.Assocs.Value_Assocs,
              (Kind => String_Expression_Value,
               S => Key),
              (Kind => Integer_Expression_Value,
               I => New_Item));
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : String;
                     New_Item : Integer) is
   begin
      Insert (Container, To_Unbounded_String (Key), New_Item);
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : Unbounded_String;
                     New_Item : Boolean) is
   begin
      Include (Container.Assocs.Value_Assocs,
              (Kind => String_Expression_Value,
               S => Key),
              (Kind => Boolean_Expression_Value,
               B => New_Item));
   end Insert;

   function Refers (Source : Dictionary'Class;
                    Target : Dictionary)
                    return Boolean is
   begin
      if Source.Assocs = null then
         return False;
      end if;
      if Source.Assocs = Target.Assocs then
         return True;
      end if;
      for C in Source.Assocs.Value_Assocs.Iterate loop
         case Element (C).Kind is
            when Dictionary_Expression_Value =>
               if Refers (Element (C).Dictionary_Value, Target) then
                  return True;
               end if;
            when List_Expression_Value =>
               if Refers (Element (C).List_Value, Target) then
                  return True;
               end if;
            when others =>
               null;
         end case;
      end loop;
      return False;
   end Refers;

   function Refers (Source : Dictionary'Class;
                    Target : List)
                    return Boolean is
   begin
      if Source.Assocs = null then
         return False;
      end if;
      for C in Source.Assocs.Value_Assocs.Iterate loop
         case Element (C).Kind is
            when Dictionary_Expression_Value =>
               if Refers (Element (C).Dictionary_Value, Target) then
                  return True;
               end if;
            when List_Expression_Value =>
               if Refers (Element (C).List_Value, Target) then
                  return True;
               end if;
            when others =>
               null;
         end case;
      end loop;
      return False;
   end Refers;

   function Refers (Source : List'Class;
                    Target : Dictionary)
                    return Boolean is
   begin
      if Source.Elements = null then
         return False;
      end if;
      for E of Source.Elements.Values loop
         case E.Kind is
            when Dictionary_Expression_Value =>
               if Refers (E.Dictionary_Value, Target) then
                  return True;
               end if;
            when List_Expression_Value =>
               if Refers (E.List_Value, Target) then
                  return True;
               end if;
            when others =>
               null;
         end case;
      end loop;
      return False;
   end Refers;

   function Refers (Source : List'Class;
                   Target : List)
                   return Boolean is
   begin
      if Source.Elements = null then
         return False;
      end if;
      if Source.Elements = Target.Elements then
         return True;
      end if;
      for E of Source.Elements.Values loop
         case E.Kind is
            when Dictionary_Expression_Value =>
               if Refers (E.Dictionary_Value, Target) then
                  return True;
               end if;
            when List_Expression_Value =>
               if Refers (E.List_Value, Target) then
                  return True;
               end if;
            when others =>
               null;
         end case;
      end loop;
      return False;
   end Refers;

   procedure Insert (Container : in out Dictionary;
                     Key : Unbounded_String;
                     New_Item : List'Class) is
   begin
      Init (Container);
      Include (Container.Assocs.Value_Assocs,
              (Kind => String_Expression_Value,
               S => Key),
              (Kind => List_Expression_Value,
               List_Value => List (New_Item)));
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : Unbounded_String;
                     New_Item : Dictionary) is
   begin
      Init (Container);
      Include (Container.Assocs.Value_Assocs,
              (Kind => String_Expression_Value,
               S => Key),
              (Kind => Dictionary_Expression_Value,
               Dictionary_Value => New_Item));
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : String;
                     New_Item : Dictionary) is
   begin
      Insert (Container, To_Unbounded_String (Key), New_Item);
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : String;
                     New_Item : Boolean) is
   begin
      Insert (Container, To_Unbounded_String (Key), New_Item);
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : String;
                     New_Item : List'Class) is
   begin
      Insert (Container, To_Unbounded_String (Key), New_Item);
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : Integer;
                     New_Item : String) is
   begin
      Init (Container);
      Include (Container.Assocs.Value_Assocs,
              (Kind => Integer_Expression_Value,
               I => Key),
              (Kind => String_Expression_Value,
               S => To_Unbounded_String (New_Item)));
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : Integer;
                     New_Item : Unbounded_String) is
   begin
      Init (Container);
      Include (Container.Assocs.Value_Assocs,
              (Kind => Integer_Expression_Value,
               I => Key),
              (Kind => String_Expression_Value,
               S => New_Item));
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : Integer;
                     New_Item : Integer) is
   begin
      Init (Container);
      Include (Container.Assocs.Value_Assocs,
              (Kind => Integer_Expression_Value,
               I => Key),
              (Kind => Integer_Expression_Value,
               I => New_Item));
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : Integer;
                     New_Item : Boolean) is
   begin
      Init (Container);
      Include (Container.Assocs.Value_Assocs,
              (Kind => Integer_Expression_Value,
               I => Key),
              (Kind => Boolean_Expression_Value,
               B => New_Item));
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : Integer;
                     New_Item : List'Class) is
   begin
      Init (Container);
      Include (Container.Assocs.Value_Assocs,
              (Kind => Integer_Expression_Value,
               I => Key),
              (Kind => List_Expression_Value,
               List_Value => List (New_Item)));
   end Insert;

   procedure Insert (Container : in out Dictionary;
                     Key : Integer;
                     New_Item : Dictionary'Class) is
   begin
      Init (Container);
      Include (Container.Assocs.Value_Assocs,
              (Kind => Integer_Expression_Value,
               I => Key),
              (Kind => Dictionary_Expression_Value,
               Dictionary_Value => Dictionary (New_Item)));
   end Insert;

   procedure Clear (Container : in out Dictionary) is
   begin
      if Container.Assocs /= null then
         Clear (Container.Assocs.Value_Assocs);
      end if;
   end Clear;

   procedure Free_Assocs is new Ada.Unchecked_Deallocation (Dictionary_Assocs,
                                                            Assocs_Access);

   procedure Free_Elements is new Ada.Unchecked_Deallocation
     (List_Elements,
      List_Elements_Access);

   procedure Finalize (Value : in out Expression_Value) is
   begin
      case Value.Kind is
         when Dictionary_Expression_Value => Finalize (Value.Dictionary_Value);
         when List_Expression_Value => Finalize (Value.List_Value);
      when others =>
         null;
      end case;
   end Finalize;

   procedure Delete_Assocs (Assocs : in out Assocs_Access) is
   begin
      for E in Assocs.Value_Assocs.Iterate loop
         Finalize (Assocs.Value_Assocs (Key (E)));
      end loop;
      Free_Assocs (Assocs);
   end Delete_Assocs;

   overriding procedure Adjust (D : in out Dictionary) is
   begin
      if D.Assocs /= null then
         D.Assocs.Ref_Count := D.Assocs.Ref_Count + 1;
      end if;
   end Adjust;

   overriding procedure Finalize (D : in out Dictionary) is
      Assocs : Assocs_Access := D.Assocs;
   begin
      D.Assocs := null;

      if Assocs /= null then
         Assocs.Ref_Count := Assocs.Ref_Count - 1;
         if Assocs.Ref_Count = 0 then
            Delete_Assocs (Assocs);
         end if;
      end if;
   end Finalize;

   procedure Delete_Elements (Elements : in out List_Elements_Access) is
   begin
      for E of Elements.Values loop
         Finalize (E);
      end loop;
      Free_Elements (Elements);
   end Delete_Elements;

   overriding procedure Adjust (L : in out List) is
   begin
      if L.Elements /= null then
         L.Elements.Ref_Count := L.Elements.Ref_Count + 1;
      end if;
   end Adjust;

   overriding procedure Finalize (L : in out List) is
      Elements : List_Elements_Access := L.Elements;
   begin
      L.Elements := null;

      if Elements /= null then
         Elements.Ref_Count := Elements.Ref_Count - 1;
         if Elements.Ref_Count = 0 then
            Delete_Elements (Elements);
         end if;
      end if;
   end Finalize;

   procedure Append (Container : in out List;
                     New_Item : Unbounded_String) is
   begin
      Init (Container);
      Container.Elements.Values.Append ((Kind => String_Expression_Value,
                                         S => New_Item));
   end Append;

   procedure Append (Container : in out List;
                     New_Item : String) is
   begin
      Append (Container, To_Unbounded_String (New_Item));
   end Append;

   procedure Append (Container : in out List;
                     New_Item : Dictionary'Class) is
   begin
      Init (Container);
      Container.Elements.Values.Append
        ((Kind => Dictionary_Expression_Value,
          Dictionary_Value => Dictionary (New_Item)));
   end Append;

   procedure Append (Container : in out List;
                     New_Item : List) is
   begin
      Init (Container);
      Container.Elements.Values.Append ((Kind => List_Expression_Value,
                                         List_Value => New_Item));
   end Append;

   procedure Clear (Container : in out List) is
   begin
      if Container.Elements /= null then
         Clear (Container.Elements.Values);
      end if;
   end Clear;

   procedure Register_Filter (Settings : in out Environment;
                              Filter : Filter_Function;
                              Name : String) is
      Position  : Filter_Maps.Cursor;
      Inserted  : Boolean;
   begin
      Settings.Filters.Insert (To_Unbounded_String (Name),
                               Filter,
                               Position,
                               Inserted
                              );
      if not Inserted then
         Settings.Filters.Replace_Element (Position, Filter);
      end if;
   end Register_Filter;

end Jintp;