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
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
|
using System.Collections.Generic;
using System.Numerics;
using Microsoft.Boogie;
using System.IO;
using System.Text;
using System;
using System.Diagnostics.Contracts;
namespace Microsoft.Dafny {
public class Parser {
public const int _EOF = 0;
public const int _ident = 1;
public const int _digits = 2;
public const int _hexdigits = 3;
public const int _decimaldigits = 4;
public const int _arrayToken = 5;
public const int _string = 6;
public const int _colon = 7;
public const int _semi = 8;
public const int _lbrace = 9;
public const int _rbrace = 10;
public const int _openparen = 11;
public const int _star = 12;
public const int _notIn = 13;
public const int maxT = 126;
const bool T = true;
const bool x = false;
const int minErrDist = 2;
public Scanner/*!*/ scanner;
public Errors/*!*/ errors;
public Token/*!*/ t; // last recognized token
public Token/*!*/ la; // lookahead token
int errDist = minErrDist;
readonly Expression/*!*/ dummyExpr;
readonly AssignmentRhs/*!*/ dummyRhs;
readonly FrameExpression/*!*/ dummyFrameExpr;
readonly Statement/*!*/ dummyStmt;
readonly Attributes.Argument/*!*/ dummyAttrArg;
readonly ModuleDecl theModule;
readonly BuiltIns theBuiltIns;
readonly bool theVerifyThisFile;
int anonymousIds = 0;
struct MemberModifiers {
public bool IsGhost;
public bool IsStatic;
}
///<summary>
/// Parses top-level things (modules, classes, datatypes, class members) from "filename"
/// and appends them in appropriate form to "module".
/// Returns the number of parsing errors encountered.
/// Note: first initialize the Scanner.
///</summary>
public static int Parse (string/*!*/ filename, ModuleDecl module, BuiltIns builtIns, Errors/*!*/ errors, bool verifyThisFile=true) /* throws System.IO.IOException */ {
Contract.Requires(filename != null);
Contract.Requires(module != null);
string s;
if (filename == "stdin.dfy") {
s = Microsoft.Boogie.ParserHelper.Fill(System.Console.In, new List<string>());
return Parse(s, filename, module, builtIns, errors, verifyThisFile);
} else {
using (System.IO.StreamReader reader = new System.IO.StreamReader(filename)) {
s = Microsoft.Boogie.ParserHelper.Fill(reader, new List<string>());
return Parse(s, filename, module, builtIns, errors, verifyThisFile);
}
}
}
///<summary>
/// Parses top-level things (modules, classes, datatypes, class members)
/// and appends them in appropriate form to "module".
/// Returns the number of parsing errors encountered.
/// Note: first initialize the Scanner.
///</summary>
public static int Parse (string/*!*/ s, string/*!*/ filename, ModuleDecl module, BuiltIns builtIns, bool verifyThisFile=true) {
Contract.Requires(s != null);
Contract.Requires(filename != null);
Contract.Requires(module != null);
Errors errors = new Errors();
return Parse(s, filename, module, builtIns, errors, verifyThisFile);
}
///<summary>
/// Parses top-level things (modules, classes, datatypes, class members)
/// and appends them in appropriate form to "module".
/// Returns the number of parsing errors encountered.
/// Note: first initialize the Scanner with the given Errors sink.
///</summary>
public static int Parse (string/*!*/ s, string/*!*/ filename, ModuleDecl module, BuiltIns builtIns,
Errors/*!*/ errors, bool verifyThisFile=true) {
Contract.Requires(s != null);
Contract.Requires(filename != null);
Contract.Requires(module != null);
Contract.Requires(errors != null);
byte[]/*!*/ buffer = cce.NonNull( UTF8Encoding.Default.GetBytes(s));
MemoryStream ms = new MemoryStream(buffer,false);
Scanner scanner = new Scanner(ms, errors, filename);
Parser parser = new Parser(scanner, errors, module, builtIns, verifyThisFile);
parser.Parse();
return parser.errors.count;
}
public Parser(Scanner/*!*/ scanner, Errors/*!*/ errors, ModuleDecl module, BuiltIns builtIns, bool verifyThisFile=true)
: this(scanner, errors) // the real work
{
// initialize readonly fields
dummyExpr = new LiteralExpr(Token.NoToken);
dummyRhs = new ExprRhs(dummyExpr, null);
dummyFrameExpr = new FrameExpression(dummyExpr.tok, dummyExpr, null);
dummyStmt = new ReturnStmt(Token.NoToken, Token.NoToken, null);
dummyAttrArg = new Attributes.Argument(Token.NoToken, "dummyAttrArg");
theModule = module;
theBuiltIns = builtIns;
theVerifyThisFile = verifyThisFile;
}
bool IsAttribute() {
Token x = scanner.Peek();
return la.kind == _lbrace && x.kind == _colon;
}
bool IsAlternative() {
Token x = scanner.Peek();
return la.kind == _lbrace && x.val == "case";
}
bool IsLoopSpec() {
return la.val == "invariant" | la.val == "decreases" | la.val == "modifies";
}
bool IsLoopSpecOrAlternative() {
Token x = scanner.Peek();
return IsLoopSpec() || (la.kind == _lbrace && x.val == "case");
}
bool IsParenStar() {
scanner.ResetPeek();
Token x = scanner.Peek();
return la.kind == _openparen && x.kind == _star;
}
bool IsIdentParen() {
Token x = scanner.Peek();
return la.kind == _ident && x.kind == _openparen;
}
bool SemiFollowsCall(bool allowSemi, Expression e) {
return allowSemi && la.kind == _semi &&
(e is FunctionCallExpr ||
(e is IdentifierSequence && ((IdentifierSequence)e).OpenParen != null));
}
/*--------------------------------------------------------------------------*/
public Parser(Scanner/*!*/ scanner, Errors/*!*/ errors) {
this.scanner = scanner;
this.errors = errors;
Token/*!*/ tok = new Token();
tok.val = "";
this.la = tok;
this.t = new Token(); // just to satisfy its non-null constraint
}
void SynErr (int n) {
if (errDist >= minErrDist) errors.SynErr(la.filename, la.line, la.col, n);
errDist = 0;
}
public void SemErr (string/*!*/ msg) {
Contract.Requires(msg != null);
if (errDist >= minErrDist) errors.SemErr(t, msg);
errDist = 0;
}
public void SemErr(IToken/*!*/ tok, string/*!*/ msg) {
Contract.Requires(tok != null);
Contract.Requires(msg != null);
errors.SemErr(tok, msg);
}
void Get () {
for (;;) {
t = la;
la = scanner.Scan();
if (la.kind <= maxT) { ++errDist; break; }
la = t;
}
}
void Expect (int n) {
if (la.kind==n) Get(); else { SynErr(n); }
}
bool StartOf (int s) {
return set[s, la.kind];
}
void ExpectWeak (int n, int follow) {
if (la.kind == n) Get();
else {
SynErr(n);
while (!StartOf(follow)) Get();
}
}
bool WeakSeparator(int n, int syFol, int repFol) {
int kind = la.kind;
if (kind == n) {Get(); return true;}
else if (StartOf(repFol)) {return false;}
else {
SynErr(n);
while (!(set[syFol, kind] || set[repFol, kind] || set[0, kind])) {
Get();
kind = la.kind;
}
return StartOf(syFol);
}
}
void Dafny() {
ClassDecl/*!*/ c; DatatypeDecl/*!*/ dt; ArbitraryTypeDecl at; IteratorDecl iter;
List<MemberDecl/*!*/> membersDefaultClass = new List<MemberDecl/*!*/>();
ModuleDecl submodule;
// to support multiple files, create a default module only if theModule is null
DefaultModuleDecl defaultModule = (DefaultModuleDecl)((LiteralModuleDecl)theModule).ModuleDef;
// theModule should be a DefaultModuleDecl (actually, the singular DefaultModuleDecl)
Contract.Assert(defaultModule != null);
while (la.kind == 14) {
Get();
Expect(6);
{
string parsedFile = t.filename;
string includedFile = t.val.Substring(1, t.val.Length - 2);
if (!Path.IsPathRooted(includedFile)) {
string basePath = Path.GetDirectoryName(parsedFile);
includedFile = Path.GetFullPath(Path.Combine(basePath, includedFile));
}
defaultModule.Includes.Add(new Include(t, includedFile));
}
}
while (StartOf(1)) {
switch (la.kind) {
case 15: case 16: case 18: {
SubModuleDecl(defaultModule, out submodule);
defaultModule.TopLevelDecls.Add(submodule);
break;
}
case 23: {
ClassDecl(defaultModule, out c);
defaultModule.TopLevelDecls.Add(c);
break;
}
case 26: case 27: {
DatatypeDecl(defaultModule, out dt);
defaultModule.TopLevelDecls.Add(dt);
break;
}
case 31: {
ArbitraryTypeDecl(defaultModule, out at);
defaultModule.TopLevelDecls.Add(at);
break;
}
case 34: {
IteratorDecl(defaultModule, out iter);
defaultModule.TopLevelDecls.Add(iter);
break;
}
case 24: case 25: case 29: case 40: case 41: case 42: case 43: case 44: case 62: case 63: case 64: {
ClassMemberDecl(membersDefaultClass, false);
break;
}
}
}
DefaultClassDecl defaultClass = null;
foreach (TopLevelDecl topleveldecl in defaultModule.TopLevelDecls) {
defaultClass = topleveldecl as DefaultClassDecl;
if (defaultClass != null) {
defaultClass.Members.AddRange(membersDefaultClass);
break;
}
}
if (defaultClass == null) { // create the default class here, because it wasn't found
defaultClass = new DefaultClassDecl(defaultModule, membersDefaultClass);
defaultModule.TopLevelDecls.Add(defaultClass);
}
Expect(0);
}
void SubModuleDecl(ModuleDefinition parent, out ModuleDecl submodule) {
ClassDecl/*!*/ c; DatatypeDecl/*!*/ dt; ArbitraryTypeDecl at; IteratorDecl iter;
Attributes attrs = null; IToken/*!*/ id;
List<MemberDecl/*!*/> namedModuleDefaultClassMembers = new List<MemberDecl>();;
List<IToken> idRefined = null, idPath = null, idAssignment = null;
ModuleDefinition module;
ModuleDecl sm;
submodule = null; // appease compiler
bool isAbstract = false;
bool opened = false;
if (la.kind == 15 || la.kind == 16) {
if (la.kind == 15) {
Get();
isAbstract = true;
}
Expect(16);
while (la.kind == 9) {
Attribute(ref attrs);
}
NoUSIdent(out id);
if (la.kind == 17) {
Get();
QualifiedName(out idRefined);
}
module = new ModuleDefinition(id, id.val, isAbstract, false, idRefined == null ? null : idRefined, parent, attrs, false);
Expect(9);
module.BodyStartTok = t;
while (StartOf(1)) {
switch (la.kind) {
case 15: case 16: case 18: {
SubModuleDecl(module, out sm);
module.TopLevelDecls.Add(sm);
break;
}
case 23: {
ClassDecl(module, out c);
module.TopLevelDecls.Add(c);
break;
}
case 26: case 27: {
DatatypeDecl(module, out dt);
module.TopLevelDecls.Add(dt);
break;
}
case 31: {
ArbitraryTypeDecl(module, out at);
module.TopLevelDecls.Add(at);
break;
}
case 34: {
IteratorDecl(module, out iter);
module.TopLevelDecls.Add(iter);
break;
}
case 24: case 25: case 29: case 40: case 41: case 42: case 43: case 44: case 62: case 63: case 64: {
ClassMemberDecl(namedModuleDefaultClassMembers, false);
break;
}
}
}
Expect(10);
module.BodyEndTok = t;
module.TopLevelDecls.Add(new DefaultClassDecl(module, namedModuleDefaultClassMembers));
submodule = new LiteralModuleDecl(module, parent);
} else if (la.kind == 18) {
Get();
if (la.kind == 19) {
Get();
opened = true;
}
NoUSIdent(out id);
if (la.kind == 20 || la.kind == 21) {
if (la.kind == 20) {
Get();
QualifiedName(out idPath);
submodule = new AliasModuleDecl(idPath, id, parent, opened);
} else {
Get();
QualifiedName(out idPath);
if (la.kind == 22) {
Get();
QualifiedName(out idAssignment);
}
submodule = new ModuleFacadeDecl(idPath, id, parent, idAssignment, opened);
}
}
if (la.kind == 8) {
while (!(la.kind == 0 || la.kind == 8)) {SynErr(127); Get();}
Get();
}
if (submodule == null) {
idPath = new List<IToken>();
idPath.Add(id);
submodule = new AliasModuleDecl(idPath, id, parent, opened);
}
} else SynErr(128);
}
void ClassDecl(ModuleDefinition/*!*/ module, out ClassDecl/*!*/ c) {
Contract.Requires(module != null);
Contract.Ensures(Contract.ValueAtReturn(out c) != null);
IToken/*!*/ id;
Attributes attrs = null;
List<TypeParameter/*!*/> typeArgs = new List<TypeParameter/*!*/>();
List<MemberDecl/*!*/> members = new List<MemberDecl/*!*/>();
IToken bodyStart;
while (!(la.kind == 0 || la.kind == 23)) {SynErr(129); Get();}
Expect(23);
while (la.kind == 9) {
Attribute(ref attrs);
}
NoUSIdent(out id);
if (la.kind == 38) {
GenericParameters(typeArgs);
}
Expect(9);
bodyStart = t;
while (StartOf(2)) {
ClassMemberDecl(members, true);
}
Expect(10);
c = new ClassDecl(id, id.val, module, typeArgs, members, attrs);
c.BodyStartTok = bodyStart;
c.BodyEndTok = t;
}
void DatatypeDecl(ModuleDefinition/*!*/ module, out DatatypeDecl/*!*/ dt) {
Contract.Requires(module != null);
Contract.Ensures(Contract.ValueAtReturn(out dt)!=null);
IToken/*!*/ id;
Attributes attrs = null;
List<TypeParameter/*!*/> typeArgs = new List<TypeParameter/*!*/>();
List<DatatypeCtor/*!*/> ctors = new List<DatatypeCtor/*!*/>();
IToken bodyStart = Token.NoToken; // dummy assignment
bool co = false;
while (!(la.kind == 0 || la.kind == 26 || la.kind == 27)) {SynErr(130); Get();}
if (la.kind == 26) {
Get();
} else if (la.kind == 27) {
Get();
co = true;
} else SynErr(131);
while (la.kind == 9) {
Attribute(ref attrs);
}
NoUSIdent(out id);
if (la.kind == 38) {
GenericParameters(typeArgs);
}
Expect(20);
bodyStart = t;
DatatypeMemberDecl(ctors);
while (la.kind == 28) {
Get();
DatatypeMemberDecl(ctors);
}
if (la.kind == 8) {
while (!(la.kind == 0 || la.kind == 8)) {SynErr(132); Get();}
Get();
}
if (co) {
dt = new CoDatatypeDecl(id, id.val, module, typeArgs, ctors, attrs);
} else {
dt = new IndDatatypeDecl(id, id.val, module, typeArgs, ctors, attrs);
}
dt.BodyStartTok = bodyStart;
dt.BodyEndTok = t;
}
void ArbitraryTypeDecl(ModuleDefinition/*!*/ module, out ArbitraryTypeDecl at) {
IToken/*!*/ id;
Attributes attrs = null;
var eqSupport = TypeParameter.EqualitySupportValue.Unspecified;
Expect(31);
while (la.kind == 9) {
Attribute(ref attrs);
}
NoUSIdent(out id);
if (la.kind == 11) {
Get();
Expect(32);
Expect(33);
eqSupport = TypeParameter.EqualitySupportValue.Required;
}
at = new ArbitraryTypeDecl(id, id.val, module, eqSupport, attrs);
if (la.kind == 8) {
while (!(la.kind == 0 || la.kind == 8)) {SynErr(133); Get();}
Get();
}
}
void IteratorDecl(ModuleDefinition module, out IteratorDecl/*!*/ iter) {
Contract.Ensures(Contract.ValueAtReturn(out iter) != null);
IToken/*!*/ id;
Attributes attrs = null;
List<TypeParameter/*!*/>/*!*/ typeArgs = new List<TypeParameter/*!*/>();
IToken openParen;
List<Formal/*!*/> ins = new List<Formal/*!*/>();
List<Formal/*!*/> outs = new List<Formal/*!*/>();
List<FrameExpression/*!*/> reads = new List<FrameExpression/*!*/>();
List<FrameExpression/*!*/> mod = new List<FrameExpression/*!*/>();
List<Expression/*!*/> decreases = new List<Expression>();
List<MaybeFreeExpression/*!*/> req = new List<MaybeFreeExpression/*!*/>();
List<MaybeFreeExpression/*!*/> ens = new List<MaybeFreeExpression/*!*/>();
List<MaybeFreeExpression/*!*/> yieldReq = new List<MaybeFreeExpression/*!*/>();
List<MaybeFreeExpression/*!*/> yieldEns = new List<MaybeFreeExpression/*!*/>();
List<Expression/*!*/> dec = new List<Expression/*!*/>();
Attributes readsAttrs = null;
Attributes modAttrs = null;
Attributes decrAttrs = null;
BlockStmt body = null;
IToken signatureEllipsis = null;
IToken bodyStart = Token.NoToken;
IToken bodyEnd = Token.NoToken;
while (!(la.kind == 0 || la.kind == 34)) {SynErr(134); Get();}
Expect(34);
while (la.kind == 9) {
Attribute(ref attrs);
}
NoUSIdent(out id);
if (la.kind == 11 || la.kind == 38) {
if (la.kind == 38) {
GenericParameters(typeArgs);
}
Formals(true, true, ins, out openParen);
if (la.kind == 35 || la.kind == 36) {
if (la.kind == 35) {
Get();
} else {
Get();
SemErr(t, "iterators don't have a 'returns' clause; did you mean 'yields'?");
}
Formals(false, true, outs, out openParen);
}
} else if (la.kind == 37) {
Get();
signatureEllipsis = t; openParen = Token.NoToken;
} else SynErr(135);
while (StartOf(3)) {
IteratorSpec(reads, mod, decreases, req, ens, yieldReq, yieldEns, ref readsAttrs, ref modAttrs, ref decrAttrs);
}
if (la.kind == 9) {
BlockStmt(out body, out bodyStart, out bodyEnd);
}
iter = new IteratorDecl(id, id.val, module, typeArgs, ins, outs,
new Specification<FrameExpression>(reads, readsAttrs),
new Specification<FrameExpression>(mod, modAttrs),
new Specification<Expression>(decreases, decrAttrs),
req, ens, yieldReq, yieldEns,
body, attrs, signatureEllipsis);
iter.BodyStartTok = bodyStart;
iter.BodyEndTok = bodyEnd;
}
void ClassMemberDecl(List<MemberDecl/*!*/>/*!*/ mm, bool allowConstructors) {
Contract.Requires(cce.NonNullElements(mm));
Method/*!*/ m;
Function/*!*/ f;
MemberModifiers mmod = new MemberModifiers();
while (la.kind == 24 || la.kind == 25) {
if (la.kind == 24) {
Get();
mmod.IsGhost = true;
} else {
Get();
mmod.IsStatic = true;
}
}
if (la.kind == 29) {
FieldDecl(mmod, mm);
} else if (la.kind == 62 || la.kind == 63 || la.kind == 64) {
FunctionDecl(mmod, out f);
mm.Add(f);
} else if (StartOf(4)) {
MethodDecl(mmod, allowConstructors, out m);
mm.Add(m);
} else SynErr(136);
}
void Attribute(ref Attributes attrs) {
Expect(9);
AttributeBody(ref attrs);
Expect(10);
}
void NoUSIdent(out IToken/*!*/ x) {
Contract.Ensures(Contract.ValueAtReturn(out x) != null);
Expect(1);
x = t;
if (x.val.StartsWith("_")) {
SemErr("cannot declare identifier beginning with underscore");
}
}
void QualifiedName(out List<IToken> ids) {
IToken id; IToken idPrime; ids = new List<IToken>();
Ident(out id);
ids.Add(id);
while (la.kind == 61) {
IdentOrDigitsSuffix(out id, out idPrime);
ids.Add(id);
if (idPrime != null) { ids.Add(idPrime); }
}
}
void Ident(out IToken/*!*/ x) {
Contract.Ensures(Contract.ValueAtReturn(out x) != null);
Expect(1);
x = t;
}
void IdentOrDigitsSuffix(out IToken x, out IToken y) {
Contract.Ensures(Contract.ValueAtReturn(out x) != null);
x = Token.NoToken;
y = null;
Expect(61);
if (la.kind == 1) {
Get();
x = t;
} else if (la.kind == 2) {
Get();
x = t;
} else if (la.kind == 4) {
Get();
x = t;
int exponent = x.val.IndexOf('e');
if (0 <= exponent) {
// this is not a legal field/destructor name
SemErr(x, "invalid IdentOrDigitsSuffix");
} else {
int dot = x.val.IndexOf('.');
if (0 <= dot) {
y = new Token();
y.pos = x.pos + dot + 1;
y.val = x.val.Substring(dot + 1);
x.val = x.val.Substring(0, dot);
y.col = x.col + dot + 1;
y.line = x.line;
y.filename = x.filename;
y.kind = x.kind;
}
}
} else SynErr(137);
}
void GenericParameters(List<TypeParameter/*!*/>/*!*/ typeArgs) {
Contract.Requires(cce.NonNullElements(typeArgs));
IToken/*!*/ id;
TypeParameter.EqualitySupportValue eqSupport;
Expect(38);
NoUSIdent(out id);
eqSupport = TypeParameter.EqualitySupportValue.Unspecified;
if (la.kind == 11) {
Get();
Expect(32);
Expect(33);
eqSupport = TypeParameter.EqualitySupportValue.Required;
}
typeArgs.Add(new TypeParameter(id, id.val, eqSupport));
while (la.kind == 30) {
Get();
NoUSIdent(out id);
eqSupport = TypeParameter.EqualitySupportValue.Unspecified;
if (la.kind == 11) {
Get();
Expect(32);
Expect(33);
eqSupport = TypeParameter.EqualitySupportValue.Required;
}
typeArgs.Add(new TypeParameter(id, id.val, eqSupport));
}
Expect(39);
}
void FieldDecl(MemberModifiers mmod, List<MemberDecl/*!*/>/*!*/ mm) {
Contract.Requires(cce.NonNullElements(mm));
Attributes attrs = null;
IToken/*!*/ id; Type/*!*/ ty;
while (!(la.kind == 0 || la.kind == 29)) {SynErr(138); Get();}
Expect(29);
if (mmod.IsStatic) { SemErr(t, "fields cannot be declared 'static'"); }
while (la.kind == 9) {
Attribute(ref attrs);
}
FIdentType(out id, out ty);
mm.Add(new Field(id, id.val, mmod.IsGhost, ty, attrs));
while (la.kind == 30) {
Get();
FIdentType(out id, out ty);
mm.Add(new Field(id, id.val, mmod.IsGhost, ty, attrs));
}
while (!(la.kind == 0 || la.kind == 8)) {SynErr(139); Get();}
Expect(8);
}
void FunctionDecl(MemberModifiers mmod, out Function/*!*/ f) {
Contract.Ensures(Contract.ValueAtReturn(out f)!=null);
Attributes attrs = null;
IToken/*!*/ id = Token.NoToken; // to please compiler
List<TypeParameter/*!*/> typeArgs = new List<TypeParameter/*!*/>();
List<Formal/*!*/> formals = new List<Formal/*!*/>();
Type/*!*/ returnType = new BoolType();
List<Expression/*!*/> reqs = new List<Expression/*!*/>();
List<Expression/*!*/> ens = new List<Expression/*!*/>();
List<FrameExpression/*!*/> reads = new List<FrameExpression/*!*/>();
List<Expression/*!*/> decreases;
Expression body = null;
bool isPredicate = false; bool isCoPredicate = false;
bool isFunctionMethod = false;
IToken openParen = null;
IToken bodyStart = Token.NoToken;
IToken bodyEnd = Token.NoToken;
IToken signatureEllipsis = null;
if (la.kind == 62) {
Get();
if (la.kind == 40) {
Get();
isFunctionMethod = true;
}
if (mmod.IsGhost) { SemErr(t, "functions cannot be declared 'ghost' (they are ghost by default)"); }
while (la.kind == 9) {
Attribute(ref attrs);
}
NoUSIdent(out id);
if (la.kind == 11 || la.kind == 38) {
if (la.kind == 38) {
GenericParameters(typeArgs);
}
Formals(true, isFunctionMethod, formals, out openParen);
Expect(7);
Type(out returnType);
} else if (la.kind == 37) {
Get();
signatureEllipsis = t;
openParen = Token.NoToken;
} else SynErr(140);
} else if (la.kind == 63) {
Get();
isPredicate = true;
if (la.kind == 40) {
Get();
isFunctionMethod = true;
}
if (mmod.IsGhost) { SemErr(t, "predicates cannot be declared 'ghost' (they are ghost by default)"); }
while (la.kind == 9) {
Attribute(ref attrs);
}
NoUSIdent(out id);
if (StartOf(5)) {
if (la.kind == 38) {
GenericParameters(typeArgs);
}
if (la.kind == 11) {
Formals(true, isFunctionMethod, formals, out openParen);
if (la.kind == 7) {
Get();
SemErr(t, "predicates do not have an explicitly declared return type; it is always bool");
}
}
} else if (la.kind == 37) {
Get();
signatureEllipsis = t;
openParen = Token.NoToken;
} else SynErr(141);
} else if (la.kind == 64) {
Get();
isCoPredicate = true;
if (mmod.IsGhost) { SemErr(t, "copredicates cannot be declared 'ghost' (they are ghost by default)"); }
while (la.kind == 9) {
Attribute(ref attrs);
}
NoUSIdent(out id);
if (StartOf(5)) {
if (la.kind == 38) {
GenericParameters(typeArgs);
}
if (la.kind == 11) {
Formals(true, isFunctionMethod, formals, out openParen);
if (la.kind == 7) {
Get();
SemErr(t, "copredicates do not have an explicitly declared return type; it is always bool");
}
}
} else if (la.kind == 37) {
Get();
signatureEllipsis = t;
openParen = Token.NoToken;
} else SynErr(142);
} else SynErr(143);
decreases = isCoPredicate ? null : new List<Expression/*!*/>();
while (StartOf(6)) {
FunctionSpec(reqs, reads, ens, decreases);
}
if (la.kind == 9) {
FunctionBody(out body, out bodyStart, out bodyEnd);
}
if (!theVerifyThisFile) { // We still need the func bodies, but don't bother verifying their correctness
List<Attributes.Argument/*!*/> args = new List<Attributes.Argument/*!*/>();
Attributes.Argument/*!*/ anArg;
anArg = new Attributes.Argument(id, new LiteralExpr(t, false));
args.Add(anArg);
attrs = new Attributes("verify", args, attrs);
}
if (DafnyOptions.O.DisallowSoundnessCheating && body == null && ens.Count > 0 && !Attributes.Contains(attrs, "axiom")) {
SemErr(t, "a function with an ensures clause must have a body, unless given the :axiom attribute");
}
if (isPredicate) {
f = new Predicate(id, id.val, mmod.IsStatic, !isFunctionMethod, typeArgs, openParen, formals,
reqs, reads, ens, new Specification<Expression>(decreases, null), body, Predicate.BodyOriginKind.OriginalOrInherited, attrs, signatureEllipsis);
} else if (isCoPredicate) {
f = new CoPredicate(id, id.val, mmod.IsStatic, typeArgs, openParen, formals,
reqs, reads, ens, body, attrs, signatureEllipsis);
} else {
f = new Function(id, id.val, mmod.IsStatic, !isFunctionMethod, typeArgs, openParen, formals, returnType,
reqs, reads, ens, new Specification<Expression>(decreases, null), body, attrs, signatureEllipsis);
}
f.BodyStartTok = bodyStart;
f.BodyEndTok = bodyEnd;
}
void MethodDecl(MemberModifiers mmod, bool allowConstructor, out Method/*!*/ m) {
Contract.Ensures(Contract.ValueAtReturn(out m) !=null);
IToken/*!*/ id = Token.NoToken;
bool hasName = false; IToken keywordToken;
Attributes attrs = null;
List<TypeParameter/*!*/>/*!*/ typeArgs = new List<TypeParameter/*!*/>();
IToken openParen;
List<Formal/*!*/> ins = new List<Formal/*!*/>();
List<Formal/*!*/> outs = new List<Formal/*!*/>();
List<MaybeFreeExpression/*!*/> req = new List<MaybeFreeExpression/*!*/>();
List<FrameExpression/*!*/> mod = new List<FrameExpression/*!*/>();
List<MaybeFreeExpression/*!*/> ens = new List<MaybeFreeExpression/*!*/>();
List<Expression/*!*/> dec = new List<Expression/*!*/>();
Attributes decAttrs = null;
Attributes modAttrs = null;
BlockStmt body = null;
bool isLemma = false;
bool isConstructor = false;
bool isCoLemma = false;
IToken signatureEllipsis = null;
IToken bodyStart = Token.NoToken;
IToken bodyEnd = Token.NoToken;
while (!(StartOf(7))) {SynErr(144); Get();}
if (la.kind == 40) {
Get();
} else if (la.kind == 41) {
Get();
isLemma = true;
} else if (la.kind == 42) {
Get();
isCoLemma = true;
} else if (la.kind == 43) {
Get();
isCoLemma = true;
errors.Warning(t, "the 'comethod' keyword has been deprecated; it has been renamed to 'colemma'");
} else if (la.kind == 44) {
Get();
if (allowConstructor) {
isConstructor = true;
} else {
SemErr(t, "constructors are only allowed in classes");
}
} else SynErr(145);
keywordToken = t;
if (isLemma) {
if (mmod.IsGhost) {
SemErr(t, "lemmas cannot be declared 'ghost' (they are automatically 'ghost')");
}
} else if (isConstructor) {
if (mmod.IsGhost) {
SemErr(t, "constructors cannot be declared 'ghost'");
}
if (mmod.IsStatic) {
SemErr(t, "constructors cannot be declared 'static'");
}
} else if (isCoLemma) {
if (mmod.IsGhost) {
SemErr(t, "colemmas cannot be declared 'ghost' (they are automatically 'ghost')");
}
}
while (la.kind == 9) {
Attribute(ref attrs);
}
if (la.kind == 1) {
NoUSIdent(out id);
hasName = true;
}
if (!hasName) {
id = keywordToken;
if (!isConstructor) {
SemErr(la, "a method must be given a name (expecting identifier)");
}
}
if (la.kind == 11 || la.kind == 38) {
if (la.kind == 38) {
GenericParameters(typeArgs);
}
Formals(true, !mmod.IsGhost, ins, out openParen);
if (la.kind == 36) {
Get();
if (isConstructor) { SemErr(t, "constructors cannot have out-parameters"); }
Formals(false, !mmod.IsGhost, outs, out openParen);
}
} else if (la.kind == 37) {
Get();
signatureEllipsis = t; openParen = Token.NoToken;
} else SynErr(146);
while (StartOf(8)) {
MethodSpec(req, mod, ens, dec, ref decAttrs, ref modAttrs);
}
if (la.kind == 9) {
BlockStmt(out body, out bodyStart, out bodyEnd);
}
if (!theVerifyThisFile) {
body = null;
List<Attributes.Argument/*!*/> args = new List<Attributes.Argument/*!*/>();
Attributes.Argument/*!*/ anArg;
anArg = new Attributes.Argument(id, new LiteralExpr(t, false));
args.Add(anArg);
attrs = new Attributes("verify", args, attrs);
}
if (Attributes.Contains(attrs, "axiom") && !mmod.IsGhost && !isLemma) {
SemErr(t, "only ghost methods can have the :axiom attribute");
}
if (DafnyOptions.O.DisallowSoundnessCheating && body == null && ens.Count > 0 && !Attributes.Contains(attrs, "axiom")) {
SemErr(t, "a method with an ensures clause must have a body, unless given the :axiom attribute");
}
if (isConstructor) {
m = new Constructor(id, hasName ? id.val : "_ctor", typeArgs, ins,
req, new Specification<FrameExpression>(mod, modAttrs), ens, new Specification<Expression>(dec, decAttrs), body, attrs, signatureEllipsis);
} else if (isCoLemma) {
m = new CoLemma(id, id.val, mmod.IsStatic, typeArgs, ins, outs,
req, new Specification<FrameExpression>(mod, modAttrs), ens, new Specification<Expression>(dec, decAttrs), body, attrs, signatureEllipsis);
} else if (isLemma) {
m = new Lemma(id, id.val, mmod.IsStatic, typeArgs, ins, outs,
req, new Specification<FrameExpression>(mod, modAttrs), ens, new Specification<Expression>(dec, decAttrs), body, attrs, signatureEllipsis);
} else {
m = new Method(id, id.val, mmod.IsStatic, mmod.IsGhost, typeArgs, ins, outs,
req, new Specification<FrameExpression>(mod, modAttrs), ens, new Specification<Expression>(dec, decAttrs), body, attrs, signatureEllipsis);
}
m.BodyStartTok = bodyStart;
m.BodyEndTok = bodyEnd;
}
void DatatypeMemberDecl(List<DatatypeCtor/*!*/>/*!*/ ctors) {
Contract.Requires(cce.NonNullElements(ctors));
Attributes attrs = null;
IToken/*!*/ id;
List<Formal/*!*/> formals = new List<Formal/*!*/>();
while (la.kind == 9) {
Attribute(ref attrs);
}
NoUSIdent(out id);
if (la.kind == 11) {
FormalsOptionalIds(formals);
}
ctors.Add(new DatatypeCtor(id, id.val, formals, attrs));
}
void FormalsOptionalIds(List<Formal/*!*/>/*!*/ formals) {
Contract.Requires(cce.NonNullElements(formals)); IToken/*!*/ id; Type/*!*/ ty; string/*!*/ name; bool isGhost;
Expect(11);
if (StartOf(9)) {
TypeIdentOptional(out id, out name, out ty, out isGhost);
formals.Add(new Formal(id, name, ty, true, isGhost));
while (la.kind == 30) {
Get();
TypeIdentOptional(out id, out name, out ty, out isGhost);
formals.Add(new Formal(id, name, ty, true, isGhost));
}
}
Expect(33);
}
void FIdentType(out IToken/*!*/ id, out Type/*!*/ ty) {
Contract.Ensures(Contract.ValueAtReturn(out id) != null); Contract.Ensures(Contract.ValueAtReturn(out ty) != null);
id = Token.NoToken;
if (la.kind == 1) {
WildIdent(out id, false);
} else if (la.kind == 2) {
Get();
id = t;
} else SynErr(147);
Expect(7);
Type(out ty);
}
void GIdentType(bool allowGhostKeyword, out IToken/*!*/ id, out Type/*!*/ ty, out bool isGhost) {
Contract.Ensures(Contract.ValueAtReturn(out id)!=null);
Contract.Ensures(Contract.ValueAtReturn(out ty)!=null);
isGhost = false;
if (la.kind == 24) {
Get();
if (allowGhostKeyword) { isGhost = true; } else { SemErr(t, "formal cannot be declared 'ghost' in this context"); }
}
IdentType(out id, out ty, true);
}
void IdentType(out IToken/*!*/ id, out Type/*!*/ ty, bool allowWildcardId) {
Contract.Ensures(Contract.ValueAtReturn(out id) != null); Contract.Ensures(Contract.ValueAtReturn(out ty) != null);
WildIdent(out id, allowWildcardId);
Expect(7);
Type(out ty);
}
void WildIdent(out IToken/*!*/ x, bool allowWildcardId) {
Contract.Ensures(Contract.ValueAtReturn(out x) != null);
Expect(1);
x = t;
if (x.val.StartsWith("_")) {
if (allowWildcardId && x.val.Length == 1) {
t.val = "_v" + anonymousIds++;
} else {
SemErr("cannot declare identifier beginning with underscore");
}
}
}
void Type(out Type/*!*/ ty) {
Contract.Ensures(Contract.ValueAtReturn(out ty) != null); IToken/*!*/ tok;
TypeAndToken(out tok, out ty);
}
void LocalIdentTypeOptional(out LocalVariable var, bool isGhost) {
IToken id; Type ty; Type optType = null;
WildIdent(out id, true);
if (la.kind == 7) {
Get();
Type(out ty);
optType = ty;
}
var = new LocalVariable(id, id, id.val, optType == null ? new InferredTypeProxy() : optType, isGhost);
}
void IdentTypeOptional(out BoundVar var) {
Contract.Ensures(Contract.ValueAtReturn(out var) != null);
IToken id; Type ty; Type optType = null;
WildIdent(out id, true);
if (la.kind == 7) {
Get();
Type(out ty);
optType = ty;
}
var = new BoundVar(id, id.val, optType == null ? new InferredTypeProxy() : optType);
}
void TypeIdentOptional(out IToken/*!*/ id, out string/*!*/ identName, out Type/*!*/ ty, out bool isGhost) {
Contract.Ensures(Contract.ValueAtReturn(out id)!=null);
Contract.Ensures(Contract.ValueAtReturn(out ty)!=null);
Contract.Ensures(Contract.ValueAtReturn(out identName)!=null);
string name = null; id = Token.NoToken; ty = new BoolType()/*dummy*/; isGhost = false;
if (la.kind == 24) {
Get();
isGhost = true;
}
if (StartOf(10)) {
TypeAndToken(out id, out ty);
if (la.kind == 7) {
Get();
UserDefinedType udt = ty as UserDefinedType;
if (udt != null && udt.TypeArgs.Count == 0) {
name = udt.Name;
} else {
SemErr(id, "invalid formal-parameter name in datatype constructor");
}
Type(out ty);
}
} else if (la.kind == 2) {
Get();
id = t; name = id.val;
Expect(7);
Type(out ty);
} else SynErr(148);
if (name != null) {
identName = name;
} else {
identName = "#" + anonymousIds++;
}
}
void TypeAndToken(out IToken/*!*/ tok, out Type/*!*/ ty) {
Contract.Ensures(Contract.ValueAtReturn(out tok)!=null); Contract.Ensures(Contract.ValueAtReturn(out ty) != null); tok = Token.NoToken; ty = new BoolType(); /*keep compiler happy*/
List<Type/*!*/>/*!*/ gt;
switch (la.kind) {
case 52: {
Get();
tok = t;
break;
}
case 53: {
Get();
tok = t; ty = new NatType();
break;
}
case 54: {
Get();
tok = t; ty = new IntType();
break;
}
case 55: {
Get();
tok = t; ty = new RealType();
break;
}
case 56: {
Get();
tok = t; gt = new List<Type/*!*/>();
if (la.kind == 38) {
GenericInstantiation(gt);
}
if (gt.Count > 1) {
SemErr("set type expects only one type argument");
}
ty = new SetType(gt.Count == 1 ? gt[0] : null);
break;
}
case 57: {
Get();
tok = t; gt = new List<Type/*!*/>();
if (la.kind == 38) {
GenericInstantiation(gt);
}
if (gt.Count > 1) {
SemErr("multiset type expects only one type argument");
}
ty = new MultiSetType(gt.Count == 1 ? gt[0] : null);
break;
}
case 58: {
Get();
tok = t; gt = new List<Type/*!*/>();
if (la.kind == 38) {
GenericInstantiation(gt);
}
if (gt.Count > 1) {
SemErr("seq type expects only one type argument");
}
ty = new SeqType(gt.Count == 1 ? gt[0] : null);
break;
}
case 59: {
Get();
tok = t; gt = new List<Type/*!*/>();
if (la.kind == 38) {
GenericInstantiation(gt);
}
if (gt.Count == 0) {
ty = new MapType(null, null);
} else if (gt.Count != 2) {
SemErr("map type expects two type arguments");
ty = new MapType(gt[0], gt.Count == 1 ? new InferredTypeProxy() : gt[1]);
} else {
ty = new MapType(gt[0], gt[1]);
}
break;
}
case 1: case 5: case 60: {
ReferenceType(out tok, out ty);
break;
}
default: SynErr(149); break;
}
}
void Formals(bool incoming, bool allowGhostKeyword, List<Formal/*!*/>/*!*/ formals, out IToken openParen) {
Contract.Requires(cce.NonNullElements(formals)); IToken/*!*/ id; Type/*!*/ ty; bool isGhost;
Expect(11);
openParen = t;
if (la.kind == 1 || la.kind == 24) {
GIdentType(allowGhostKeyword, out id, out ty, out isGhost);
formals.Add(new Formal(id, id.val, ty, incoming, isGhost));
while (la.kind == 30) {
Get();
GIdentType(allowGhostKeyword, out id, out ty, out isGhost);
formals.Add(new Formal(id, id.val, ty, incoming, isGhost));
}
}
Expect(33);
}
void IteratorSpec(List<FrameExpression/*!*/>/*!*/ reads, List<FrameExpression/*!*/>/*!*/ mod, List<Expression/*!*/> decreases,
List<MaybeFreeExpression/*!*/>/*!*/ req, List<MaybeFreeExpression/*!*/>/*!*/ ens,
List<MaybeFreeExpression/*!*/>/*!*/ yieldReq, List<MaybeFreeExpression/*!*/>/*!*/ yieldEns,
ref Attributes readsAttrs, ref Attributes modAttrs, ref Attributes decrAttrs) {
Expression/*!*/ e; FrameExpression/*!*/ fe; bool isFree = false; bool isYield = false; Attributes ensAttrs = null;
while (!(StartOf(11))) {SynErr(150); Get();}
if (la.kind == 50) {
Get();
while (IsAttribute()) {
Attribute(ref readsAttrs);
}
if (StartOf(12)) {
FrameExpression(out fe);
reads.Add(fe);
while (la.kind == 30) {
Get();
FrameExpression(out fe);
reads.Add(fe);
}
}
while (!(la.kind == 0 || la.kind == 8)) {SynErr(151); Get();}
Expect(8);
} else if (la.kind == 45) {
Get();
while (IsAttribute()) {
Attribute(ref modAttrs);
}
if (StartOf(12)) {
FrameExpression(out fe);
mod.Add(fe);
while (la.kind == 30) {
Get();
FrameExpression(out fe);
mod.Add(fe);
}
}
while (!(la.kind == 0 || la.kind == 8)) {SynErr(152); Get();}
Expect(8);
} else if (StartOf(13)) {
if (la.kind == 46) {
Get();
isFree = true;
}
if (la.kind == 51) {
Get();
isYield = true;
}
if (la.kind == 47) {
Get();
Expression(out e, false);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(153); Get();}
Expect(8);
if (isYield) {
yieldReq.Add(new MaybeFreeExpression(e, isFree));
} else {
req.Add(new MaybeFreeExpression(e, isFree));
}
} else if (la.kind == 48) {
Get();
while (IsAttribute()) {
Attribute(ref ensAttrs);
}
Expression(out e, false);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(154); Get();}
Expect(8);
if (isYield) {
yieldEns.Add(new MaybeFreeExpression(e, isFree, ensAttrs));
} else {
ens.Add(new MaybeFreeExpression(e, isFree, ensAttrs));
}
} else SynErr(155);
} else if (la.kind == 49) {
Get();
while (IsAttribute()) {
Attribute(ref decrAttrs);
}
DecreasesList(decreases, false);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(156); Get();}
Expect(8);
} else SynErr(157);
}
void BlockStmt(out BlockStmt/*!*/ block, out IToken bodyStart, out IToken bodyEnd) {
Contract.Ensures(Contract.ValueAtReturn(out block) != null);
List<Statement/*!*/> body = new List<Statement/*!*/>();
Expect(9);
bodyStart = t;
while (StartOf(14)) {
Stmt(body);
}
Expect(10);
bodyEnd = t;
block = new BlockStmt(bodyStart, bodyEnd, body);
}
void MethodSpec(List<MaybeFreeExpression/*!*/>/*!*/ req, List<FrameExpression/*!*/>/*!*/ mod, List<MaybeFreeExpression/*!*/>/*!*/ ens,
List<Expression/*!*/>/*!*/ decreases, ref Attributes decAttrs, ref Attributes modAttrs) {
Contract.Requires(cce.NonNullElements(req)); Contract.Requires(cce.NonNullElements(mod)); Contract.Requires(cce.NonNullElements(ens)); Contract.Requires(cce.NonNullElements(decreases));
Expression/*!*/ e; FrameExpression/*!*/ fe; bool isFree = false; Attributes ensAttrs = null;
while (!(StartOf(15))) {SynErr(158); Get();}
if (la.kind == 45) {
Get();
while (IsAttribute()) {
Attribute(ref modAttrs);
}
if (StartOf(12)) {
FrameExpression(out fe);
mod.Add(fe);
while (la.kind == 30) {
Get();
FrameExpression(out fe);
mod.Add(fe);
}
}
while (!(la.kind == 0 || la.kind == 8)) {SynErr(159); Get();}
Expect(8);
} else if (la.kind == 46 || la.kind == 47 || la.kind == 48) {
if (la.kind == 46) {
Get();
isFree = true;
}
if (la.kind == 47) {
Get();
Expression(out e, false);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(160); Get();}
Expect(8);
req.Add(new MaybeFreeExpression(e, isFree));
} else if (la.kind == 48) {
Get();
while (IsAttribute()) {
Attribute(ref ensAttrs);
}
Expression(out e, false);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(161); Get();}
Expect(8);
ens.Add(new MaybeFreeExpression(e, isFree, ensAttrs));
} else SynErr(162);
} else if (la.kind == 49) {
Get();
while (IsAttribute()) {
Attribute(ref decAttrs);
}
DecreasesList(decreases, true);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(163); Get();}
Expect(8);
} else SynErr(164);
}
void FrameExpression(out FrameExpression/*!*/ fe) {
Contract.Ensures(Contract.ValueAtReturn(out fe) != null);
Expression/*!*/ e;
IToken/*!*/ id;
string fieldName = null; IToken feTok = null;
fe = null;
if (StartOf(16)) {
Expression(out e, false);
feTok = e.tok;
if (la.kind == 65) {
Get();
Ident(out id);
fieldName = id.val; feTok = id;
}
fe = new FrameExpression(feTok, e, fieldName);
} else if (la.kind == 65) {
Get();
Ident(out id);
fieldName = id.val;
fe = new FrameExpression(id, new ImplicitThisExpr(id), fieldName);
} else SynErr(165);
}
void Expression(out Expression e, bool allowSemi) {
Expression e0; IToken endTok;
EquivExpression(out e, allowSemi);
if (SemiFollowsCall(allowSemi, e)) {
Expect(8);
endTok = t;
Expression(out e0, allowSemi);
e = new StmtExpr(e.tok,
new UpdateStmt(e.tok, endTok, new List<Expression>(), new List<AssignmentRhs>() { new ExprRhs(e, null) }),
e0);
}
}
void DecreasesList(List<Expression/*!*/> decreases, bool allowWildcard) {
Expression/*!*/ e;
PossiblyWildExpression(out e);
if (!allowWildcard && e is WildcardExpr) {
SemErr(e.tok, "'decreases *' is only allowed on loops and tail-recursive methods");
} else {
decreases.Add(e);
}
while (la.kind == 30) {
Get();
PossiblyWildExpression(out e);
if (!allowWildcard && e is WildcardExpr) {
SemErr(e.tok, "'decreases *' is only allowed on loops and tail-recursive methods");
} else {
decreases.Add(e);
}
}
}
void GenericInstantiation(List<Type/*!*/>/*!*/ gt) {
Contract.Requires(cce.NonNullElements(gt)); Type/*!*/ ty;
Expect(38);
Type(out ty);
gt.Add(ty);
while (la.kind == 30) {
Get();
Type(out ty);
gt.Add(ty);
}
Expect(39);
}
void ReferenceType(out IToken/*!*/ tok, out Type/*!*/ ty) {
Contract.Ensures(Contract.ValueAtReturn(out tok) != null); Contract.Ensures(Contract.ValueAtReturn(out ty) != null);
tok = Token.NoToken; ty = new BoolType(); /*keep compiler happy*/
List<Type/*!*/>/*!*/ gt;
List<IToken> path;
if (la.kind == 60) {
Get();
tok = t; ty = new ObjectType();
} else if (la.kind == 5) {
Get();
tok = t; gt = new List<Type/*!*/>();
GenericInstantiation(gt);
if (gt.Count != 1) {
SemErr("array type expects exactly one type argument");
}
int dims = 1;
if (tok.val.Length != 5) {
dims = int.Parse(tok.val.Substring(5));
}
ty = theBuiltIns.ArrayType(tok, dims, gt[0], true);
} else if (la.kind == 1) {
Ident(out tok);
gt = new List<Type/*!*/>();
path = new List<IToken>();
while (la.kind == 61) {
path.Add(tok);
Get();
Ident(out tok);
}
if (la.kind == 38) {
GenericInstantiation(gt);
}
ty = (tok.val == "real") ? (Type)Microsoft.Dafny.Type.Real : new UserDefinedType(tok, tok.val, gt, path);
} else SynErr(166);
}
void FunctionSpec(List<Expression/*!*/>/*!*/ reqs, List<FrameExpression/*!*/>/*!*/ reads, List<Expression/*!*/>/*!*/ ens, List<Expression/*!*/> decreases) {
Contract.Requires(cce.NonNullElements(reqs));
Contract.Requires(cce.NonNullElements(reads));
Contract.Requires(decreases == null || cce.NonNullElements(decreases));
Expression/*!*/ e; FrameExpression/*!*/ fe;
if (la.kind == 47) {
while (!(la.kind == 0 || la.kind == 47)) {SynErr(167); Get();}
Get();
Expression(out e, false);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(168); Get();}
Expect(8);
reqs.Add(e);
} else if (la.kind == 50) {
Get();
if (StartOf(17)) {
PossiblyWildFrameExpression(out fe);
reads.Add(fe);
while (la.kind == 30) {
Get();
PossiblyWildFrameExpression(out fe);
reads.Add(fe);
}
}
while (!(la.kind == 0 || la.kind == 8)) {SynErr(169); Get();}
Expect(8);
} else if (la.kind == 48) {
Get();
Expression(out e, false);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(170); Get();}
Expect(8);
ens.Add(e);
} else if (la.kind == 49) {
Get();
if (decreases == null) {
SemErr(t, "'decreases' clauses are meaningless for copredicates, so they are not allowed");
decreases = new List<Expression/*!*/>();
}
DecreasesList(decreases, false);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(171); Get();}
Expect(8);
} else SynErr(172);
}
void FunctionBody(out Expression/*!*/ e, out IToken bodyStart, out IToken bodyEnd) {
Contract.Ensures(Contract.ValueAtReturn(out e) != null); e = dummyExpr;
Expect(9);
bodyStart = t;
Expression(out e, true);
Expect(10);
bodyEnd = t;
}
void PossiblyWildFrameExpression(out FrameExpression/*!*/ fe) {
Contract.Ensures(Contract.ValueAtReturn(out fe) != null); fe = dummyFrameExpr;
if (la.kind == 12) {
Get();
fe = new FrameExpression(t, new WildcardExpr(t), null);
} else if (StartOf(12)) {
FrameExpression(out fe);
} else SynErr(173);
}
void PossiblyWildExpression(out Expression/*!*/ e) {
Contract.Ensures(Contract.ValueAtReturn(out e)!=null);
e = dummyExpr;
if (la.kind == 12) {
Get();
e = new WildcardExpr(t);
} else if (StartOf(16)) {
Expression(out e, false);
} else SynErr(174);
}
void Stmt(List<Statement/*!*/>/*!*/ ss) {
Statement/*!*/ s;
OneStmt(out s);
ss.Add(s);
}
void OneStmt(out Statement/*!*/ s) {
Contract.Ensures(Contract.ValueAtReturn(out s) != null); IToken/*!*/ x; IToken/*!*/ id; string label = null;
s = dummyStmt; /* to please the compiler */
BlockStmt bs;
IToken bodyStart, bodyEnd;
int breakCount;
while (!(StartOf(18))) {SynErr(175); Get();}
switch (la.kind) {
case 9: {
BlockStmt(out bs, out bodyStart, out bodyEnd);
s = bs;
break;
}
case 83: {
AssertStmt(out s);
break;
}
case 72: {
AssumeStmt(out s);
break;
}
case 84: {
PrintStmt(out s);
break;
}
case 1: case 2: case 3: case 4: case 11: case 28: case 54: case 55: case 113: case 114: case 115: case 116: case 117: case 118: {
UpdateStmt(out s);
break;
}
case 24: case 29: {
VarDeclStatement(out s);
break;
}
case 76: {
IfStmt(out s);
break;
}
case 80: {
WhileStmt(out s);
break;
}
case 82: {
MatchStmt(out s);
break;
}
case 85: case 86: {
ForallStmt(out s);
break;
}
case 88: {
CalcStmt(out s);
break;
}
case 87: {
ModifyStmt(out s);
break;
}
case 66: {
Get();
x = t;
NoUSIdent(out id);
Expect(7);
OneStmt(out s);
s.Labels = new LList<Label>(new Label(x, id.val), s.Labels);
break;
}
case 67: {
Get();
x = t; breakCount = 1; label = null;
if (la.kind == 1) {
NoUSIdent(out id);
label = id.val;
} else if (la.kind == 8 || la.kind == 67) {
while (la.kind == 67) {
Get();
breakCount++;
}
} else SynErr(176);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(177); Get();}
Expect(8);
s = label != null ? new BreakStmt(x, t, label) : new BreakStmt(x, t, breakCount);
break;
}
case 51: case 70: {
ReturnStmt(out s);
break;
}
case 37: {
SkeletonStmt(out s);
break;
}
default: SynErr(178); break;
}
}
void AssertStmt(out Statement/*!*/ s) {
Contract.Ensures(Contract.ValueAtReturn(out s) != null); IToken/*!*/ x;
Expression e = dummyExpr; Attributes attrs = null;
IToken dotdotdot = null;
Expect(83);
x = t;
while (IsAttribute()) {
Attribute(ref attrs);
}
if (StartOf(16)) {
Expression(out e, false);
} else if (la.kind == 37) {
Get();
dotdotdot = t;
} else SynErr(179);
Expect(8);
if (dotdotdot != null) {
s = new SkeletonStatement(new AssertStmt(x, t, new LiteralExpr(x, true), attrs), dotdotdot, null);
} else {
s = new AssertStmt(x, t, e, attrs);
}
}
void AssumeStmt(out Statement/*!*/ s) {
Contract.Ensures(Contract.ValueAtReturn(out s) != null); IToken/*!*/ x;
Expression e = dummyExpr; Attributes attrs = null;
IToken dotdotdot = null;
Expect(72);
x = t;
while (IsAttribute()) {
Attribute(ref attrs);
}
if (StartOf(16)) {
Expression(out e, false);
} else if (la.kind == 37) {
Get();
dotdotdot = t;
} else SynErr(180);
Expect(8);
if (dotdotdot != null) {
s = new SkeletonStatement(new AssumeStmt(x, t, new LiteralExpr(x, true), attrs), dotdotdot, null);
} else {
s = new AssumeStmt(x, t, e, attrs);
}
}
void PrintStmt(out Statement/*!*/ s) {
Contract.Ensures(Contract.ValueAtReturn(out s) != null); IToken/*!*/ x; Attributes.Argument/*!*/ arg;
List<Attributes.Argument/*!*/> args = new List<Attributes.Argument/*!*/>();
Expect(84);
x = t;
AttributeArg(out arg, false);
args.Add(arg);
while (la.kind == 30) {
Get();
AttributeArg(out arg, false);
args.Add(arg);
}
Expect(8);
s = new PrintStmt(x, t, args);
}
void UpdateStmt(out Statement/*!*/ s) {
List<Expression> lhss = new List<Expression>();
List<AssignmentRhs> rhss = new List<AssignmentRhs>();
Expression e; AssignmentRhs r;
Expression lhs0;
IToken x, endTok = Token.NoToken;
Attributes attrs = null;
IToken suchThatAssume = null;
Expression suchThat = null;
Lhs(out e);
x = e.tok;
if (la.kind == 8 || la.kind == 9) {
while (la.kind == 9) {
Attribute(ref attrs);
}
Expect(8);
endTok = t; rhss.Add(new ExprRhs(e, attrs));
} else if (la.kind == 30 || la.kind == 69 || la.kind == 71) {
lhss.Add(e); lhs0 = e;
while (la.kind == 30) {
Get();
Lhs(out e);
lhss.Add(e);
}
if (la.kind == 69) {
Get();
x = t;
Rhs(out r, lhs0);
rhss.Add(r);
while (la.kind == 30) {
Get();
Rhs(out r, lhs0);
rhss.Add(r);
}
} else if (la.kind == 71) {
Get();
x = t;
if (la.kind == 72) {
Get();
suchThatAssume = t;
}
Expression(out suchThat, false);
} else SynErr(181);
Expect(8);
endTok = t;
} else if (la.kind == 7) {
Get();
SemErr(t, "invalid statement (did you forget the 'label' keyword?)");
} else SynErr(182);
if (suchThat != null) {
s = new AssignSuchThatStmt(x, endTok, lhss, suchThat, suchThatAssume);
} else {
if (lhss.Count == 0 && rhss.Count == 0) {
s = new BlockStmt(x, endTok, new List<Statement>()); // error, give empty statement
} else {
s = new UpdateStmt(x, endTok, lhss, rhss);
}
}
}
void VarDeclStatement(out Statement/*!*/ s) {
IToken x = null, assignTok = null; bool isGhost = false;
LocalVariable d;
AssignmentRhs r; IdentifierExpr lhs0;
List<LocalVariable> lhss = new List<LocalVariable>();
List<AssignmentRhs> rhss = new List<AssignmentRhs>();
IToken suchThatAssume = null;
Expression suchThat = null;
Attributes attrs = null;
IToken endTok;
if (la.kind == 24) {
Get();
isGhost = true; x = t;
}
Expect(29);
if (!isGhost) { x = t; }
while (la.kind == 9) {
Attribute(ref attrs);
}
LocalIdentTypeOptional(out d, isGhost);
lhss.Add(d); d.Attributes = attrs; attrs = null;
while (la.kind == 30) {
Get();
while (la.kind == 9) {
Attribute(ref attrs);
}
LocalIdentTypeOptional(out d, isGhost);
lhss.Add(d); d.Attributes = attrs; attrs = null;
}
if (la.kind == 69 || la.kind == 71) {
if (la.kind == 69) {
Get();
assignTok = t;
lhs0 = new IdentifierExpr(lhss[0].Tok, lhss[0].Name);
Rhs(out r, lhs0);
rhss.Add(r);
while (la.kind == 30) {
Get();
Rhs(out r, lhs0);
rhss.Add(r);
}
} else {
Get();
assignTok = t;
if (la.kind == 72) {
Get();
suchThatAssume = t;
}
Expression(out suchThat, false);
}
}
Expect(8);
endTok = t;
ConcreteUpdateStatement update;
if (suchThat != null) {
var ies = new List<Expression>();
foreach (var lhs in lhss) {
ies.Add(new IdentifierExpr(lhs.Tok, lhs.Name));
}
update = new AssignSuchThatStmt(assignTok, endTok, ies, suchThat, suchThatAssume);
} else if (rhss.Count == 0) {
update = null;
} else {
var ies = new List<Expression>();
foreach (var lhs in lhss) {
ies.Add(new AutoGhostIdentifierExpr(lhs.Tok, lhs.Name));
}
update = new UpdateStmt(assignTok, endTok, ies, rhss);
}
s = new VarDeclStmt(x, endTok, lhss, update);
}
void IfStmt(out Statement/*!*/ ifStmt) {
Contract.Ensures(Contract.ValueAtReturn(out ifStmt) != null); IToken/*!*/ x;
Expression guard = null; IToken guardEllipsis = null;
BlockStmt/*!*/ thn;
BlockStmt/*!*/ bs;
Statement/*!*/ s;
Statement els = null;
IToken bodyStart, bodyEnd, endTok;
List<GuardedAlternative> alternatives;
ifStmt = dummyStmt; // to please the compiler
Expect(76);
x = t;
if (IsAlternative()) {
AlternativeBlock(out alternatives, out endTok);
ifStmt = new AlternativeStmt(x, endTok, alternatives);
} else if (StartOf(19)) {
if (StartOf(20)) {
Guard(out guard);
} else {
Get();
guardEllipsis = t;
}
BlockStmt(out thn, out bodyStart, out bodyEnd);
endTok = thn.EndTok;
if (la.kind == 77) {
Get();
if (la.kind == 76) {
IfStmt(out s);
els = s; endTok = s.EndTok;
} else if (la.kind == 9) {
BlockStmt(out bs, out bodyStart, out bodyEnd);
els = bs; endTok = bs.EndTok;
} else SynErr(183);
}
if (guardEllipsis != null) {
ifStmt = new SkeletonStatement(new IfStmt(x, endTok, guard, thn, els), guardEllipsis, null);
} else {
ifStmt = new IfStmt(x, endTok, guard, thn, els);
}
} else SynErr(184);
}
void WhileStmt(out Statement/*!*/ stmt) {
Contract.Ensures(Contract.ValueAtReturn(out stmt) != null); IToken/*!*/ x;
Expression guard = null; IToken guardEllipsis = null;
List<MaybeFreeExpression/*!*/> invariants = new List<MaybeFreeExpression/*!*/>();
List<Expression/*!*/> decreases = new List<Expression/*!*/>();
Attributes decAttrs = null;
Attributes modAttrs = null;
List<FrameExpression/*!*/> mod = null;
BlockStmt/*!*/ body = null; IToken bodyEllipsis = null;
IToken bodyStart = null, bodyEnd = null, endTok = Token.NoToken;
List<GuardedAlternative> alternatives;
stmt = dummyStmt; // to please the compiler
Expect(80);
x = t;
if (IsLoopSpecOrAlternative()) {
LoopSpec(out invariants, out decreases, out mod, ref decAttrs, ref modAttrs);
AlternativeBlock(out alternatives, out endTok);
stmt = new AlternativeLoopStmt(x, endTok, invariants, new Specification<Expression>(decreases, decAttrs), new Specification<FrameExpression>(mod, modAttrs), alternatives);
} else if (StartOf(19)) {
if (StartOf(20)) {
Guard(out guard);
Contract.Assume(guard == null || cce.Owner.None(guard));
} else {
Get();
guardEllipsis = t;
}
LoopSpec(out invariants, out decreases, out mod, ref decAttrs, ref modAttrs);
if (la.kind == 9) {
BlockStmt(out body, out bodyStart, out bodyEnd);
endTok = body.EndTok;
} else if (la.kind == 37) {
Get();
bodyEllipsis = t; endTok = t;
} else SynErr(185);
if (guardEllipsis != null || bodyEllipsis != null) {
if (mod != null) {
SemErr(mod[0].E.tok, "'modifies' clauses are not allowed on refining loops");
}
if (body == null) {
body = new BlockStmt(x, endTok, new List<Statement>());
}
stmt = new WhileStmt(x, endTok, guard, invariants, new Specification<Expression>(decreases, decAttrs), new Specification<FrameExpression>(null, null), body);
stmt = new SkeletonStatement(stmt, guardEllipsis, bodyEllipsis);
} else {
// The following statement protects against crashes in case of parsing errors
body = body ?? new BlockStmt(x, endTok, new List<Statement>());
stmt = new WhileStmt(x, endTok, guard, invariants, new Specification<Expression>(decreases, decAttrs), new Specification<FrameExpression>(mod, modAttrs), body);
}
} else SynErr(186);
}
void MatchStmt(out Statement/*!*/ s) {
Contract.Ensures(Contract.ValueAtReturn(out s) != null);
Token x; Expression/*!*/ e; MatchCaseStmt/*!*/ c;
List<MatchCaseStmt/*!*/> cases = new List<MatchCaseStmt/*!*/>();
Expect(82);
x = t;
Expression(out e, true);
Expect(9);
while (la.kind == 78) {
CaseStatement(out c);
cases.Add(c);
}
Expect(10);
s = new MatchStmt(x, t, e, cases);
}
void ForallStmt(out Statement/*!*/ s) {
Contract.Ensures(Contract.ValueAtReturn(out s) != null);
IToken/*!*/ x = Token.NoToken;
bool usesOptionalParen = false;
List<BoundVar/*!*/> bvars = null;
Attributes attrs = null;
Expression range = null;
var ens = new List<MaybeFreeExpression/*!*/>();
bool isFree;
Expression/*!*/ e;
BlockStmt/*!*/ block;
IToken bodyStart, bodyEnd;
if (la.kind == 85) {
Get();
x = t;
} else if (la.kind == 86) {
Get();
x = t;
errors.Warning(t, "the 'parallel' keyword has been deprecated; the comprehension statement now uses the keyword 'forall' (and the parentheses around the bound variables are now optional)");
} else SynErr(187);
if (la.kind == 11) {
Get();
usesOptionalParen = true;
}
if (la.kind == 1) {
List<BoundVar/*!*/> bvarsX; Attributes attrsX; Expression rangeX;
QuantifierDomain(out bvarsX, out attrsX, out rangeX);
bvars = bvarsX; attrs = attrsX; range = rangeX;
}
if (bvars == null) { bvars = new List<BoundVar>(); }
if (range == null) { range = new LiteralExpr(x, true); }
if (la.kind == 33) {
Get();
if (!usesOptionalParen) { SemErr(t, "found but didn't expect a close parenthesis"); }
} else if (la.kind == 9 || la.kind == 46 || la.kind == 48) {
if (usesOptionalParen) { SemErr(t, "expecting close parenthesis"); }
} else SynErr(188);
while (la.kind == 46 || la.kind == 48) {
isFree = false;
if (la.kind == 46) {
Get();
isFree = true;
}
Expect(48);
Expression(out e, false);
Expect(8);
ens.Add(new MaybeFreeExpression(e, isFree));
}
BlockStmt(out block, out bodyStart, out bodyEnd);
s = new ForallStmt(x, block.EndTok, bvars, attrs, range, ens, block);
}
void CalcStmt(out Statement/*!*/ s) {
Contract.Ensures(Contract.ValueAtReturn(out s) != null);
Token x;
CalcStmt.CalcOp/*!*/ op, calcOp = Microsoft.Dafny.CalcStmt.DefaultOp, resOp = Microsoft.Dafny.CalcStmt.DefaultOp;
var lines = new List<Expression/*!*/>();
var hints = new List<BlockStmt/*!*/>();
CalcStmt.CalcOp stepOp;
var stepOps = new List<CalcStmt.CalcOp>();
CalcStmt.CalcOp maybeOp;
Expression/*!*/ e;
BlockStmt/*!*/ h;
IToken opTok;
IToken danglingOperator = null;
Expect(88);
x = t;
if (StartOf(21)) {
CalcOp(out opTok, out calcOp);
maybeOp = calcOp.ResultOp(calcOp); // guard against non-transitive calcOp (like !=)
if (maybeOp == null) {
SemErr(opTok, "the main operator of a calculation must be transitive");
}
resOp = calcOp;
}
Expect(9);
while (StartOf(16)) {
Expression(out e, false);
lines.Add(e); stepOp = calcOp; danglingOperator = null;
Expect(8);
if (StartOf(21)) {
CalcOp(out opTok, out op);
maybeOp = resOp.ResultOp(op);
if (maybeOp == null) {
SemErr(opTok, "this operator cannot continue this calculation");
} else {
stepOp = op;
resOp = maybeOp;
danglingOperator = opTok;
}
}
stepOps.Add(stepOp);
Hint(out h);
hints.Add(h);
if (h.Body.Count != 0) { danglingOperator = null; }
}
Expect(10);
if (danglingOperator != null) {
SemErr(danglingOperator, "a calculation cannot end with an operator");
}
if (lines.Count > 0) {
// Repeat the last line to create a dummy line for the dangling hint
lines.Add(lines[lines.Count - 1]);
}
s = new CalcStmt(x, t, calcOp, lines, hints, stepOps, resOp);
}
void ModifyStmt(out Statement s) {
IToken tok; IToken endTok = Token.NoToken;
Attributes attrs = null;
FrameExpression fe; var mod = new List<FrameExpression>();
BlockStmt body = null; IToken bodyStart;
IToken ellipsisToken = null;
Expect(87);
tok = t;
while (IsAttribute()) {
Attribute(ref attrs);
}
if (StartOf(22)) {
if (StartOf(12)) {
FrameExpression(out fe);
mod.Add(fe);
while (la.kind == 30) {
Get();
FrameExpression(out fe);
mod.Add(fe);
}
} else {
Get();
ellipsisToken = t;
}
}
if (la.kind == 9) {
BlockStmt(out body, out bodyStart, out endTok);
} else if (la.kind == 8) {
while (!(la.kind == 0 || la.kind == 8)) {SynErr(189); Get();}
Get();
endTok = t;
} else SynErr(190);
s = new ModifyStmt(tok, endTok, mod, attrs, body);
if (ellipsisToken != null) {
s = new SkeletonStatement(s, ellipsisToken, null);
}
}
void ReturnStmt(out Statement/*!*/ s) {
IToken returnTok = null;
List<AssignmentRhs> rhss = null;
AssignmentRhs r;
bool isYield = false;
if (la.kind == 70) {
Get();
returnTok = t;
} else if (la.kind == 51) {
Get();
returnTok = t; isYield = true;
} else SynErr(191);
if (StartOf(23)) {
Rhs(out r, null);
rhss = new List<AssignmentRhs>(); rhss.Add(r);
while (la.kind == 30) {
Get();
Rhs(out r, null);
rhss.Add(r);
}
}
Expect(8);
if (isYield) {
s = new YieldStmt(returnTok, t, rhss);
} else {
s = new ReturnStmt(returnTok, t, rhss);
}
}
void SkeletonStmt(out Statement s) {
List<IToken> names = null;
List<Expression> exprs = null;
IToken tok, dotdotdot, whereTok;
Expression e;
Expect(37);
dotdotdot = t;
if (la.kind == 68) {
Get();
names = new List<IToken>(); exprs = new List<Expression>(); whereTok = t;
Ident(out tok);
names.Add(tok);
while (la.kind == 30) {
Get();
Ident(out tok);
names.Add(tok);
}
Expect(69);
Expression(out e, false);
exprs.Add(e);
while (la.kind == 30) {
Get();
Expression(out e, false);
exprs.Add(e);
}
if (exprs.Count != names.Count) {
SemErr(whereTok, exprs.Count < names.Count ? "not enough expressions" : "too many expressions");
names = null; exprs = null;
}
}
Expect(8);
s = new SkeletonStatement(dotdotdot, t, names, exprs);
}
void Rhs(out AssignmentRhs r, Expression receiverForInitCall) {
Contract.Ensures(Contract.ValueAtReturn<AssignmentRhs>(out r) != null);
IToken/*!*/ x, newToken; Expression/*!*/ e;
Type ty = null;
List<Expression> ee = null;
List<Expression> args = null;
r = dummyRhs; // to please compiler
Attributes attrs = null;
if (la.kind == 73) {
Get();
newToken = t;
TypeAndToken(out x, out ty);
if (la.kind == 11 || la.kind == 61 || la.kind == 74) {
if (la.kind == 74) {
Get();
ee = new List<Expression>();
Expressions(ee);
Expect(75);
UserDefinedType tmp = theBuiltIns.ArrayType(x, ee.Count, new IntType(), true);
} else {
x = null; args = new List<Expression/*!*/>();
if (la.kind == 61) {
Get();
Ident(out x);
}
Expect(11);
if (StartOf(16)) {
Expressions(args);
}
Expect(33);
}
}
if (ee != null) {
r = new TypeRhs(newToken, ty, ee);
} else if (args != null) {
r = new TypeRhs(newToken, ty, x == null ? null : x.val, receiverForInitCall, args);
} else {
r = new TypeRhs(newToken, ty);
}
} else if (la.kind == 12) {
Get();
r = new HavocRhs(t);
} else if (StartOf(16)) {
Expression(out e, false);
r = new ExprRhs(e);
} else SynErr(192);
while (la.kind == 9) {
Attribute(ref attrs);
}
r.Attributes = attrs;
}
void Lhs(out Expression e) {
e = dummyExpr; // the assignment is to please the compiler, the dummy value to satisfy contracts in the event of a parse error
if (la.kind == 1) {
DottedIdentifiersAndFunction(out e);
while (la.kind == 61 || la.kind == 74) {
Suffix(ref e);
}
} else if (StartOf(24)) {
ConstAtomExpression(out e);
Suffix(ref e);
while (la.kind == 61 || la.kind == 74) {
Suffix(ref e);
}
} else SynErr(193);
}
void Expressions(List<Expression/*!*/>/*!*/ args) {
Contract.Requires(cce.NonNullElements(args)); Expression/*!*/ e;
Expression(out e, true);
args.Add(e);
while (la.kind == 30) {
Get();
Expression(out e, true);
args.Add(e);
}
}
void AlternativeBlock(out List<GuardedAlternative> alternatives, out IToken endTok) {
alternatives = new List<GuardedAlternative>();
IToken x;
Expression e;
List<Statement> body;
Expect(9);
while (la.kind == 78) {
Get();
x = t;
Expression(out e, true);
Expect(79);
body = new List<Statement>();
while (StartOf(14)) {
Stmt(body);
}
alternatives.Add(new GuardedAlternative(x, e, body));
}
Expect(10);
endTok = t;
}
void Guard(out Expression e) {
Expression/*!*/ ee; e = null;
if (la.kind == 12) {
Get();
e = null;
} else if (IsParenStar()) {
Expect(11);
Expect(12);
Expect(33);
e = null;
} else if (StartOf(16)) {
Expression(out ee, true);
e = ee;
} else SynErr(194);
}
void LoopSpec(out List<MaybeFreeExpression/*!*/> invariants, out List<Expression/*!*/> decreases, out List<FrameExpression/*!*/> mod, ref Attributes decAttrs, ref Attributes modAttrs) {
FrameExpression/*!*/ fe;
invariants = new List<MaybeFreeExpression/*!*/>();
MaybeFreeExpression invariant = null;
decreases = new List<Expression/*!*/>();
mod = null;
while (StartOf(25)) {
if (la.kind == 46 || la.kind == 81) {
Invariant(out invariant);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(195); Get();}
Expect(8);
invariants.Add(invariant);
} else if (la.kind == 49) {
while (!(la.kind == 0 || la.kind == 49)) {SynErr(196); Get();}
Get();
while (IsAttribute()) {
Attribute(ref decAttrs);
}
DecreasesList(decreases, true);
while (!(la.kind == 0 || la.kind == 8)) {SynErr(197); Get();}
Expect(8);
} else {
while (!(la.kind == 0 || la.kind == 45)) {SynErr(198); Get();}
Get();
while (IsAttribute()) {
Attribute(ref modAttrs);
}
mod = mod ?? new List<FrameExpression>();
if (StartOf(12)) {
FrameExpression(out fe);
mod.Add(fe);
while (la.kind == 30) {
Get();
FrameExpression(out fe);
mod.Add(fe);
}
}
while (!(la.kind == 0 || la.kind == 8)) {SynErr(199); Get();}
Expect(8);
}
}
}
void Invariant(out MaybeFreeExpression/*!*/ invariant) {
bool isFree = false; Expression/*!*/ e; List<string> ids = new List<string>(); invariant = null; Attributes attrs = null;
while (!(la.kind == 0 || la.kind == 46 || la.kind == 81)) {SynErr(200); Get();}
if (la.kind == 46) {
Get();
isFree = true;
}
Expect(81);
while (IsAttribute()) {
Attribute(ref attrs);
}
Expression(out e, false);
invariant = new MaybeFreeExpression(e, isFree, attrs);
}
void CaseStatement(out MatchCaseStmt/*!*/ c) {
Contract.Ensures(Contract.ValueAtReturn(out c) != null);
IToken/*!*/ x, id;
List<BoundVar/*!*/> arguments = new List<BoundVar/*!*/>();
BoundVar/*!*/ bv;
List<Statement/*!*/> body = new List<Statement/*!*/>();
Expect(78);
x = t;
Ident(out id);
if (la.kind == 11) {
Get();
IdentTypeOptional(out bv);
arguments.Add(bv);
while (la.kind == 30) {
Get();
IdentTypeOptional(out bv);
arguments.Add(bv);
}
Expect(33);
}
Expect(79);
while (StartOf(14)) {
Stmt(body);
}
c = new MatchCaseStmt(x, id.val, arguments, body);
}
void AttributeArg(out Attributes.Argument arg, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out arg) != null); Expression/*!*/ e; arg = dummyAttrArg;
if (la.kind == 6) {
Get();
arg = new Attributes.Argument(t, t.val.Substring(1, t.val.Length-2));
} else if (StartOf(16)) {
Expression(out e, allowSemi);
arg = new Attributes.Argument(t, e);
} else SynErr(201);
}
void QuantifierDomain(out List<BoundVar> bvars, out Attributes attrs, out Expression range) {
bvars = new List<BoundVar>();
BoundVar/*!*/ bv;
attrs = null;
range = null;
IdentTypeOptional(out bv);
bvars.Add(bv);
while (la.kind == 30) {
Get();
IdentTypeOptional(out bv);
bvars.Add(bv);
}
while (IsAttribute()) {
Attribute(ref attrs);
}
if (la.kind == 28) {
Get();
Expression(out range, true);
}
}
void CalcOp(out IToken x, out CalcStmt.CalcOp/*!*/ op) {
var binOp = BinaryExpr.Opcode.Eq; // Returns Eq if parsing fails because it is compatible with any other operator
Expression k = null;
x = null;
switch (la.kind) {
case 32: {
Get();
x = t; binOp = BinaryExpr.Opcode.Eq;
if (la.kind == 89) {
Get();
Expect(74);
Expression(out k, true);
Expect(75);
}
break;
}
case 38: {
Get();
x = t; binOp = BinaryExpr.Opcode.Lt;
break;
}
case 39: {
Get();
x = t; binOp = BinaryExpr.Opcode.Gt;
break;
}
case 90: {
Get();
x = t; binOp = BinaryExpr.Opcode.Le;
break;
}
case 91: {
Get();
x = t; binOp = BinaryExpr.Opcode.Ge;
break;
}
case 92: {
Get();
x = t; binOp = BinaryExpr.Opcode.Neq;
break;
}
case 93: {
Get();
x = t; binOp = BinaryExpr.Opcode.Neq;
break;
}
case 94: {
Get();
x = t; binOp = BinaryExpr.Opcode.Le;
break;
}
case 95: {
Get();
x = t; binOp = BinaryExpr.Opcode.Ge;
break;
}
case 96: case 97: {
EquivOp();
x = t; binOp = BinaryExpr.Opcode.Iff;
break;
}
case 98: case 99: {
ImpliesOp();
x = t; binOp = BinaryExpr.Opcode.Imp;
break;
}
case 100: case 101: {
ExpliesOp();
x = t; binOp = BinaryExpr.Opcode.Exp;
break;
}
default: SynErr(202); break;
}
if (k == null) {
op = new Microsoft.Dafny.CalcStmt.BinaryCalcOp(binOp);
} else {
op = new Microsoft.Dafny.CalcStmt.TernaryCalcOp(k);
}
}
void Hint(out BlockStmt s) {
Contract.Ensures(Contract.ValueAtReturn(out s) != null); // returns an empty block statement if the hint is empty
var subhints = new List<Statement/*!*/>();
IToken bodyStart, bodyEnd;
BlockStmt/*!*/ block;
Statement/*!*/ calc;
Token x = la;
IToken endTok = x;
while (la.kind == 9 || la.kind == 88) {
if (la.kind == 9) {
BlockStmt(out block, out bodyStart, out bodyEnd);
endTok = block.EndTok; subhints.Add(block);
} else {
CalcStmt(out calc);
endTok = calc.EndTok; subhints.Add(calc);
}
}
s = new BlockStmt(x, endTok, subhints); // if the hint is empty x is the first token of the next line, but it doesn't matter cause the block statement is just used as a container
}
void EquivOp() {
if (la.kind == 96) {
Get();
} else if (la.kind == 97) {
Get();
} else SynErr(203);
}
void ImpliesOp() {
if (la.kind == 98) {
Get();
} else if (la.kind == 99) {
Get();
} else SynErr(204);
}
void ExpliesOp() {
if (la.kind == 100) {
Get();
} else if (la.kind == 101) {
Get();
} else SynErr(205);
}
void EquivExpression(out Expression e0, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out e0) != null); IToken/*!*/ x; Expression/*!*/ e1;
ImpliesExpliesExpression(out e0, allowSemi);
while (la.kind == 96 || la.kind == 97) {
EquivOp();
x = t;
ImpliesExpliesExpression(out e1, allowSemi);
e0 = new BinaryExpr(x, BinaryExpr.Opcode.Iff, e0, e1);
}
}
void ImpliesExpliesExpression(out Expression e0, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out e0) != null); IToken/*!*/ x; Expression/*!*/ e1;
LogicalExpression(out e0, allowSemi);
if (StartOf(26)) {
if (la.kind == 98 || la.kind == 99) {
ImpliesOp();
x = t;
ImpliesExpression(out e1, allowSemi);
e0 = new BinaryExpr(x, BinaryExpr.Opcode.Imp, e0, e1);
} else {
ExpliesOp();
x = t;
LogicalExpression(out e1, allowSemi);
e0 = new BinaryExpr(x, BinaryExpr.Opcode.Exp, e0, e1);
while (la.kind == 100 || la.kind == 101) {
ExpliesOp();
x = t;
LogicalExpression(out e1, allowSemi);
e0 = new BinaryExpr(x, BinaryExpr.Opcode.Exp, e0, e1);
}
}
}
}
void LogicalExpression(out Expression e0, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out e0) != null); IToken/*!*/ x; Expression/*!*/ e1;
RelationalExpression(out e0, allowSemi);
if (StartOf(27)) {
if (la.kind == 102 || la.kind == 103) {
AndOp();
x = t;
RelationalExpression(out e1, allowSemi);
e0 = new BinaryExpr(x, BinaryExpr.Opcode.And, e0, e1);
while (la.kind == 102 || la.kind == 103) {
AndOp();
x = t;
RelationalExpression(out e1, allowSemi);
e0 = new BinaryExpr(x, BinaryExpr.Opcode.And, e0, e1);
}
} else {
OrOp();
x = t;
RelationalExpression(out e1, allowSemi);
e0 = new BinaryExpr(x, BinaryExpr.Opcode.Or, e0, e1);
while (la.kind == 104 || la.kind == 105) {
OrOp();
x = t;
RelationalExpression(out e1, allowSemi);
e0 = new BinaryExpr(x, BinaryExpr.Opcode.Or, e0, e1);
}
}
}
}
void ImpliesExpression(out Expression e0, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out e0) != null); IToken/*!*/ x; Expression/*!*/ e1;
LogicalExpression(out e0, allowSemi);
if (la.kind == 98 || la.kind == 99) {
ImpliesOp();
x = t;
ImpliesExpression(out e1, allowSemi);
e0 = new BinaryExpr(x, BinaryExpr.Opcode.Imp, e0, e1);
}
}
void RelationalExpression(out Expression e, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out e) != null);
IToken x, firstOpTok = null; Expression e0, e1, acc = null; BinaryExpr.Opcode op;
List<Expression> chain = null;
List<BinaryExpr.Opcode> ops = null;
List<Expression/*?*/> prefixLimits = null;
Expression k;
int kind = 0; // 0 ("uncommitted") indicates chain of ==, possibly with one !=
// 1 ("ascending") indicates chain of ==, <, <=, possibly with one !=
// 2 ("descending") indicates chain of ==, >, >=, possibly with one !=
// 3 ("illegal") indicates illegal chain
// 4 ("disjoint") indicates chain of disjoint set operators
bool hasSeenNeq = false;
Term(out e0, allowSemi);
e = e0;
if (StartOf(28)) {
RelOp(out x, out op, out k);
firstOpTok = x;
Term(out e1, allowSemi);
if (k == null) {
e = new BinaryExpr(x, op, e0, e1);
if (op == BinaryExpr.Opcode.Disjoint)
acc = new BinaryExpr(x, BinaryExpr.Opcode.Add, e0, e1); // accumulate first two operands.
} else {
Contract.Assert(op == BinaryExpr.Opcode.Eq || op == BinaryExpr.Opcode.Neq);
e = new TernaryExpr(x, op == BinaryExpr.Opcode.Eq ? TernaryExpr.Opcode.PrefixEqOp : TernaryExpr.Opcode.PrefixNeqOp, k, e0, e1);
}
while (StartOf(28)) {
if (chain == null) {
chain = new List<Expression>();
ops = new List<BinaryExpr.Opcode>();
prefixLimits = new List<Expression>();
chain.Add(e0); ops.Add(op); prefixLimits.Add(k); chain.Add(e1);
switch (op) {
case BinaryExpr.Opcode.Eq:
kind = 0; break;
case BinaryExpr.Opcode.Neq:
kind = 0; hasSeenNeq = true; break;
case BinaryExpr.Opcode.Lt:
case BinaryExpr.Opcode.Le:
kind = 1; break;
case BinaryExpr.Opcode.Gt:
case BinaryExpr.Opcode.Ge:
kind = 2; break;
case BinaryExpr.Opcode.Disjoint:
kind = 4; break;
default:
kind = 3; break;
}
}
e0 = e1;
RelOp(out x, out op, out k);
switch (op) {
case BinaryExpr.Opcode.Eq:
if (kind != 0 && kind != 1 && kind != 2) { SemErr(x, "chaining not allowed from the previous operator"); }
break;
case BinaryExpr.Opcode.Neq:
if (hasSeenNeq) { SemErr(x, "a chain cannot have more than one != operator"); }
if (kind != 0 && kind != 1 && kind != 2) { SemErr(x, "this operator cannot continue this chain"); }
hasSeenNeq = true; break;
case BinaryExpr.Opcode.Lt:
case BinaryExpr.Opcode.Le:
if (kind == 0) { kind = 1; }
else if (kind != 1) { SemErr(x, "this operator chain cannot continue with an ascending operator"); }
break;
case BinaryExpr.Opcode.Gt:
case BinaryExpr.Opcode.Ge:
if (kind == 0) { kind = 2; }
else if (kind != 2) { SemErr(x, "this operator chain cannot continue with a descending operator"); }
break;
case BinaryExpr.Opcode.Disjoint:
if (kind != 4) { SemErr(x, "can only chain disjoint (!!) with itself."); kind = 3; }
break;
default:
SemErr(x, "this operator cannot be part of a chain");
kind = 3; break;
}
Term(out e1, allowSemi);
ops.Add(op); prefixLimits.Add(k); chain.Add(e1);
if (k != null) {
Contract.Assert(op == BinaryExpr.Opcode.Eq || op == BinaryExpr.Opcode.Neq);
e = new TernaryExpr(x, op == BinaryExpr.Opcode.Eq ? TernaryExpr.Opcode.PrefixEqOp : TernaryExpr.Opcode.PrefixNeqOp, k, e0, e1);
} else if (op == BinaryExpr.Opcode.Disjoint) {
e = new BinaryExpr(x, BinaryExpr.Opcode.And, e, new BinaryExpr(x, op, acc, e1));
acc = new BinaryExpr(x, BinaryExpr.Opcode.Add, acc, e1); //e0 has already been added.
} else {
e = new BinaryExpr(x, BinaryExpr.Opcode.And, e, new BinaryExpr(x, op, e0, e1));
}
}
}
if (chain != null) {
e = new ChainingExpression(firstOpTok, chain, ops, prefixLimits, e);
}
}
void AndOp() {
if (la.kind == 102) {
Get();
} else if (la.kind == 103) {
Get();
} else SynErr(206);
}
void OrOp() {
if (la.kind == 104) {
Get();
} else if (la.kind == 105) {
Get();
} else SynErr(207);
}
void Term(out Expression e0, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out e0) != null); IToken/*!*/ x; Expression/*!*/ e1; BinaryExpr.Opcode op;
Factor(out e0, allowSemi);
while (la.kind == 108 || la.kind == 109) {
AddOp(out x, out op);
Factor(out e1, allowSemi);
e0 = new BinaryExpr(x, op, e0, e1);
}
}
void RelOp(out IToken/*!*/ x, out BinaryExpr.Opcode op, out Expression k) {
Contract.Ensures(Contract.ValueAtReturn(out x) != null);
x = Token.NoToken; op = BinaryExpr.Opcode.Add/*(dummy)*/;
IToken y;
k = null;
switch (la.kind) {
case 32: {
Get();
x = t; op = BinaryExpr.Opcode.Eq;
if (la.kind == 89) {
Get();
Expect(74);
Expression(out k, true);
Expect(75);
}
break;
}
case 38: {
Get();
x = t; op = BinaryExpr.Opcode.Lt;
break;
}
case 39: {
Get();
x = t; op = BinaryExpr.Opcode.Gt;
break;
}
case 90: {
Get();
x = t; op = BinaryExpr.Opcode.Le;
break;
}
case 91: {
Get();
x = t; op = BinaryExpr.Opcode.Ge;
break;
}
case 92: {
Get();
x = t; op = BinaryExpr.Opcode.Neq;
if (la.kind == 89) {
Get();
Expect(74);
Expression(out k, true);
Expect(75);
}
break;
}
case 106: {
Get();
x = t; op = BinaryExpr.Opcode.In;
break;
}
case 13: {
Get();
x = t; op = BinaryExpr.Opcode.NotIn;
break;
}
case 107: {
Get();
x = t; y = Token.NoToken;
if (la.kind == 107) {
Get();
y = t;
}
if (y == Token.NoToken) {
SemErr(x, "invalid RelOp");
} else if (y.pos != x.pos + 1) {
SemErr(x, "invalid RelOp (perhaps you intended \"!!\" with no intervening whitespace?)");
} else {
x.val = "!!";
op = BinaryExpr.Opcode.Disjoint;
}
break;
}
case 93: {
Get();
x = t; op = BinaryExpr.Opcode.Neq;
break;
}
case 94: {
Get();
x = t; op = BinaryExpr.Opcode.Le;
break;
}
case 95: {
Get();
x = t; op = BinaryExpr.Opcode.Ge;
break;
}
default: SynErr(208); break;
}
}
void Factor(out Expression e0, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out e0) != null); IToken/*!*/ x; Expression/*!*/ e1; BinaryExpr.Opcode op;
UnaryExpression(out e0, allowSemi);
while (la.kind == 12 || la.kind == 110 || la.kind == 111) {
MulOp(out x, out op);
UnaryExpression(out e1, allowSemi);
e0 = new BinaryExpr(x, op, e0, e1);
}
}
void AddOp(out IToken x, out BinaryExpr.Opcode op) {
Contract.Ensures(Contract.ValueAtReturn(out x) != null); x = Token.NoToken; op=BinaryExpr.Opcode.Add/*(dummy)*/;
if (la.kind == 108) {
Get();
x = t; op = BinaryExpr.Opcode.Add;
} else if (la.kind == 109) {
Get();
x = t; op = BinaryExpr.Opcode.Sub;
} else SynErr(209);
}
void UnaryExpression(out Expression e, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out e) != null); IToken/*!*/ x; e = dummyExpr;
switch (la.kind) {
case 109: {
Get();
x = t;
UnaryExpression(out e, allowSemi);
e = new NegationExpression(x, e);
break;
}
case 107: case 112: {
NegOp();
x = t;
UnaryExpression(out e, allowSemi);
e = new UnaryExpr(x, UnaryExpr.Opcode.Not, e);
break;
}
case 24: case 29: case 56: case 66: case 72: case 76: case 82: case 83: case 85: case 88: case 121: case 122: case 123: {
EndlessExpression(out e, allowSemi);
break;
}
case 1: {
DottedIdentifiersAndFunction(out e);
while (la.kind == 61 || la.kind == 74) {
Suffix(ref e);
}
break;
}
case 9: case 74: {
DisplayExpr(out e);
while (la.kind == 61 || la.kind == 74) {
Suffix(ref e);
}
break;
}
case 57: {
MultiSetExpr(out e);
while (la.kind == 61 || la.kind == 74) {
Suffix(ref e);
}
break;
}
case 59: {
Get();
x = t;
if (la.kind == 74) {
MapDisplayExpr(x, out e);
while (la.kind == 61 || la.kind == 74) {
Suffix(ref e);
}
} else if (la.kind == 1) {
MapComprehensionExpr(x, out e, allowSemi);
} else if (StartOf(29)) {
SemErr("map must be followed by literal in brackets or comprehension.");
} else SynErr(210);
break;
}
case 2: case 3: case 4: case 11: case 28: case 54: case 55: case 113: case 114: case 115: case 116: case 117: case 118: {
ConstAtomExpression(out e);
while (la.kind == 61 || la.kind == 74) {
Suffix(ref e);
}
break;
}
default: SynErr(211); break;
}
}
void MulOp(out IToken x, out BinaryExpr.Opcode op) {
Contract.Ensures(Contract.ValueAtReturn(out x) != null); x = Token.NoToken; op = BinaryExpr.Opcode.Add/*(dummy)*/;
if (la.kind == 12) {
Get();
x = t; op = BinaryExpr.Opcode.Mul;
} else if (la.kind == 110) {
Get();
x = t; op = BinaryExpr.Opcode.Div;
} else if (la.kind == 111) {
Get();
x = t; op = BinaryExpr.Opcode.Mod;
} else SynErr(212);
}
void NegOp() {
if (la.kind == 107) {
Get();
} else if (la.kind == 112) {
Get();
} else SynErr(213);
}
void EndlessExpression(out Expression e, bool allowSemi) {
IToken/*!*/ x;
Expression e0, e1;
Statement s;
e = dummyExpr;
switch (la.kind) {
case 76: {
Get();
x = t;
Expression(out e, true);
Expect(119);
Expression(out e0, true);
Expect(77);
Expression(out e1, allowSemi);
e = new ITEExpr(x, e, e0, e1);
break;
}
case 82: {
MatchExpression(out e, allowSemi);
break;
}
case 85: case 121: case 122: case 123: {
QuantifierGuts(out e, allowSemi);
break;
}
case 56: {
ComprehensionExpr(out e, allowSemi);
break;
}
case 72: case 83: case 88: {
StmtInExpr(out s);
Expression(out e, allowSemi);
e = new StmtExpr(s.Tok, s, e);
break;
}
case 24: case 29: {
LetExpr(out e, allowSemi);
break;
}
case 66: {
NamedExpr(out e, allowSemi);
break;
}
default: SynErr(214); break;
}
}
void DottedIdentifiersAndFunction(out Expression e) {
IToken id, idPrime; IToken openParen = null;
List<Expression> args = null;
List<IToken> idents = new List<IToken>();
Ident(out id);
idents.Add(id);
while (la.kind == 61) {
IdentOrDigitsSuffix(out id, out idPrime);
idents.Add(id);
if (idPrime != null) { idents.Add(idPrime); id = idPrime; }
}
if (la.kind == 11 || la.kind == 89) {
args = new List<Expression>();
if (la.kind == 89) {
Get();
id.val = id.val + "#"; Expression k;
Expect(74);
Expression(out k, true);
Expect(75);
args.Add(k);
}
Expect(11);
openParen = t;
if (StartOf(16)) {
Expressions(args);
}
Expect(33);
}
e = new IdentifierSequence(idents, openParen, args);
}
void Suffix(ref Expression e) {
Contract.Requires(e != null); Contract.Ensures(e!=null); IToken/*!*/ id, x; List<Expression/*!*/>/*!*/ args;
Expression e0 = null; Expression e1 = null; Expression/*!*/ ee; bool anyDots = false; List<Expression> multipleLengths = null; bool takeRest = false;
List<Expression> multipleIndices = null;
bool func = false;
if (la.kind == 61) {
IdentOrDigitsSuffix(out id, out x);
if (x != null) {
// process id as a Suffix in its own right
e = new ExprDotName(id, e, id.val);
id = x; // move to the next Suffix
}
if (la.kind == 11 || la.kind == 89) {
args = new List<Expression/*!*/>(); func = true;
if (la.kind == 89) {
Get();
id.val = id.val + "#"; Expression k;
Expect(74);
Expression(out k, true);
Expect(75);
args.Add(k);
}
Expect(11);
IToken openParen = t;
if (StartOf(16)) {
Expressions(args);
}
Expect(33);
e = new FunctionCallExpr(id, id.val, e, openParen, args);
}
if (!func) { e = new ExprDotName(id, e, id.val); }
} else if (la.kind == 74) {
Get();
x = t;
if (StartOf(16)) {
Expression(out ee, true);
e0 = ee;
if (la.kind == 120) {
Get();
anyDots = true;
if (StartOf(16)) {
Expression(out ee, true);
e1 = ee;
}
} else if (la.kind == 69) {
Get();
Expression(out ee, true);
e1 = ee;
} else if (la.kind == 7 || la.kind == 75) {
while (la.kind == 7) {
Get();
if (multipleLengths == null) {
multipleLengths = new List<Expression>();
multipleLengths.Add(e0);
}
takeRest = true;
if (StartOf(16)) {
Expression(out ee, true);
multipleLengths.Add(ee);
takeRest = false;
}
}
} else if (la.kind == 30 || la.kind == 75) {
while (la.kind == 30) {
Get();
Expression(out ee, true);
if (multipleIndices == null) {
multipleIndices = new List<Expression>();
multipleIndices.Add(e0);
}
multipleIndices.Add(ee);
}
} else SynErr(215);
} else if (la.kind == 120) {
Get();
anyDots = true;
if (StartOf(16)) {
Expression(out ee, true);
e1 = ee;
}
} else SynErr(216);
if (multipleIndices != null) {
e = new MultiSelectExpr(x, e, multipleIndices);
// make sure an array class with this dimensionality exists
UserDefinedType tmp = theBuiltIns.ArrayType(x, multipleIndices.Count, new IntType(), true);
} else {
if (!anyDots && e0 == null) {
/* a parsing error occurred */
e0 = dummyExpr;
}
Contract.Assert(anyDots || e0 != null);
if (anyDots) {
//Contract.Assert(e0 != null || e1 != null);
e = new SeqSelectExpr(x, false, e, e0, e1);
} else if (multipleLengths != null || takeRest) {
Expression prev = new LiteralExpr(x, 0);
List<Expression> seqs = new List<Expression>();
if (multipleLengths != null)
{
foreach (var len in multipleLengths)
{
var end = new BinaryExpr(x, BinaryExpr.Opcode.Add, prev, len);
seqs.Add(new SeqSelectExpr(x, false, e, prev, end));
prev = end;
}
}
if (takeRest)
{
seqs.Add(new SeqSelectExpr(x, false, e, prev, null));
}
e = new SeqDisplayExpr(x, seqs);
} else if (e1 == null) {
Contract.Assert(e0 != null);
e = new SeqSelectExpr(x, true, e, e0, null);
} else {
Contract.Assert(e0 != null);
e = new SeqUpdateExpr(x, e, e0, e1);
}
}
Expect(75);
} else SynErr(217);
}
void DisplayExpr(out Expression e) {
Contract.Ensures(Contract.ValueAtReturn(out e) != null);
IToken/*!*/ x = null; List<Expression/*!*/>/*!*/ elements;
e = dummyExpr;
if (la.kind == 9) {
Get();
x = t; elements = new List<Expression/*!*/>();
if (StartOf(16)) {
Expressions(elements);
}
e = new SetDisplayExpr(x, elements);
Expect(10);
} else if (la.kind == 74) {
Get();
x = t; elements = new List<Expression/*!*/>();
if (StartOf(16)) {
Expressions(elements);
}
e = new SeqDisplayExpr(x, elements);
Expect(75);
} else SynErr(218);
}
void MultiSetExpr(out Expression e) {
Contract.Ensures(Contract.ValueAtReturn(out e) != null);
IToken/*!*/ x = null; List<Expression/*!*/>/*!*/ elements;
e = dummyExpr;
Expect(57);
x = t;
if (la.kind == 9) {
Get();
elements = new List<Expression/*!*/>();
if (StartOf(16)) {
Expressions(elements);
}
e = new MultiSetDisplayExpr(x, elements);
Expect(10);
} else if (la.kind == 11) {
Get();
x = t; elements = new List<Expression/*!*/>();
Expression(out e, true);
e = new MultiSetFormingExpr(x, e);
Expect(33);
} else if (StartOf(30)) {
SemErr("multiset must be followed by multiset literal or expression to coerce in parentheses.");
} else SynErr(219);
}
void MapDisplayExpr(IToken/*!*/ mapToken, out Expression e) {
Contract.Ensures(Contract.ValueAtReturn(out e) != null);
List<ExpressionPair/*!*/>/*!*/ elements= new List<ExpressionPair/*!*/>() ;
e = dummyExpr;
Expect(74);
if (StartOf(16)) {
MapLiteralExpressions(out elements);
}
e = new MapDisplayExpr(mapToken, elements);
Expect(75);
}
void MapComprehensionExpr(IToken mapToken, out Expression e, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out e) != null);
BoundVar bv;
List<BoundVar> bvars = new List<BoundVar>();
Expression range = null;
Expression body;
IdentTypeOptional(out bv);
bvars.Add(bv);
if (la.kind == 28) {
Get();
Expression(out range, true);
}
QSep();
Expression(out body, allowSemi);
e = new MapComprehension(mapToken, bvars, range ?? new LiteralExpr(mapToken, true), body);
}
void ConstAtomExpression(out Expression e) {
Contract.Ensures(Contract.ValueAtReturn(out e) != null);
IToken/*!*/ x; BigInteger n; Basetypes.BigDec d;
e = dummyExpr;
switch (la.kind) {
case 113: {
Get();
e = new LiteralExpr(t, false);
break;
}
case 114: {
Get();
e = new LiteralExpr(t, true);
break;
}
case 115: {
Get();
e = new LiteralExpr(t);
break;
}
case 2: case 3: {
Nat(out n);
e = new LiteralExpr(t, n);
break;
}
case 4: {
Dec(out d);
e = new LiteralExpr(t, d);
break;
}
case 116: {
Get();
e = new ThisExpr(t);
break;
}
case 117: {
Get();
x = t;
Expect(11);
Expression(out e, true);
Expect(33);
e = new FreshExpr(x, e);
break;
}
case 118: {
Get();
x = t;
Expect(11);
Expression(out e, true);
Expect(33);
e = new OldExpr(x, e);
break;
}
case 28: {
Get();
x = t;
Expression(out e, true);
e = new UnaryExpr(x, UnaryExpr.Opcode.SeqLength, e);
Expect(28);
break;
}
case 11: {
Get();
x = t;
Expression(out e, true);
e = new ParensExpression(x, e);
Expect(33);
break;
}
case 55: {
Get();
x = t;
Expect(11);
IToken openParen = t;
Expression(out e, true);
Expect(33);
IToken classTok = new Token(t.line, t.col); classTok.val = "Real";
IToken fnTok = new Token(t.line, t.col); fnTok.val = "IntToReal";
//e = new IdentifierSequence(new List<IToken>() { classTok, fnTok }, openParen, new List<Expression/*!*/>() { e });
e = new FunctionCallExpr(x, "IntToReal", new StaticReceiverExpr(x, theBuiltIns.RealClass), openParen, new List<Expression/*!*/>() { e });
break;
}
case 54: {
Get();
x = t;
Expect(11);
IToken openParen = t;
Expression(out e, true);
Expect(33);
IToken classTok = new Token(t.line, t.col); classTok.val = "Real";
IToken fnTok = new Token(t.line, t.col); fnTok.val = "RealToInt";
//e = new IdentifierSequence(new List<IToken>() { classTok, fnTok }, openParen, new List<Expression/*!*/>() { e });
e = new FunctionCallExpr(x, "RealToInt", new StaticReceiverExpr(x, theBuiltIns.RealClass), openParen, new List<Expression/*!*/>() { e });
break;
}
default: SynErr(220); break;
}
}
void Nat(out BigInteger n) {
n = BigInteger.Zero;
if (la.kind == 2) {
Get();
try {
n = BigInteger.Parse(t.val);
} catch (System.FormatException) {
SemErr("incorrectly formatted number");
n = BigInteger.Zero;
}
} else if (la.kind == 3) {
Get();
try {
// note: leading 0 required when parsing positive hex numbers
n = BigInteger.Parse("0" + t.val.Substring(2), System.Globalization.NumberStyles.HexNumber);
} catch (System.FormatException) {
SemErr("incorrectly formatted number");
n = BigInteger.Zero;
}
} else SynErr(221);
}
void Dec(out Basetypes.BigDec d) {
d = Basetypes.BigDec.ZERO;
Expect(4);
try {
d = Basetypes.BigDec.FromString(t.val);
} catch (System.FormatException) {
SemErr("incorrectly formatted number");
d = Basetypes.BigDec.ZERO;
}
}
void MapLiteralExpressions(out List<ExpressionPair> elements) {
Expression/*!*/ d, r;
elements = new List<ExpressionPair/*!*/>();
Expression(out d, true);
Expect(69);
Expression(out r, true);
elements.Add(new ExpressionPair(d,r));
while (la.kind == 30) {
Get();
Expression(out d, true);
Expect(69);
Expression(out r, true);
elements.Add(new ExpressionPair(d,r));
}
}
void QSep() {
if (la.kind == 124) {
Get();
} else if (la.kind == 125) {
Get();
} else SynErr(222);
}
void MatchExpression(out Expression e, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out e) != null); IToken/*!*/ x; MatchCaseExpr/*!*/ c;
List<MatchCaseExpr/*!*/> cases = new List<MatchCaseExpr/*!*/>();
Expect(82);
x = t;
Expression(out e, allowSemi);
while (la.kind == 78) {
CaseExpression(out c, allowSemi);
cases.Add(c);
}
e = new MatchExpr(x, e, cases);
}
void QuantifierGuts(out Expression q, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out q) != null); IToken/*!*/ x = Token.NoToken;
bool univ = false;
List<BoundVar/*!*/> bvars;
Attributes attrs;
Expression range;
Expression/*!*/ body;
if (la.kind == 85 || la.kind == 121) {
Forall();
x = t; univ = true;
} else if (la.kind == 122 || la.kind == 123) {
Exists();
x = t;
} else SynErr(223);
QuantifierDomain(out bvars, out attrs, out range);
QSep();
Expression(out body, allowSemi);
if (univ) {
q = new ForallExpr(x, bvars, range, body, attrs);
} else {
q = new ExistsExpr(x, bvars, range, body, attrs);
}
}
void ComprehensionExpr(out Expression q, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out q) != null);
IToken x = Token.NoToken;
BoundVar bv;
List<BoundVar/*!*/> bvars = new List<BoundVar>();
Expression range;
Expression body = null;
Expect(56);
x = t;
IdentTypeOptional(out bv);
bvars.Add(bv);
while (la.kind == 30) {
Get();
IdentTypeOptional(out bv);
bvars.Add(bv);
}
Expect(28);
Expression(out range, allowSemi);
if (la.kind == 124 || la.kind == 125) {
QSep();
Expression(out body, allowSemi);
}
if (body == null && bvars.Count != 1) { SemErr(t, "a set comprehension with more than one bound variable must have a term expression"); }
q = new SetComprehension(x, bvars, range, body);
}
void StmtInExpr(out Statement s) {
s = dummyStmt;
if (la.kind == 83) {
AssertStmt(out s);
} else if (la.kind == 72) {
AssumeStmt(out s);
} else if (la.kind == 88) {
CalcStmt(out s);
} else SynErr(224);
}
void LetExpr(out Expression e, bool allowSemi) {
IToken x = null;
bool isGhost = false;
var letLHSs = new List<CasePattern>();
var letRHSs = new List<Expression>();
CasePattern pat;
bool exact = true;
e = dummyExpr;
if (la.kind == 24) {
Get();
isGhost = true; x = t;
}
Expect(29);
if (!isGhost) { x = t; }
CasePattern(out pat);
if (isGhost) { pat.Vars.Iter(bv => bv.IsGhost = true); }
letLHSs.Add(pat);
while (la.kind == 30) {
Get();
CasePattern(out pat);
if (isGhost) { pat.Vars.Iter(bv => bv.IsGhost = true); }
letLHSs.Add(pat);
}
if (la.kind == 69) {
Get();
} else if (la.kind == 71) {
Get();
exact = false;
foreach (var lhs in letLHSs) {
if (lhs.Arguments != null) {
SemErr(lhs.tok, "LHS of let-such-that expression must be variables, not general patterns");
}
}
} else SynErr(225);
Expression(out e, false);
letRHSs.Add(e);
while (la.kind == 30) {
Get();
Expression(out e, false);
letRHSs.Add(e);
}
Expect(8);
Expression(out e, allowSemi);
e = new LetExpr(x, letLHSs, letRHSs, e, exact);
}
void NamedExpr(out Expression e, bool allowSemi) {
IToken/*!*/ x, d;
e = dummyExpr;
Expression expr;
Expect(66);
x = t;
NoUSIdent(out d);
Expect(7);
Expression(out e, allowSemi);
expr = e;
e = new NamedExpr(x, d.val, expr);
}
void CasePattern(out CasePattern pat) {
IToken id; List<CasePattern> arguments;
BoundVar bv;
pat = null;
if (IsIdentParen()) {
Ident(out id);
Expect(11);
arguments = new List<CasePattern>();
if (la.kind == 1) {
CasePattern(out pat);
arguments.Add(pat);
while (la.kind == 30) {
Get();
CasePattern(out pat);
arguments.Add(pat);
}
}
Expect(33);
pat = new CasePattern(id, id.val, arguments);
} else if (la.kind == 1) {
IdentTypeOptional(out bv);
pat = new CasePattern(bv.tok, bv);
} else SynErr(226);
}
void CaseExpression(out MatchCaseExpr c, bool allowSemi) {
Contract.Ensures(Contract.ValueAtReturn(out c) != null); IToken/*!*/ x, id;
List<BoundVar/*!*/> arguments = new List<BoundVar/*!*/>();
BoundVar/*!*/ bv;
Expression/*!*/ body;
Expect(78);
x = t;
Ident(out id);
if (la.kind == 11) {
Get();
IdentTypeOptional(out bv);
arguments.Add(bv);
while (la.kind == 30) {
Get();
IdentTypeOptional(out bv);
arguments.Add(bv);
}
Expect(33);
}
Expect(79);
Expression(out body, allowSemi);
c = new MatchCaseExpr(x, id.val, arguments, body);
}
void Forall() {
if (la.kind == 85) {
Get();
} else if (la.kind == 121) {
Get();
} else SynErr(227);
}
void Exists() {
if (la.kind == 122) {
Get();
} else if (la.kind == 123) {
Get();
} else SynErr(228);
}
void AttributeBody(ref Attributes attrs) {
string aName;
List<Attributes.Argument/*!*/> aArgs = new List<Attributes.Argument/*!*/>();
Attributes.Argument/*!*/ aArg;
Expect(7);
Expect(1);
aName = t.val;
if (StartOf(31)) {
AttributeArg(out aArg, true);
aArgs.Add(aArg);
while (la.kind == 30) {
Get();
AttributeArg(out aArg, true);
aArgs.Add(aArg);
}
}
attrs = new Attributes(aName, aArgs, attrs);
}
public void Parse() {
la = new Token();
la.val = "";
Get();
Dafny();
Expect(0);
Expect(0);
}
static readonly bool[,]/*!*/ set = {
{T,T,T,T, T,x,x,x, T,T,x,T, x,x,x,x, x,x,x,x, x,x,x,T, T,x,T,T, T,T,x,x, x,x,T,x, x,T,x,x, T,T,T,T, T,T,T,T, T,T,T,T, x,x,T,T, x,x,x,x, x,x,x,x, x,x,T,T, x,x,T,x, T,x,x,x, T,x,x,x, T,T,T,T, T,T,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,T,T, T,T,T,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, T,x,T,x, x,x,x,T, T,T,T,T, x,T,x,T, x,x,T,x, x,x,x,x, T,T,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,T,x,x, x,T,x,x, x,x,x,x, x,x,x,x, T,T,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,T,T, T,T,T,T, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,T,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{T,x,x,x, x,x,x,x, x,T,T,T, x,x,x,T, T,x,T,x, x,x,x,T, T,T,T,T, x,T,x,T, x,x,T,x, x,x,T,x, T,T,T,T, T,x,x,T, T,T,T,x, x,x,x,x, x,x,x,x, x,x,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, T,T,T,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,T,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,T,T, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,T,T,x, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,T,T,T, T,T,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,T,x,x, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,T,T,T, T,T,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,T,T, T,T,T,T, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,T,T,T, T,x,x,x, x,T,x,T, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,x,T, x,x,x,x, x,T,T,x, x,x,x,x, T,x,T,x, T,x,x,x, x,x,T,T, x,T,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, x,T,x,x, T,T,T,T, T,T,T,x, x,T,T,T, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,x,x,T, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,T,T,T, T,x,x,x, x,T,x,T, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, T,T,x,x, x,x,x,x, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,T, x,x,T,T, x,x,x,x, x,x,x,x, x,x,T,T, x,x,T,x, T,x,x,x, T,x,x,x, T,x,T,T, T,T,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,T,T, T,T,T,x, x,x,x,x, x,x,x,x},
{T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,T,T, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,T,T,T, T,x,x,x, x,T,x,T, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,x,T, x,x,x,x, x,x,T,x, x,x,x,x, T,x,T,x, T,x,x,x, x,x,T,T, x,T,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, x,T,x,x, T,T,T,T, T,T,T,x, x,T,T,T, x,x,x,x},
{x,T,T,T, T,x,x,x, x,T,x,T, T,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,x,T, x,x,x,x, x,T,T,x, x,x,x,x, T,x,T,x, T,x,x,x, x,x,T,T, x,T,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, x,T,x,x, T,T,T,T, T,T,T,x, x,T,T,T, x,x,x,x},
{T,T,T,T, T,x,x,x, x,T,x,T, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, T,T,x,x, x,x,x,x, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,T, x,x,T,T, x,x,x,x, x,x,x,x, x,x,T,T, x,x,T,x, T,x,x,x, T,x,x,x, T,x,T,T, T,T,T,T, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,T,T, T,T,T,x, x,x,x,x, x,x,x,x},
{x,T,T,T, T,x,x,x, x,T,x,T, T,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, T,T,x,x, x,x,x,x, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,x,T, x,x,x,x, x,x,T,x, x,x,x,x, T,x,T,x, T,x,x,x, x,x,T,T, x,T,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, x,T,x,x, T,T,T,T, T,T,T,x, x,T,T,T, x,x,x,x},
{x,T,T,T, T,x,x,x, x,T,x,T, T,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,x,T, x,x,x,x, x,x,T,x, x,x,x,x, T,x,T,x, T,x,x,x, x,x,T,T, x,T,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, x,T,x,x, T,T,T,T, T,T,T,x, x,T,T,T, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, x,x,T,T, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,T,T, T,T,T,T, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,T,T,T, T,x,x,x, x,T,x,T, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, T,T,x,x, x,x,x,x, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,x,T, x,x,x,x, x,T,T,x, x,x,x,x, T,x,T,x, T,x,x,x, x,x,T,T, x,T,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, x,T,x,x, T,T,T,T, T,T,T,x, x,T,T,T, x,x,x,x},
{x,T,T,T, T,x,x,x, x,T,x,T, T,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,x,T, x,x,x,x, x,x,T,x, x,x,x,x, T,T,T,x, T,x,x,x, x,x,T,T, x,T,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, x,T,x,x, T,T,T,T, T,T,T,x, x,T,T,T, x,x,x,x},
{x,x,T,T, T,x,x,x, x,x,x,T, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,T,T, T,T,T,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,T,x, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,x, x,x,x,x, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, x,x,T,T, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,T,T, x,x,x,x, x,x,x,x, x,x,T,T, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x},
{x,x,x,x, x,x,x,T, T,T,T,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,x,T,x, T,T,x,x, x,T,T,T, x,x,x,x, x,T,T,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,x,x, x,T,x,x, x,x,x,T, x,T,T,T, x,T,x,x, x,x,x,x, x,x,T,T, T,T,T,T, T,T,T,T, T,T,T,T, T,T,T,T, T,T,T,T, x,x,x,x, x,x,x,T, T,x,x,x, T,T,x,x},
{x,x,x,x, x,x,x,T, T,T,T,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,x,T,x, T,T,x,x, x,T,T,T, x,x,x,x, x,T,T,x, T,T,x,x, x,x,x,x, x,x,x,x, x,T,x,x, x,T,x,x, x,T,x,x, x,x,T,T, x,T,T,T, x,T,x,x, x,x,x,x, x,x,T,T, T,T,T,T, T,T,T,T, T,T,T,T, T,T,T,T, T,T,T,T, x,x,x,x, x,x,x,T, T,x,x,x, T,T,x,x},
{x,T,T,T, T,x,T,x, x,T,x,T, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,T,T, T,T,x,T, x,x,x,x, x,x,T,x, x,x,x,x, T,x,T,x, T,x,x,x, x,x,T,T, x,T,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, x,T,x,x, T,T,T,T, T,T,T,x, x,T,T,T, x,x,x,x}
};
} // end Parser
public class Errors {
public int count = 0; // number of errors detected
public System.IO.TextWriter/*!*/ errorStream = Console.Out; // error messages go to this stream
public string errMsgFormat = "{0}({1},{2}): error: {3}"; // 0=filename, 1=line, 2=column, 3=text
public string warningMsgFormat = "{0}({1},{2}): warning: {3}"; // 0=filename, 1=line, 2=column, 3=text
public void SynErr(string filename, int line, int col, int n) {
SynErr(filename, line, col, GetSyntaxErrorString(n));
}
public virtual void SynErr(string filename, int line, int col, string/*!*/ msg) {
Contract.Requires(msg != null);
errorStream.WriteLine(errMsgFormat, filename, line, col, msg);
count++;
}
string GetSyntaxErrorString(int n) {
string s;
switch (n) {
case 0: s = "EOF expected"; break;
case 1: s = "ident expected"; break;
case 2: s = "digits expected"; break;
case 3: s = "hexdigits expected"; break;
case 4: s = "decimaldigits expected"; break;
case 5: s = "arrayToken expected"; break;
case 6: s = "string expected"; break;
case 7: s = "colon expected"; break;
case 8: s = "semi expected"; break;
case 9: s = "lbrace expected"; break;
case 10: s = "rbrace expected"; break;
case 11: s = "openparen expected"; break;
case 12: s = "star expected"; break;
case 13: s = "notIn expected"; break;
case 14: s = "\"include\" expected"; break;
case 15: s = "\"abstract\" expected"; break;
case 16: s = "\"module\" expected"; break;
case 17: s = "\"refines\" expected"; break;
case 18: s = "\"import\" expected"; break;
case 19: s = "\"opened\" expected"; break;
case 20: s = "\"=\" expected"; break;
case 21: s = "\"as\" expected"; break;
case 22: s = "\"default\" expected"; break;
case 23: s = "\"class\" expected"; break;
case 24: s = "\"ghost\" expected"; break;
case 25: s = "\"static\" expected"; break;
case 26: s = "\"datatype\" expected"; break;
case 27: s = "\"codatatype\" expected"; break;
case 28: s = "\"|\" expected"; break;
case 29: s = "\"var\" expected"; break;
case 30: s = "\",\" expected"; break;
case 31: s = "\"type\" expected"; break;
case 32: s = "\"==\" expected"; break;
case 33: s = "\")\" expected"; break;
case 34: s = "\"iterator\" expected"; break;
case 35: s = "\"yields\" expected"; break;
case 36: s = "\"returns\" expected"; break;
case 37: s = "\"...\" expected"; break;
case 38: s = "\"<\" expected"; break;
case 39: s = "\">\" expected"; break;
case 40: s = "\"method\" expected"; break;
case 41: s = "\"lemma\" expected"; break;
case 42: s = "\"colemma\" expected"; break;
case 43: s = "\"comethod\" expected"; break;
case 44: s = "\"constructor\" expected"; break;
case 45: s = "\"modifies\" expected"; break;
case 46: s = "\"free\" expected"; break;
case 47: s = "\"requires\" expected"; break;
case 48: s = "\"ensures\" expected"; break;
case 49: s = "\"decreases\" expected"; break;
case 50: s = "\"reads\" expected"; break;
case 51: s = "\"yield\" expected"; break;
case 52: s = "\"bool\" expected"; break;
case 53: s = "\"nat\" expected"; break;
case 54: s = "\"int\" expected"; break;
case 55: s = "\"real\" expected"; break;
case 56: s = "\"set\" expected"; break;
case 57: s = "\"multiset\" expected"; break;
case 58: s = "\"seq\" expected"; break;
case 59: s = "\"map\" expected"; break;
case 60: s = "\"object\" expected"; break;
case 61: s = "\".\" expected"; break;
case 62: s = "\"function\" expected"; break;
case 63: s = "\"predicate\" expected"; break;
case 64: s = "\"copredicate\" expected"; break;
case 65: s = "\"`\" expected"; break;
case 66: s = "\"label\" expected"; break;
case 67: s = "\"break\" expected"; break;
case 68: s = "\"where\" expected"; break;
case 69: s = "\":=\" expected"; break;
case 70: s = "\"return\" expected"; break;
case 71: s = "\":|\" expected"; break;
case 72: s = "\"assume\" expected"; break;
case 73: s = "\"new\" expected"; break;
case 74: s = "\"[\" expected"; break;
case 75: s = "\"]\" expected"; break;
case 76: s = "\"if\" expected"; break;
case 77: s = "\"else\" expected"; break;
case 78: s = "\"case\" expected"; break;
case 79: s = "\"=>\" expected"; break;
case 80: s = "\"while\" expected"; break;
case 81: s = "\"invariant\" expected"; break;
case 82: s = "\"match\" expected"; break;
case 83: s = "\"assert\" expected"; break;
case 84: s = "\"print\" expected"; break;
case 85: s = "\"forall\" expected"; break;
case 86: s = "\"parallel\" expected"; break;
case 87: s = "\"modify\" expected"; break;
case 88: s = "\"calc\" expected"; break;
case 89: s = "\"#\" expected"; break;
case 90: s = "\"<=\" expected"; break;
case 91: s = "\">=\" expected"; break;
case 92: s = "\"!=\" expected"; break;
case 93: s = "\"\\u2260\" expected"; break;
case 94: s = "\"\\u2264\" expected"; break;
case 95: s = "\"\\u2265\" expected"; break;
case 96: s = "\"<==>\" expected"; break;
case 97: s = "\"\\u21d4\" expected"; break;
case 98: s = "\"==>\" expected"; break;
case 99: s = "\"\\u21d2\" expected"; break;
case 100: s = "\"<==\" expected"; break;
case 101: s = "\"\\u21d0\" expected"; break;
case 102: s = "\"&&\" expected"; break;
case 103: s = "\"\\u2227\" expected"; break;
case 104: s = "\"||\" expected"; break;
case 105: s = "\"\\u2228\" expected"; break;
case 106: s = "\"in\" expected"; break;
case 107: s = "\"!\" expected"; break;
case 108: s = "\"+\" expected"; break;
case 109: s = "\"-\" expected"; break;
case 110: s = "\"/\" expected"; break;
case 111: s = "\"%\" expected"; break;
case 112: s = "\"\\u00ac\" expected"; break;
case 113: s = "\"false\" expected"; break;
case 114: s = "\"true\" expected"; break;
case 115: s = "\"null\" expected"; break;
case 116: s = "\"this\" expected"; break;
case 117: s = "\"fresh\" expected"; break;
case 118: s = "\"old\" expected"; break;
case 119: s = "\"then\" expected"; break;
case 120: s = "\"..\" expected"; break;
case 121: s = "\"\\u2200\" expected"; break;
case 122: s = "\"exists\" expected"; break;
case 123: s = "\"\\u2203\" expected"; break;
case 124: s = "\"::\" expected"; break;
case 125: s = "\"\\u2022\" expected"; break;
case 126: s = "??? expected"; break;
case 127: s = "this symbol not expected in SubModuleDecl"; break;
case 128: s = "invalid SubModuleDecl"; break;
case 129: s = "this symbol not expected in ClassDecl"; break;
case 130: s = "this symbol not expected in DatatypeDecl"; break;
case 131: s = "invalid DatatypeDecl"; break;
case 132: s = "this symbol not expected in DatatypeDecl"; break;
case 133: s = "this symbol not expected in ArbitraryTypeDecl"; break;
case 134: s = "this symbol not expected in IteratorDecl"; break;
case 135: s = "invalid IteratorDecl"; break;
case 136: s = "invalid ClassMemberDecl"; break;
case 137: s = "invalid IdentOrDigitsSuffix"; break;
case 138: s = "this symbol not expected in FieldDecl"; break;
case 139: s = "this symbol not expected in FieldDecl"; break;
case 140: s = "invalid FunctionDecl"; break;
case 141: s = "invalid FunctionDecl"; break;
case 142: s = "invalid FunctionDecl"; break;
case 143: s = "invalid FunctionDecl"; break;
case 144: s = "this symbol not expected in MethodDecl"; break;
case 145: s = "invalid MethodDecl"; break;
case 146: s = "invalid MethodDecl"; break;
case 147: s = "invalid FIdentType"; break;
case 148: s = "invalid TypeIdentOptional"; break;
case 149: s = "invalid TypeAndToken"; break;
case 150: s = "this symbol not expected in IteratorSpec"; break;
case 151: s = "this symbol not expected in IteratorSpec"; break;
case 152: s = "this symbol not expected in IteratorSpec"; break;
case 153: s = "this symbol not expected in IteratorSpec"; break;
case 154: s = "this symbol not expected in IteratorSpec"; break;
case 155: s = "invalid IteratorSpec"; break;
case 156: s = "this symbol not expected in IteratorSpec"; break;
case 157: s = "invalid IteratorSpec"; break;
case 158: s = "this symbol not expected in MethodSpec"; break;
case 159: s = "this symbol not expected in MethodSpec"; break;
case 160: s = "this symbol not expected in MethodSpec"; break;
case 161: s = "this symbol not expected in MethodSpec"; break;
case 162: s = "invalid MethodSpec"; break;
case 163: s = "this symbol not expected in MethodSpec"; break;
case 164: s = "invalid MethodSpec"; break;
case 165: s = "invalid FrameExpression"; break;
case 166: s = "invalid ReferenceType"; break;
case 167: s = "this symbol not expected in FunctionSpec"; break;
case 168: s = "this symbol not expected in FunctionSpec"; break;
case 169: s = "this symbol not expected in FunctionSpec"; break;
case 170: s = "this symbol not expected in FunctionSpec"; break;
case 171: s = "this symbol not expected in FunctionSpec"; break;
case 172: s = "invalid FunctionSpec"; break;
case 173: s = "invalid PossiblyWildFrameExpression"; break;
case 174: s = "invalid PossiblyWildExpression"; break;
case 175: s = "this symbol not expected in OneStmt"; break;
case 176: s = "invalid OneStmt"; break;
case 177: s = "this symbol not expected in OneStmt"; break;
case 178: s = "invalid OneStmt"; break;
case 179: s = "invalid AssertStmt"; break;
case 180: s = "invalid AssumeStmt"; break;
case 181: s = "invalid UpdateStmt"; break;
case 182: s = "invalid UpdateStmt"; break;
case 183: s = "invalid IfStmt"; break;
case 184: s = "invalid IfStmt"; break;
case 185: s = "invalid WhileStmt"; break;
case 186: s = "invalid WhileStmt"; break;
case 187: s = "invalid ForallStmt"; break;
case 188: s = "invalid ForallStmt"; break;
case 189: s = "this symbol not expected in ModifyStmt"; break;
case 190: s = "invalid ModifyStmt"; break;
case 191: s = "invalid ReturnStmt"; break;
case 192: s = "invalid Rhs"; break;
case 193: s = "invalid Lhs"; break;
case 194: s = "invalid Guard"; break;
case 195: s = "this symbol not expected in LoopSpec"; break;
case 196: s = "this symbol not expected in LoopSpec"; break;
case 197: s = "this symbol not expected in LoopSpec"; break;
case 198: s = "this symbol not expected in LoopSpec"; break;
case 199: s = "this symbol not expected in LoopSpec"; break;
case 200: s = "this symbol not expected in Invariant"; break;
case 201: s = "invalid AttributeArg"; break;
case 202: s = "invalid CalcOp"; break;
case 203: s = "invalid EquivOp"; break;
case 204: s = "invalid ImpliesOp"; break;
case 205: s = "invalid ExpliesOp"; break;
case 206: s = "invalid AndOp"; break;
case 207: s = "invalid OrOp"; break;
case 208: s = "invalid RelOp"; break;
case 209: s = "invalid AddOp"; break;
case 210: s = "invalid UnaryExpression"; break;
case 211: s = "invalid UnaryExpression"; break;
case 212: s = "invalid MulOp"; break;
case 213: s = "invalid NegOp"; break;
case 214: s = "invalid EndlessExpression"; break;
case 215: s = "invalid Suffix"; break;
case 216: s = "invalid Suffix"; break;
case 217: s = "invalid Suffix"; break;
case 218: s = "invalid DisplayExpr"; break;
case 219: s = "invalid MultiSetExpr"; break;
case 220: s = "invalid ConstAtomExpression"; break;
case 221: s = "invalid Nat"; break;
case 222: s = "invalid QSep"; break;
case 223: s = "invalid QuantifierGuts"; break;
case 224: s = "invalid StmtInExpr"; break;
case 225: s = "invalid LetExpr"; break;
case 226: s = "invalid CasePattern"; break;
case 227: s = "invalid Forall"; break;
case 228: s = "invalid Exists"; break;
default: s = "error " + n; break;
}
return s;
}
public void SemErr(IToken/*!*/ tok, string/*!*/ msg) { // semantic errors
Contract.Requires(tok != null);
Contract.Requires(msg != null);
SemErr(tok.filename, tok.line, tok.col, msg);
}
public virtual void SemErr(string filename, int line, int col, string/*!*/ msg) {
Contract.Requires(msg != null);
errorStream.WriteLine(errMsgFormat, filename, line, col, msg);
count++;
}
public void Warning(IToken/*!*/ tok, string/*!*/ msg) { // warnings
Contract.Requires(tok != null);
Contract.Requires(msg != null);
Warning(tok.filename, tok.line, tok.col, msg);
}
public virtual void Warning(string filename, int line, int col, string msg) {
Contract.Requires(msg != null);
errorStream.WriteLine(warningMsgFormat, filename, line, col, msg);
}
} // Errors
public class FatalError: Exception {
public FatalError(string m): base(m) {}
}
}
|