Source file tokenbuffer.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
[%%prepare_logger]
module Xlist = Diffast_misc.Xlist
module Xqueue = Diffast_misc.Xqueue
module Xstring = Diffast_misc.Xstring
open Common
module Loc = Ast.Loc
module Aux = Parser_aux
module L = Label
module C = Context
let sprintf = Printf.sprintf
let lexposs_to_offsets (st_pos, ed_pos) =
st_pos.Lexing.pos_cnum, ed_pos.Lexing.pos_cnum
type branch_tag =
| BTifdef of string * Loc.t
| BTifndef of string * Loc.t
| BTelsif of string
| BTelse
| BTcontext
| BTselected
| BTdummy
let branch_tag_to_string = function
| BTifdef(s, loc) -> sprintf "BTifdef:%s[%s]" s (Loc.to_string ~short:true loc)
| BTifndef(s, loc) -> sprintf "BTifndef:%s[%s]" s (Loc.to_string ~short:true loc)
| BTelsif s -> "BTelsif:"^s
| BTelse -> "BTelse"
| BTcontext -> "BTcontext"
| BTselected -> "BTselected"
| BTdummy -> "BTdummy"
let loc_of_branch_tag = function
| BTifdef(_, loc)
| BTifndef(_, loc) -> loc
| btag -> raise (Invalid_argument (sprintf "Tokenbuffer.loc_of_branch_tag: %s" (branch_tag_to_string btag)))
[%%capture_path
module F (Stat : Aux.STATE_T) = struct
module Tokens = Tokens_
module Tok = Token.F (Stat)
module U = Ulexer.F (Stat)
module A = Aux.F (Stat)
open Tokens
open Stat
let merge_locs = PB.merge_locs ~cache:(Some env#fname_ext_cache)
let make_branch_tag iftok idtok loc =
let id =
match idtok with
| IDENTIFIER s -> s
| tok ->
raise (Invalid_argument
("Tokenbuffer.make_branch_tag: invalid idtoken: "^(Token.rawtoken_to_string tok)))
in
match iftok with
| PP_IFDEF -> BTifdef(id, loc)
| PP_IFNDEF -> BTifndef(id, loc)
| PP_ELSIF -> BTelsif id
| PP_ELSE -> BTelse
| tok ->
raise (Invalid_argument
("Tokenbuffer.make_branch_tag: invalid iftoken: "^(Token.rawtoken_to_string tok)))
type token = Tokens.token * Lexing.position * Lexing.position
type partial_parser = (token, Ast.partial) MenhirLib.Convert.revised
exception Not_active
exception Buffer_empty
exception Incomplete
type parse_result =
| Rcomplete of Ast.partial
| Rincomplete
| Runknown
let peek_nth queue n =
let q = ref [] in
Queue.iter (fun x -> q := x :: !q) queue;
try
List.nth (List.rev !q) (n - 1)
with
_ -> raise Buffer_empty
let check_partial partial =
let nodes = Ast.nodes_of_partial partial in
let nnodes = ref 0 in
let nerrs = ref 0 in
List.iter
(fun node ->
Ast.visit
(fun nd ->
incr nnodes;
if L.is_error nd#label then
incr nerrs
) node
) nodes;
[%debug_log "nnodes=%d, nerrs=%d" !nnodes !nerrs];
!nnodes, !nerrs
class buffer_base = object (self : 'self)
val mutable queue : Token.qtoken_t Xqueue.c = new Xqueue.c
val mutable ast_cache = Runknown
method get_list_rev = queue#fold (fun l x -> x :: l) []
method get_list = List.rev self#get_list_rev
method _set_queue q = queue <- q
method _raw = queue
method clear = queue#clear
method _add qtoken =
ast_cache <- Runknown;
queue#add qtoken
method add qtoken =
self#_add qtoken
method iter f =
queue#iter f
method size = queue#length
method total_size =
let sz = ref 0 in
self#iter
(fun (tok, _) ->
sz := !sz + (Tok.size tok)
);
!sz
method is_empty = queue#is_empty
method peek =
try
queue#peek
with
Xqueue.Empty -> raise Buffer_empty
method peek_nth n =
try
queue#peek_nth n
with
_ -> raise Buffer_empty
method take =
try
queue#take
with
Xqueue.Empty -> raise Buffer_empty
method receive_all : 'self -> unit =
fun buf ->
let q = buf#_raw in
if q#length > 0 then
ast_cache <- Runknown;
q#transfer queue
method dump =
[%debug_log "%s" self#to_string]
method to_string =
let buf = Buffer.create 0 in
self#iter
(fun qtok ->
Buffer.add_string buf
(sprintf "%s\n" (Token.qtoken_to_string qtok))
);
Buffer.contents buf
method get_loc =
try
let qtoken = queue#peek in
let _, st = qtoken in
[%debug_log "start token: %s" (Token.qtoken_to_string qtoken)];
let ed = ref st in
self#iter (fun (_, loc) -> ed := loc);
merge_locs st !ed
with
Xqueue.Empty -> raise Buffer_empty
method get_last =
if self#size > 0 then
let last = ref (EOF, Loc.dummy) in
queue#iter (fun x -> last := x);
!last
else
raise Buffer_empty
method parse_by : ?cache:bool -> partial_parser -> Ast.partial =
fun ?(cache=true) _parser ->
[%debug_log "called"];
match ast_cache with
| Rcomplete p ->
[%debug_log "using cached value (complete)"];
p
| Rincomplete ->
[%debug_log "using cached value (incomplete)"];
raise Incomplete
| Runknown ->
let copied = queue#copy in
let last_tok = ref EOP in
let last_loc = ref Loc.dummy in
let eop_flag = ref false in
let scanner() =
try
let qtoken = copied#take in
let tok, loc = qtoken in
[%debug_log "---> %s" (Token.qtoken_to_string qtoken)];
let st, ed = Loc.to_lexposs loc in
last_tok := tok;
last_loc := loc;
tok, st, ed
with
| Xqueue.Empty ->
if !eop_flag then
raise End_of_file
else begin
let _, ed = Loc.to_lexposs !last_loc in
eop_flag := true;
begin %debug_block
[%debug_log "last token: %s" (Token.qtoken_to_string (!last_tok, !last_loc))];
[%debug_log "EOP[%s]" (Loc.to_string ~short:true (Loc.of_lexposs ed ed))];
end;
EOP, ed, ed
end
in
env#set_partial_parsing_flag;
try
let p = _parser scanner in
env#clear_partial_parsing_flag;
[%debug_log "PARSED"];
if cache then
ast_cache <- Rcomplete p;
p
with
_ ->
[%debug_log "FAILED"];
env#clear_partial_parsing_flag;
if cache then
ast_cache <- Rincomplete;
raise Incomplete
end
class buffer (btag : branch_tag) = object (self : 'self)
inherit buffer_base
val mutable context = C.unknown()
method set_context c =
[%debug_log "[%s]: %s" (branch_tag_to_string btag) (C.to_string c)];
context <- c
method get_context = context
method get_tag = btag
method get_copy =
[%debug_log "called"];
let copy = new buffer btag in
copy#_set_queue queue#copy;
copy#set_context context;
copy
method! parse_by : ?cache:bool -> partial_parser -> Ast.partial =
fun ?(cache=true) _parser ->
[%debug_log "called"];
match ast_cache with
| Rcomplete p ->
[%debug_log "using cached value (complete)"];
p
| Rincomplete ->
[%debug_log "using cached value (incomplete)"];
raise Incomplete
| Runknown ->
let copied = queue#copy in
let last_tok = ref EOP in
let last_loc = ref Loc.dummy in
let prev_tok = ref EOP in
let prev_loc = ref Loc.dummy in
let eop_flag = ref false in
let scanner() =
try
let qtoken = copied#take in
let tok, loc = qtoken in
[%debug_log "%s" (Token.qtoken_to_string qtoken)];
let st, ed = Loc.to_lexposs loc in
prev_tok := !last_tok;
prev_loc := !last_loc;
last_tok := tok;
last_loc := loc;
context_stack#deactivate_top;
if env#context_enter_flag then begin
env#clear_context_enter_flag;
env#set_last_active_ofss (Loc.to_offsets !last_loc)
end;
if env#context_activate_flag then begin
env#clear_context_activate_flag;
env#set_last_active_ofss (Loc.to_offsets !prev_loc)
end;
tok, st, ed
with
| Xqueue.Empty ->
if !eop_flag then
raise End_of_file
else begin
let _, ed = Loc.to_lexposs !last_loc in
eop_flag := true;
begin %debug_block
[%debug_log "last token: %s" (Token.qtoken_to_string (!last_tok, !last_loc))];
[%debug_log "EOP[%s]" (Loc.to_string ~short:true (Loc.of_lexposs ed ed))];
end;
EOP, ed, ed
end
in
env#set_partial_parsing_flag;
try
let p = _parser scanner in
env#clear_partial_parsing_flag;
[%debug_log "[%s] parsed!" (branch_tag_to_string btag)];
if cache then
ast_cache <- Rcomplete p;
p
with
_ ->
[%debug_log "[%s] failed to parse!" (branch_tag_to_string btag)];
env#clear_partial_parsing_flag;
if cache then
ast_cache <- Rincomplete;
raise Incomplete
method to_partial partial =
match partial with
| Ast.Pdescription_list ds ->
self#clear;
List.iter (fun d -> self#add (Tok.of_description d)) ds
| Ast.Pmodule_item_list mis ->
self#clear;
List.iter (fun mi -> self#add (Tok.of_module_item mi)) mis
| Ast.Pgenerate_item_list gis ->
self#clear;
List.iter (fun gi -> self#add (Tok.of_generate_item gi)) gis
| Ast.Pblock_decl_stmt_list bdss ->
self#clear;
List.iter
(fun bds ->
let tok =
if Ast.is_block_item_declaration bds then
Tok.of_block_item_declaration bds
else
if Ast.is_stmt bds then
Tok.of_stmt bds
else begin
Printer.dump bds;
assert false
end
in
self#add tok
) bdss
| Ast.Pcase_item_list cis ->
self#clear;
List.iter (fun ci -> self#add (Tok.of_case_item ci)) cis
| Ast.Pcase_inside_item_list ciis ->
self#clear;
List.iter (fun cii -> self#add (Tok.of_case_inside_item cii)) ciis
| Ast.Pcellpin_list _ -> begin
let rawtok, loc = self#get_last in
match rawtok with
| COMMA -> ()
| _ ->
let _, ed = Loc.to_lexposs loc in
let ed_ = Loc.incr_lexpos ed in
self#add (PB.make_qtoken COMMA ed_ ed_)
end
| Ast.Plist_of_ports _ -> begin
let rawtok, loc = self#get_last in
match rawtok with
| COMMA -> ()
| _ ->
let _, ed = Loc.to_lexposs loc in
let ed_ = Loc.incr_lexpos ed in
self#add (PB.make_qtoken COMMA ed_ ed_)
end
| Ast.Pexpr e -> begin
self#clear;
self#add (Tok.of_expr e)
end
end
class branching_buffer btag = object (self)
val stack = Stack.create()
val mutable context = C.unknown()
method get_tag = btag
method get_context = context
method branch_tag = btag
method set_context c =
[%debug_log "[%s]: %s" (branch_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 "called"];
let buf = new buffer btag in
buf#set_context context;
Stack.push buf stack
method add qtoken =
self#top#add qtoken
method iter_branch f =
Stack.iter f stack
method iter f = self#top#iter f
method select_branches partial_parser_selector =
begin %debug_block
[%debug_log "context: %s" (C.to_string context)];
[%debug_log "branches:"];
self#iter_branch
(fun buf ->
[%debug_log "%s [%s] (ntokens=%d) [%s]"
(branch_tag_to_string buf#get_tag)
(try Loc.to_string ~short:true buf#get_loc with _ -> "-")
buf#size
(C.to_string buf#get_context)];
buf#dump
);
end;
let key = loc_of_branch_tag btag in
context_stack#recover key;
env#recover key;
let selected_buf = ref (new buffer BTselected) in
(!selected_buf)#set_context context;
let ignored_regions = ref [] in
context_stack#suspend;
try
if not self#top#get_context.C.is_active then
raise Not_active;
[%debug_log "current context: %s" (C.to_string (context_stack#top))];
let _parser = partial_parser_selector context in
let blist = ref [] in
let ignored = ref [] in
let multi_mode = ref false in
self#iter_branch
(fun br ->
try
let partial = br#parse_by _parser in
let nnodes, nerrs = check_partial partial in
if nnodes > 0 && nerrs = 0 then
multi_mode := true;
if nnodes = nerrs then
ignored := br::!ignored
else begin
br#to_partial partial;
blist := br::!blist
end
with Incomplete ->
ignored := br::!ignored
);
context_stack#resume;
if !blist = [] || not !multi_mode then begin
[%debug_log "all branches are incomplete"];
raise Incomplete
end
else begin
[%debug_log "merging selected branches..."];
List.iter
(fun br ->
[%debug_log "selected branch: %s" (branch_tag_to_string br#get_tag)];
(!selected_buf)#receive_all br
) !blist;
List.iter
(fun br ->
try
let loc = br#get_loc in
[%debug_log "ignored region: %s" (Loc.to_string ~short:true loc)];
ignored_regions := loc :: !ignored_regions
with Buffer_empty -> ()
) !ignored;
!selected_buf, !ignored_regions
end
with
Incomplete | Not_active -> begin
context_stack#resume;
[%debug_log "selecting the largest or specified branch..."];
begin
self#iter_branch
(fun buf ->
[%debug_log "checking %s" (branch_tag_to_string buf#get_tag)];
match buf#get_tag with
| BTifdef(id, _) | BTelsif id -> begin
try
if env#macro_defined id then
selected_buf := buf
with Not_found ->
if (!selected_buf)#total_size < buf#total_size then
selected_buf := buf
end
| BTifndef(id, _) -> begin
try
if not (env#macro_defined id) then
selected_buf := buf
with Not_found ->
if (!selected_buf)#total_size < buf#total_size then
selected_buf := buf
end
| BTelse ->
if (!selected_buf)#total_size < buf#total_size then
selected_buf := buf
| _ -> assert false
)
end;
self#iter_branch
(fun buf ->
if buf != !selected_buf then
try
ignored_regions := (buf#get_loc) :: !ignored_regions
with Buffer_empty -> ()
);
[%debug_log "largest: %s" (branch_tag_to_string !selected_buf#get_tag)];
!selected_buf, !ignored_regions
end
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 c (partial_parser_selector : C.t -> partial_parser) = object (self)
val mutable last_token = EOF, Loc.dummy_lexpos, Loc.dummy_lexpos
val mutable prev_token = EOF, Loc.dummy_lexpos, Loc.dummy_lexpos
val mutable last_taken = EOL
val mutable _last_taken = EOL
val mutable last_taken_loc = Loc.dummy
val mutable _last_taken_loc = Loc.dummy
val mutable last_taken_no_pp_branch = EOL
val mutable _last_taken_no_pp_branch = EOL
val mutable last_taken_no_pp_branch_loc = Loc.dummy
val mutable _last_taken_no_pp_branch_loc = Loc.dummy
val prebuf : (Token.qtoken_t Queue.t) = Queue.create()
val stack = Stack.create()
val context_buf = new buffer BTcontext
val mutable serialized = new buffer BTdummy
val mutable is_serialized = false
val mutable in_undef = false
val mutable ignored_regions : Loc.t list = []
method partial_parser_selector = partial_parser_selector
method size_of_serialized = serialized#size
method get_last_taken =
[%debug_log "%s" (Token.rawtoken_to_string last_taken)];
last_taken
method set_last_taken tok =
last_taken <- _last_taken;
_last_taken <- tok
method get_last_taken_loc =
[%debug_log "%s" (Loc.to_string last_taken_loc)];
last_taken_loc
method set_last_taken_loc loc =
last_taken_loc <- _last_taken_loc;
_last_taken_loc <- loc
method get_last_taken_no_pp_branch =
[%debug_log "%s" (Token.rawtoken_to_string last_taken_no_pp_branch)];
last_taken_no_pp_branch
method _get_last_taken_no_pp_branch =
[%debug_log "%s" (Token.rawtoken_to_string _last_taken_no_pp_branch)];
_last_taken_no_pp_branch
method set_last_taken_no_pp_branch tok =
last_taken_no_pp_branch <- _last_taken_no_pp_branch;
[%debug_log "last_taken_no_pp_branch --> %s"
(Token.rawtoken_to_string last_taken_no_pp_branch)];
_last_taken_no_pp_branch <- tok
method get_last_taken_no_pp_branch_loc =
[%debug_log "%s" (Loc.to_string last_taken_no_pp_branch_loc)];
last_taken_no_pp_branch_loc
method set_last_taken_no_pp_branch_loc loc =
last_taken_no_pp_branch_loc <- _last_taken_no_pp_branch_loc;
_last_taken_no_pp_branch_loc <- loc
method get_last_token = last_token
method get_last_rawtok = Tok.to_rawtoken last_token
method get_last_offsets = lexposs_to_offsets (Tok.to_lexposs last_token)
method set_last_token token = last_token <- token
method get_prev_token = prev_token
method get_prev_rawtok = Tok.to_rawtoken prev_token
method get_prev_offsets = lexposs_to_offsets (Tok.to_lexposs prev_token)
method set_prev_token token = prev_token <- token
method prebuf_add x =
[%debug_log "%s" (Token.qtoken_to_string x)];
Queue.add x prebuf
method prebuf_take =
try
let x = Queue.take prebuf in
[%debug_log "%s" (Token.qtoken_to_string x)];
x
with
Queue.Empty -> raise Buffer_empty
method prebuf_peek =
try
Queue.peek prebuf
with
Queue.Empty -> raise Buffer_empty
method prebuf_peek_nth n =
peek_nth prebuf n
method prebuf_size = Queue.length prebuf
method add_to_context qtoken =
[%debug_log "added %s" (Token.qtoken_to_string qtoken)];
context_buf#_add qtoken
method set_context c =
[%debug_log "%s" (C.to_string c)];
context_buf#set_context (C.copy_context c)
method get_context = context_buf#get_context
method clear_context =
[%debug_log "called"];
context_buf#clear;
context_buf#set_context (C.copy_context context_stack#top)
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 "bbuf[%s] context=%s"
(branch_tag_to_string bbuf#branch_tag) (C.to_string cnt)];
if not (C.is_unknown cnt) && cnt.C.is_active then begin
start := Some (bbuf#top, loc_of_branch_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 "loc: %s"
(try Loc.to_string ~short:true cpy#get_loc with Buffer_empty -> "-")];
(cpy : buffer), key
method is_serialized = is_serialized
method in_undef = in_undef
method begin_undef = in_undef <- true
method end_undef = in_undef <- false
method size = Stack.length stack
method in_branch = not (Stack.is_empty stack)
method current_bbuf = Stack.top stack
method is_context_empty = context_buf#is_empty
method begin_branch ~open_if btag =
[%debug_log "open_if=%B, branch depth: %d" open_if self#size];
let bbuf = new branching_buffer btag in
if not self#in_branch then begin
let c = context_buf#get_context in
if not context_buf#is_empty then begin
begin %debug_block
[%debug_log "location of context buffer: %s"
(Loc.to_string ~short:true context_buf#get_loc)];
[%debug_log "context of context buffer: %s"
(C.to_string c)];
[%debug_log "parsing context buffer..."];
end;
context_stack#checkpoint Aux.top_key;
env#checkpoint Aux.top_key;
begin
try
if open_if then
raise Incomplete;
ignore (context_buf#parse_by (partial_parser_selector c));
with
Incomplete | Not_found -> [%debug_log "failed to parse context_buf"];
end;
if open_if then
context_stack#deactivate_top_no_delay;
bbuf#set_context (C.copy_context context_stack#top);
self#clear_context;
end
else begin
[%debug_log "context buffer is empty"];
bbuf#set_context (C.copy_context c)
end
end
else begin
[%debug_log "in branch"];
try
let branch_context, key = self#get_branch_context in
let c = branch_context#get_context in
if not branch_context#is_empty then begin
begin %debug_block
[%debug_log "branch context buffer: %s (context: %s)"
(Loc.to_string ~short:true branch_context#get_loc) (C.to_string c)];
[%debug_log "parsing branch context buffer..."];
end;
context_stack#recover key;
env#recover key;
begin
try
if open_if then
raise Incomplete;
ignore (branch_context#parse_by (partial_parser_selector c))
with
Incomplete | Not_found ->
[%debug_log "failed to parse branch context"]
end;
if open_if then
context_stack#deactivate_top_no_delay;
bbuf#set_context (C.copy_context context_stack#top);
end
else begin
[%debug_log "branch context is empty"];
bbuf#set_context (C.copy_context c)
end
with
Not_found -> bbuf#set_context (C.copy_context context_stack#top)
end;
[%debug_log "[%s][%s]: current depth=%d (current context: %s)"
(branch_tag_to_string btag) (C.to_string bbuf#get_context) (self#size)
(C.to_string context_stack#top)];
let bcont = bbuf#get_context in
if not (C.is_unknown bcont) then begin
let key = loc_of_branch_tag btag in
context_stack#checkpoint key;
env#checkpoint key
end;
Stack.push bbuf stack
method end_branch ~open_if =
[%debug_log "open_if=%B, branch depth=%d" open_if (self#size)];
let check_buf (buf : buffer) =
[%debug_log "checking %s..." (branch_tag_to_string buf#get_tag)];
try
let _parser = partial_parser_selector buf#get_context in
context_stack#suspend;
try
if not buf#get_context.C.is_active then
raise Not_active;
let partial = buf#parse_by _parser in
context_stack#resume;
if not open_if then
buf#to_partial partial;
context_stack#activate_top_no_delay;
with
Incomplete | Not_active ->
context_stack#resume;
context_stack#deactivate_top_no_delay
with
Not_found -> ()
in
let bbuf = Stack.pop stack in
let selected, ignored =
bbuf#select_branches partial_parser_selector
in
ignored_regions <- ignored_regions @ ignored;
try
let buf = self#current_bbuf#top in
[%debug_log "transferring selected branch to %s..." (branch_tag_to_string buf#get_tag)];
buf#receive_all selected;
check_buf buf
with
Stack.Empty ->
is_serialized <- true;
serialized <- selected;
check_buf serialized;
context_stack#recover Aux.top_key;
env#recover Aux.top_key;
method peek =
let tok, loc = serialized#peek in
tok, loc
method peek_nth n =
serialized#peek_nth n
method take =
let tok, loc = serialized#take in
if serialized#is_empty then
is_serialized <- false;
tok, loc
method add qtoken =
(self#current_bbuf)#add qtoken
method new_branch btag =
[%debug_log "btag=%s" (branch_tag_to_string btag)];
(self#current_bbuf)#new_branch btag
method dump_top =
(self#current_bbuf)#dump
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 ignored_offsets = List.map Loc.to_offsets ignored_regions
initializer
context_buf#set_context (C.toplevel());
context_stack#register_push_callback
(fun c ->
[%debug_log "push_callback: called"];
if not self#in_branch then begin
context_stack#checkpoint Aux.top_key;
env#checkpoint Aux.top_key;
self#clear_context;
self#set_context c
end
);
context_stack#register_pop_callback
(fun terminate_surrounding_construct c ->
[%debug_log "pop_callback: called"];
if not self#in_branch then begin
if terminate_surrounding_construct then begin
context_stack#activate_top;
context_stack#checkpoint Aux.top_key;
env#checkpoint Aux.top_key;
self#clear_context;
self#set_context c
end
end
);
context_stack#register_activate_callback
(fun c ->
[%debug_log "activate_callback: called"];
if not self#in_branch then begin
context_stack#checkpoint Aux.top_key;
env#checkpoint Aux.top_key;
self#clear_context;
self#set_context c
end
);
context_stack#register_deactivate_callback
(fun c ->
[%debug_log "deactivate_callback: called"];
if not self#in_branch then begin
context_stack#checkpoint Aux.top_key;
env#checkpoint Aux.top_key;
self#clear_context;
self#set_context c
end
)
end
let find_macro id =
try
env#find_macro id
with
Not_found ->
env#lex_find_macro id
let hide_macro id =
env#macrotbl#hide id;
env#lex_macrotbl#hide id;
fun () ->
env#macrotbl#expose id;
env#lex_macrotbl#expose id
let rec expand_line _ st ed line =
[%debug_log "line=^%s$" line];
let ulexbuf = U.from_string line in
let scanner () = U.token ulexbuf in
let buf = new buffer_base in
begin
try
while true do
try
let tok, _ = scanner() in
[%debug_log "tok=%s" (Token.rawtoken_to_string tok)];
match tok with
| EOF -> raise Sedlexing.MalFormed
| PP_MACRO_ID id -> begin
try
match find_macro id with
| Macro.Object line0 ->
let buf0 = expand_line line0.Macro.ln_loc st ed line0.Macro.ln_raw in
buf#receive_all buf0
| _ -> assert false
with
Not_found -> assert false
end
| PP_MACRO_APPL(id, args) -> begin
let line0, base_loc0 =
try
match find_macro id with
| Macro.Function(params, l) -> begin
try
List.fold_left2
(fun s param arg ->
let pat = Str.regexp (String.concat "" ["{";param;"}"]) in
Str.global_replace pat arg s
) l.Macro.ln_raw params args,
l.Macro.ln_loc
with
Invalid_argument _ ->
Common.fail_to_parse
~head:(Printf.sprintf "[%s:%d:%d]"
env#current_filename
st.Lexing.pos_lnum
(st.Lexing.pos_cnum - st.Lexing.pos_bol))
"invalid number of macro arguments"
end
| _ -> assert false
with
Not_found -> assert false
in
let buf0 = expand_line base_loc0 st ed line0 in
buf#receive_all buf0
end
| _ -> buf#add (PB.make_qtoken tok st ed)
with
Sedlexing.MalFormed -> raise Exit
done
with
Exit -> ()
end;
buf
exception Token_found of Token.qtoken_t
class tokensource (tbuf : c) ulexbuf = object (self)
val ulexbuf_stack : Sedlexing.lexbuf Stack.t = Stack.create()
initializer
Stack.push ulexbuf ulexbuf_stack
method tokenbuf = tbuf
method in_included = (Stack.length ulexbuf_stack) > 1
method current_ulexbuf = Stack.top ulexbuf_stack
method enter_source (src : Source.c) =
[%debug_log "%s" src#filename];
let ulbuf = src#get_ulexbuf in
Stack.push ulbuf ulexbuf_stack;
ulbuf
method exit_source =
[%debug_log "called"];
let _ = Stack.pop ulexbuf_stack in
env#pop_loc;
env#exit_source
method get_token() =
[%debug_log "prebuf_size=%d" tbuf#prebuf_size];
let ulexbuf = self#current_ulexbuf in
if tbuf#prebuf_size < 1 then begin
let qtoken = U.token ulexbuf in
self#record_qtoken qtoken;
self#conv_token qtoken;
end;
let qtoken =
try
tbuf#prebuf_take
with
Buffer_empty -> self#get_token()
in
match qtoken with
| EOF, _ -> begin
if self#in_included then begin
self#exit_source;
self#get_token()
end
else
qtoken
end
| _ -> qtoken
method peek_nth_token n =
let ulexbuf = self#current_ulexbuf in
let rec prebuf_peek m =
try
tbuf#prebuf_peek_nth m
with
Buffer_empty ->
for _ = 1 to (m - tbuf#prebuf_size) do
let qtoken = U.token ulexbuf in
self#conv_token qtoken
done;
prebuf_peek m
in
if tbuf#is_serialized then
try
tbuf#peek_nth n
with
Buffer_empty ->
prebuf_peek (n - tbuf#size_of_serialized)
else
prebuf_peek n
method peek_token() =
let skip_until_endif_flag = ref false in
let n = ref 1 in
let branch_depth = ref 0 in
try
while true do
let qtoken = self#peek_nth_token !n in
let rawtok, _ = qtoken in
[%debug_log "peeking %dth token: %s" !n (Token.qtoken_to_string qtoken)];
if Tok.is_compiler_directive rawtok then begin
begin
match rawtok with
| PP_IFDEF
| PP_IFNDEF
-> incr branch_depth; incr n
| PP_UNDEF
| PP_DEFAULT_DECAY_TIME
| PP_DEFAULT_TRIREG_STRENGTH
| PP_ERROR
| PP_DEFAULT_NETTYPE
-> incr n
| PP_TIMESCALE
| PP_LINE
| PP_PRAGMA
-> n := !n + 3
| PP_ELSE ->
if !branch_depth = 0 then
skip_until_endif_flag := true
| PP_ELSIF ->
if !branch_depth = 0 then
skip_until_endif_flag := true;
incr n
| PP_ENDIF ->
if !branch_depth > 0 then
decr branch_depth;
skip_until_endif_flag := false
| _ -> ()
end;
incr n
end
else
if !skip_until_endif_flag then
incr n
else
raise (Token_found qtoken)
done;
EOF, Loc.dummy
with
Token_found qtoken -> qtoken
method conv_token qtoken =
[%debug_log "%s" (Token.qtoken_to_string qtoken)];
let tok, loc = qtoken in
let st, ed = Loc.to_lexposs loc in
let ln = st.Lexing.pos_lnum in
let divide qtkn =
let _, loc = qtkn in
let st, ed = Loc.to_lexposs loc in
let _st = Loc.incr_lexpos st in
let _ed = Loc.decr_lexpos ed in
st, _st, _ed, ed
in
begin
match tok with
| GENERATE ->
let st, _st, _ed, ed = divide qtoken in
tbuf#prebuf_add (PB.make_qtoken GENERATE st _st);
tbuf#prebuf_add (PB.make_qtoken GENERATE_ _ed ed)
| BEGIN ->
let st, _st, _ed, ed = divide qtoken in
tbuf#prebuf_add (PB.make_qtoken BEGIN st _st);
tbuf#prebuf_add (PB.make_qtoken BEGIN_ _ed ed)
| END ->
let st, _st, _ed, ed = divide qtoken in
tbuf#prebuf_add (PB.make_qtoken END st _st);
tbuf#prebuf_add (PB.make_qtoken END_ _ed ed)
| FORK ->
let st, _st, _ed, ed = divide qtoken in
tbuf#prebuf_add (PB.make_qtoken FORK st _st);
tbuf#prebuf_add (PB.make_qtoken FORK_ _ed ed)
| JOIN js ->
let st, _st, _ed, ed = divide qtoken in
tbuf#prebuf_add (PB.make_qtoken (JOIN js) st _st);
tbuf#prebuf_add (PB.make_qtoken JOIN_ _ed ed)
| ENDMODULE ->
let st, _st, _ed, ed = divide qtoken in
tbuf#prebuf_add (PB.make_qtoken ENDMODULE st _st);
tbuf#prebuf_add (PB.make_qtoken ENDMODULE_ _ed ed)
| MATCHES ->
let st, _st, _ed, ed = divide qtoken in
tbuf#prebuf_add (PB.make_qtoken MATCHES st _st);
tbuf#prebuf_add (PB.make_qtoken MATCHES_ _ed ed)
| INSIDE ->
let st, _st, _ed, ed = divide qtoken in
tbuf#prebuf_add (PB.make_qtoken INSIDE st _st);
tbuf#prebuf_add (PB.make_qtoken INSIDE_ _ed ed)
| PP_MACRO_ID id -> begin
try
match find_macro id with
| Macro.Object l -> begin
let recover = hide_macro id in
self#handle_macro_line st ed id [] l l.Macro.ln_raw;
recover()
end
| _ -> assert false
with
Not_found -> assert false
end
| PP_MACRO_APPL(id, args) -> begin
let line, raw =
try
match find_macro id with
| Macro.Function(params, l) -> begin
try
l,
List.fold_left2
(fun s param arg ->
let pat = Str.regexp (String.concat "" ["{";param;"}"]) in
Str.global_replace pat arg s
) l.Macro.ln_raw params args
with
Invalid_argument _ ->
Common.fail_to_parse
~head:(sprintf "[%s:%d:%d]"
env#current_filename
ln
(st.Lexing.pos_cnum - st.Lexing.pos_bol))
(sprintf "invalid number of macro arguments: %s" id)
end
| _ -> assert false
with
Not_found -> assert false
in
let recover = hide_macro id in
self#handle_macro_line st ed id args line raw;
recover()
end
| _ -> tbuf#prebuf_add qtoken
end
method record_qtoken qtoken =
[%debug_log "%s" (Token.qtoken_to_string qtoken)];
let tok, loc = qtoken in
tbuf#set_last_taken tok;
tbuf#set_last_taken_loc loc;
begin
match tok with
| PP_IFDEF | PP_IFNDEF | PP_ELSE | PP_ELSIF | PP_ENDIF
-> ()
| _ ->
tbuf#set_last_taken_no_pp_branch tok;
tbuf#set_last_taken_no_pp_branch_loc loc
end
method handle_macro_line st ed id args line raw =
let args_str =
let s = Xlist.to_string (fun x -> x) "," args in
if s = "" then "" else String.concat "" ["("; s; ")"]
in
[%debug_log "id:%s args:%s line:%s [%s]"
id args_str raw (Macro.stat_to_string line.Macro.ln_stat)];
let ida = id^args_str in
match line.Macro.ln_stat with
| Macro.Resolved obj -> begin
[%debug_log "macro stat: RESOLVED"];
let tok = Obj.obj obj in
let t = PB.make_qtoken tok st ed in
tbuf#prebuf_add t
end
| Macro.Unresolved ->
[%debug_log "macro stat: UNRESOLVED"];
let buf = expand_line line.Macro.ln_loc st ed raw in
let buf_as_list = buf#get_list in
begin
[%debug_log "buf#get_list (%d):" (List.length buf_as_list)];
begin %debug_block
List.iter
(fun (tok, _) ->
[%debug_log " %s" (Token.rawtoken_to_string tok)])
buf_as_list
end;
match buf_as_list with
| [] -> begin
end
| [(IDENTIFIER _, _)] -> tbuf#prebuf_add (PB.make_qtoken (PP_MACRO_NAME ida) st ed)
| [(STRING_LITERAL _, _)] -> tbuf#prebuf_add (PB.make_qtoken (PP_MACRO_CONST_STR ida) st ed)
| [(INTEGRAL_NUMBER _, _)] -> tbuf#prebuf_add (PB.make_qtoken (PP_MACRO_CONST_INT ida) st ed)
| [(REAL_NUMBER _, _)]
| [(TIME_NUMBER _, _)]
->
tbuf#prebuf_add (PB.make_qtoken (PP_MACRO_CONST ida) st ed)
| [(INTEGRAL_NUMBER _, _);(INTEGRAL_NUMBER _, _)]
->
tbuf#prebuf_add (PB.make_qtoken (PP_MACRO_CONST_INT ida) st ed)
| (IDENTIFIER id', _)::_ when id' = id ->
buf#iter
(fun (tok, _) ->
tbuf#prebuf_add (PB.make_qtoken tok st ed)
)
| xs ->
buf#add (PB.make_qtoken EOP Loc.dummy_lexpos Loc.dummy_lexpos);
let rec is_concat = function
| _ :: (PP_CONCAT,_) :: rest -> is_concat rest
| [_] -> true
| _ -> false
in
let contexts =
if is_concat xs then
[ C.expr(), PP_MACRO_NAME ida, (fun t -> tbuf#prebuf_add t) ]
else
[
C.pev_expr(), PP_MACRO_EXPR ida, (fun t -> tbuf#prebuf_add t);
C.ev_expr(), PP_MACRO_EXPR ida, (fun t -> tbuf#prebuf_add t);
C.expr(), PP_MACRO_EXPR ida, (fun t -> tbuf#prebuf_add t);
]
in
env#checkpoint Loc.dummy;
let last_tok = tbuf#get_last_taken_no_pp_branch in
let _ = last_tok in
[%debug_log "last_tok=%s" (Token.rawtoken_to_string last_tok)];
try
List.iter
(fun (context, tok, handler) ->
[%debug_log "trying to parse with context:%s..." (C.to_string context)];
let _parser = tbuf#partial_parser_selector context in
try
env#recover Loc.dummy;
let _ = buf#parse_by ~cache:false _parser in
handler (PB.make_qtoken tok st ed);
Macro.resolve_line line tok;
env#recover ~remove:true Loc.dummy;
raise Exit
with
| Incomplete -> ()
) contexts;
env#recover ~remove:true Loc.dummy;
[%debug_log "all attempts failed, expanding..."];
let add_to_tbuf() =
buf#iter
(fun (tok, _) ->
if tok = EOP then
raise Exit
else
tbuf#prebuf_add (PB.make_qtoken tok st ed)
)
in
add_to_tbuf()
with
Exit -> ()
end
end
let hack_token tokensrc qtoken =
let tokenbuf = tokensrc#tokenbuf in
let _tok, _loc = qtoken in
let st, ed = _loc.Loc.start_offset, _loc.Loc.end_offset + 1 in
let tok = ref _tok in
let loc = ref _loc in
[%debug_log "%s" (Token.qtoken_to_string qtoken)];
let peek_next() =
let tok, _ = tokensrc#peek_token() in
tok
in
begin
match !tok with
| PP_IDENTIFIER s ->
begin
match tokenbuf#get_last_rawtok with
| LT_EQ -> begin
let next_tok = peek_next() in
[%debug_log "next tok: %s" (Token.rawtoken_to_string next_tok)];
match next_tok with
| SEMICOLON | RPAREN | DOT -> ()
| _ -> tok := NB_ASSIGN_POSTFIX s
end
| _ -> ()
end
| IDENTIFIER _ -> ()
| BYTE | INT | REF | TYPE -> begin
let next_tok = peek_next() in
begin
match next_tok with
| COMMA | SEMICOLON | EQ | RBRACE | RPAREN
| OR | AND | AMP_AMP | EQ_EQ ->
let s = Token.rawtoken_to_rep !tok in
env#current_source#set_lang_spec_v2005;
let st_pos, ed_pos = Loc.to_lexposs !loc in
parse_warning st_pos ed_pos
"System Verilog keyword \"%s\" used as an identifier: maybe \"%s\""
s (Source.lang_spec_to_string env#current_source#lang_spec);
tok := IDENTIFIER s
| _ -> ()
end;
match tokenbuf#get_last_rawtok with
| DOT ->
let s = Token.rawtoken_to_rep !tok in
env#current_source#set_lang_spec_v2005;
let st_pos, ed_pos = Loc.to_lexposs !loc in
parse_warning st_pos ed_pos
"System Verilog keyword \"%s\" used as an identifier: maybe \"%s\""
s (Source.lang_spec_to_string env#current_source#lang_spec);
tok := IDENTIFIER s
| _ -> ()
end
| COLON_COLON ->
if env#scoped_flag then
let next_tok = peek_next() in
begin
match next_tok with
| IDENTIFIER _ -> ()
| _ -> env#clear_scoped_flag; A.end_scope()
end
| COMMA ->
if env#in_table then
let next_tok = peek_next() in
begin
match next_tok with
| IDENTIFIER _ -> tok := COMMA__I
| _ -> ()
end
| CONST ->
let next_tok = peek_next() in
begin
match next_tok with
| REF -> tok := CONST__R
| _ -> ()
end
| CONTEXT -> begin
match tokenbuf#get_last_rawtok with
| STRING_LITERAL _ -> ()
| _ ->
env#current_source#set_lang_spec_v2005;
let st_pos, ed_pos = Loc.to_lexposs !loc in
parse_warning st_pos ed_pos
"System Verilog keyword \"context\" used as an identifier: maybe \"%s\""
(Source.lang_spec_to_string env#current_source#lang_spec);
tok := IDENTIFIER "context"
end
| ENDTABLE -> env#end_table
| FUNCTION ->
if env#pvstate = 2 then begin
tok := FUNCTION__PV;
env#set_pvstate 0
end
| GLOBAL ->
let next_tok = peek_next() in
begin
match next_tok with
| CLOCKING -> ()
| _ ->
env#current_source#set_lang_spec_sv2005;
let st_pos, ed_pos = Loc.to_lexposs !loc in
parse_warning st_pos ed_pos
"System Verilog (2009) keyword \"global\" used as an identifier: maybe \"%s\""
(Source.lang_spec_to_string env#current_source#lang_spec);
tok := IDENTIFIER "global"
end
| LOCAL ->
let next_tok = peek_next() in
begin
match next_tok with
| COLON_COLON -> tok := LOCAL__CC
| COMMA | SEMICOLON | EQ | RBRACE | RPAREN | LBRACKET
| OR | AND | AMP_AMP ->
env#current_source#set_lang_spec_v2005;
let st_pos, ed_pos = Loc.to_lexposs !loc in
parse_warning st_pos ed_pos
"System Verilog keyword \"local\" used as an identifier: maybe \"%s\""
(Source.lang_spec_to_string env#current_source#lang_spec);
tok := IDENTIFIER "local"
| _ -> ()
end
| LPAREN ->
let next_tok = peek_next() in
begin
match next_tok with
| STRENGTH _ -> tok := LPAREN__S
| SUPPLY0 -> tok := LPAREN__S
| SUPPLY1 -> tok := LPAREN__S
| _ -> ()
end
| NEW ->
let next_tok = peek_next() in
begin
match next_tok with
| LPAREN -> tok := NEW__P
| _ -> ()
end
| PURE -> env#set_pvstate 1
| SEMICOLON -> env#set_pvstate 0
| STATIC ->
let next_tok = peek_next() in
begin
match next_tok with
| CONSTRAINT -> tok := STATIC__C
| _ -> ()
end
| TABLE -> env#begin_table
| TASK ->
if env#pvstate = 2 then begin
tok := TASK__PV;
env#set_pvstate 0
end
| VIRTUAL ->
if env#pvstate = 1 then
env#set_pvstate 2
else
env#set_pvstate 0;
let next_tok = peek_next() in
begin
match next_tok with
| CLASS -> tok := VIRTUAL__C
| INTERFACE -> tok := VIRTUAL__I
| IDENTIFIER _ -> tok := VIRTUAL__ID
| _ -> ()
end
| WITHx ->
let next_tok = peek_next() in
begin
match next_tok with
| LPAREN -> tok := WITH__P
| LBRACKET -> tok := WITH__B
| LBRACE -> tok := WITH__C
| _ -> ()
end
| _ ->
if env#pvstate = 1 then
env#set_pvstate 0
end;
begin
match !tok with
| IDENTIFIER s ->
let attr_opt = ref None in
begin
if Xstring.startswith s "PATHPULSE$" then
tok := PATHPULSE_IDENTIFIER s
else if env#in_table then
let symbol =
match s with
| "x" | "X" -> SYMBOL_xX s
| "b" | "B" -> SYMBOL_bB s
| "r" | "R" | "f" | "F" | "p" | "P" | "n" | "N" -> SYMBOL_rRfFpPnN s
| _ -> IDENTIFIER s
in
tok := symbol
else
try
match env#lookup_identifier s with
| ((Aux.IApackage) as a)::_ -> tok := PACKAGE_IDENTIFIER s; attr_opt := Some a
| ((Aux.IAclass _) as a)::_ -> tok := CLASS_IDENTIFIER s; attr_opt := Some a
| (Aux.IAtype)::_ -> tok := TYPE_IDENTIFIER s
| (Aux.IAcovergroup)::_ -> tok := COVERGROUP_IDENTIFIER s
| (Aux.IAclocking)::_ -> ()
| (Aux.IAproperty)::_ -> ()
| ((Aux.IAextern_method) )::_ -> ()
| _ -> ()
with Not_found -> ()
end;
begin
match !attr_opt with
| Some Aux.IApackage | Some (Aux.IAclass _) ->
let next_tok = peek_next() in
begin
match next_tok with
| COLON_COLON ->
env#set_scoped_flag;
A.begin_scope();
begin
match !attr_opt with
| Some Aux.IApackage -> env#import_any s
| Some (Aux.IAclass tblr) -> env#_import_any !tblr
| _ -> assert false
end
| _ -> ()
end
| _ ->
if env#scoped_flag then begin
A.end_scope();
env#clear_scoped_flag
end
end
| _ -> ()
end;
begin
match !tok with
| IDENTIFIER s -> begin
match tokenbuf#get_last_rawtok with
| IMPORT -> begin
match peek_next() with
| COLON_COLON -> tok := PACKAGE_IDENTIFIER s
| _ -> ()
end
| _ -> ()
end
| _ -> ()
end;
if _tok <> !tok then
[%debug_log " --> %s" (Token.rawtoken_to_string !tok)];
tokenbuf#set_prev_token tokenbuf#get_last_token;
tokenbuf#set_last_token (!tok, Loc.mklexpos st, Loc.mklexpos ed);
!tok, !loc
let rec pp tokensrc =
let tokenbuf = tokensrc#tokenbuf in
[%debug_log "called"];
let next() =
let qtoken = tokensrc#get_token() in
let tok, _ = qtoken in
[%debug_log "%s" (Token.qtoken_to_string qtoken)];
tok
in
let mkbtag = make_branch_tag in
let consume() =
let qtoken = tokensrc#get_token() in
let tok, loc = qtoken in
let buffer_add() =
tokenbuf#add (hack_token tokensrc qtoken)
in
[%debug_log "%s" (Token.qtoken_to_string qtoken)];
match tok with
| PP_IFDEF | PP_IFNDEF ->
let btag = mkbtag tok (next()) loc in
let qtok = tokensrc#peek_token() in
let ntok, _ = qtok in
[%debug_log "case %s: next token: %s (compiler directives skipped)"
(Token.rawtoken_to_string tok) (Token.qtoken_to_string qtok)];
let open_if =
match ntok with
| ELSE -> true
| _ -> false
in
tokenbuf#begin_branch ~open_if btag;
[%debug_log "calling pp"];
pp tokensrc
| PP_ELSIF ->
tokenbuf#new_branch (mkbtag tok (next()) loc);
[%debug_log "calling pp"];
pp tokensrc
| PP_ELSE ->
tokenbuf#new_branch (mkbtag tok (IDENTIFIER "dummy") loc);
[%debug_log "calling pp"];
pp tokensrc
| PP_ENDIF -> begin
let qtok = tokensrc#peek_token() in
let ntok, _ = qtok in
[%debug_log "case PP_ENDIF: next token: %s (compiler directives skipped)"
(Token.qtoken_to_string qtok)];
let open_if =
match ntok with
| ELSE -> true
| _ -> false
in
tokenbuf#end_branch ~open_if;
[%debug_log "calling pp"];
pp tokensrc
end
| PP_LINE | PP_INCLUDE _ | PP_SYS_INCLUDE _ | PP_ERROR
| PP_TIMESCALE | PP_RESETALL | PP_DEFAULT_NETTYPE | PP_PRAGMA
| PP_BEGIN_KEYWORDS | PP_END_KEYWORDS | PP_DEFAULT_DECAY_TIME
| PP_DEFAULT_TRIREG_STRENGTH | PP_DELAY_MODE_DISTRIBUTED
| PP_DELAY_MODE_PATH | PP_DELAY_MODE_UNIT | PP_DELAY_MODE_ZERO
| PP_CELLDEFINE | PP_ENDCELLDEFINE | PP_UNCONNECTED_DRIVE
| PP_NOUNCONNECTED_DRIVE
->
if tokenbuf#in_branch then begin
buffer_add();
[%debug_log "calling pp"];
pp tokensrc
end
else begin
tokenbuf#add_to_context (tok, loc);
tok, loc
end
| PP_UNDEFINEALL ->
if tokenbuf#in_branch then begin
buffer_add();
[%debug_log "calling pp"];
pp tokensrc
end
else begin
tokenbuf#add_to_context (tok, loc);
env#macrotbl#clear;
tok, loc
end
| _ ->
if tokenbuf#in_branch then begin
[%debug_log "in_branch"];
begin
match tok with
| EOF -> raise End_of_file;
| _ -> ()
end;
buffer_add();
[%debug_log "calling pp"];
pp tokensrc
end
else begin
begin
match tok with
| PP_DEFINE__IDENT__BODY(id, body) -> begin
env#define_macro id body
end
| PP_UNDEF__IDENT id -> begin
env#undefine_macro id
end
| _ -> ()
end;
if tokenbuf#is_context_empty then
if context_stack#top_is_active && not context_stack#top_is_unknown then
tokenbuf#set_context context_stack#top;
begin
match tok with
| BEGIN_ | END_ | FORK_ | JOIN_ | ENDMODULE_ | MATCHES_ | INSIDE_
-> ()
| _ -> context_stack#deactivate_top
end;
tokenbuf#add_to_context (tok, loc);
tok, loc
end
in
if tokenbuf#is_serialized then begin
try
tokenbuf#take
with
Buffer_empty -> [%debug_log "calling consume (serialized)"]; consume()
end
else begin
[%debug_log "calling consume"];
consume()
end
end
]