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 | -- There is a missing semicolon in the middle of this file; it
-- complained that the LR1 parser has a stack more than 50 deep. So we
-- increased the recover stack size.
--EMACSCMD:(setq skip-recase-test t)
pragma License (Modified_GPL);
with Ada.Exceptions;
with Ada.Strings.Bounded;
with Ada.Text_IO;
with SAL;
with WisiToken.Semantic_Checks;
package body Wisi is
use WisiToken;
Chars_Per_Int : constant Integer := Integer'Width;
----------
-- body subprogram specs (as needed), alphabetical
function Indent_Nil_P (Indent : in Indent_Type) return Boolean;
function Max_Anchor_ID
(Data : in out Parse_Data_Type;
First_Line : in Line_Number_Type;
Last_Line : in Line_Number_Type)
return Integer;
function Paren_In_Anchor_Line
(Data : in out Parse_Data_Type'Class;
Anchor_Token : in Augmented_Token;
Offset : in Integer)
return Integer;
----------
-- body subprograms bodies, alphabetical
function Image (Anchor_IDs : in Anchor_ID_Vectors.Vector) return String
is
use Ada.Strings.Unbounded;
Result : Unbounded_String := +"(";
begin
for I in Anchor_IDs.First_Index .. Anchor_IDs.Last_Index loop
Result := Result & Integer'Image (Anchor_IDs (I));
if I /= Anchor_IDs.Last_Index then
Result := Result & ", ";
else
Result := Result & ")";
end if;
end loop;
return -Result;
end Image;
function Image (Indent : in Indent_Type) return String
is begin
case Indent.Label is
when Not_Set =>
return "(" & Indent_Label'Image (Indent.Label) & ")";
when Int =>
return "(" & Indent_Label'Image (Indent.Label) & Integer'Image (Indent.Int_Indent) & ")";
when Anchor_Nil =>
return "(" & Indent_Label'Image (Indent.Label) & ", " & Image (Indent.Anchor_Nil_IDs) & ", nil)";
when Anchor_Int =>
return "(" & Indent_Label'Image (Indent.Label) & ", " & Image (Indent.Anchor_Int_IDs) & ", " & Integer'Image
(Indent.Anchor_Int_Indent) & ")";
when Anchored =>
return "(" & Indent_Label'Image (Indent.Label) & ", " & Integer'Image (Indent.Anchored_ID) & ", " &
Integer'Image (Indent.Anchored_Delta) & ")";
when Anchor_Anchored =>
return "(" & Indent_Label'Image (Indent.Label) & ", " & Image (Indent.Anchor_Anchored_IDs) & Integer'Image
(Indent.Anchor_Anchored_ID) & ", " & Integer'Image (Indent.Anchor_Anchored_Delta) & ")";
end case;
end Image;
procedure Indent_Apply_Anchored
(Delta_Indent : in Simple_Delta_Type;
Indent : in out Indent_Type)
with Pre => Delta_Indent.Label = Anchored
-- comment after aspect_specification, before 'is'
is begin
-- [2] wisi-elisp-parse--apply-anchored; add Delta_Indent to Indent
case Indent.Label is
when Not_Set =>
Indent := (Anchored, Delta_Indent.Anchored_ID, Delta_Indent.Anchored_Delta);
when Int =>
if Delta_Indent.Anchored_Accumulate then
Indent := (Anchored, Delta_Indent.Anchored_ID, Indent.Int_Indent + Delta_Indent.Anchored_Delta);
end if;
when Anchor_Nil =>
Indent :=
(Anchor_Anchored,
Indent.Anchor_Nil_IDs,
Delta_Indent.Anchored_ID,
Delta_Indent.Anchored_Delta);
when Anchor_Int =>
if Delta_Indent.Anchored_Accumulate then
Indent :=
(Anchor_Anchored,
Indent.Anchor_Int_IDs,
Delta_Indent.Anchored_ID,
Delta_Indent.Anchored_Delta + Indent.Anchor_Int_Indent);
end if;
when Anchored | Anchor_Anchored =>
-- already anchored
null;
end case;
end Indent_Apply_Anchored;
procedure Indent_Apply_Int (Indent : in out Indent_Type; Offset : in Integer)
is begin
-- [2] wisi-elisp-parse--apply-int; add an Int indent to Indent
case Indent.Label is
when Not_Set =>
Indent := (Int, Offset);
when Int =>
Indent.Int_Indent := Indent.Int_Indent + Offset;
when Anchor_Nil =>
Indent :=
(Label => Anchor_Int,
Anchor_Int_IDs => Indent.Anchor_Nil_IDs,
Anchor_Int_Indent => Offset);
when Anchor_Int =>
Indent.Anchor_Int_Indent := Indent.Anchor_Int_Indent + Offset;
when Anchored | Anchor_Anchored =>
null;
end case;
end Indent_Apply_Int;
procedure Indent_Line
(Data : in out Parse_Data_Type;
Line : in Line_Number_Type;
Delta_Indent : in Delta_Type)
is
-- See note in Indent_Anchored_2 for why we can't use renames here.
Indent : Indent_Type := Data.Indents (Line);
begin
case Delta_Indent.Label is
when Simple =>
case Delta_Indent.Simple_Delta.Label is
when None =>
null;
when Int =>
Indent_Apply_Int (Indent, Delta_Indent.Simple_Delta.Int_Delta);
when Anchored =>
Indent_Apply_Anchored (Delta_Indent.Simple_Delta, Indent);
end case;
when Hanging =>
if Delta_Indent.Hanging_Accumulate or Indent_Nil_P (Data.Indents (Line)) then
if Line = Delta_Indent.Hanging_First_Line then
-- Apply delta_1
case Delta_Indent.Hanging_Delta_1.Label is
when None =>
null;
when Int =>
Indent_Apply_Int (Indent, Delta_Indent.Hanging_Delta_1.Int_Delta);
when Anchored =>
Indent_Apply_Anchored (Delta_Indent.Hanging_Delta_1, Indent);
end case;
else
if Delta_Indent.Hanging_Paren_State = Data.Line_Paren_State (Line) then
case Delta_Indent.Hanging_Delta_2.Label is
when None =>
null;
when Int =>
Indent_Apply_Int (Indent, Delta_Indent.Hanging_Delta_2.Int_Delta);
when Anchored =>
Indent_Apply_Anchored (Delta_Indent.Hanging_Delta_2, Indent);
end case;
end if;
end if;
end if;
end case;
if Trace_Action > Extra then
Ada.Text_IO.Put_Line (";; indent_line: " & Line_Number_Type'Image (Line) & " => " & Image (Indent));
end if;
Data.Indents.Replace_Element (Line, Indent);
end Indent_Line;
function Indent_Nil_P (Indent : in Indent_Type) return Boolean
is begin
return Indent.Label in Not_Set | Anchor_Nil;
end Indent_Nil_P;
function Max_Anchor_ID
(Data : in out Parse_Data_Type;
First_Line : in Line_Number_Type;
Last_Line : in Line_Number_Type)
return Integer
is
Result : Integer := First_Anchor_ID - 1;
begin
for Line in First_Line .. Last_Line loop
declare
Indent : Indent_Type renames Data.Indents (Line);
begin
case Indent.Label is
when Not_Set | Int =>
null;
when Anchor_Nil =>
Result := Integer'Max (Result, Indent.Anchor_Nil_IDs (Indent.Anchor_Nil_IDs.First_Index));
when Anchor_Int =>
Result := Integer'Max (Result, Indent.Anchor_Int_IDs (Indent.Anchor_Int_IDs.First_Index));
when Anchored =>
Result := Integer'Max (Result, Indent.Anchored_ID);
when Anchor_Anchored =>
Result := Integer'Max (Result, Indent.Anchor_Anchored_ID);
end case;
end;
end loop;
return Result;
end Max_Anchor_ID;
function Paren_In_Anchor_Line
(Data : in out Parse_Data_Type'Class;
Anchor_Token : in Augmented_Token;
Offset : in Integer)
return Integer
is
Left_Paren_ID : Token_ID renames Data.Left_Paren_ID;
Right_Paren_ID : Token_ID renames Data.Right_Paren_ID;
I : Base_Token_Index := Anchor_Token.First_Terminals_Index;
Paren_Count : Integer := 0;
Paren_Char_Pos : Buffer_Pos := Invalid_Buffer_Pos;
Text_Begin_Pos : Buffer_Pos := Invalid_Buffer_Pos;
begin
-- [1] wisi-elisp-parse--paren-in-anchor-line. That uses elisp syntax-ppss; here
-- we search Terminals.
loop
declare
Tok : Augmented_Token renames Data.Terminals (I);
begin
if Tok.ID = Left_Paren_ID then
Paren_Count := Paren_Count + 1;
if Paren_Count = 1 then
Paren_Char_Pos := Tok.Char_Region.First;
end if;
elsif Tok.ID = Right_Paren_ID then
Paren_Count := Paren_Count - 1;
end if;
if Tok.First then
Text_Begin_Pos := Tok.Char_Region.First;
exit;
end if;
end;
I := I - 1;
end loop;
if Paren_Char_Pos /= Invalid_Buffer_Pos and Text_Begin_Pos /= Invalid_Buffer_Pos then
return 1 + Offset + Integer (Paren_Char_Pos - Text_Begin_Pos);
else
return Offset;
end if;
end Paren_In_Anchor_Line;
procedure Put (Cache : in Navigate_Cache_Type)
is
package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length (Max => 2 + 11 * Chars_Per_Int);
use Bounded;
Line : Bounded_String := To_Bounded_String ("[");
procedure Append (Item : in Nil_Buffer_Pos)
is begin
if Item.Set then
Append (Line, Buffer_Pos'Image (Item.Item));
else
Append (Line, " -1");
end if;
end Append;
begin
Append (Line, Navigate_Cache_Code);
Append (Line, Buffer_Pos'Image (Cache.Pos));
Append (Line, Token_ID'Image (Cache.Statement_ID));
Append (Line, Token_ID'Image (Cache.ID));
Append (Line, Integer'Image (Cache.Length));
Append (Line, Integer'Image (Navigate_Class_Type'Pos (Cache.Class)));
Append (Cache.Containing_Pos);
Append (Cache.Prev_Pos);
Append (Cache.Next_Pos);
Append (Cache.End_Pos);
Append (Line, ']');
Ada.Text_IO.Put_Line (To_String (Line));
end Put;
procedure Put (Cache : in WisiToken.Buffer_Region)
is begin
Ada.Text_IO.Put_Line
("[" & Name_Property_Code & Buffer_Pos'Image (Cache.First) & Buffer_Pos'Image (Cache.Last) & "]");
end Put;
procedure Put (Cache : in Face_Cache_Type)
is
package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length (Max => 2 + 4 * Chars_Per_Int);
use Bounded;
Line : Bounded_String := To_Bounded_String ("[");
begin
if Cache.Face.Set then
Append (Line, Face_Property_Code);
Append (Line, Buffer_Pos'Image (Cache.Region.First));
Append (Line, Buffer_Pos'Image (Cache.Region.Last));
Append (Line, Integer'Image (Cache.Face.Item));
Append (Line, ']');
Ada.Text_IO.Put_Line (To_String (Line));
end if;
end Put;
procedure Put (Line_Number : in Line_Number_Type; Item : in Indent_Type)
is begin
-- All Anchors must be resolved at this point, but not all lines have
-- an indent computed. A negative indent is an error in either the
-- grammar indent rules or the algorithms in this package.
case Item.Label is
when Not_Set =>
-- Especially with partial parse, we have no idea what this indent should be.
null;
when Int =>
declare
-- We can easily get negative indents when there are syntax errors.
Ind : constant Integer := Integer'Max (0, Item.Int_Indent);
begin
Ada.Text_IO.Put_Line
('[' & Indent_Code & Line_Number_Type'Image (Line_Number) & Integer'Image (Ind) & ']');
end;
when Anchor_Nil | Anchor_Int | Anchored | Anchor_Anchored =>
raise SAL.Programmer_Error with "Indent item has non-int label: " & Indent_Label'Image (Item.Label);
end case;
end Put;
procedure Put
(Item : in Parse.LR.Configuration;
Terminals : in Augmented_Token_Arrays.Vector;
Descriptor : in WisiToken.Descriptor;
Embedded_Quote_Escape_Doubled : in Boolean)
is
use Ada.Containers;
use Ada.Strings.Unbounded;
use Parse.LR;
use Parse.LR.Config_Op_Arrays, Parse.LR.Config_Op_Array_Refs;
Line : Unbounded_String := To_Unbounded_String ("[");
Last_Op : Config_Op := (Fast_Forward, WisiToken.Token_Index'Last);
begin
if Trace_Action > Detail then
Ada.Text_IO.Put_Line (";; " & Parse.LR.Image (Item.Ops, Descriptor));
end if;
Append (Line, Recover_Code);
if Length (Item.Ops) = 0 then
Append (Line, "]");
else
for I in First_Index (Item.Ops) .. Last_Index (Item.Ops) loop
declare
Op : Config_Op renames Constant_Ref (Item.Ops, I);
begin
case Op.Op is
when Fast_Forward =>
if Last_Op.Op in Insert then
Append (Line, "][]]");
elsif Last_Op.Op in Delete then
Append (Line, "]]");
end if;
Last_Op := Op;
when Undo_Reduce | Push_Back =>
null;
when Insert =>
if Last_Op.Op = Fast_Forward then
Append (Line, "[");
Append (Line, Buffer_Pos'Image (Terminals (Op.Ins_Token_Index).Char_Region.First));
Append (Line, "[");
elsif Last_Op.Op = Delete then
Append (Line, "]][");
Append (Line, Buffer_Pos'Image (Terminals (Op.Ins_Token_Index).Char_Region.First));
Append (Line, "[");
else
-- Last_Op.Op = Insert
null;
end if;
Append (Line, Token_ID'Image (Op.Ins_ID));
Last_Op := Op;
when Delete =>
declare
Skip : Boolean := False;
begin
if Last_Op.Op = Fast_Forward then
Append (Line, "[");
Append (Line, Buffer_Pos'Image (Terminals (Op.Del_Token_Index).Char_Region.First));
Append (Line, "[][");
elsif Last_Op.Op = Insert then
Append (Line, "][");
elsif Last_Op.Op = Delete then
if Embedded_Quote_Escape_Doubled and then
((Last_Op.Del_ID = Descriptor.String_1_ID and Op.Del_ID = Descriptor.String_1_ID) or
(Last_Op.Del_ID = Descriptor.String_2_ID and Op.Del_ID = Descriptor.String_2_ID))
then
declare
Tok_1 : Augmented_Token renames Terminals (Last_Op.Del_Token_Index);
Tok_2 : Augmented_Token renames Terminals (Op.Del_Token_Index);
begin
if Tok_1.Char_Region.Last + 1 = Tok_2.Char_Region.First then
-- Buffer text was '"""', lexer repair changed it to '""""'. The
-- repaired text looks like a single string with an embedded quote.
-- But here, it is two STRING_LITERAL tokens. Don't send the second
-- delete to elisp. See test/ada_mode-recover_string_quote_1.adb
Skip := True;
end if;
end;
end if;
end if;
if not Skip then
Append (Line, Token_ID'Image (Op.Del_ID));
end if;
end;
Last_Op := Op;
end case;
end;
end loop;
case Last_Op.Op is
when Fast_Forward =>
Append (Line, "]");
when Undo_Reduce | Push_Back =>
null;
when Insert =>
Append (Line, "][]]]");
when Delete =>
Append (Line, "]]]");
end case;
end if;
Ada.Text_IO.Put_Line (To_String (Line));
end Put;
procedure Resolve_Anchors (Data : in out Parse_Data_Type)
is
Begin_Indent : Integer renames Data.Begin_Indent;
Anchor_Indent : array (First_Anchor_ID .. Data.Max_Anchor_ID) of Integer;
begin
if Trace_Action > Outline then
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line (";; Begin_Indent: " & Integer'Image (Data.Begin_Indent));
for I in Data.Indents.First_Index .. Data.Indents.Last_Index loop
Ada.Text_IO.Put_Line (";; " & Line_Number_Type'Image (I) & ", " & Image (Data.Indents (I)));
end loop;
Ada.Text_IO.Put_Line (";; resolve anchors");
end if;
for I in Data.Indents.First_Index .. Data.Indents.Last_Index loop
declare
Indent : constant Indent_Type := Data.Indents (I);
begin
case Indent.Label is
when Not_Set =>
-- Indent not computed, therefore not output.
null;
when Int =>
Data.Indents.Replace_Element (I, (Int, Indent.Int_Indent + Begin_Indent));
when Anchor_Nil =>
for I of Indent.Anchor_Nil_IDs loop
Anchor_Indent (I) := Begin_Indent;
end loop;
Data.Indents.Replace_Element (I, (Int, Begin_Indent));
when Anchor_Int =>
for I of Indent.Anchor_Int_IDs loop
Anchor_Indent (I) := Indent.Anchor_Int_Indent + Begin_Indent;
end loop;
Data.Indents.Replace_Element (I, (Int, Indent.Anchor_Int_Indent + Begin_Indent));
when Anchored =>
Data.Indents.Replace_Element
(I, (Int, Anchor_Indent (Indent.Anchored_ID) + Indent.Anchored_Delta));
when Anchor_Anchored =>
declare
Temp : constant Integer :=
Anchor_Indent (Indent.Anchor_Anchored_ID) + Indent.Anchor_Anchored_Delta;
begin
for I of Indent.Anchor_Anchored_IDs loop
Anchor_Indent (I) := Temp;
end loop;
Data.Indents.Replace_Element (I, (Int, Temp));
end;
end case;
end;
end loop;
end Resolve_Anchors;
procedure Set_End
(Data : in out Parse_Data_Type;
Containing_Pos : in Buffer_Pos;
End_Pos : in Buffer_Pos)
is
use Navigate_Cursor_Lists;
I : Cursor := Data.End_Positions.First;
Delete_Cache : Boolean;
Temp : Cursor;
begin
loop
exit when not Has_Element (I);
declare
Cache : Navigate_Cache_Type renames Data.Navigate_Caches (Element (I));
begin
if Cache.Pos in Containing_Pos .. End_Pos then
Cache.End_Pos := (True, End_Pos);
Delete_Cache := True;
else
Delete_Cache := False;
end if;
end;
if Delete_Cache then
Temp := Next (I);
Delete (Data.End_Positions, I);
I := Temp;
else
Next (I);
end if;
end loop;
end Set_End;
----------
-- public subprograms (declaration order)
procedure Initialize
(Data : in out Parse_Data_Type;
Lexer : in WisiToken.Lexer.Handle;
Descriptor : access constant WisiToken.Descriptor;
Source_File_Name : in String;
Post_Parse_Action : in Post_Parse_Action_Type;
Begin_Line : in Line_Number_Type;
End_Line : in Line_Number_Type;
Begin_Indent : in Integer;
Params : in String)
is
pragma Unreferenced (Params);
begin
Data.Line_Begin_Pos.Set_First_Last
(First => Begin_Line,
Last => End_Line);
-- + 1 for data on line following last line; see Lexer_To_Augmented.
Data.Line_Paren_State.Set_First_Last
(First => Begin_Line,
Last => End_Line + 1);
Data.Lexer := Lexer;
Data.Descriptor := Descriptor;
Data.Source_File_Name := +Source_File_Name;
Data.Post_Parse_Action := Post_Parse_Action;
case Post_Parse_Action is
when Navigate | Face =>
null;
when Indent =>
Data.Indents.Set_First_Last
(First => Begin_Line,
Last => End_Line);
Data.Begin_Indent := Begin_Indent;
end case;
Data.Reset;
exception
when E : others =>
raise SAL.Programmer_Error with "wisi.initialize: " & Ada.Exceptions.Exception_Name (E) & ": " &
Ada.Exceptions.Exception_Message (E);
end Initialize;
overriding procedure Reset (Data : in out Parse_Data_Type)
is begin
Data.Terminals.Clear;
Data.Leading_Non_Grammar.Clear;
-- Data.Line_Begin_Pos set in Initialize, overwritten in Lexer_To_Augmented
-- Data.Line_Begin_Token ""
for S of Data.Line_Paren_State loop
S := 0;
end loop;
Data.Current_Paren_State := 0;
Data.Navigate_Caches.Finalize;
Data.Navigate_Caches.Initialize;
Data.End_Positions.Clear;
Data.Name_Caches.Finalize;
Data.Name_Caches.Initialize;
Data.Face_Caches.Finalize;
Data.Face_Caches.Initialize;
for I in Data.Indents.First_Index .. Data.Indents.Last_Index loop
Data.Indents.Replace_Element (I, (Label => Not_Set));
end loop;
Data.Max_Anchor_ID := First_Anchor_ID - 1;
end Reset;
function Source_File_Name (Data : in Parse_Data_Type) return String
is begin
return -Data.Source_File_Name;
end Source_File_Name;
function Post_Parse_Action (Data : in Parse_Data_Type) return Post_Parse_Action_Type
is begin
return Data.Post_Parse_Action;
end Post_Parse_Action;
overriding
procedure Lexer_To_Augmented
(Data : in out Parse_Data_Type;
Token : in Base_Token;
Lexer : not null access WisiToken.Lexer.Instance'Class)
is
use all type Ada.Containers.Count_Type;
begin
if Lexer.First then
Data.Line_Begin_Pos (Token.Line) := Token.Char_Region.First;
if Token.Line > Data.Line_Begin_Pos.First_Index and then
Data.Line_Begin_Pos (Token.Line - 1) = Invalid_Buffer_Pos
then
-- Previous token contains multiple lines; ie %code in wisitoken_grammar.wy
declare
First_Set_Line : Line_Number_Type;
begin
for Line in reverse Data.Line_Begin_Pos.First_Index .. Token.Line - 1 loop
if Data.Line_Begin_Pos (Line) /= Invalid_Buffer_Pos then
First_Set_Line := Line;
exit;
end if;
end loop;
for Line in First_Set_Line + 1 .. Token.Line - 1 loop
Data.Line_Begin_Pos (Line) := Data.Line_Begin_Pos (First_Set_Line); -- good enough
end loop;
end;
end if;
end if;
if Token.ID < Data.Descriptor.First_Terminal then
-- Non-grammar token
if Token.ID = Data.Descriptor.New_Line_ID then
Data.Line_Paren_State (Token.Line + 1) := Data.Current_Paren_State;
end if;
if Data.Terminals.Length = 0 then
Data.Leading_Non_Grammar.Append ((Token with Lexer.First));
else
declare
Containing_Token : Augmented_Token renames Data.Terminals (Data.Terminals.Last_Index);
Trailing_Blank : constant Boolean :=
Token.ID = Data.Descriptor.New_Line_ID and
(Containing_Token.Non_Grammar.Length > 0 and then
Containing_Token.Non_Grammar
(Containing_Token.Non_Grammar.Last_Index).ID = Data.Descriptor.New_Line_ID);
begin
if Lexer.First and
(Token.ID in Data.First_Comment_ID .. Data.Last_Comment_ID or
Trailing_Blank)
then
if Containing_Token.First_Trailing_Comment_Line = Invalid_Line_Number then
Containing_Token.First_Trailing_Comment_Line := Token.Line;
end if;
Containing_Token.Last_Trailing_Comment_Line := Token.Line;
end if;
Containing_Token.Non_Grammar.Append ((Token with Lexer.First));
end;
end if;
else
-- grammar token
declare
Temp : constant Augmented_Token :=
(Token.ID,
Byte_Region => Token.Byte_Region,
Line => Token.Line,
Column => Token.Column,
Char_Region => Token.Char_Region,
Deleted => False,
First => Lexer.First,
Paren_State => Data.Current_Paren_State,
First_Terminals_Index => Data.Terminals.Last_Index + 1,
Last_Terminals_Index => Data.Terminals.Last_Index + 1,
First_Indent_Line => (if Lexer.First then Token.Line else Invalid_Line_Number),
Last_Indent_Line => (if Lexer.First then Token.Line else Invalid_Line_Number),
First_Trailing_Comment_Line => Invalid_Line_Number, -- Set by Reduce
Last_Trailing_Comment_Line => Invalid_Line_Number,
Non_Grammar => <>);
begin
if Token.ID = Data.Left_Paren_ID then
Data.Current_Paren_State := Data.Current_Paren_State + 1;
elsif Token.ID = Data.Right_Paren_ID then
Data.Current_Paren_State := Data.Current_Paren_State - 1;
end if;
Data.Terminals.Append (Temp);
end;
end if;
end Lexer_To_Augmented;
overriding
procedure Delete_Token
(Data : in out Parse_Data_Type;
Deleted_Token_Index : in WisiToken.Token_Index)
is
use all type Ada.Containers.Count_Type;
Deleted_Token : Augmented_Token renames Data.Terminals (Deleted_Token_Index);
Prev_Token_Index : Base_Token_Index := Deleted_Token_Index - 1;
Next_Token_Index : Base_Token_Index := Deleted_Token_Index + 1;
begin
if Deleted_Token.Deleted then
-- This can happen if error recovery screws up.
if WisiToken.Trace_Action > WisiToken.Detail then
Ada.Text_IO.Put_Line (";; delete token again; ignored " & Image (Deleted_Token, Data.Descriptor.all));
end if;
return;
end if;
if WisiToken.Trace_Action > WisiToken.Detail then
Ada.Text_IO.Put_Line (";; delete token " & Image (Deleted_Token, Data.Descriptor.all));
end if;
Deleted_Token.Deleted := True;
if Deleted_Token.Non_Grammar.Length > 0 then
-- Move Non_Grammar to previous non-deleted token
loop
exit when Prev_Token_Index = Base_Token_Index'First;
exit when Data.Terminals (Prev_Token_Index).Deleted = False;
Prev_Token_Index := Prev_Token_Index - 1;
end loop;
if Prev_Token_Index = Base_Token_Index'First then
Deleted_Token.Non_Grammar (Deleted_Token.Non_Grammar.First_Index).First := Deleted_Token.First;
Data.Leading_Non_Grammar.Append (Deleted_Token.Non_Grammar);
else
declare
Prev_Token : Augmented_Token renames Data.Terminals (Prev_Token_Index);
begin
Prev_Token.Non_Grammar.Append (Deleted_Token.Non_Grammar);
if Deleted_Token.First_Trailing_Comment_Line /= Invalid_Line_Number then
if Prev_Token.First_Trailing_Comment_Line = Invalid_Line_Number then
Prev_Token.First_Trailing_Comment_Line := Deleted_Token.First_Trailing_Comment_Line;
end if;
Prev_Token.Last_Trailing_Comment_Line := Deleted_Token.Last_Trailing_Comment_Line;
end if;
end;
end if;
end if;
-- Data.Terminals.Last_Index is Wisi_EOI; it is never deleted
loop
exit when Data.Terminals (Next_Token_Index).Deleted = False;
Next_Token_Index := Next_Token_Index + 1;
exit when Next_Token_Index = Data.Terminals.Last_Index;
end loop;
if Deleted_Token.First and
(Next_Token_Index = Data.Terminals.Last_Index or else
Data.Terminals (Next_Token_Index).Line > Deleted_Token.Line)
then
-- Deleted_Token.Line is now blank; add to previous token non
-- grammar.
if Prev_Token_Index > Base_Token_Index'First then
declare
Prev_Token : Augmented_Token renames Data.Terminals (Prev_Token_Index);
begin
if Prev_Token.First_Trailing_Comment_Line = Invalid_Line_Number then
Prev_Token.First_Trailing_Comment_Line := Deleted_Token.Line;
Prev_Token.Last_Trailing_Comment_Line := Deleted_Token.Line;
else
if Prev_Token.First_Trailing_Comment_Line > Deleted_Token.Line then
Prev_Token.First_Trailing_Comment_Line := Deleted_Token.Line;
end if;
if Prev_Token.Last_Trailing_Comment_Line < Deleted_Token.Line then
Prev_Token.Last_Trailing_Comment_Line := Deleted_Token.Line;
end if;
end if;
end;
end if;
end if;
if Deleted_Token.First and Next_Token_Index < Data.Terminals.Last_Index then
if not Data.Terminals (Next_Token_Index).First then
declare
Next_Token : Augmented_Token renames Data.Terminals (Next_Token_Index);
begin
Next_Token.First := True;
Next_Token.First_Indent_Line := Deleted_Token.First_Indent_Line;
Next_Token.Last_Indent_Line := Deleted_Token.Last_Indent_Line;
end;
end if;
end if;
end Delete_Token;
overriding
procedure Reduce
(Data : in out Parse_Data_Type;
Tree : in out Syntax_Trees.Tree'Class;
Nonterm : in Syntax_Trees.Valid_Node_Index;
Tokens : in Syntax_Trees.Valid_Node_Index_Array)
is
Aug_Nonterm : constant Augmented_Token_Access := new Augmented_Token'
(ID => Tree.ID (Nonterm),
Byte_Region => Tree.Byte_Region (Nonterm),
others => <>);
Trailing_Comment_Done : Boolean := False;
begin
Tree.Set_Augmented (Nonterm, Base_Token_Class_Access (Aug_Nonterm));
for I in reverse Tokens'Range loop
-- 'reverse' to find token containing trailing comments; last
-- non-virtual and non-empty token.
if Tree.Byte_Region (Tokens (I)) /= Null_Buffer_Region then
-- Token not entirely virtual
declare
Aug_Token : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Tokens (I));
begin
if Data.Post_Parse_Action = Indent then
if Aug_Token.First_Terminals_Index /= Augmented_Token_Arrays.No_Index then
Aug_Nonterm.First_Terminals_Index := Aug_Token.First_Terminals_Index;
end if;
if Aug_Nonterm.Last_Terminals_Index = Augmented_Token_Arrays.No_Index then
Aug_Nonterm.Last_Terminals_Index := Aug_Token.Last_Terminals_Index;
end if;
Aug_Nonterm.First := Aug_Nonterm.First or Aug_Token.First;
if Aug_Token.First_Indent_Line /= Invalid_Line_Number then
Aug_Nonterm.First_Indent_Line := Aug_Token.First_Indent_Line;
elsif Trailing_Comment_Done and Aug_Token.First_Trailing_Comment_Line /= Invalid_Line_Number then
Aug_Nonterm.First_Indent_Line := Aug_Token.First_Trailing_Comment_Line;
end if;
if Aug_Nonterm.Last_Indent_Line = Invalid_Line_Number then
if Trailing_Comment_Done and Aug_Token.Last_Trailing_Comment_Line /= Invalid_Line_Number then
Aug_Nonterm.Last_Indent_Line := Aug_Token.Last_Trailing_Comment_Line;
elsif Aug_Token.Last_Indent_Line /= Invalid_Line_Number then
Aug_Nonterm.Last_Indent_Line := Aug_Token.Last_Indent_Line;
end if;
end if;
if not Trailing_Comment_Done then
Aug_Nonterm.First_Trailing_Comment_Line := Aug_Token.First_Trailing_Comment_Line;
Aug_Nonterm.Last_Trailing_Comment_Line := Aug_Token.Last_Trailing_Comment_Line;
Trailing_Comment_Done := True;
end if;
end if; -- Compute_Indent
if Aug_Token.Line /= Invalid_Line_Number then
Aug_Nonterm.Line := Aug_Token.Line;
Aug_Nonterm.Column := Aug_Token.Column;
end if;
if Aug_Nonterm.Char_Region.First > Aug_Token.Char_Region.First then
Aug_Nonterm.Char_Region.First := Aug_Token.Char_Region.First;
end if;
if Aug_Nonterm.Char_Region.Last < Aug_Token.Char_Region.Last then
Aug_Nonterm.Char_Region.Last := Aug_Token.Char_Region.Last;
end if;
Aug_Nonterm.Paren_State := Aug_Token.Paren_State;
end;
end if; -- Aug_Token not virtual
end loop;
end Reduce;
procedure Statement_Action
(Data : in out Parse_Data_Type;
Tree : in Syntax_Trees.Tree;
Nonterm : in Syntax_Trees.Valid_Node_Index;
Tokens : in Syntax_Trees.Valid_Node_Index_Array;
Params : in Statement_Param_Array)
is
Nonterm_Tok : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Nonterm);
First_Item : Boolean := True;
Start_Set : Boolean := False;
Override_Start_Set : Boolean := False;
Containing_Pos : Nil_Buffer_Pos := Nil;
begin
for Pair of Params loop
if not (Pair.Index in Tokens'Range) then
raise Fatal_Error with Error_Message
(File_Name => -Data.Source_File_Name,
Line => Nonterm_Tok.Line,
Column => Nonterm_Tok.Column,
Message => "wisi-statement-action: " & Trimmed_Image (Tree.Production_ID (Nonterm)) &
" token index" & SAL.Peek_Type'Image (Pair.Index) &
" not in tokens range (" & SAL.Peek_Type'Image (Tokens'First) & " .." &
SAL.Peek_Type'Image (Tokens'Last) & "); bad grammar action.");
elsif Tree.Byte_Region (Tokens (Pair.Index)) /= Null_Buffer_Region then
declare
use all type WisiToken.Syntax_Trees.Node_Label;
Token : constant Aug_Token_Ref :=
(if Pair.Class = Statement_End and then
Tree.Label (Tokens (Pair.Index)) = WisiToken.Syntax_Trees.Nonterm
then To_Aug_Token_Ref (Data.Terminals (Tree.Max_Terminal_Index (Tokens (Pair.Index))))
else Get_Aug_Token (Data, Tree, Tokens (Pair.Index)));
Cache_Pos : constant Buffer_Pos := Token.Char_Region.First;
Cursor : Navigate_Cache_Trees.Cursor := Navigate_Cache_Trees.Find
(Data.Navigate_Caches.Iterate, Cache_Pos,
Direction => Navigate_Cache_Trees.Unknown);
begin
if Navigate_Cache_Trees.Has_Element (Cursor) then
declare
Cache : Navigate_Cache_Type renames Data.Navigate_Caches (Cursor);
begin
if Pair.Class = Statement_Start then
if Start_Set then
Cache.Class := Motion;
else
Cache.Class := Statement_Start;
Start_Set := True;
end if;
elsif Override_Start_Set then
Cache.Class := Statement_Start;
Start_Set := True;
else
Cache.Class := Pair.Class;
end if;
Cache.Statement_ID := Tree.ID (Nonterm);
Cache.Containing_Pos := Containing_Pos;
end;
else
Cursor := Data.Navigate_Caches.Insert
((Pos => Cache_Pos,
Statement_ID => Tree.ID (Nonterm),
ID => Token.ID,
Length => Length (Token.Char_Region),
Class => (if Override_Start_Set then Statement_Start else Pair.Class),
Containing_Pos => Containing_Pos,
others => Nil));
end if;
Data.End_Positions.Append (Cursor);
if First_Item then
First_Item := False;
if Override_Start_Set or Pair.Class = Statement_Start then
Override_Start_Set := False;
Containing_Pos := (True, Token.Char_Region.First);
-- Set containing on all contained caches
declare
use Navigate_Cache_Trees;
Iterator : constant Navigate_Cache_Trees.Iterator := Data.Navigate_Caches.Iterate;
Cursor : Navigate_Cache_Trees.Cursor := Find_In_Range
(Iterator, Ascending, Nonterm_Tok.Char_Region.First + 1, -- don't set containing on start
Nonterm_Tok.Char_Region.Last);
begin
loop
exit when not Has_Element (Cursor);
declare
Cache : Navigate_Cache_Type renames Data.Navigate_Caches (Cursor);
begin
if not Cache.Containing_Pos.Set then
Cache.Containing_Pos := Containing_Pos;
end if;
exit when Nonterm_Tok.Char_Region.Last < Cache.Pos + 1;
end;
Cursor := Iterator.Next (Cursor);
end loop;
end;
end if;
end if;
if Pair.Class = Statement_End and Containing_Pos.Set then
Set_End (Data, Containing_Pos.Item, Cache_Pos);
end if;
end;
else
-- Token.Byte_Region is null
if First_Item and Pair.Class = Statement_Start then
Override_Start_Set := True;
end if;
end if;
end loop;
end Statement_Action;
procedure Name_Action
(Data : in out Parse_Data_Type;
Tree : in WisiToken.Syntax_Trees.Tree;
Nonterm : in Syntax_Trees.Valid_Node_Index;
Tokens : in WisiToken.Syntax_Trees.Valid_Node_Index_Array;
Name : in WisiToken.Positive_Index_Type)
is
use all type WisiToken.Syntax_Trees.Node_Label;
begin
if not (Name in Tokens'Range) then
declare
Token : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Tokens (Tokens'First));
begin
raise Fatal_Error with Error_Message
(File_Name => -Data.Source_File_Name,
Line => Token.Line,
Column => Token.Column,
Message => "wisi-name-action: " & Trimmed_Image (Tree.Production_ID (Nonterm)) & " name (" &
Trimmed_Image (Name) & ") not in Tokens range (" & SAL.Peek_Type'Image (Tokens'First) & " .." &
SAL.Peek_Type'Image (Tokens'Last) & "); bad grammar action.");
end;
end if;
if Tree.Label (Tokens (Name)) = Syntax_Trees.Virtual_Terminal then
return;
end if;
declare
use Name_Cache_Trees;
Name_Token : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Tokens (Name));
Cursor : constant Name_Cache_Trees.Cursor := Find
(Data.Name_Caches.Iterate, Name_Token.Char_Region.First,
Direction => Name_Cache_Trees.Unknown);
begin
if Name_Token.Char_Region = Null_Buffer_Region then
return;
elsif Has_Element (Cursor) then
raise Fatal_Error with Error_Message
(File_Name => -Data.Source_File_Name,
Line => Name_Token.Line,
Column => Name_Token.Column,
Message => "wisi-name-action: name set twice.");
else
Data.Name_Caches.Insert (Name_Token.Char_Region);
end if;
end;
end Name_Action;
procedure Containing_Action
(Data : in out Parse_Data_Type;
Tree : in Syntax_Trees.Tree;
Nonterm : in Syntax_Trees.Valid_Node_Index;
Tokens : in Syntax_Trees.Valid_Node_Index_Array;
Containing : in Positive_Index_Type;
Contained : in Positive_Index_Type)
is
pragma Unreferenced (Data, Tree, Nonterm, Tokens, Containing, Contained);
begin
null;
end Containing_Action;
function "+" (Item : in Token_ID) return Token_ID_Lists.List
is begin
return Result : Token_ID_Lists.List do
Result.Append (Item);
end return;
end "+";
function "&" (List : in Token_ID_Lists.List; Item : in Token_ID) return Token_ID_Lists.List
is begin
return Result : Token_ID_Lists.List := List do
Result.Append (Item);
end return;
end "&";
function "&" (Left, Right : in Token_ID) return Token_ID_Lists.List
is begin
return Result : Token_ID_Lists.List do
Result.Append (Left);
Result.Append (Right);
end return;
end "&";
procedure Motion_Action
(Data : in out Parse_Data_Type;
Tree : in Syntax_Trees.Tree;
Nonterm : in Syntax_Trees.Valid_Node_Index;
Tokens : in Syntax_Trees.Valid_Node_Index_Array;
Params : in Motion_Param_Array)
is
-- [2] wisi-motion-action
use Navigate_Cache_Trees;
use all type Ada.Containers.Count_Type;
Start : Nil_Buffer_Pos := (Set => False);
Iter : constant Iterator := Data.Navigate_Caches.Iterate;
Prev_Cache_Cur : Cursor;
Cache_Cur : Cursor;
begin
for Param of Params loop
if Tree.Byte_Region (Tokens (Param.Index)) /= Null_Buffer_Region then
declare
use all type WisiToken.Syntax_Trees.Node_Label;
Token : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Tokens (Param.Index));
Region : constant Buffer_Region := Token.Char_Region;
begin
if not Start.Set then
Start := (True, Region.First);
end if;
case Tree.Label (Tokens (Param.Index)) is
when Shared_Terminal =>
Cache_Cur := Find (Iter, Region.First);
when Virtual_Terminal | Virtual_Identifier =>
return;
when Syntax_Trees.Nonterm =>
Is_Nonterm := True;
if Tree.Is_Virtual (Tokens (Param.Index)) then
return;
elsif Param.ID = Invalid_Token_ID then
Cache_Cur := Find (Iter, Region.First);
else
Cache_Cur := Find (Iter, Ascending, Region.First, Region.Last, Predicate'access);
loop
exit when not Has_Element (Cache_Cur);
exit when Data.Navigate_Caches (Cache_Cur).Pos > Region.Last -- ERROR: missing semicolon
if Data.Navigate_Caches (Cache_Cur).ID = Param.ID then
Cache_Cur := No_Element;
exit;
end if;
Cache_Cur := Next (Iter, Cache_Cur);
end loop;
end if;
end case;
if not Has_Element (Cache_Cur) then
raise Fatal_Error with Error_Message
(File_Name => -Data.Source_File_Name,
Line => Token.Line,
Column => Token.Column,
Message => "wisi-motion-action: token " &
WisiToken.Image (Token.ID, Data.Descriptor.all) &
" has no cache; add to statement-action for " &
Trimmed_Image (Tree.Production_ID (Nonterm)) & ".");
end if;
if Has_Element (Prev_Cache_Cur) then
declare
Cache : Navigate_Cache_Type renames Data.Navigate_Caches (Cache_Cur);
begin
if not Cache.Prev_Pos.Set then
Cache.Prev_Pos := (True, Data.Navigate_Caches (Prev_Cache_Cur).Pos);
end if;
end;
declare
Prev_Cache : Navigate_Cache_Type renames Data.Navigate_Caches (Prev_Cache_Cur);
begin
if not Prev_Cache.Next_Pos.Set then
Prev_Cache.Next_Pos := (True, Region.First);
end if;
end;
end if;
loop
-- Set Prev_Cache_Cur to last motion cache in nonterm chain
exit when not Cache_Cur.Next.Set;
Cache_Cur := Find (Iter, Cache_Cur.Next.Pos);
pragma Assert (Has_Element, Cache_Cur); -- otherwise there's a bug in this subprogram.
end loop;
Prev_Cache_Cur := Cache_Cur;
end;
end if;
end loop;
end Motion_Action;
procedure Face_Apply_Action
(Data : in out Parse_Data_Type;
Tree : in Syntax_Trees.Tree;
Nonterm : in Syntax_Trees.Valid_Node_Index;
Tokens : in Syntax_Trees.Valid_Node_Index_Array;
Params : in Face_Apply_Param_Array)
is
pragma Unreferenced (Nonterm);
-- [2] wisi-face-apply-action
use Face_Cache_Trees;
Iter : constant Iterator := Data.Face_Caches.Iterate;
Cache_Cur : Cursor;
Suffix_Cur : Cursor;
begin
for Param of Params loop
if Tree.Byte_Region (Tokens (Param.Index)) /= Null_Buffer_Region then
declare
Token : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Tokens (Param.Index));
begin
Cache_Cur := Find (Iter, Token.Char_Region.First, Direction => Ascending);
if Has_Element (Cache_Cur) then
declare
Cache : Face_Cache_Type renames Data.Face_Caches (Cache_Cur);
begin
case Cache.Class is
when Prefix =>
Cache.Face := (True, Param.Prefix_Face);
-- Check for suffix
Suffix_Cur := Next (Iter, Cache_Cur);
if Has_Element (Suffix_Cur) then
declare
Suf_Cache : Face_Cache_Type renames Data.Face_Caches (Suffix_Cur);
begin
if Suffix = Suf_Cache.Class and
Inside (Suf_Cache.Region.First, Token.Char_Region)
then
Suf_Cache.Face := (True, Param.Suffix_Face);
end if;
end;
end if;
when Suffix =>
Cache.Face := (True, Param.Suffix_Face);
end case;
end;
else
Data.Face_Caches.Insert ((Token.Char_Region, Suffix, (True, Param.Suffix_Face)));
end if;
end;
end if;
end loop;
end Face_Apply_Action;
procedure Face_Apply_List_Action
(Data : in out Parse_Data_Type;
Tree : in Syntax_Trees.Tree;
Nonterm : in Syntax_Trees.Valid_Node_Index;
Tokens : in Syntax_Trees.Valid_Node_Index_Array;
Params : in Face_Apply_Param_Array)
is
pragma Unreferenced (Nonterm);
-- [2] wisi-face-apply-list-action
use Face_Cache_Trees;
Iter : constant Iterator := Data.Face_Caches.Iterate;
Cache_Cur : Cursor;
begin
for Param of Params loop
if Tree.Byte_Region (Tokens (Param.Index)) /= Null_Buffer_Region then
declare
Token : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Tokens (Param.Index));
begin
Cache_Cur := Find_In_Range (Iter, Ascending, Token.Char_Region.First, Token.Char_Region.Last);
loop
exit when not Has_Element (Cache_Cur) or else
Data.Face_Caches (Cache_Cur).Region.First > Token.Char_Region.Last;
declare
Cache : Face_Cache_Type renames Data.Face_Caches (Cache_Cur);
begin
case Cache.Class is
when Prefix =>
Cache.Face := (True, Param.Prefix_Face);
when Suffix =>
Cache.Face := (True, Param.Suffix_Face);
end case;
end;
Cache_Cur := Next (Iter, Cache_Cur);
end loop;
end;
end if;
end loop;
end Face_Apply_List_Action;
procedure Face_Mark_Action
(Data : in out Parse_Data_Type;
Tree : in Syntax_Trees.Tree;
Nonterm : in Syntax_Trees.Valid_Node_Index;
Tokens : in Syntax_Trees.Valid_Node_Index_Array;
Params : in Face_Mark_Param_Array)
is
pragma Unreferenced (Nonterm);
-- [2] wisi-face-apply-action
use Face_Cache_Trees;
Iter : constant Iterator := Data.Face_Caches.Iterate;
Cache_Cur : Cursor;
begin
for Param of Params loop
if Tree.Byte_Region (Tokens (Param.Index)) /= Null_Buffer_Region then
declare
Token : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Tokens (Param.Index));
begin
Cache_Cur := Find (Iter, Token.Char_Region.First, Direction => Ascending);
if Has_Element (Cache_Cur) then
declare
Cache : Face_Cache_Type renames Data.Face_Caches (Cache_Cur);
Other_Cur : Cursor := Find_In_Range
(Iter, Ascending, Cache.Region.Last + 1, Token.Char_Region.Last);
Temp : Cursor;
begin
loop
exit when not Has_Element (Other_Cur) or else
Data.Face_Caches (Other_Cur).Region.First > Token.Char_Region.Last;
Temp := Other_Cur;
Other_Cur := Next (Iter, Other_Cur);
Delete (Data.Face_Caches, Temp);
end loop;
Cache.Class := Param.Class;
Cache.Region.Last := Token.Char_Region.Last;
end;
else
Data.Face_Caches.Insert ((Token.Char_Region, Param.Class, (Set => False)));
end if;
end;
end if;
end loop;
end Face_Mark_Action;
procedure Face_Remove_Action
(Data : in out Parse_Data_Type;
Tree : in Syntax_Trees.Tree;
Nonterm : in Syntax_Trees.Valid_Node_Index;
Tokens : in Syntax_Trees.Valid_Node_Index_Array;
Params : in Face_Remove_Param_Array)
is
pragma Unreferenced (Nonterm);
-- [2] wisi-face-remove-action
use Face_Cache_Trees;
Iter : constant Iterator := Data.Face_Caches.Iterate;
Cache_Cur : Cursor;
Temp : Cursor;
begin
for I of Params loop
if Tree.Byte_Region (Tokens (I)) /= Null_Buffer_Region then
declare
Token : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Tokens (I));
begin
Cache_Cur := Find_In_Range (Iter, Ascending, Token.Char_Region.First, Token.Char_Region.Last);
loop
exit when not Has_Element (Cache_Cur) or else
Data.Face_Caches (Cache_Cur).Region.First > Token.Char_Region.Last;
Temp := Cache_Cur;
Cache_Cur := Next (Iter, Cache_Cur);
Delete (Data.Face_Caches, Temp);
end loop;
end;
end if;
end loop;
end Face_Remove_Action;
function "+" (Item : in Integer) return Indent_Arg_Arrays.Vector
is begin
return Result : Indent_Arg_Arrays.Vector do
Result.Append (Item);
end return;
end "+";
function "&" (List : in Indent_Arg_Arrays.Vector; Item : in Integer) return Indent_Arg_Arrays.Vector
is begin
return Result : Indent_Arg_Arrays.Vector := List do
Result.Append (Item);
end return;
end "&";
function "&" (Left, Right : in Integer) return Indent_Arg_Arrays.Vector
is begin
return Result : Indent_Arg_Arrays.Vector do
Result.Append (Left);
Result.Append (Right);
end return;
end "&";
function Image (Item : in Simple_Indent_Param) return String
is begin
return "(" & Simple_Indent_Param_Label'Image (Item.Label) &
(case Item.Label is
when None => "",
when Int => Integer'Image (Item.Int_Delta),
when Anchored_Label => Positive_Index_Type'Image (Item.Anchored_Index) & "," &
Integer'Image (Item.Anchored_Delta),
when Language => "<language_function>") & ")";
end Image;
function Image (Item : in Indent_Param) return String
is begin
return "(" & Indent_Param_Label'Image (Item.Label) & ", " &
(case Item.Label is
when Simple => Image (Item.Param),
when Hanging_Label =>
Image (Item.Hanging_Delta_1) & ", " & Image (Item.Hanging_Delta_2) & ")");
end Image;
function Image (Item : in Indent_Pair) return String
is begin
return "(" & Image (Item.Code_Delta) &
(if Item.Comment_Present
then ", " & Image (Item.Comment_Delta)
else "") & ")";
end Image;
procedure Indent_Action_0
(Data : in out Parse_Data_Type'Class;
Tree : in Syntax_Trees.Tree;
Nonterm : in Syntax_Trees.Valid_Node_Index;
Tokens : in Syntax_Trees.Valid_Node_Index_Array;
Params : in Indent_Param_Array)
is begin
if Trace_Action > Outline then
Ada.Text_IO.Put_Line (";; indent_action_0: " & Tree.Image (Nonterm, Data.Descriptor.all));
end if;
-- [2] wisi-indent-action
for I in Tokens'Range loop
if Tree.Byte_Region (Tokens (I)) /= Null_Buffer_Region and
I in Params'Range -- in some translated EBNF, not every token has an indent param
then
declare
use all type WisiToken.Syntax_Trees.Node_Index;
use all type SAL.Base_Peek_Type;
Tree_Token : constant Syntax_Trees.Valid_Node_Index := Tokens (I);
Token : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Tree_Token);
Pair : Indent_Pair renames Params (I);
Code_Delta : Delta_Type;
Comment_Param : Indent_Param;
Comment_Param_Set : Boolean := False;
Comment_Delta : Delta_Type;
begin
if Trace_Action > Detail then
Ada.Text_IO.Put_Line
(";; indent_action_0 a: " & Tree.Image (Tree_Token, Data.Descriptor.all) & ": " & Image (Pair));
end if;
if Token.First_Indent_Line /= Invalid_Line_Number then
Code_Delta := Indent_Compute_Delta
(Data, Tree, Tokens, Pair.Code_Delta, Tree_Token, Indenting_Comment => False);
Indent_Token_1 (Data, Token, Code_Delta, Indenting_Comment => False);
end if;
if Token.First_Trailing_Comment_Line /= Invalid_Line_Number then
if Pair.Comment_Present then
Comment_Param := Pair.Comment_Delta;
Comment_Param_Set := True;
elsif I < Tokens'Last then
Comment_Param := Params (I + 1).Code_Delta;
Comment_Param_Set := True;
end if;
if Comment_Param_Set then
Comment_Delta := Indent_Compute_Delta
(Data, Tree, Tokens, Comment_Param, Tree_Token, Indenting_Comment => True);
Indent_Token_1 (Data, Token, Comment_Delta, Indenting_Comment => True);
end if;
end if;
end;
end if;
end loop;
end Indent_Action_0;
procedure Indent_Action_1
(Data : in out Parse_Data_Type'Class;
Tree : in Syntax_Trees.Tree;
Nonterm : in Syntax_Trees.Valid_Node_Index;
Tokens : in Syntax_Trees.Valid_Node_Index_Array;
N : in Positive_Index_Type;
Params : in Indent_Param_Array)
is
use all type Syntax_Trees.Node_Label;
begin
-- [2] wisi-indent-action*
for I in Tokens'First .. N loop
if Tree.Label (Tokens (I)) /= Virtual_Terminal and then
Get_Aug_Token (Data, Tree, Tokens (I)).First
then
Indent_Action_0 (Data, Tree, Nonterm, Tokens, Params);
return;
end if;
end loop;
end Indent_Action_1;
function Indent_Hanging_1
(Data : in out Parse_Data_Type;
Tree : in Syntax_Trees.Tree;
Tokens : in Syntax_Trees.Valid_Node_Index_Array;
Tree_Indenting : in Syntax_Trees.Valid_Node_Index;
Indenting_Comment : in Boolean;
Delta_1 : in Simple_Indent_Param;
Delta_2 : in Simple_Indent_Param;
Option : in Boolean;
Accumulate : in Boolean)
return Delta_Type
is
Indenting_Token : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Tree_Indenting);
begin
-- [2] wisi-elisp-parse--hanging-1
if Indenting_Comment then
return Indent_Compute_Delta
(Data, Tree, Tokens, (Simple, Delta_1), Tree_Indenting, Indenting_Comment);
else
return
(Hanging,
Hanging_First_Line => Indenting_Token.Line,
Hanging_Paren_State => Indenting_Token.Paren_State,
Hanging_Delta_1 => Indent_Compute_Delta
(Data, Tree, Tokens, (Simple, Delta_1), Tree_Indenting, Indenting_Comment).Simple_Delta,
Hanging_Delta_2 =>
(if (not Option) or
Indenting_Token.Line = Indenting_Token.First_Indent_Line -- first token in tok is first on line
then Indent_Compute_Delta
(Data, Tree, Tokens, (Simple, Delta_2), Tree_Indenting, Indenting_Comment).Simple_Delta
else Indent_Compute_Delta
(Data, Tree, Tokens, (Simple, Delta_1), Tree_Indenting, Indenting_Comment).Simple_Delta),
Hanging_Accumulate => Accumulate);
end if;
end Indent_Hanging_1;
procedure Put_Language_Action
(Data : in Parse_Data_Type;
Content : in String)
is
pragma Unreferenced (Data);
begin
Ada.Text_IO.Put_Line ("[" & Language_Action_Code & Content & "]");
end Put_Language_Action;
procedure Put (Data : in out Parse_Data_Type; Parser : in Parse.Base_Parser'Class)
is
use all type Ada.Containers.Count_Type;
Last_Term : constant Base_Token_Index := Parser.Tree.Max_Terminal_Index (Parser.Tree.Root);
function Get_Last_Char_Pos return Buffer_Pos
is begin
if Parser.Terminals.Length = 0 then
-- All comments, or empty
if Data.Leading_Non_Grammar.Length > 0 then
return Data.Leading_Non_Grammar (Data.Leading_Non_Grammar.Last_Index).Char_Region.Last;
else
return Buffer_Pos'First;
end if;
else
if Last_Term = Invalid_Token_Index then
-- All grammar tokens inserted by recover
if Data.Leading_Non_Grammar.Length > 0 then
return Data.Leading_Non_Grammar (Data.Leading_Non_Grammar.Last_Index).Char_Region.Last;
else
return Buffer_Pos'First;
end if;
else
if Data.Terminals (Last_Term).Non_Grammar.Length > 0 then
return Data.Terminals (Last_Term).Non_Grammar
(Data.Terminals (Last_Term).Non_Grammar.Last_Index).Char_Region.Last;
else
return Parser.Terminals (Last_Term).Char_Region.Last;
end if;
end if;
end if;
end Get_Last_Char_Pos;
Last_Char_Pos : constant Buffer_Pos := Get_Last_Char_Pos;
function Get_Last_Line return Line_Number_Type
is begin
for I in Data.Line_Begin_Pos.First_Index .. Data.Line_Begin_Pos.Last_Index loop
if Data.Line_Begin_Pos (I) = Invalid_Buffer_Pos then
raise SAL.Programmer_Error with "line_begin_pos" & Line_Number_Type'Image (I) & " invalid";
end if;
if Data.Line_Begin_Pos (I) > Last_Char_Pos then
if I > Line_Number_Type'First then
return I - 1;
else
return I;
end if;
end if;
end loop;
return Data.Line_Begin_Pos.Last_Index;
end Get_Last_Line;
begin
if Trace_Action > Outline then
Ada.Text_IO.Put_Line
(";; last_char_pos:" & Buffer_Pos'Image (Last_Char_Pos + 1) &
" last_line:" & Line_Number_Type'Image (Get_Last_Line));
end if;
-- +1 to match Emacs region
Ada.Text_IO.Put_Line ('[' & End_Code & Buffer_Pos'Image (Last_Char_Pos + 1) & ']');
case Data.Post_Parse_Action is
when Navigate =>
for Cache of Data.Navigate_Caches loop
Put (Cache);
end loop;
for Cache of Data.Name_Caches loop
Put (Cache);
end loop;
when Face =>
for Cache of Data.Face_Caches loop
Put (Cache);
end loop;
when Indent =>
Resolve_Anchors (Data);
if Trace_Action > Outline then
Ada.Text_IO.Put_Line (";; indent leading non_grammar");
end if;
for Token of Data.Leading_Non_Grammar loop
if Token.First then
Put (Token.Line, (Int, Data.Begin_Indent));
end if;
end loop;
-- It may be that not all lines in Data.Indents were parsed.
if Trace_Action > Outline then
Ada.Text_IO.Put_Line (";; indent grammar");
end if;
for I in Data.Indents.First_Index .. Get_Last_Line loop
Put (I, Data.Indents (I));
end loop;
end case;
end Put;
procedure Put (Lexer_Errors : in Lexer.Error_Lists.List)
is begin
for Item of Lexer_Errors loop
Ada.Text_IO.Put_Line
('[' & Lexer_Error_Code & Buffer_Pos'Image (Item.Char_Pos) &
" ""lexer error" &
(if Item.Recover_Char (1) = ASCII.NUL
then """"
elsif Item.Recover_Char (1) = '"'
then """ ?\"""
else """ ?" & Item.Recover_Char (1)) &
"]");
if Item.Recover_Char (2) /= ASCII.NUL then
raise SAL.Programmer_Error with "lexer error with non-ascii or multiple repair char";
end if;
end loop;
end Put;
procedure Put
(Data : in Parse_Data_Type;
Lexer_Errors : in Lexer.Error_Lists.List;
Parse_Errors : in Parse.LR.Parse_Error_Lists.List;
Tree : in Syntax_Trees.Tree)
is
use all type SAL.Base_Peek_Type;
use Ada.Text_IO;
use Semantic_Checks;
function Safe_Pos (Node : in Syntax_Trees.Valid_Node_Index) return Buffer_Pos
is
-- Return a reasonable position for the error at Node.
--
-- In a successful parse with error recovery, Node is a terminal with
-- an augmented token in Data.Terminals, so that is the first
-- choice.
--
-- If this is an error due to a bad recovery, Node may be a virtual
-- token, with no position information, so we try to get information
-- from its parent.
use Syntax_Trees;
N : Node_Index := Node;
begin
loop
if Tree.Label (N) /= Virtual_Terminal then
declare
Ref : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, N);
begin
if Ref.Char_Region /= Null_Buffer_Region then
return Ref.Element.Char_Region.First;
end if;
end;
end if;
N := Tree.Parent (N);
exit when N = Invalid_Node_Index;
end loop;
return Buffer_Pos'First;
end Safe_Pos;
function Safe_Pos (Token : in Recover_Token) return Buffer_Pos
is begin
if Token.Name /= Null_Buffer_Region then
return Token.Name.First;
elsif Token.Byte_Region = Null_Buffer_Region then
return Buffer_Pos'First;
else
return Token.Byte_Region.First;
end if;
end Safe_Pos;
begin
Put (Lexer_Errors);
for Item of Parse_Errors loop
-- We don't include parser id here; not very useful.
case Item.Label is
when Parse.LR.Action =>
Put_Line
('[' & Parser_Error_Code & Buffer_Pos'Image (Safe_Pos (Item.Error_Token)) &
" ""syntax error: expecting " & Image (Item.Expecting, Data.Descriptor.all) &
", found '" & Image (Tree.ID (Item.Error_Token), Data.Descriptor.all) & "'""]");
when Parse.LR.Check =>
Put_Line
('[' & Check_Error_Code & Integer'Image
(Semantic_Checks.Check_Status_Label'Pos (Item.Check_Status.Label)) &
(case Item.Check_Status.Label is
when Ok => "",
when Error =>
Buffer_Pos'Image (Safe_Pos (Item.Check_Status.Begin_Name)) &
Buffer_Pos'Image (Safe_Pos (Item.Check_Status.End_Name)) &
" ""block name error""]"));
when Parse.LR.Message =>
Put_Line
('[' & Parser_Error_Code & Buffer_Pos'Image (Buffer_Pos'First) &
" """ & (-Item.Msg) & """]");
end case;
if Item.Recover.Stack.Depth > 0 then
Put (Item.Recover, Data.Terminals, Data.Descriptor.all, Data.Embedded_Quote_Escape_Doubled);
end if;
end loop;
end Put;
procedure Put_Error (Data : in Parse_Data_Type; Line_Number : in Line_Number_Type; Message : in String)
is
use Ada.Text_IO;
begin
Put_Line ("(error """ & Error_Message (-Data.Source_File_Name, Line_Number, 0, Message) & """)");
end Put_Error;
----------
-- Spec visible private subprograms, alphabetical
function Image (Item : in Simple_Delta_Type) return String
is begin
return "(" & Simple_Delta_Labels'Image (Item.Label) &
(case Item.Label is
when None => "",
when Int => Integer'Image (Item.Int_Delta),
when Anchored => Integer'Image (Item.Anchored_ID) & Integer'Image (Item.Anchored_Delta) & " " &
Boolean'Image (Item.Anchored_Accumulate))
& ")";
end Image;
function Image (Item : in Delta_Type) return String
is begin
return "(" & Delta_Labels'Image (Item.Label) &
(case Item.Label is
when Simple => " " & Image (Item.Simple_Delta),
when Hanging => Line_Number_Type'Image (Item.Hanging_First_Line) & Integer'Image
(Item.Hanging_Paren_State) &
" " & Image (Item.Hanging_Delta_1) & " " & Image (Item.Hanging_Delta_2) & " " &
Boolean'Image (Item.Hanging_Accumulate)) & ")";
end Image;
function Current_Indent_Offset
(Data : in Parse_Data_Type;
Anchor_Token : in Augmented_Token'Class;
Offset : in Integer)
return Integer
is begin
return Offset + Integer (Anchor_Token.Char_Region.First - Data.Line_Begin_Pos (Anchor_Token.Line));
end Current_Indent_Offset;
function Find
(Data : in Parse_Data_Type;
ID : in Token_ID;
Token : in Augmented_Token'Class)
return Base_Token_Index
is begin
-- linear search for ID.
for I in Token.First_Terminals_Index .. Token.Last_Terminals_Index loop
if Data.Terminals (I).ID = ID then
return I;
end if;
end loop;
return Augmented_Token_Arrays.No_Index;
end Find;
function First_Line
(Token : in Augmented_Token;
Indenting_Comment : in Boolean)
return Line_Number_Type
is begin
return
(if Indenting_Comment then
(if Token.First_Trailing_Comment_Line = Invalid_Line_Number
then Token.Line
else Token.First_Trailing_Comment_Line)
else
(if Token.First_Indent_Line = Invalid_Line_Number
then Token.Line
else Token.First_Indent_Line));
end First_Line;
function Get_Aug_Token
(Data : in Parse_Data_Type'Class;
Tree : in Syntax_Trees.Tree'Class;
Tree_Index : in Syntax_Trees.Valid_Node_Index)
return Aug_Token_Ref
is
use all type Syntax_Trees.Node_Label;
begin
return
(case Tree.Label (Tree_Index) is
when Shared_Terminal => To_Aug_Token_Ref (Data.Terminals (Tree.Terminal (Tree_Index))),
when Virtual_Terminal => raise SAL.Programmer_Error with "wisi_runtime.get_aug_token virtual terminal",
when Virtual_Identifier => raise SAL.Programmer_Error with "wisi_runtime.get_aug_token virtual identifier",
when Nonterm => To_Aug_Token_Ref (Tree.Augmented (Tree_Index)));
end Get_Aug_Token;
function Get_Text
(Data : in Parse_Data_Type;
Tree : in WisiToken.Syntax_Trees.Tree;
Tree_Index : in WisiToken.Syntax_Trees.Valid_Node_Index)
return String
is
use all type Syntax_Trees.Node_Label;
begin
case Tree.Label (Tree_Index) is
when Shared_Terminal | Nonterm =>
return Data.Lexer.Buffer_Text (Tree.Byte_Region (Tree_Index));
when Virtual_Terminal | Virtual_Identifier =>
raise SAL.Programmer_Error;
end case;
end Get_Text;
function Elisp_Escape_Quotes (Item : in String) return String
is
Result : String (Item'First .. Item'First + Item'Length * 2);
Last : Integer := Item'First - 1;
begin
for I in Item'Range loop
if Item (I) = '"' then
Last := Last + 1;
Result (Last) := '\';
end if;
Last := Last + 1;
Result (Last) := Item (I);
end loop;
return Result (Result'First .. Last);
end Elisp_Escape_Quotes;
overriding
function Image
(Item : in Augmented_Token;
Descriptor : in WisiToken.Descriptor)
return String
is
ID_Image : constant String := Image (Item.ID, Descriptor);
begin
if Item.Line /= Invalid_Line_Number and Trace_Action <= Detail then
return "(" & ID_Image &
Line_Number_Type'Image (Item.Line) & ":" & Trimmed_Image (Integer (Item.Column)) & ")";
elsif Item.Char_Region = Null_Buffer_Region then
return "(" & ID_Image & ")";
else
return "(" & ID_Image & ", " & Image (Item.Char_Region) & ")";
end if;
end Image;
function Indent_Anchored_2
(Data : in out Parse_Data_Type;
Anchor_Line : in Line_Number_Type;
Last_Line : in Line_Number_Type;
Offset : in Integer;
Accumulate : in Boolean)
return Delta_Type
is
-- [2] wisi-elisp-parse--anchored-2; return an anchored delta
use Anchor_ID_Vectors;
-- We can't use a Reference here, because the Element in reference
-- types is constrained (as are all allocated objects of access
-- types; AARM 4.8 (6/3)), and we may need to change the Label.
Indent : Indent_Type := Data.Indents (Anchor_Line);
Anchor_ID : constant Integer := 1 + Max_Anchor_ID (Data, Anchor_Line, Last_Line);
begin
Data.Max_Anchor_ID := Integer'Max (Data.Max_Anchor_ID, Anchor_ID);
case Indent.Label is
when Not_Set =>
Indent := (Anchor_Nil, To_Vector (Anchor_ID, 1));
if Trace_Action > Extra then
Ada.Text_IO.Put_Line
(";; indent_anchored: " & Line_Number_Type'Image (Anchor_Line) & " => " & Image (Indent));
end if;
when Int =>
Indent := (Anchor_Int, To_Vector (Anchor_ID, 1), Indent.Int_Indent);
if Trace_Action > Extra then
Ada.Text_IO.Put_Line
(";; indent_anchored: " & Line_Number_Type'Image (Anchor_Line) & " => " & Image (Indent));
end if;
when Anchor_Nil =>
Indent.Anchor_Nil_IDs := Anchor_ID & Indent.Anchor_Nil_IDs;
when Anchor_Int =>
Indent.Anchor_Int_IDs := Anchor_ID & Indent.Anchor_Int_IDs;
when Anchored =>
Indent := (Anchor_Anchored, To_Vector (Anchor_ID, 1), Indent.Anchored_ID, Indent.Anchored_Delta);
when Anchor_Anchored =>
Indent.Anchor_Anchored_IDs := Anchor_ID & Indent.Anchor_Anchored_IDs;
end case;
Data.Indents.Replace_Element (Anchor_Line, Indent);
return (Simple, (Anchored, Anchor_ID, Offset, Accumulate));
end Indent_Anchored_2;
function Indent_Compute_Delta
(Data : in out Parse_Data_Type'Class;
Tree : in Syntax_Trees.Tree;
Tokens : in Syntax_Trees.Valid_Node_Index_Array;
Param : in Indent_Param;
Tree_Indenting : in Syntax_Trees.Valid_Node_Index;
Indenting_Comment : in Boolean)
return Delta_Type
is
Indenting_Token : constant Aug_Token_Ref := Get_Aug_Token (Data, Tree, Tree_Indenting);
begin
-- [2] wisi-elisp-parse--indent-compute-delta, which evals wisi-anchored*, wisi-hanging*.
case Param.Label is
when Simple =>
case Param.Param.Label is
when None =>
return (Simple, (Label => None));
when Int =>
return (Simple, (Int, Param.Param.Int_Delta));
when Anchored_Label =>
if Indenting_Token.Byte_Region = Null_Buffer_Region or
Tree.Byte_Region (Tokens (Param.Param.Anchored_Index)) = Null_Buffer_Region
then
-- One of these is an entirely virtual token
return Null_Delta;
else
declare
Anchor_Token : constant Aug_Token_Ref := Get_Aug_Token
(Data, Tree, Tokens (Param.Param.Anchored_Index));
begin
case Anchored_Label'(Param.Param.Label) is
when Anchored_0 =>
-- [2] wisi-anchored, wisi-anchored-1
return Indent_Anchored_2
(Data,
Anchor_Line => Anchor_Token.Line,
Last_Line => Indenting_Token.Last_Line (Indenting_Comment),
Offset => Current_Indent_Offset (Data, Anchor_Token, Param.Param.Anchored_Delta),
Accumulate => True);
when Anchored_1 =>
-- [2] wisi-anchored%
return Indent_Anchored_2
(Data,
Anchor_Line => Anchor_Token.Line,
Last_Line => Indenting_Token.Last_Line (Indenting_Comment),
Offset => Paren_In_Anchor_Line (Data, Anchor_Token, Param.Param.Anchored_Delta),
Accumulate => True);
when Anchored_2 =>
-- [2] wisi-anchored%-
return Indent_Anchored_2
(Data,
Anchor_Line => Anchor_Token.Line,
Last_Line => Indenting_Token.Last_Line (Indenting_Comment),
Offset => Paren_In_Anchor_Line (Data, Anchor_Token, Param.Param.Anchored_Delta),
Accumulate => False);
when Anchored_3 =>
-- [2] wisi-anchored*
if Indenting_Token.First then
return Indent_Anchored_2
(Data,
Anchor_Line => Anchor_Token.Line,
Last_Line => Indenting_Token.Last_Line (Indenting_Comment),
Offset => Current_Indent_Offset (Data, Anchor_Token, Param.Param.Anchored_Delta),
Accumulate => True);
else
return Null_Delta;
end if;
when Anchored_4 =>
-- [2] wisi-anchored*-
return Indent_Anchored_2
(Data,
Anchor_Line => Anchor_Token.Line,
Last_Line => Indenting_Token.Last_Line (Indenting_Comment),
Offset => Current_Indent_Offset (Data, Anchor_Token, Param.Param.Anchored_Delta),
Accumulate => False);
end case;
end;
end if;
when Language =>
return Param.Param.Function_Ptr
(Data, Tree, Tokens, Tree_Indenting, Indenting_Comment, Param.Param.Args);
end case;
when Hanging_Label =>
case Hanging_Label'(Param.Label) is
when Hanging_0 => -- wisi-hanging
return Indent_Hanging_1
(Data, Tree, Tokens, Tree_Indenting, Indenting_Comment, Param.Hanging_Delta_1,
Param.Hanging_Delta_2,
Option => False, Accumulate => True);
when Hanging_1 => -- wisi-hanging-
return Indent_Hanging_1
(Data, Tree, Tokens, Tree_Indenting, Indenting_Comment, Param.Hanging_Delta_1,
Param.Hanging_Delta_2,
Option => False, Accumulate => False);
when Hanging_2 => -- wisi-hanging%
return Indent_Hanging_1
(Data, Tree, Tokens, Tree_Indenting, Indenting_Comment, Param.Hanging_Delta_1,
Param.Hanging_Delta_2,
Option => True, Accumulate => True);
when Hanging_3 => -- wisi-hanging%-
return Indent_Hanging_1
(Data, Tree, Tokens, Tree_Indenting, Indenting_Comment, Param.Hanging_Delta_1,
Param.Hanging_Delta_2,
Option => True, Accumulate => False);
end case;
end case;
end Indent_Compute_Delta;
procedure Indent_Token_1
(Data : in out Parse_Data_Type;
Indenting_Token : in Augmented_Token'Class;
Delta_Indent : in Delta_Type;
Indenting_Comment : in Boolean)
is
-- Aplly Delta_Indent to Indenting_Token
First_Line : constant Line_Number_Type := Indenting_Token.First_Line (Indenting_Comment);
Last_Line : constant Line_Number_Type := Indenting_Token.Last_Line (Indenting_Comment);
begin
if Trace_Action > Detail then
Ada.Text_IO.Put_Line
(";; indent_token_1: " & Indenting_Token.Image (Data.Descriptor.all) & " " & Image (Delta_Indent) &
Line_Number_Type'Image (First_Line) & " .." & Line_Number_Type'Image (Last_Line) &
(if Indenting_Comment then " comment" else ""));
end if;
for Line in First_Line .. Last_Line loop
if Data.Indent_Comment_Col_0 then
declare
use all type Ada.Text_IO.Count;
Indent : Boolean := True;
begin
if Data.Line_Begin_Token.all (Line - 1) /= Augmented_Token_Arrays.No_Index then
for Tok of Data.Terminals (Data.Line_Begin_Token.all (Line - 1)).Non_Grammar loop
if Tok.Line = Line and then
Tok.ID in Data.First_Comment_ID .. Data.Last_Comment_ID and then
Tok.Column = 0
then
Indent := False;
exit;
end if;
end loop;
end if;
if Indent then
Indent_Line (Data, Line, Delta_Indent);
else
Indent_Line (Data, Line, (Simple, (Int, 0)));
end if;
end;
else
Indent_Line (Data, Line, Delta_Indent);
end if;
end loop;
end Indent_Token_1;
function Last_Line
(Token : in Augmented_Token;
Indenting_Comment : in Boolean)
return Line_Number_Type
is begin
return
(if Indenting_Comment then
(if Token.Last_Trailing_Comment_Line = Invalid_Line_Number
then Token.Line
else Token.Last_Trailing_Comment_Line)
else
(if Token.Last_Indent_Line = Invalid_Line_Number
then Token.Line
else Token.Last_Indent_Line));
end Last_Line;
end Wisi;
|