Source file tokenpp.ml
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
[%%prepare_logger]
module Xlist = Diffast_misc.Xlist
module Xqueue = Diffast_misc.Xqueue
module Xset = Diffast_misc.Xset
module Astloc = Langs_common.Astloc
module Layeredloc = Langs_common.Layeredloc
module Parserlib_base = Langs_common.Parserlib_base
module Loc = Astloc
module LLoc = Layeredloc
module Aux = Parser_aux
module PB = Parserlib_base
module C = Context
module TB = Tokenbuffer
module B = Branch
module Partial = Ast.Partial
open Labels
module PPD = PpDirective
module DL = Common.DirectiveLine
[%%capture_path
let check_partial partial =
let nodes = Partial.get_nodes partial in
let nnodes = ref 0 in
let nerrs = ref 0 in
List.iter
(fun node ->
Ast.visit
(fun nd ->
incr nnodes;
if Label.is_error nd#label then
incr nerrs
) node
) nodes;
[%debug_log "nnodes=%d, nerrs=%d" !nnodes !nerrs];
!nnodes, !nerrs
]
[%%capture_path
module F (Stat : Aux.STATE_T) = struct
module TrunkF = Trunk.F (Stat)
module TokenF = Token.F (Stat)
module TBF = TB.F (Stat)
module BF = B.F (Stat)
module U = Ulexer.F (Stat)
module P = Parser.Make (Stat)
module A = Aux.F (Stat)
open Tokens_
open Stat
let lexbuf_from_line loc ofs line =
[%debug_log "loc=%s ofs=%d line=%s" (Loc.to_string ~short:true loc) ofs (String.escaped line)];
let lexbuf = U.from_string line in
let st_pos = Loc.to_start_lexpos loc in
let base_pos = Loc.incr_n_lexpos ofs st_pos in
Sedlexing.set_position lexbuf base_pos;
lexbuf
let lloc_to_loc (lloc : LLoc.c) =
lloc#to_loc ~cache:(Some env#fname_ext_cache) ()
let merge_locs = PB.merge_locs ~cache:(Some env#fname_ext_cache)
let mkbtag = BF.make_tag
class branching_buffer btag = object (self)
val stack = Stack.create()
val mutable context = C.unknown()
method get_tag = btag
method get_context =
context
method set_context c =
[%debug_log "[%s]: %s" (B.tag_to_string btag) (C.to_string c)];
context <- c;
self#iter_branch (fun buf -> buf#set_context c)
method top =
Stack.top stack
method new_branch btag =
[%debug_log "creating new branch (nbranches=%d)" self#nbranches];
let buf = new TBF.c btag in
buf#set_context context;
Stack.push buf stack
method add token =
self#top#add token
method iter_branch f =
Stack.iter f stack
method iter f = self#top#iter f
method nbranches = Stack.length stack
method dump =
let count = ref 0 in
self#iter_branch
(fun br ->
Printf.printf "[%d: BRANCH BEGIN]\n" !count;
br#dump;
Printf.printf "[%d: BRANCH END]\n" !count;
incr count
)
initializer
self#new_branch btag
end
class bogus_buf = object
val mutable context = C.unknown()
val mutable is_empty = true
val mutable last_added =
PB.make_qtoken NOTHING Loc.dummy_lexpos Loc.dummy_lexpos
method is_empty = is_empty
method set_context c = context <- c
method get_context = context
method clear = is_empty <- true
method _add (qtoken : Token.qtoken_t) =
let tok, _ = qtoken in
if not (TokenF.is_pp_directive tok) then begin
[%debug_log "adding %s" (Token.qtoken_to_string qtoken)];
last_added <- qtoken
end;
is_empty <- false
method get_last = last_added
method get_copy = {<context=(C.copy_context context);is_empty=is_empty;last_added=last_added>}
end
class context_buffer = object (self)
val mutable stack = Stack.create()
val checkpoint_tbl = Hashtbl.create 0
method current_is_empty = self#current#is_empty
method set_context c =
[%debug_log "%s" (Context.to_string c)];
self#current#set_context (Context.copy_context c)
method get_context = self#current#get_context
method clear_context =
[%debug_log "called"];
self#current#clear;
self#current#set_context (Context.copy_context context_stack#top)
method current =
try
Stack.top stack
with
Stack.Empty ->
[%warn_log "context buffer is empty"];
let cbuf = new bogus_buf in
Stack.push cbuf stack;
cbuf
method stack_size = Stack.length stack
method enter_context =
[%debug_log "stack size: %d" self#stack_size];
let cbuf = new bogus_buf in
Stack.push cbuf stack
method exit_context =
[%debug_log "stack size: %d" self#stack_size];
try
let buf = Stack.pop stack in
self#current#_add buf#get_last
with
Stack.Empty -> failwith "Tokenpp.context_buffer#exit_context"
method copy_stack s =
let copy = Stack.create() in
let bs = ref [] in
Stack.iter
(fun buf ->
bs := buf#get_copy :: !bs
) s;
List.iter
(fun buf ->
Stack.push buf copy
) !bs;
copy
method checkpoint (key : C.key_t) =
[%debug_log "key=%s size=%d\n%s" (C.key_to_string key) self#stack_size self#to_string];
let copy = self#copy_stack stack in
Hashtbl.replace checkpoint_tbl key copy
method recover key =
[%debug_log "key=%s size=%d\n%s" (C.key_to_string key) self#stack_size self#to_string];
try
stack <- self#copy_stack (Hashtbl.find checkpoint_tbl key)
with
Not_found ->
[%fatal_log "stack not found: key=%s" (C.key_to_string key)];
raise (Common.Internal_error "Tokenpp.context_buffer#recover")
method to_string =
let buf = Buffer.create 0 in
Stack.iter
(fun b ->
Buffer.add_string buf (Printf.sprintf "%s\n" (Context.to_string b#get_context))
) stack;
Buffer.contents buf
end
type open_section_tag =
| OSTnone
| OSTif
| OSTdo
| OSTselect
| OSTforall
| OSTwhere
| OSTtype
| OSTfunction
| OSTsubroutine
| OSTpu
let open_section_tag_to_string = function
| OSTnone -> "OSTnone"
| OSTif -> "OSTif"
| OSTdo -> "OSTdo"
| OSTselect -> "OSTselect"
| OSTforall -> "OSTforall"
| OSTwhere -> "OSTwhere"
| OSTtype -> "OSTtype"
| OSTfunction -> "OSTfunction"
| OSTsubroutine -> "OSTsubroutine"
| OSTpu -> "OSTpu"
exception Branch_canceled
let mutually_exclusive cond0 cond1 =
let b = ("!"^cond0) = cond1 || cond0 = ("!"^cond1) in
[%debug_log "%s vs %s -> %B" cond0 cond1 b];
b
class buffer ?(context=None) lv partial_parser_selector (tokensrc : Tokensource.c) =
let new_context =
match context with
| None -> true
| _ -> false
in
object (self)
val top_key = C.mktopkey lv
val stack = Stack.create()
val mutable ignored_regions : Loc.t list = []
val context_buf =
match context with
| None -> new context_buffer
| Some cb -> cb
val branch_tag_stack = Stack.create()
val branches_with_else = Xset.create 0
method has_else btag =
Xset.mem branches_with_else btag
method reg_branch_with_else btag =
Xset.add branches_with_else btag
method clear_context =
[%debug_log "[%d] called" lv];
context_buf#clear_context
method enter_context =
[%debug_log "[%d] context buffer stack size: %d" lv context_buf#stack_size];
context_buf#enter_context
method exit_context =
[%debug_log "[%d] context buffer stack size: %d" lv context_buf#stack_size];
context_buf#exit_context
method checkpoint (key : C.key_t) =
context_stack#checkpoint key;
env#checkpoint key;
context_buf#checkpoint key
method recover (key : C.key_t) =
context_stack#recover key;
env#recover key;
context_buf#recover key
method branch_depth =
Stack.length branch_tag_stack
method enter_branch btag =
[%debug_log "[%d] pushing %s" lv (B.tag_to_string btag)];
Stack.push btag branch_tag_stack
method exit_branch =
let btag = Stack.pop branch_tag_stack in
let _ = btag in
[%debug_log "[%d] poped %s" lv (B.tag_to_string btag)]
method nbbuf = Stack.length stack
method in_branch = not (Stack.is_empty stack)
method current_bbuf = Stack.top stack
method add ?(raise_if_failed=false) qtoken =
[%debug_log "[%d] adding %s (raise_if_failed=%B)"
lv (Token.qtoken_to_string qtoken) raise_if_failed];
try
(self#current_bbuf)#add qtoken
with
Stack.Empty ->
if env#discarded_branch_entry_count > 0 then begin
if raise_if_failed then
raise Branch_canceled
end
else
Common.fail_to_parse
~head:(Printf.sprintf "[%s]" (Loc.to_string (Token.qtoken_to_loc qtoken)))
"failed to parse pp-branch"
method dump_top =
(self#current_bbuf)#dump
method get_branch_context =
let start = ref None in
let bufs = ref [] in
begin
try
Stack.iter
(fun bbuf ->
let cnt = bbuf#top#get_context in
[%debug_log "[%d] bbuf[%s] context=%s" lv
(B.tag_to_string bbuf#get_tag) (C.to_string cnt)];
if not (C.is_unknown cnt) && C.is_active cnt then begin
start := Some (bbuf#top, B.key_loc_of_tag bbuf#get_tag);
raise Exit
end
else
bufs := bbuf#top :: !bufs;
) stack
with
Exit -> ()
end;
match !start with
| None -> raise Not_found
| Some (sbuf, key) ->
let cpy = sbuf#get_copy in
List.iter
(fun buf ->
cpy#receive_all buf#get_copy
) !bufs;
[%debug_log "[%d] loc: %s" lv (try Loc.to_string cpy#get_loc with TB.Empty -> "-")];
cpy, key
method dump_ignored_regions =
if ignored_regions <> [] then
Printf.printf "ignored regions:\n";
Loc.dump_locs ignored_regions
method ignored_LOC =
Loc.lines_of_locs ignored_regions
method ignored_regions = ignored_regions
method add_ignored_region r =
[%debug_log "%s" (Loc.to_string ~show_ext:true r)];
ignored_regions <- r :: ignored_regions
method add_ignored_regions rs =
begin %debug_block
List.iter (fun r -> [%debug_log "%s" (Loc.to_string ~show_ext:true r)]) rs
end;
ignored_regions <- rs @ ignored_regions
method begin_branch ~open_if btag =
[%debug_log "[%d] open_if=%B, nbbuf=%d context=%s btag=%s"
lv open_if self#nbbuf (C.to_string context_stack#top) (B.tag_to_string btag)];
let bbuf = new branching_buffer btag in
if self#in_branch then begin
[%debug_log "in branch"];
end
else begin
[%debug_log "not in branch"];
let cbuf = context_buf#current in
let c = cbuf#get_context in
[%debug_log "[%d] context of context buffer: %s" lv (C.to_string c)];
if cbuf#is_empty then begin
[%debug_log "[%d] context buffer is empty" lv];
bbuf#set_context (C.copy_context c)
end
else begin
[%debug_log "[%d] last non-pp qtoken in context buffer: %s" lv
(Token.qtoken_to_string cbuf#get_last)];
self#checkpoint top_key;
let bbuf_context = ref (Context.copy_context context_stack#top) in
if open_if then
context_stack#deactivate_top_no_delay;
begin
let last_tok, _ = cbuf#get_last in
match last_tok with
| EOL | SEMICOLON
| SPEC_PART_CONSTRUCT _ | EXEC_PART_CONSTRUCT _
| DERIVED_TYPE_DEF_PART _ | END_FRAGMENT
| FUNCTION_HEAD _ | SUBROUTINE_HEAD _ | PU_TAIL _ | STMT _
| SUBPROGRAM _ | PROGRAM_UNIT _
| NOTHING
| INCLUDE__FILE _
-> ()
| _ ->
[%debug_log "changing context: %s -> %s (last_tok=%s)"
(C.tag_to_string (C.get_tag !bbuf_context))
(C.tag_to_string C.Tin_stmt) (Token.rawtoken_to_string last_tok)];
C.set_tag (!bbuf_context) C.Tin_stmt
end;
bbuf#set_context !bbuf_context
end
end;
[%debug_log "[%d][%s][%s]: nbbuf=%d (current context: %s)" lv
(B.tag_to_string btag) (C.to_string bbuf#get_context) self#nbbuf
(C.to_string context_stack#top)];
let bcont = bbuf#get_context in
if not (C.is_unknown bcont) then begin
let key = C.mkkey lv (B.key_loc_of_tag btag) in
self#checkpoint key
end;
[%debug_log "stack size: %d" self#nbbuf];
Stack.push bbuf stack;
[%debug_log "bbuf pushed: %s" (B.tag_to_string bbuf#get_tag)]
method end_branch ~open_if endif_loc =
ignore open_if;
[%debug_log "[%d] open_if=%B, nbbuf=%d" lv open_if self#nbbuf];
let bbuf = Stack.pop stack in
[%debug_log "bbuf poped: %s" (B.tag_to_string bbuf#get_tag)];
let selected, ignored = self#select_branches endif_loc bbuf in
self#add_ignored_regions ignored;
[%debug_log "[%d] sending to tokensrc" lv];
selected#dump;
let key = C.mkkey lv (B.key_loc_of_tag bbuf#get_tag) in
tokensrc#prepend_queue ~copy:false selected#_raw;
self#recover key
method add_to_context qtoken =
[%debug_log "[%d] adding %s" lv (Token.qtoken_to_string qtoken)];
context_buf#current#_add qtoken
method set_context c =
[%debug_log "[%d] context=%s" lv (Context.to_string c)];
context_buf#current#set_context (Context.copy_context c)
method discard_line =
begin
try
while true do
let qtoken = tokensrc#get() in
let tok, _ = qtoken in
[%debug_log "[%d] discarding %s" lv (Token.rawtoken_to_string tok)];
match tok with
| EOL -> raise Exit
| _ -> ()
done
with
Exit -> ()
end;
[%debug_log "finished"]
method discard_branch ?(start_opt=None) () =
let branch_depth = ref 0 in
let loc_opt = ref start_opt in
let last_eol = ref None in
begin
try
while true do
let qtoken = tokensrc#get() in
let tok, loc = qtoken in
begin
match !loc_opt with
| None -> loc_opt := Some loc
| _ -> ()
end;
[%debug_log "[%d] discarding %s" lv (Token.rawtoken_to_string tok)];
match tok with
| PP_BRANCH (PPD.Ifdef _ | PPD.Ifndef _ | PPD.If _) -> incr branch_depth
| PP_BRANCH (PPD.Endif _) -> begin
if !branch_depth = 0 then begin
let ignored =
match !loc_opt with
| Some loc' -> merge_locs loc' loc
| None -> loc
in
self#add_ignored_region ignored;
raise Exit
end;
if !branch_depth > 0 then
decr branch_depth
end
| EOL ->
last_eol := Some qtoken
| OCL _ | OMP _ | XLF _ | ACC _ | DEC _ -> ()
| _ ->
if TokenF.is_pp_directive tok then
()
else
last_eol := None
done
with
Exit -> ()
end;
begin
match !last_eol with
| Some t -> tokensrc#prepend t
| _ -> ()
end;
[%debug_log "finished"]
method private consume () =
let qtoken = tokensrc#get() in
let tok, loc = qtoken in
let buffer_add ?(raise_if_failed=false) () =
[%debug_log "[%d] in_branch=%B" lv self#in_branch];
let t =
if self#in_branch then
qtoken
else
TBF.hack_token tokensrc qtoken
in
self#add ~raise_if_failed t
in
[%debug_log "[%d] %s[%s]" lv
(Token.rawtoken_to_string tok) (Loc.to_string ~show_ext:true loc)];
match tok with
| RAW {DL.tag=DL.OCL;DL.line=line;_} -> begin
[%debug_log "processing %s" (Token.rawtoken_to_string tok)];
env#current_source#add_ext_Fujitsu;
let ofs = 4 in
let ulb = lexbuf_from_line loc ofs (line^"\n") in
let scanner () = PB.qtoken_to_token (U.scan_ocl ulb) in
try
let nd = (PB.mkparser P.ocl) scanner in
nd#set_lloc (LLoc.merge (env#mklloc loc) nd#lloc);
let tok' = OCL nd in
if self#in_branch then begin
let t = (tok', loc) in
self#add t;
self#pp ()
end
else
tok', loc
with
_ ->
Common.parse_warning_loc loc "failed to parse ocl:%s" line;
self#pp ()
end
| RAW {DL.tag=DL.DEC;DL.head=prefix;DL.line=line;_} -> begin
[%debug_log "processing %s" (Token.rawtoken_to_string tok)];
env#current_source#add_ext_Intel;
let ofs = (String.length prefix) + 1 in
let ulb = lexbuf_from_line loc ofs (line^"\n") in
let scanner () = PB.qtoken_to_token (U.scan_dec ulb) in
try
let nd = (PB.mkparser P.dec) scanner in
nd#set_lloc (LLoc.merge (env#mklloc loc) nd#lloc);
let tok' = DEC nd in
if self#in_branch then begin
let t = (tok', loc) in
self#add t;
self#pp ()
end
else
tok', loc
with
_ ->
Common.parse_warning_loc loc "failed to parse dec:%s" line;
self#pp ()
end
| RAW {DL.tag=DL.XLF;
DL.head=trigger;
DL.line=line;
DL.queue=q;
DL.fixed_cont=_;
DL.free_cont=free_cont;
_
} -> begin
[%debug_log "processing %s" (Token.rawtoken_to_string tok)];
env#current_source#add_ext_IBM;
let queue = q in
begin
let cur_free_cont = ref free_cont in
try
while true do
let ntok = tokensrc#peek_nth_rawtok 1 in
[%debug_log "next token: %s" (Token.rawtoken_to_string ntok)];
match ntok with
| RAW {DL.tag=DL.XLF;
DL.line=nline;
DL.queue=nq;
DL.fixed_cont=nfix_cont;
DL.free_cont=nfree_cont;
_
} ->
if !cur_free_cont || nfix_cont then begin
[%debug_log "continued: %s" nline];
let _ = tokensrc#get() in
nq#transfer queue;
cur_free_cont := nfree_cont
end
else
raise Exit
| _ -> raise Exit
done
with
Exit -> ()
end;
let last_t = ref (Obj.repr qtoken) in
queue#iter (fun t -> last_t := t);
queue#add (Obj.repr (EOL, Token.qtoken_to_loc (Obj.obj !last_t)));
begin %debug_block
[%debug_log "queue:"];
queue#iter (fun r -> [%debug_log " %s" (Token.qtoken_to_string (Obj.obj r))])
end;
let scanner () =
let t =
try
Obj.obj queue#take
with
Xqueue.Empty -> raise Sedlexing.MalFormed
in
let rt, _ = t in
let _ = rt in
[%debug_log "--> %s" (Token.rawtoken_to_string rt)];
PB.qtoken_to_token t
in
try
let nd = (PB.mkparser P.xlf) scanner in
nd#set_lloc (LLoc.merge (env#mklloc loc) nd#lloc);
let tok' = XLF nd in
if self#in_branch then begin
let t = (tok', loc) in
self#add t;
self#pp ()
end
else
tok', loc
with
exn ->
Common.parse_warning_loc loc "failed to parse xlf: [%s]:%s" line (Printexc.to_string exn);
self#pp ()
end
| RAW {DL.tag=DL.OMP;
DL.line=line;
DL.queue=q;
DL.fixed_cont=_;
DL.free_cont=free_cont;
_
} -> begin
[%debug_log "processing %s" (Token.rawtoken_to_string tok)];
let queue = q in
begin
let cur_free_cont = ref free_cont in
try
while true do
let ntok = tokensrc#peek_nth_rawtok 1 in
[%debug_log "next token: %s" (Token.rawtoken_to_string ntok)];
match ntok with
| RAW {DL.tag=DL.OMP;
DL.line=nline;
DL.queue=nq;
DL.fixed_cont=nfix_cont;
DL.free_cont=nfree_cont;
_
} ->
let _ = nline in
if !cur_free_cont || nfix_cont then begin
[%debug_log "continued: %s" nline];
let _ = tokensrc#get() in
nq#transfer queue;
cur_free_cont := nfree_cont
end
else begin
raise Exit
end
| _ -> raise Exit
done
with
Exit -> ()
end;
let last_t = ref (Obj.repr qtoken) in
queue#iter (fun t -> last_t := t);
queue#add (Obj.repr (EOL, Token.qtoken_to_loc (Obj.obj !last_t)));
begin %debug_block
[%debug_log "queue:"];
queue#iter (fun r -> [%debug_log " %s" (Token.qtoken_to_string (Obj.obj r))])
end;
let queue2 = new Xqueue.c in
let _ =
queue#fold
(fun tmp_opt r ->
let t = Obj.obj r in
match Token.qtoken_to_rawtoken t with
| IDENTIFIER i -> begin
match tmp_opt with
| Some (tmp, _) -> begin
let tmp_tok, tmp_loc = tmp in
match tmp_tok with
| IDENTIFIER tmp_i ->
let st, _ = Loc.to_lexposs tmp_loc in
let _, ed = Loc.to_lexposs (Token.qtoken_to_loc t) in
let t' = PB.make_qtoken (IDENTIFIER (tmp_i^i)) st ed in
Some (t', true)
| _ -> assert false
end
| None -> Some (t, false)
end
| _ -> begin
begin
match tmp_opt with
| Some (tmp, merged) ->
if merged then begin
match Token.qtoken_to_rawtoken tmp with
| IDENTIFIER i ->
let tok = U.find_omp_keyword i in
let loc = Token.qtoken_to_loc tmp in
let t = (tok, loc) in
queue2#add t
| _ -> assert false
end
else begin
queue2#add tmp
end
| None -> ()
end;
queue2#add (Obj.obj r);
None
end
) None
in
begin %debug_block
[%debug_log "queue2:"];
queue2#iter (fun t -> [%debug_log " %s" (Token.qtoken_to_string t)])
end;
let queue3 = new Xqueue.c in
let qadd x = queue3#add x in
let _ =
queue2#fold (U.check_omp_separated_keyword qadd) None
in
begin %debug_block
[%debug_log "queue3:"];
queue3#iter (fun t -> [%debug_log " %s" (Token.qtoken_to_string t)])
end;
let tok_hist = ref [] in
let paren_level = ref 0 in
let is_type_kw = function
| KINDED_TYPE_SPEC _
| DOUBLE_PRECISION _
| DOUBLE_COMPLEX _
| CHARACTER _
| KIND _
| LEN _ -> true
| _ -> false
in
let in_omp_kind_context() =
match !tok_hist with
| LPAREN::(OMP_SCHEDULE|OMP_DIST_SCHEDULE)::_ -> true
| _ -> false
in
let in_omp_default_context() =
match !tok_hist with
| LPAREN::OMP_DEFAULT::_ -> true
| _ -> false
in
let in_omp_depend_context() =
match !tok_hist with
| LPAREN::OMP_DEPEND::_ -> true
| _ -> false
in
let in_omp_proc_bind_context() =
match !tok_hist with
| LPAREN::OMP_PROC_BIND::_ -> true
| _ -> false
in
let in_omp_map_type_context = function
| Some COLON -> begin
match !tok_hist with
| LPAREN::OMP_MAP::_ -> true
| _ -> false
end
| Some _
| None -> false
in
let in_type_context() =
let rec scan = function
| COLON::rest -> rest
| _::rest -> scan rest
| [] -> []
in
match scan !tok_hist with
| _::LPAREN::OMP_DECLARE_REDUCTION::_ -> true
| _ -> false
in
let scanner () =
let final_t =
try
let t = queue3#take in
let tok, loc = t in
begin
match tok with
| LPAREN -> incr paren_level
| RPAREN -> decr paren_level
| _ -> ()
end;
begin
match tok with
| IDENTIFIER _ -> t
| _ -> begin
try
let kw = U.get_omp_keyword_string tok in
let mk_ident_token() =
(IDENTIFIER kw, loc)
in
let next_tok_opt =
try
let next_t = queue3#peek in
Some (Token.qtoken_to_rawtoken next_t)
with
Xqueue.Empty -> None
in
if in_omp_kind_context() then begin
[%debug_log "in omp_kind context"];
t
end
else if in_omp_default_context() then begin
[%debug_log "in omp_default context"];
t
end
else if in_omp_depend_context() then begin
[%debug_log "in omp_depend context"];
t
end
else if in_omp_proc_bind_context() then begin
[%debug_log "in omp_proc_bind context"];
t
end
else if in_omp_map_type_context next_tok_opt then begin
[%debug_log "in omp_map_type context"];
t
end
else if in_type_context() then begin
[%debug_log "in type context"];
if is_type_kw tok then
t
else begin
[%debug_log "<non type keyword> --> <identifier> (in type context)"];
mk_ident_token()
end
end
else if !paren_level > 0 then begin
[%debug_log "in paren"];
mk_ident_token()
end
else
t
with
Not_found -> t
end
end
with
Xqueue.Empty -> raise Sedlexing.MalFormed
in
let final_tok, _ = final_t in
tok_hist := final_tok :: !tok_hist;
[%debug_log "--> %s" (Token.rawtoken_to_string final_tok)];
PB.qtoken_to_token final_t
in
try
let nd = (PB.mkparser P.omp) scanner in
nd#set_lloc (LLoc.merge (env#mklloc loc) nd#lloc);
let tok' = OMP nd in
if self#in_branch then begin
let t = (tok', loc) in
self#add t;
self#pp ()
end
else
tok', loc
with
exn ->
Common.parse_warning_loc loc
"failed to parse omp: [%s]:%s" line (Printexc.to_string exn);
self#pp ()
end
| RAW {DL.tag=DL.ACC;
DL.line=line;
DL.queue=q;
DL.fixed_cont=_;
DL.free_cont=free_cont;
_
} -> begin
[%debug_log "processing %s" (Token.rawtoken_to_string tok)];
let queue = q in
begin
let cur_free_cont = ref free_cont in
try
while true do
let ntok = tokensrc#peek_nth_rawtok 1 in
[%debug_log "next token: %s" (Token.rawtoken_to_string ntok)];
match ntok with
| RAW {DL.tag=DL.ACC;
DL.line=nline;
DL.queue=nq;
DL.fixed_cont=nfix_cont;
DL.free_cont=nfree_cont;
_
} ->
let _ = nline in
if !cur_free_cont || nfix_cont then begin
[%debug_log "continued: %s" nline];
let _ = tokensrc#get() in
nq#transfer queue;
cur_free_cont := nfree_cont
end
else
raise Exit
| _ -> raise Exit
done
with
Exit -> ()
end;
let last_t = ref (Obj.repr qtoken) in
queue#iter (fun t -> last_t := t);
queue#add (Obj.repr (EOL, Token.qtoken_to_loc (Obj.obj !last_t)));
begin %debug_block
[%debug_log "queue:"];
queue#iter (fun r -> [%debug_log " %s" (Token.qtoken_to_string (Obj.obj r))])
end;
let scanner () =
let t =
try
Obj.obj queue#take
with
Xqueue.Empty -> raise Sedlexing.MalFormed
in
let rt, _ = t in
let _ = rt in
[%debug_log "--> %s" (Token.rawtoken_to_string rt)];
PB.qtoken_to_token t
in
try
let nd = (PB.mkparser P.acc) scanner in
nd#set_lloc (LLoc.merge (env#mklloc loc) nd#lloc);
let tok' = ACC nd in
if self#in_branch then begin
let t = (tok', loc) in
self#add t;
self#pp ()
end
else
tok', loc
with
exn ->
Common.parse_warning_loc loc
"failed to parse acc: [%s]:%s" line (Printexc.to_string exn);
self#pp ()
end
| PP_BRANCH (PPD.Ifdef id | PPD.Ifndef id) -> begin
[%debug_log "processing %s" (Token.rawtoken_to_string tok)];
let btag = mkbtag tok id loc loc in
self#enter_branch btag;
if self#branch_depth = 1 then begin
self#begin_branch ~open_if:false btag
end
else begin
buffer_add()
end;
[%debug_log "[%d] calling pp" lv];
self#pp ()
end
| PP_BRANCH (PPD.If _) -> begin
[%debug_log "processing %s" (Token.rawtoken_to_string tok)];
let btag = mkbtag tok "" loc loc in
self#enter_branch btag;
if self#branch_depth = 1 then begin
self#begin_branch ~open_if:false btag
end
else begin
buffer_add()
end;
[%debug_log "[%d] calling pp" lv];
self#pp ()
end
| PP_BRANCH (PPD.Elif _) -> begin
let branch_depth = self#branch_depth in
[%debug_log "processing %s (branch depth: %d)"
(Token.rawtoken_to_string tok) branch_depth];
if branch_depth = 1 then begin
let kloc =
try
let tag = Stack.top branch_tag_stack in
[%debug_log "top branch tag: %s" (B.tag_to_string tag)];
B.key_loc_of_tag tag
with
_ -> loc
in
self#current_bbuf#new_branch (mkbtag tok "" loc kloc)
end
else if branch_depth = 0 then begin
Common.parse_warning_loc loc "ignoring dangling #elif"
end
else begin
try
buffer_add ~raise_if_failed:true ()
with
Branch_canceled -> self#discard_branch ~start_opt:(Some loc) ()
end;
[%debug_log "[%d] calling pp" lv];
self#pp ()
end
| PP_BRANCH PPD.Else -> begin
let branch_depth = self#branch_depth in
[%debug_log "processing %s (branch depth: %d)"
(Token.rawtoken_to_string tok) branch_depth];
if branch_depth = 1 then begin
let top_btag = Stack.top branch_tag_stack in
self#reg_branch_with_else top_btag;
let kloc =
try
B.key_loc_of_tag top_btag
with
_ -> loc
in
self#current_bbuf#new_branch (mkbtag tok "" loc kloc)
end
else if branch_depth = 0 then begin
Common.parse_warning_loc loc "ignoring dangling #else"
end
else begin
try
buffer_add ~raise_if_failed:true ()
with
Branch_canceled -> self#discard_branch ~start_opt:(Some loc) ()
end;
[%debug_log "[%d] calling pp" lv];
self#pp ()
end
| PP_BRANCH (PPD.Endif(br, plv)) -> begin
let branch_depth = self#branch_depth in
[%debug_log "processing %s (branch depth: %d)"
(Token.rawtoken_to_string tok) self#branch_depth];
begin
try
if branch_depth = 0 then begin
Common.parse_warning_loc loc "ignoring dangling #endif"
end
else begin
if branch_depth = 1 then begin
let to_be_discarded = ref 0 in
let top_btag = Stack.top branch_tag_stack in
[%debug_log "has_else=%B" (self#has_else top_btag)];
let is_virtual_else =
not (self#has_else top_btag) &&
match br with
| PPD.If cond -> begin
match tokensrc#peek_nth_rawtok 1 with
| END_FRAGMENT -> begin
match tokensrc#peek_nth_rawtok 2 with
| PP_BRANCH (PPD.If cond') -> begin
[%debug_log "paren level: %d -> %d" plv env#lex_paren_level];
let b =
mutually_exclusive cond cond' || plv <> env#lex_paren_level
in
if b then
to_be_discarded := 2;
b
end
| _ -> false
end
| PP_BRANCH (PPD.If cond') -> begin
[%debug_log "paren level: %d -> %d" plv env#lex_paren_level];
let b =
mutually_exclusive cond cond' || plv <> env#lex_paren_level
in
if b then
to_be_discarded := 1;
b
end
| _ -> false
end
| _ -> false
in
[%debug_log "is_virtual_else=%B to_be_discarded=%d" is_virtual_else !to_be_discarded];
if is_virtual_else then begin
for _ = 1 to !to_be_discarded do
let t, _ = tokensrc#discard ~skip_pp_branch:false () in
let _ = t in
[%debug_log "discarded: %s" (Token.rawtoken_to_string t)]
done;
let kloc =
try
B.key_loc_of_tag top_btag
with
_ -> loc
in
self#current_bbuf#new_branch (mkbtag tok "" loc kloc)
end
else begin
self#end_branch ~open_if:false loc;
self#exit_branch
end
end
else begin
buffer_add ~raise_if_failed:true ();
self#exit_branch
end
end
with
Branch_canceled -> env#decr_discarded_branch_entry_count
end;
[%debug_log "[%d] calling pp" lv];
self#pp ()
end
| PP_INCLUDE__FILE _ | INCLUDE__FILE _ | OPTIONS__OPTS _ -> begin
[%debug_log "processing %s" (Token.rawtoken_to_string tok)];
if self#in_branch then begin
buffer_add();
[%debug_log "[%d] calling pp" lv];
self#pp ()
end
else begin
self#add_to_context (tok, loc);
tok, loc
end
end
| PP_UNDEF__IDENT id -> begin
[%debug_log "processing %s" (Token.rawtoken_to_string tok)];
if self#in_branch then begin
buffer_add();
[%debug_log "[%d] calling pp" lv];
self#pp ()
end
else begin
env#undefine_macro id;
self#add_to_context (tok, loc);
tok, loc
end
end
| PP_DEFINE__IDENT__BODY(id, body) -> begin
[%debug_log "processing %s" (Token.rawtoken_to_string tok)];
if self#in_branch then begin
[%debug_log "branch: %s" (B.tag_to_string self#current_bbuf#top#get_tag)];
let unconditional =
lv = 0 &&
match self#current_bbuf#top#get_tag with
| B.Tifndef(id', _) -> id' = id
| _ -> false
in
[%debug_log "unconditional=%B" unconditional];
if unconditional then
env#define_macro ~conditional:false id body;
buffer_add();
[%debug_log "[%d] calling pp" lv];
self#pp ()
end
else begin
env#define_macro ~conditional:(lv > 0) id body;
self#add_to_context (tok, loc);
tok, loc
end
end
| _ -> begin
[%debug_log "processing %s" (Token.rawtoken_to_string tok)];
[%debug_log "[%d] in_branch=%B" lv self#in_branch];
if self#in_branch then begin
begin
match tok with
| EOF _ -> begin
Common.parse_warning_loc loc "non-terminated pp-branch";
try
self#end_branch ~open_if:false loc;
self#exit_branch
with
Branch_canceled -> env#decr_discarded_branch_entry_count
end
| _ -> buffer_add()
end;
[%debug_log "[%d] calling pp" lv];
self#pp ()
end
else begin
if context_buf#current_is_empty then
if context_stack#top_is_active && not context_stack#top_is_unknown then
self#set_context context_stack#top;
self#add_to_context (tok, loc);
tok, loc
end
end
method pp () =
[%debug_log "[%d] tokenbuf#in_branch=%B" lv self#in_branch];
[%debug_log "[%d] calling consume" lv];
self#consume ()
method select_branches endif_loc bbuf =
let context = bbuf#get_context in
begin %debug_block
[%debug_log "[%d] context: %s" lv (C.to_string context)];
[%debug_log "[%d] branches:" lv];
bbuf#iter_branch
(fun buf ->
[%debug_log "%s [%s] (ntokens=%d) [%s]"
(B.tag_to_string buf#get_tag)
(try Loc.to_string buf#get_loc with _ -> "-")
buf#size
(C.to_string buf#get_context)];
buf#dump
);
end;
let selected_buf = ref (new TBF.c B.Tselected) in
(!selected_buf)#set_context context;
let ignored_regions = ref [] in
let tag_loc = B.loc_of_tag bbuf#get_tag in
[%debug_log "tag_loc: %s" (Loc.to_string ~show_ext:true tag_loc)];
let key = C.mkkey lv tag_loc in
let bbuf_top_last_EOL_opt = bbuf#top#get_last_EOL in
let bbuf_top_length = bbuf#top#total_length_no_pp in
[%debug_log "bbuf_top_last_EOL_opt: %s (buf length: %d)"
(Common.opt_to_string Token.qtoken_to_string bbuf_top_last_EOL_opt)
bbuf_top_length];
let bbuf_top_last_EOL_exists =
match bbuf_top_last_EOL_opt with
| Some _ -> true
| None -> false
in
let adjust_last ?(context=C.dummy) buf =
begin %debug_block
[%debug_log "context: %s" (C.to_string context)];
[%debug_log "buf length: %d" buf#total_length];
buf#dump;
end;
if buf#total_length > 0 && bbuf_top_length > 0 then
let pp_only =
try
buf#iter
(fun (t, _) ->
if not (TokenF.is_pp_directive t || TokenF.is_include t) then
raise Exit
);
true
with
Exit -> false
in
[%debug_log "pp_only=%B" pp_only];
let directive_only =
try
buf#iter
(fun (t, _) ->
if not (TokenF.is_directive t) then
raise Exit
);
true
with
Exit -> false
in
[%debug_log "directive_only=%B" directive_only];
if pp_only || directive_only then
buf#set_irregular;
if buf#ends_with_EOL then begin
[%debug_log "buffer ends with EOL"];
match bbuf_top_last_EOL_opt with
| Some _ -> begin
match context.C.tag with
| C.Texpr | C.Tin_stmt -> begin
[%debug_log "removing EOL..."];
buf#remove_last_EOL
end
| _ -> ()
end
| None -> begin
if bbuf#top#is_regular then begin
[%debug_log "removing EOL..."];
buf#remove_last_EOL
end
end
end
else begin
[%debug_log "buffer does not end with EOL"];
match bbuf_top_last_EOL_opt with
| Some t -> begin
match context.C.tag with
| C.Texpr | C.Tin_stmt -> ()
| _ ->
if not pp_only && not directive_only then begin
[%debug_log "adding EOL..."];
buf#_add t
end
end
| None -> ()
end
in
let top_context = bbuf#top#get_context in
[%debug_log "[%d] top context: %s" lv (C.to_string top_context)];
let blist = ref [] in
try
if not (C.is_active top_context) then
raise C.Not_active;
[%debug_log "[%d] current context: %s" lv (C.to_string (context_stack#top))];
[%debug_log "context: %s" (C.to_string context)];
let parsers = partial_parser_selector context in
[%debug_log "%d parsers selected" (List.length parsers)];
let ignored = ref [] in
let multi_mode = ref false in
[%debug_log "nbranches=%d" bbuf#nbranches];
let incomplete_flag = ref true in
bbuf#iter_branch
(fun br ->
adjust_last ~context:top_context br;
br#remove_first_END_FRAGMENT;
let pcount = ref 1 in
try
List.iter
(fun _parser ->
try
self#recover key;
[%debug_log "trying to parse with the %s parser (context=%s)"
(Common.num_to_ordinal !pcount) (C.to_string context)];
let use_cache_incomplete = !pcount = 1 in
let partial = self#parse ~use_cache_incomplete _parser br in
let nnodes, nerrs = check_partial partial in
if nnodes > 0 && nerrs = 0 then
multi_mode := true;
if nnodes = 0 then begin
[%debug_log "empty branch"]
end
else if nnodes = nerrs then begin
[%debug_log "ignoring branch:"];
br#dump;
ignored := br::!ignored
end
else begin
br#to_partial ~context_opt:(Some context) partial;
blist := br::!blist
end;
[%debug_log "successfully parsed"];
incomplete_flag := false;
raise Exit
with
TB.Incomplete ->
[%debug_log "INCOMPLETE"];
incr pcount
) parsers;
[%debug_log "ignoring branch:"];
br#dump;
ignored := br::!ignored
with
Exit -> ()
);
[%debug_log "incomplete_flag=%B multi_mode=%B" !incomplete_flag !multi_mode];
if !incomplete_flag || !blist = [] || not !multi_mode then begin
[%debug_log "[%d] incomplete branch" lv];
raise TB.Incomplete
end
else begin
let get_open_section_list br =
let reverse_mode = ref false in
let cstack = Stack.create() in
begin
try
List.iter
(fun nd ->
match nd#label with
| Label.Stmt stmt -> begin
match Stmt.get_raw_stmt stmt with
| Stmt.DoStmt _ -> Stack.push (nd, OSTdo) cstack
| Stmt.ForallConstructStmt _ -> Stack.push (nd, OSTforall) cstack
| Stmt.IfThenStmt _ -> Stack.push (nd, OSTif) cstack
| Stmt.SelectCaseStmt _ -> Stack.push (nd, OSTselect) cstack
| Stmt.WhereConstructStmt _ -> Stack.push (nd, OSTwhere) cstack
| Stmt.DerivedTypeStmt _ -> Stack.push (nd, OSTtype) cstack
| Stmt.FunctionStmt _ -> Stack.push (nd, OSTfunction) cstack
| Stmt.SubroutineStmt _ -> Stack.push (nd, OSTsubroutine) cstack
| Stmt.ModuleStmt _ -> Stack.push (nd, OSTpu) cstack
| Stmt.SubmoduleStmt _ -> Stack.push (nd, OSTpu) cstack
| Stmt.BlockDataStmt _ -> Stack.push (nd, OSTpu) cstack
| Stmt.EndDoStmt _ -> begin
match Stack.top cstack with
| (_, OSTdo) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.EndForallStmt _ -> begin
match Stack.top cstack with
| (_, OSTforall) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.EndIfStmt _ -> begin
match Stack.top cstack with
| (_, OSTif) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.EndSelectStmt _ -> begin
match Stack.top cstack with
| (_, OSTselect) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.EndWhereStmt _ -> begin
match Stack.top cstack with
| (_, OSTwhere) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.EndTypeStmt _ -> begin
match Stack.top cstack with
| (_, OSTtype) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.EndFunctionStmt _ -> begin
match Stack.top cstack with
| (_, OSTfunction) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.EndSubroutineStmt _ -> begin
match Stack.top cstack with
| (_, OSTsubroutine) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.EndModuleStmt _ -> begin
match Stack.top cstack with
| (_, OSTpu) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.EndSubmoduleStmt _ -> begin
match Stack.top cstack with
| (_, OSTpu) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.EndBlockDataStmt _ -> begin
match Stack.top cstack with
| (_, OSTpu) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.EndStmt -> begin
match Stack.top cstack with
| (_, OSTpu)
| (_, OSTfunction)
| (_, OSTsubroutine) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| _ -> ()
end
| Label.PpBranchDo -> Stack.push (nd, OSTdo) cstack
| Label.PpBranchForall -> Stack.push (nd, OSTforall) cstack
| Label.PpBranchIf -> Stack.push (nd, OSTif) cstack
| Label.PpBranchSelect -> Stack.push (nd, OSTselect) cstack
| Label.PpBranchWhere -> Stack.push (nd, OSTwhere) cstack
| Label.PpBranchDerivedType -> Stack.push (nd, OSTtype) cstack
| Label.PpBranchFunction -> Stack.push (nd, OSTfunction) cstack
| Label.PpBranchSubroutine -> Stack.push (nd, OSTsubroutine) cstack
| Label.PpBranchPu -> Stack.push (nd, OSTpu) cstack
| Label.PpBranchEndDo -> begin
match Stack.top cstack with
| (_, OSTdo) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchEndForall -> begin
match Stack.top cstack with
| (_, OSTforall) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchEndIf -> begin
match Stack.top cstack with
| (_, OSTif) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchEndSelect -> begin
match Stack.top cstack with
| (_, OSTselect) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchEndWhere -> begin
match Stack.top cstack with
| (_, OSTwhere) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchEndType -> begin
match Stack.top cstack with
| (_, OSTtype) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchEndFunction -> begin
match Stack.top cstack with
| (_, OSTfunction) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchEndSubroutine -> begin
match Stack.top cstack with
| (_, OSTsubroutine) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchEndPu -> begin
match Stack.top cstack with
| (_, OSTpu) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| _ -> ()
) br#children
with
Stack.Empty ->
[%debug_log "reverse mode"];
reverse_mode := true;
Stack.clear cstack;
try
List.iter
(fun nd ->
match nd#label with
| Label.Stmt stmt -> begin
match Stmt.get_raw_stmt stmt with
| Stmt.EndDoStmt _ -> Stack.push (nd, OSTdo) cstack
| Stmt.EndForallStmt _ -> Stack.push (nd, OSTforall) cstack
| Stmt.EndIfStmt _ -> Stack.push (nd, OSTif) cstack
| Stmt.EndSelectStmt _ -> Stack.push (nd, OSTselect) cstack
| Stmt.EndWhereStmt _ -> Stack.push (nd, OSTwhere) cstack
| Stmt.EndTypeStmt _ -> Stack.push (nd, OSTtype) cstack
| Stmt.EndFunctionStmt _ -> Stack.push (nd, OSTfunction) cstack
| Stmt.EndSubroutineStmt _ -> Stack.push (nd, OSTsubroutine) cstack
| Stmt.EndStmt -> Stack.push (nd, OSTpu) cstack
| Stmt.DoStmt _ -> begin
match Stack.top cstack with
| (_, OSTdo) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.ForallConstructStmt _ -> begin
match Stack.top cstack with
| (_, OSTforall) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.IfThenStmt _ -> begin
match Stack.top cstack with
| (_, OSTif) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.SelectCaseStmt _ -> begin
match Stack.top cstack with
| (_, OSTselect) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.WhereConstructStmt _ -> begin
match Stack.top cstack with
| (_, OSTwhere) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.DerivedTypeStmt _ -> begin
match Stack.top cstack with
| (_, OSTtype) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.FunctionStmt _ -> begin
match Stack.top cstack with
| (_, OSTpu)
| (_, OSTfunction) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.SubroutineStmt _ -> begin
match Stack.top cstack with
| (_, OSTpu)
| (_, OSTsubroutine) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.ModuleStmt _ -> begin
match Stack.top cstack with
| (_, OSTpu) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.SubmoduleStmt _ -> begin
match Stack.top cstack with
| (_, OSTpu) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Stmt.BlockDataStmt _ -> begin
match Stack.top cstack with
| (_, OSTpu) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| _ -> ()
end
| Label.PpBranchEndDo -> Stack.push (nd, OSTdo) cstack
| Label.PpBranchEndForall -> Stack.push (nd, OSTforall) cstack
| Label.PpBranchEndIf -> Stack.push (nd, OSTif) cstack
| Label.PpBranchEndSelect -> Stack.push (nd, OSTselect) cstack
| Label.PpBranchEndWhere -> Stack.push (nd, OSTwhere) cstack
| Label.PpBranchEndType -> Stack.push (nd, OSTtype) cstack
| Label.PpBranchEndFunction -> Stack.push (nd, OSTfunction) cstack
| Label.PpBranchEndSubroutine -> Stack.push (nd, OSTsubroutine) cstack
| Label.PpBranchEndPu -> Stack.push (nd, OSTpu) cstack
| Label.PpBranchDo -> begin
match Stack.top cstack with
| (_, OSTdo) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchForall -> begin
match Stack.top cstack with
| (_, OSTforall) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchIf -> begin
match Stack.top cstack with
| (_, OSTif) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchSelect -> begin
match Stack.top cstack with
| (_, OSTselect) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchWhere -> begin
match Stack.top cstack with
| (_, OSTwhere) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchDerivedType -> begin
match Stack.top cstack with
| (_, OSTtype) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchFunction -> begin
match Stack.top cstack with
| (_, OSTpu)
| (_, OSTfunction) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchSubroutine -> begin
match Stack.top cstack with
| (_, OSTpu)
| (_, OSTsubroutine) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| Label.PpBranchPu -> begin
match Stack.top cstack with
| (_, OSTpu) -> ignore (Stack.pop cstack)
| _ -> raise TB.Incomplete
end
| _ -> ()
) (List.rev br#children)
with
Stack.Empty -> raise TB.Incomplete
end;
let open_section_list = ref [] in
Stack.iter (fun (n, t) -> open_section_list := (n, t)::!open_section_list) cstack;
!open_section_list, !reverse_mode
in
let mknd part br =
if part = [] then
[]
else
match br#label with
| Label.PpSectionIf _
| Label.PpSectionElif _
| Label.PpSectionIfdef _
| Label.PpSectionIfndef _
| Label.PpSectionElse
-> [new Ast.node ~lloc:(Ast.lloc_of_nodes part) ~children:part br#label]
| _ -> assert false
in
let mkspec = Partial.mkspec in
begin
match !blist with
| [br] -> begin
[%debug_log "[%d] selected branch: %s" lv (B.tag_to_string br#get_tag)];
let selected_lloc = Ast.lloc_of_locs tag_loc endif_loc in
let selected_loc = lloc_to_loc selected_lloc in
[%debug_log "selected_loc: %s" (Loc.to_string ~show_ext:true selected_loc)];
(!selected_buf)#receive_all br;
let need_eol =
match context.C.tag with
| C.Taction_stmt -> true
| C.Texpr -> bbuf_top_last_EOL_exists
| _ -> false
in
[%debug_log "need_eol=%B" need_eol];
if need_eol then
let _, ed = Loc.to_lexposs selected_loc in
(!selected_buf)#add (PB.make_qtoken EOL ed ed);
end
| _ -> begin
[%debug_log "[%d] merging selected branches..." lv];
let open_section_flag = ref false in
let open_construct_flag = ref false in
let get_tag spec = Partial.tag_of_spec spec in
let _children, length, tags =
List.fold_left
(fun (lst, len, tags) br ->
let br_as_list = br#get_list in
match br_as_list with
| [(tok, _)] -> begin
match tok with
| PROGRAM_UNIT(spec, nd) | SUBPROGRAM(spec, nd)
| INTERFACE_SPEC(spec, nd)
| CASE_BLOCK(spec, nd)
| SPEC_PART_CONSTRUCT(spec, nd)
| DATA_STMT_SET(spec, nd) | TYPE_SPEC(spec, nd)
| VARIABLE(spec, nd) | EXPR(spec, nd)
| DERIVED_TYPE_DEF_PART(spec, nd) ->
nd :: lst, (Partial.length_of_spec spec) + len, (get_tag spec) :: tags
| EXEC_PART_CONSTRUCT(spec, nd) ->
open_section_flag := true;
open_construct_flag := true;
nd :: lst, (Partial.length_of_spec spec) + len, (get_tag spec) :: tags
| STMT(spec, nd) ->
open_section_flag := true;
open_construct_flag := true;
nd :: lst, (Partial.length_of_spec spec) + len, C.Tstmts :: tags
| FUNCTION_HEAD(spec, nd) | SUBROUTINE_HEAD(spec, nd)
| PU_TAIL(spec, nd) ->
open_section_flag := true;
nd :: lst, (Partial.length_of_spec spec) + len, (get_tag spec) :: tags
| FUNCTION_STMT_HEAD(spec, nd) | SUBROUTINE_STMT_HEAD(spec, nd) ->
nd :: lst, (Partial.length_of_spec spec) + len, (get_tag spec) :: tags
| _ ->
begin %debug_block
[%debug_log "br#get_list:"];
List.iter
(fun (t, _) ->
[%debug_log " %s" (Token.rawtoken_to_string t)]
) br_as_list
end;
assert false
end
| [(tok, _);(EOL, _)] -> begin
match tok with
| ACTION_STMT(spec, nd) ->
nd :: lst, (Partial.length_of_spec spec) + len, C.Taction_stmt :: tags
| _ -> assert false
end
| _ -> begin
begin %debug_block
[%debug_log "br#get_list:"];
List.iter
(fun (t, _) ->
[%debug_log " %s" (Token.rawtoken_to_string t)]
) br_as_list
end;
assert false
end
) ([], 0, []) !blist
in
let children = List.rev _children in
[%debug_log "tag_loc: %s" (Loc.to_string ~show_ext:true tag_loc)];
[%debug_log "endif_loc: %s" (Loc.to_string ~show_ext:true endif_loc)];
let selected_lloc = Ast.lloc_of_locs tag_loc endif_loc in
let selected_loc = lloc_to_loc selected_lloc in
[%debug_log "selected_loc: %s" (Loc.to_string ~show_ext:true selected_loc)];
let tag =
match tags with
| [] -> context.C.tag
| [t] -> t
| t :: rest ->
if List.for_all (fun x -> x = t) rest then
t
else
context.C.tag
in
[%debug_log "tag of branch: %s" (C.tag_to_string tag)];
let basic_case() =
let tok, need_eol =
let node = new Ast.node ~lloc:selected_lloc ~children Label.PpBranch in
match tag with
| C.Tunknown -> assert false
| C.Ttoplevel -> PROGRAM_UNIT(mkspec ~length (), node), false
| C.Tprogram_unit -> PROGRAM_UNIT(mkspec ~length (), node), false
| C.Tspec__exec -> STMT(mkspec ~length (), node), false
| C.Tspecification_part -> SPEC_PART_CONSTRUCT(mkspec ~length (), node), false
| C.Texecution_part -> EXEC_PART_CONSTRUCT(mkspec ~length (), node), false
| C.Tsubprograms -> SUBPROGRAM(mkspec ~length (), node), false
| C.Tinterface_spec -> INTERFACE_SPEC(mkspec ~length (), node), false
| C.Tcase_block -> CASE_BLOCK(mkspec ~length (), node), false
| C.Tvariable -> VARIABLE(mkspec ~length (), node), bbuf_top_last_EOL_exists
| C.Texpr -> EXPR(mkspec ~length (), node), bbuf_top_last_EOL_exists
| C.Tstmts -> STMT(mkspec ~length (), node), false
| C.Tdata_stmt_sets -> DATA_STMT_SET(mkspec ~length (), node), false
| C.Ttype_spec -> TYPE_SPEC(mkspec ~length (), node), false
| C.Taction_stmt -> ACTION_STMT(mkspec ~length (), node), true
| C.Tderived_type_def_part -> DERIVED_TYPE_DEF_PART(mkspec ~length (), node), false
| C.Tonlys -> ONLY_(mkspec ~length (), node), false
| C.Tfunction_stmt_head -> FUNCTION_STMT_HEAD(mkspec ~length (), node), false
| C.Tsubroutine_stmt_head -> SUBROUTINE_STMT_HEAD(mkspec ~length (), node), false
| C.Tassignment_stmt -> assert false
| C.Ttype_declaration_stmt -> assert false
| C.Tfunction_stmt -> assert false
| C.Ttype_bound_proc_part -> assert false
| C.Tfunction_head -> assert false
| C.Tsubroutine_head -> assert false
| C.Tpu_tail -> assert false
| C.Tin_stmt -> assert false
in
[%debug_log "tok=%s need_eol=%B" (Token.rawtoken_to_string tok) need_eol];
(!selected_buf)#add (tok, selected_loc);
if need_eol then
let _, ed = Loc.to_lexposs selected_loc in
(!selected_buf)#add (PB.make_qtoken EOL ed ed);
in
if !open_section_flag then begin
let open_section_list_list = ref [] in
let br_count = ref (-1) in
let reverse_mode = ref false in
List.iter
(fun br ->
incr br_count;
[%debug_log "branch [%d]\n%s" !br_count (Printer.to_string br)];
let open_section_list, rev_mode = get_open_section_list br in
reverse_mode := rev_mode;
open_section_list_list := open_section_list :: !open_section_list_list;
) _children;
let crosscutting_open_sections_list =
let rec doit res list_list =
try
let cand, rest =
List.split
(List.map
(function
| [] -> raise Exit
| open_section::rest -> open_section, rest
) list_list)
in
match cand with
| [] -> res
| (_, tag)::_ ->
if List.for_all (fun (_, t) -> t = tag) cand then
doit (cand::res) rest
else
res
with
Exit -> res
in
List.rev (doit [] !open_section_list_list)
in
begin %debug_block
let c = ref 0 in
List.iter
(fun crosscutting_open_sections ->
[%debug_log "open sections[%d]: [%s]" !c
(Xlist.to_string (fun (_, t) -> open_section_tag_to_string t) ";" crosscutting_open_sections)];
incr c
) crosscutting_open_sections_list
end;
let open_section_tag_a =
Array.of_list
(List.map
(function
| [] -> assert false
| (_, t)::_ -> t
) crosscutting_open_sections_list)
in
let ncrosscutting = List.length crosscutting_open_sections_list in
[%debug_log "ncrosscutting=%d" ncrosscutting];
let crosscutting = ncrosscutting > 0 in
[%debug_log "crosscutting=%B" crosscutting];
if crosscutting then begin
let nbranches = List.length children in
[%debug_log "nbranches=%d" nbranches];
let part_matrix = Array.make_matrix (ncrosscutting + 1) nbranches [] in
let part_count = ref 0 in
let br_a =
let f =
if !reverse_mode then
fun br -> List.rev br#children
else
fun br -> br#children
in
Array.of_list (List.map f children)
in
List.iter
(fun crosscutting_open_sections ->
let open_sections = List.map (fun (n, _) -> n) crosscutting_open_sections in
let br_count = ref 0 in
List.iter2
(fun br open_section ->
[%debug_log "branch [%d]" !br_count];
[%debug_log "open_section: %s" (Label.to_string open_section#label)];
let _former, latter =
let stmts = br_a.(!br_count) in
[%debug_log "stmts: %s"
(Xlist.to_string (fun x -> Label.to_string x#label) "; " stmts)];
let dtv_and_open_sec =
match stmts with
| [stmt0; _] -> Label.is_pp_directive stmt0#label
| _ -> false
in
if dtv_and_open_sec then
[], stmts
else
let rec scan f = function
| [] -> f, []
| stmt::rest as l -> begin
[%debug_log "%s" (Label.to_string stmt#label)];
if stmt == open_section then
f, l
else
scan (stmt::f) rest
end
in
scan [] stmts
in
let former =
if !reverse_mode then
_former
else
List.rev _former
in
[%debug_log "part=%d br=%d" !part_count !br_count];
[%debug_log "former(part):\n%s" (Xlist.to_string (fun n -> n#to_string) "\n" former)];
[%debug_log "latter:\n%s\n" (Xlist.to_string (fun n -> n#to_string) "\n" latter)];
part_matrix.(!part_count).(!br_count) <- mknd former br;
br_a.(!br_count) <- latter;
incr br_count;
) children open_sections;
incr part_count;
) crosscutting_open_sections_list;
let _ =
let i = ref 0 in
List.iter
(fun br ->
[%debug_log "branch [%d]" !i];
let last =
if !reverse_mode then
List.rev br_a.(!i)
else
br_a.(!i)
in
[%debug_log "last part:\n%s\n" (Xlist.to_string (fun n -> n#to_string) "\n" last)];
part_matrix.(ncrosscutting).(!i) <- mknd last br;
incr i
) children
in
let former_children = List.flatten (Array.to_list part_matrix.(0)) in
let token_list_for_reverse_mode = ref [] in
[%debug_log "former children:\n%s\n"
(Xlist.to_string (fun n -> n#to_string) "\n" former_children)];
if former_children <> [] then begin
let fc =
former_children
in
let lloc = Ast.lloc_of_nodes fc in
let nd = new Ast.node ~lloc ~children:fc Label.PpBranch in
let tok =
if !open_construct_flag then
STMT(mkspec(), nd)
else
PROGRAM_UNIT(mkspec(), nd)
in
let qtoken = (tok, lloc#to_loc ~cache:(Some env#fname_ext_cache) ()) in
if !reverse_mode then
token_list_for_reverse_mode := qtoken :: !token_list_for_reverse_mode
else
(!selected_buf)#add qtoken
end;
for i = 1 to ncrosscutting do
let latter_children = List.flatten (Array.to_list part_matrix.(i)) in
let open_section_tag = open_section_tag_a.(i - 1) in
[%debug_log "[%d] latter children:\n%s\n"
i (Xlist.to_string (fun n -> n#to_string) "\n" latter_children)];
if latter_children <> [] then begin
let lc =
latter_children
in
let lab =
match open_section_tag with
| OSTnone -> assert false
| OSTif -> Label.PpBranchIf
| OSTdo -> Label.PpBranchDo
| OSTselect -> Label.PpBranchSelect
| OSTforall -> Label.PpBranchForall
| OSTwhere -> Label.PpBranchWhere
| OSTtype -> Label.PpBranchDerivedType
| OSTfunction -> Label.PpBranchFunction
| OSTsubroutine -> Label.PpBranchSubroutine
| OSTpu -> Label.PpBranchPu
in
let lloc = Ast.lloc_of_nodes lc in
let nd = new Ast.node ~lloc ~children:lc lab in
let tok =
if !reverse_mode then
match open_section_tag with
| OSTnone -> assert false
| OSTif -> END_IF_STMT(mkspec(), nd)
| OSTdo -> END_DO_STMT(mkspec(), nd)
| OSTselect -> END_SELECT_STMT(mkspec(), nd)
| OSTforall -> END_FORALL_STMT(mkspec(), nd)
| OSTwhere -> END_WHERE_STMT(mkspec(), nd)
| OSTtype -> END_TYPE_STMT(mkspec(), nd)
| OSTfunction -> assert false
| OSTsubroutine -> assert false
| OSTpu -> PU_TAIL(mkspec(), nd)
else
match open_section_tag with
| OSTnone -> assert false
| OSTif -> IF_THEN_STMT(mkspec(), nd)
| OSTdo -> DO_STMT(mkspec(), nd)
| OSTselect -> SELECT_CASE_STMT(mkspec(), nd)
| OSTforall -> FORALL_CONSTRUCT_STMT(mkspec(), nd)
| OSTwhere -> WHERE_CONSTRUCT_STMT(mkspec(), nd)
| OSTtype -> DERIVED_TYPE_STMT(mkspec(), nd)
| OSTfunction -> FUNCTION_HEAD(mkspec(), nd)
| OSTsubroutine -> SUBROUTINE_HEAD(mkspec(), nd)
| OSTpu -> assert false
in
let qtoken =
tok, lloc#to_loc ~cache:(Some env#fname_ext_cache) ()
in
if !reverse_mode then
token_list_for_reverse_mode := qtoken :: !token_list_for_reverse_mode
else
(!selected_buf)#add qtoken
end
done;
if !reverse_mode then
List.iter
(fun qtoken ->
(!selected_buf)#add qtoken
) !token_list_for_reverse_mode
end
else begin
basic_case()
end
end
else begin
basic_case()
end
end
end;
begin %debug_block
[%debug_log "selected buffer:"];
!selected_buf#dump;
end;
List.iter
(fun br ->
try
br#dump;
let loc = br#get_loc in
[%debug_log "[%d] ignored region: %s" lv (Loc.to_string loc)];
if lv = 0 then
ignored_regions := loc :: !ignored_regions
with
TB.Empty -> ()
) !ignored;
!selected_buf, !ignored_regions
end
with
TB.Incomplete | C.Not_active -> begin
context_stack#resume;
[%debug_log "[%d] selecting (the largest) branch..." lv];
begin
try
bbuf#iter_branch
(fun buf ->
[%debug_log "[%d] checking %s" lv (B.tag_to_string buf#get_tag)];
adjust_last buf;
match buf#get_tag with
| B.Tifdef(id, _) -> begin
[%debug_log "size=%d" buf#total_length];
[%debug_log "id=%s" id];
if id <> "_OPENMP" then
if (!selected_buf)#total_length < buf#total_length then begin
selected_buf := buf;
end
end
| B.Tifndef(id, _) -> begin
[%debug_log "size=%d" buf#total_length];
[%debug_log "id=%s" id];
if env#macrotbl#is_unconditionally_defined id then begin
[%debug_log "%s is unconditionally defined" id]
end
else
if (!selected_buf)#total_length < buf#total_length then begin
selected_buf := buf
end
end
| B.Tif(cond, _) | B.Telif(cond, _, _) -> begin
let _ = cond in
[%debug_log "size=%d" buf#total_length];
[%debug_log "cond=%s" cond];
if (!selected_buf)#total_length < buf#total_length then begin
selected_buf := buf;
end
end
| B.Telse(_, _) -> begin
[%debug_log "size=%d" buf#total_length];
if (!selected_buf)#total_length < buf#total_length then begin
selected_buf := buf
end
end
| t ->
let _ = t in
[%debug_log "invalid branch tag: %s" (B.tag_to_string t)];
assert false
)
with
Exit -> ()
end;
bbuf#iter_branch
(fun buf ->
if lv = 0 && buf != !selected_buf then begin
[%debug_log "ignored buf:"];
buf#dump;
if buf#total_length > 0 then
try
ignored_regions := buf#get_loc :: !ignored_regions
with
TB.Empty -> ()
end
);
[%debug_log "[%d] selected: %s" lv (B.tag_to_string !selected_buf#get_tag)];
self#recover key;
[%debug_log "finished"];
!selected_buf, !ignored_regions
end
method parse ?(use_cache_incomplete=true) (_parser : TB.partial_parser) (tokenbuf : TBF.c) =
let cache =
if use_cache_incomplete then
tokenbuf#ast_cache
else
match tokenbuf#ast_cache with
| TB.Rincomplete -> TB.Runknown
| r -> r
in
[%debug_log "LEVEL=%d" lv];
[%debug_log "branch tag: %s" (B.tag_to_string tokenbuf#get_tag)];
match cache with
| TB.Rcomplete p ->
[%debug_log "[%d] using cached value (complete)" lv];
p
| TB.Rincomplete ->
[%debug_log "[%d] using cached value (incomplete)" lv];
raise TB.Incomplete
| TB.Runknown ->
[%debug_log "[%d] parsing..." lv];
tokenbuf#dump;
let tokensrc = tokenbuf#get_tokensrc in
let ppbuf =
new buffer
(lv+1) partial_parser_selector (tokensrc :> Tokensource.c)
in
if C.is_program_unit tokenbuf#get_context then
ppbuf#exit_context;
let scanner() =
try
let _qtoken = ppbuf#pp () in
let qtoken =
match Token.qtoken_to_rawtoken _qtoken with
| PP_DEFINE__IDENT__BODY(_, body) -> begin
begin
match tokenbuf#get_tag with
| B.Tifndef(_, _) -> Macro.body_clear_conditional body
| _ -> ()
end;
_qtoken
end
| PP_UNDEF__IDENT _ -> _qtoken
| _ -> begin
[%debug_log "[%d] calling TBF.hack_token" lv];
TBF.hack_token (tokensrc :> Tokensource.c) _qtoken;
end
in
[%debug_log "[%d] ---------> %s" lv (Token.qtoken_to_string qtoken)];
PB.qtoken_to_token qtoken
with
| Tokensource.Empty ->
if tokensrc#eop_flag then begin
[%debug_log "raising End_of_file"];
raise End_of_file
end
else begin
let _, ed = Loc.to_lexposs tokensrc#get_last_loc in
begin %debug_block
[%debug_log "[%d] last token: %s[%s]" lv
(Token.rawtoken_to_string tokensrc#get_last_rawtok)
(Loc.to_string tokensrc#get_last_loc)];
[%debug_log "[%d] EOP[%s]" lv
(Loc.to_string (Astloc.of_lexposs ed ed))];
end;
let eop = PB.make_token EOP ed ed in
tokensrc#set_eop_flag;
eop
end
in
env#set_partial_parsing_flag;
try
let p = _parser scanner in
ppbuf#finish;
self#add_ignored_regions ppbuf#ignored_regions;
env#clear_partial_parsing_flag;
[%debug_log "[%d] result: PARSED" lv];
Partial.set_length p tokenbuf#total_length;
tokenbuf#set_ast_cache (TB.Rcomplete p);
p
with
exn ->
let _ = exn in
ppbuf#finish;
[%debug_log "[%d] result: FAILED (raised exception: %s)" lv
(Printexc.to_string exn)];
env#clear_partial_parsing_flag;
tokenbuf#set_ast_cache TB.Rincomplete;
raise TB.Incomplete
method finish =
context_stack#unregister_push_callback;
context_stack#unregister_pop_callback;
context_stack#unregister_activate_callback;
context_stack#unregister_deactivate_callback
initializer
[%debug_log "init: LEVEL=%d" lv];
if new_context then begin
self#enter_context;
context_buf#current#set_context (Context.toplevel());
self#enter_context;
context_buf#current#set_context (Context.program_unit());
self#enter_context;
context_buf#current#set_context (Context.spec__exec());
end;
context_stack#register_push_callback
(fun c ->
[%debug_log "[%d] push_callback: %s" lv (C.to_string c)];
if self#in_branch then begin
()
end
else begin
self#checkpoint top_key;
self#enter_context;
self#set_context c
end
);
context_stack#register_pop_callback
(fun c ->
[%debug_log "[%d] pop_callback: %s" lv (C.to_string c)];
if self#in_branch then begin
()
end
else begin
context_stack#activate_top;
self#checkpoint top_key;
self#exit_context;
self#set_context c
end
);
context_stack#register_activate_callback
(fun c ->
[%debug_log "[%d] activate_callback: %s" lv (C.to_string c)];
if not self#in_branch then begin
self#checkpoint top_key;
self#clear_context;
self#set_context c
end
);
context_stack#register_deactivate_callback
(fun c ->
[%debug_log "[%d] deactivate_callback: %s" lv (C.to_string c)];
if not self#in_branch then begin
self#checkpoint top_key;
self#clear_context;
self#set_context c
end
)
end
end
]