package ocp-indent-nlfork

  1. Overview
  2. Docs

Source file indentBlock.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
(**************************************************************************)
(*                                                                        *)
(*  Copyright 2011 Jun Furuse                                             *)
(*  Copyright 2012,2015 OCamlPro                                          *)
(*                                                                        *)
(*  All rights reserved.This file is distributed under the terms of the   *)
(*  GNU Lesser General Public License version 3.0 with linking            *)
(*  exception.                                                            *)
(*                                                                        *)
(*  TypeRex is distributed in the hope that it will be useful,            *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *)
(*  Lesser GNU General Public License for more details.                   *)
(*                                                                        *)
(**************************************************************************)

open Nstream
open Approx_lexer
open Util

module Node = struct

  (* Node kind *)
  type kind =
    | KParen
    | KBrace
    | KBracket
    | KBracketBar
    | KLet
    | KAnd of kind
    | KLetIn
    | KIn

    | KExpr of int
    (* actually handles also patterns / types / ... *)
    (* Parameter:Priority - next expression is deindented if the op has
       lower priority *)

    | KBody of kind
    | KArrow of kind
    | KColon
    | KType
    | KException
    | KOpen
    | KInclude
    | KVal
    | KBar of kind
    | KUnknown
    | KStruct
    | KSig
    | KModule
    | KBegin
    | KObject
    | KMatch
    | KTry
    | KWith of kind
    | KLoop
    | KIf
    | KThen
    | KElse
    | KDo
    | KFun
    | KWhen
    | KExternal
    | KExtendedExpr of string list
    | KExtendedItem of string list
    | KAttrId of string list * bool
    | KComment (* Complete comment *)
    | KOCamldocCode (* Complete OCamldoc code *)

    (* Stores the original token and line offset for alignment of
       comment continuations *)
    | KInComment of Nstream.token
                    * int
                    * bool (* no indent *)
                    * bool ref (* aligned stars at bol *)
    | KInOCamldocVerbatim
    | KInOCamldocCode
    | KInString of bool (* do indent *)
    | KInQuotation

    | KInStringIndent
    | KInQuotationIndent
    | KInCommentIndent

  (* Priority of open expression constructs (see below for operators) *)
  let prio = function
    | KIn | KArrow _ -> 0
    | KThen | KElse -> 10
    | KExpr i -> i
    | _ -> -10

  let prio_max = 200
  let prio_dot = 160
  let prio_apply = 140
  let expr_atom = KExpr prio_max
  let expr_apply = KExpr 140
  (* Special operators that should break arrow indentation have this prio
     (eg monad operators, >>=) *)
  let prio_flatop = 59
  let prio_semi = 5

  let rec follow = function
    | KAnd k
    | KBody k
    | KWith k -> follow k
    | k -> k

  let rec string_of_kind = function
    | KExpr i -> Printf.sprintf "KExpr(%d)" i
    | KParen -> "KParen"
    | KBrace -> "KBrace"
    | KBracket -> "KBracket"
    | KBracketBar -> "KBracketBar"
    (* | KField -> "KField" *)
    | KLet -> "KLet"
    | KIn -> "KIn"
    | KAnd k -> aux "KAnd" k
    | KLetIn -> "KLetIn"
    | KBody k -> aux "KBody" k
    | KArrow k -> aux "KArrow" k
    | KColon -> "KColon"
    | KVal -> "KVal"
    | KBar k -> aux "KBar" k
    | KOpen -> "KOpen"
    | KInclude -> "KInclude"
    | KUnknown -> "KUnknown"
    | KType -> "Ktype"
    | KException -> "KException"
    | KStruct -> "KStruct"
    | KSig -> "KSig"
    | KModule -> "KModule"
    | KBegin -> "KBegin"
    | KObject -> "KObject"
    | KMatch -> "KMatch"
    | KTry -> "KTry"
    | KWith k -> aux "KWith" k
    | KLoop -> "KLoop"
    | KIf -> "KIf"
    | KThen -> "Kthen"
    | KElse -> "KElse"
    | KDo -> "KDo"
    | KFun -> "KFun"
    | KWhen -> "KWhen"
    | KExternal -> "KExternal"
    | KExtendedExpr name ->
        Printf.sprintf "KExtendedExpr(%s)" (String.concat "." (List.rev name))
    | KExtendedItem name ->
        Printf.sprintf "KExtendedItem(%s)" (String.concat "." (List.rev name))
    | KAttrId(name, dotted) ->
        Printf.sprintf "KAttrId(%s,%B)"
          (String.concat "." (List.rev name)) dotted
    | KComment -> "KComment"
    | KOCamldocCode -> "KOCamldocCode"

    | KInComment (_, _, b1, b2) ->
        Printf.sprintf "KInComment(%B, %B)" b1 !b2
    | KInOCamldocVerbatim -> "KInOCamldocVerbatim"
    | KInOCamldocCode -> "KInOCamldocCode"
    | KInString b -> Printf.sprintf "KInString(%b)" b
    | KInQuotation -> "KInQuotation"
    | KInStringIndent -> "KInStringIndent"
    | KInQuotationIndent -> "KInQuotationIndent"
    | KInCommentIndent -> "KInCommentIndent"

  and aux str k =
    Printf.sprintf "%s(%s)" str (string_of_kind k)

  (* A node:

     - has a kind
     - has the current line offset [indent]
     - has the current token offset [column]
     - has a inner padding [pad]
     - has a line count [count]

             XXX XXX XXX [
                                XXX
                         ]

             XXX XXX XXX [
                   XXX
             ]

     <indent>
     <----------x-------->
                         <-pad->
             <-pad->
  *)

  type t = {
    kind: kind;
    indent: int; (* expression starting column *)
    column: int; (* starting column of the token *)
    pad: int; (* padding: how much children should be indented from
                 current line *)
    line_indent: int; (* starting column of the current line *)
    line: int; (* starting line of the expression *)
  }

  let to_string i t =
    Printf.sprintf "%s%s %d|%d-%d-%d(%d)"
      (String.make i ' ') (string_of_kind t.kind) t.line
      t.line_indent t.indent t.column t.pad

  let default = {
    kind = KUnknown;
    indent = 0;
    column = 0;
    pad = 0;
    line = 0;
    line_indent = 0;
  }

  let shift node n =
    let n = max n (- node.indent) in
    { node with indent = node.indent + n; column = node.column + n }

end

module Path = struct

  open Node
  type t = Node.t list

  let to_string t =
    String.concat " \027[35m/\027[m "
      (List.map (fun n -> Node.to_string 0 n) (List.rev t))

  let top = function
    | [] -> Node.default
    | t :: _ -> t

  let indent = function
    | [] -> 0
    | t :: _ -> t.indent

  let pad = function
    | [] -> 0
    | t :: _ -> t.pad

  let maptop f = function
    | [] | {kind=KInOCamldocCode}::_ as l  -> l
    | t::l -> f t :: l

  let shift path n =
    maptop (fun t -> Node.shift t n) path

  let in_string = function
    | { kind = KInString _ } :: _ -> true
    | { kind = KInStringIndent } :: { kind = KInString _ } :: _ -> true
    | _ -> false

  let rec is_indented_string = function
    | { kind = ( KInString indent )} :: _ -> indent
    | { kind = ( KInStringIndent )} :: path -> is_indented_string path
    | _ -> false

  let in_quotation = function
    | { kind = ( KInQuotation | KInQuotationIndent )} :: _ -> true
    | _ -> false

  let in_comment = function
    | { kind = ( KInComment _ | KInCommentIndent )} :: _ -> true
    | _ -> false

  let in_ocamldoc_verbatim = function
    | { kind = KInOCamldocVerbatim } :: _ -> true
    | _ -> false

  let in_non_indented_comment = function
    | { kind = KInComment (_,_,b,_) } :: _ -> b
    | { kind = KInOCamldocVerbatim } :: _ -> true
    | _ -> false

end

open Node

(* A block is: *)
type t = {
  path: Path.t;  (* a node path to go to this block *)
  last: Nstream.token list; (* the last token of this block
                               (when a comment, it is stacked to keep the
                               last meaningful token)
                               Excludes EOL and ESCAPED_EOL. *)
  toff: int;     (* the last token offset *)
  orig: int;     (* the original starting column for this block *)
  newlines: int; (* how many consecutive EOL in the previous tokens ?
                    Special case: -1, means lat token = "ESCAPED_EOL" *)
  starts_line: bool; (* was the previous token preceded by EOL ? *)
  pp_stack: Path.t list;
}

let shift t n =
  { t with path = Path.shift t.path n }

let to_string t =
  Path.to_string t.path

let empty = {
  path = [];
  last = [];
  toff = 0;
  orig = 0;
  newlines = 1;
  starts_line = false;
  pp_stack = [];
}

(*
(* Does the token close a top LET construct ? *)
(* NB: we do this with another way below, but this one might be more robust *)
let rec close_top_let = function
  | None -> true
  | Some t ->
      match t.token with
      | COMMENT _ -> assert false (* COMMENT must be skipped *)

      (* Tokens that allow a let-in after them *)
      | AMPERSAND | AMPERAMPER | BARBAR | BEGIN | COLONCOLON | COLONEQUAL
      | COMMA | DO | DOWNTO | ELSE | EQUAL | GREATER | IF | IN
      | INFIXOP0 _ | INFIXOP1 _ | INFIXOP2 _ | INFIXOP3 _ | INFIXOP4 _
      | LBRACE | LBRACELESS
      | LBRACKET | LBRACKETBAR | LBRACKETLESS | LBRACKETGREATER
      | LESS | LESSMINUS | LPAREN | MATCH | MINUS | MINUSDOT | MINUSGREATER | OR
      | PLUS | PLUSDOT | QUESTION | QUESTIONQUESTION | SEMI | STAR | THEN
      | TO | TRY | WHEN | WHILE
      | TILDE -> false

      | _ -> true
*)

(* Go back to the node path until [f] holds *)
let rec unwind f path = match path with
  | { kind } :: _ when f kind -> path
  | { kind = KAttrId _ } :: { kind } :: _ when f kind -> path
  (* never remove the KattrId following a KExtendedItem *)
  | { kind=KInOCamldocCode } :: _ -> path
  | _ :: path -> unwind f path
  | [] -> []

(* Unwinds the path while [f] holds,
   returning the last step for which it does *)
let unwind_while f path =
  let rec aux acc = function
    | { kind } as h :: p when f kind -> aux h p
    | p -> acc :: p
  in
  match path with
  | { kind=KInOCamldocCode } :: _ -> None
  | { kind } as h :: p when f kind -> Some (aux h p)
  | _ -> None

let top_kind = function
  | KStruct|KSig|KParen|KBegin|KObject|KExtendedItem _ -> true
  | _ -> false

let stritem_kind = function
  | KModule|KVal|KLet|KExternal|KType|KException|KOpen|KInclude -> true
  | _ -> false

(* Unwind the struct/sig top *)
let unwind_top = unwind top_kind

(* Get the parent node *)
let parent = function
  | [] | { kind = KInOCamldocCode } :: _ as t -> t
  | _ :: t -> t

let rec skip_comment stream =
  match Nstream.next stream with
  | None -> stream
  | Some (token, stream) ->
      match token.token with
      | COMMENT_CONTENT | EOL ->
          skip_comment stream
      | COMMENT_CLOSE ->
          stream
      | STRING_OPEN ->
          let stream = skip_string stream in
          skip_comment stream
      | PPX_QUOTATION_OPEN ->
          let stream = skip_ppx_quotation stream in
          skip_comment stream
      | COMMENT_CODE_OPEN ->
          let stream = skip_ocamldoc_code stream in
          skip_comment stream
      | COMMENT_VERB_OPEN ->
          let stream = skip_ocamldoc_verbatim stream in
          skip_comment stream
      | EOF -> stream
      | _ ->
          Printf.eprintf "Unexpected token: %s\n%!"
            (Approx_tokens.string_of_tok token.token);
          assert false

and skip_string stream =
  match Nstream.next stream with
  | None -> stream
  | Some (token, stream) ->
      match token.token with
      | STRING_CONTENT | EOL | ESCAPED_EOL -> skip_string stream
      | STRING_CLOSE -> stream
      | EOF -> stream
      | _ ->
          Printf.eprintf "Unexpected token: %s\n%!"
            (Approx_tokens.string_of_tok token.token);
          assert false

and skip_ppx_quotation stream =
  match Nstream.next stream with
  | None -> stream
  | Some (token, stream) ->
      match token.token with
      | PPX_QUOTATION_CONTENT | EOL -> skip_ppx_quotation stream
      | PPX_QUOTATION_CLOSE -> stream
      | EOF -> stream
      | _ ->
          Printf.eprintf "Unexpected token: %s\n%!"
            (Approx_tokens.string_of_tok token.token);
          assert false

and skip_p4_quotation stream =
  match Nstream.next stream with
  | None -> stream
  | Some (token, stream) ->
      match token.token with
      | P4_QUOTATION_CONTENT | EOL -> skip_p4_quotation stream
      | P4_QUOTATION_CLOSE -> stream
      | EOF -> stream
      | _ ->
          Printf.eprintf "Unexpected token: %s\n%!"
            (Approx_tokens.string_of_tok token.token);
          assert false

and skip_ocamldoc_code stream =
  match Nstream.next stream with
  | None -> stream
  | Some (token, stream) ->
      match token.token with
      | COMMENT_CODE_CLOSE -> stream
      | COMMENT_OPEN_CLOSE -> skip_ocamldoc_code stream
      | COMMENT_OPEN | COMMENT_OPEN_EOL ->
          let stream = skip_comment stream in
          skip_ocamldoc_code stream
      | STRING_OPEN ->
          let stream = skip_string stream in
          skip_ocamldoc_code stream
      | PPX_QUOTATION_OPEN ->
          let stream = skip_ppx_quotation stream in
          skip_ocamldoc_code stream
      | P4_QUOTATION_OPEN ->
          let stream = skip_p4_quotation stream in
          skip_ocamldoc_code stream
      | EOF -> stream
      | _ -> skip_ocamldoc_code stream

and skip_ocamldoc_verbatim stream =
  match Nstream.next stream with
  | None -> stream
  | Some (token, stream) ->
      match token.token with
      | COMMENT_VERB_CLOSE -> stream
      | COMMENT_CODE_OPEN ->
          let stream = skip_comment stream in
          skip_ocamldoc_verbatim stream
      | STRING_OPEN ->
          let stream = skip_string stream in
          skip_ocamldoc_verbatim stream
      | PPX_QUOTATION_OPEN ->
          let stream = skip_ppx_quotation stream in
          skip_ocamldoc_verbatim stream
      | COMMENT_CONTENT | EOL ->
          skip_ocamldoc_verbatim stream
      | EOF -> stream
      | _ ->
          Printf.eprintf "Unexpected token: %s\n%!"
            (Approx_tokens.string_of_tok token.token);
          assert false

(* Get the next token, skipping comments (and in-comment tokens) *)
let rec next_token_full ?(newlines = 0) stream =
  match Nstream.next stream with
  | None -> None
  | Some
      ({ token = COMMENT_OPEN_CLOSE }, stream) ->
      next_token_full stream
  | Some
      ({ token = ( COMMENT_OPEN | COMMENT_OPEN_EOL ) }, stream) ->
      next_token_full (skip_comment stream)
  | Some ({ token = EOL }, stream) ->
      next_token_full ~newlines:(newlines + 1) stream
  | Some (tok, stream) ->
      Some (tok, newlines, stream)

let next_token stream =
  match next_token_full stream with
  | None -> None
  | Some (t, _, _) -> Some t.token

let last_token t =
  let rec loop = function
    | [] -> None
    | { token = COMMENT_CLOSE } :: tokens -> loop tokens
    | t :: _ -> Some t.token in
  loop t.last

let rec skip_string_content stream =
  match Nstream.next stream with
  | Some ({ token = STRING_CONTENT }, stream) ->
      skip_string_content stream
  | _ -> stream

(* a more efficient way to do this would be to store a
   "context-type" in the stack *)
let rec is_inside_type path =
  match unwind (function
      | KParen | KBegin | KBracket | KBrace | KBracketBar
      | KVal | KLet | KLetIn | KBody (KVal | KLet | KLetIn)
      | KBody(KType|KExternal) | KColon
      | KStruct | KSig | KObject -> true
      | _ -> false)
      path
  with
  | {kind=KBody(KVal|KType|KExternal) | KColon}::_ -> true
  | {kind=KParen | KBegin | KBracket | KBrace}::p ->
      is_inside_type p
  | _ -> false

(* Returns None if the current token ends a line, the offset of
   the next token otherwise *)
let next_offset tok stream =
  match next_token_full stream with
  | None -> None
  | Some (next, _, _) ->
      if Region.end_line tok.region < Region.start_line next.region
      then None
      else Some next.offset

let reset_padding ?(pad=0) path =
  Path.maptop (fun n -> {n with pad}) path

let reset_line_indent config current_line path =
  let limit_overindent =
    match config.IndentConfig.i_max_indent with
    | Some m ->
        let m = max 0 (m - config.IndentConfig.i_base) in
        fun i -> min i m
    | None -> fun i -> i
  in
  let rec aux acc = function
    | {line} as t :: r when line = current_line ->
        aux (t::acc) r
    | p ->
        let p, acc, extra = match acc with
          | {kind = KParen|KBracket|KBrace|KBracketBar} as acc1 :: acc
            when acc1.line_indent = acc1.column
            ->
              (* ignore those if at start of line *)
              acc1 :: p, acc, acc1.pad
          | _ -> p, acc, 0
        in
        List.fold_left (fun p t ->
          {t with indent = t.line_indent
                           + limit_overindent (t.indent - t.line_indent)
                           + extra}
          ::p)
          p acc
  in
  aux [] path

let dump t =
  Printf.eprintf "\027[35m# \027[32m%d%8S\027[m %d; %s\n%!"
    (match t.last with tok::_ -> (String.length tok.between) | _ -> 0)
    (match t.last with tok::_ -> shorten_string 30 tok.substr
                     | _ -> "")
    t.newlines
    (to_string t)

(* different kinds of position:
   [T]: token aligned: the child is aligned with the token position
   [L]: line aligned: the child is aligned with the begining of line
   [A]: absolute position *)
type pos = L | T | A of int (* position *)

(* indent configuration of the infix operators *)
let op_prio_align_indent config =
  let open IndentConfig in
  let align, indent = match config.i_align_ops with
    | true -> T, 0
    | false -> L, config.i_base
  in
  let is_monadop s =
    match String.sub s 0 (min 2 (String.length s)) with
    | ">>" | ">|" | "@@" | "@>" -> true
    | _ -> false
  in
  let is_monadop s =
    is_monadop s
    (* "*>>=", "+>>>", "/>>|", etc. *)
    || (String.length s > 3
        && is_monadop (String.sub s 1 2))
  in
  function
  (* anything else : -10 *)
  (* in -> : 0 *)
  | SEMI -> prio_semi,L,-2
  (* special negative indent is only honored at beginning of line *)
  (* then else : 10 *)
  | BAR -> 10,T,-2
  | OF -> 20,L,2
  | LESSMINUS | COLONEQUAL -> 20,L,config.i_base
  | COMMA -> 30,align,-2
  | MINUSGREATER -> 32,L,0 (* is an operator only in types *)
  | COLON -> 35,T,config.i_base
  | COLONGREATER -> 35,L,config.i_base
  | OR | BARBAR -> 40,T,0
  | AMPERSAND | AMPERAMPER -> 50,T,0
  | (INFIXOP0 s | INFIXOP1 s | INFIXOP2 s | INFIXOP3 s | INFIXOP4 s)
    (* these should deindent fun -> *)
    when is_monadop s
    -> prio_flatop,L,0
  | INFIXOP0 s ->
     (match String.sub s 0 (min 2 (String.length s)) with
      | "|!" | "|>" -> prio_flatop,T,0
      | _ -> 60,align,indent)
  | EQUAL | LESS | GREATER -> 60,align,0
  | INFIXOP1 _ -> 70,align,indent
  | COLONCOLON -> 80,align,indent
  | INFIXOP2 _ | PLUSDOT | PLUS | MINUSDOT | MINUS -> 90,align,indent
  | INFIXOP3 _ | STAR -> 100,align,indent
  | INFIXOP4 _ -> 110,align,indent
  (* apply: 140 *)
  | AS -> prio_apply,L,0
  | TILDE | QUESTION -> prio_apply,L,config.i_base
  | LABEL _ | OPTLABEL _ ->
      if config.i_align_params = Always then 145,T,config.i_base
      else 145,L,config.i_base
  | SHARP -> 150,align,config.i_base
  | DOT -> prio_dot,align,config.i_base
  | token ->
      Printf.eprintf "Unexpected token: %s\n%!"
        (Approx_tokens.string_of_tok token);
      assert false

let handle_dotted block tok =
  let starts_line = block.newlines <> 0 in
  let current_line = Region.start_line tok.region in
  let is_attr_id = function
    | { kind = KAttrId (_, dotted) } :: _ -> not dotted
    | _ -> false in
  let make_dotted_attr_id = function
    | { kind = KAttrId (names, _) } as node ::
      ({ kind = (KExtendedItem [] | KExtendedExpr [])} :: _ as path) ->
        { node with kind = KAttrId (names, true) } :: path
    | _ -> assert false in
  let is_dotted_attr_id = function
    | { kind = KExtendedExpr [] } :: _ -> true
    | { kind = KExtendedItem [] } :: _ -> true
    | { kind = KAttrId (_, dotted) } :: _ -> dotted
    | _ -> false in
  let make_attr_id name = function
    | ({ kind = (KExtendedItem [] | KExtendedExpr []);
         indent; pad; } :: _ as path) ->
          let indent =
            if starts_line then indent + pad
            else indent + pad + String.length tok.between - 1 in
          let column =
            if starts_line then indent else block.toff + tok.offset in
          { kind = (KAttrId ([name], false)); indent;
            line_indent = indent; column; line = current_line;
            pad = 0 } :: path
    | ({ kind = KAttrId (names, _)} as node) :: path ->
        { node with kind = KAttrId (name :: names, false); } :: path
    | _ -> assert false in
  if is_dotted_attr_id block.path then
    match tok.token with
    | LIDENT s | UIDENT s ->
        Some (make_attr_id s block.path)
    | AND | AS | ASSERT | BEGIN | CLASS | CONSTRAINT | DO | DONE
    | DOWNTO | ELSE | END | EXCEPTION | EXTERNAL | FALSE | FOR | FUN
    | FUNCTION | FUNCTOR | IF | IN | INCLUDE | INHERIT | INITIALIZER
    | LAZY | LET | MATCH | METHOD | MODULE | MUTABLE | NEW | OBJECT | OF
    | OPEN | OR | PRIVATE | REC | SIG | STRUCT | THEN | TO | TRUE | TRY
    | TYPE | VAL | VIRTUAL | WHEN | WHILE | WITH ->
        Some (make_attr_id tok.substr block.path)
    | _ -> None
  else if is_attr_id block.path then
    match tok.token with
    | DOT -> Some (make_dotted_attr_id block.path)
    | _ -> None
  else
    None

(* Take a block, a token stream and a token.
   Return the new block stack. *)
let rec update_path config block stream tok =
  let open IndentConfig in
  let starts_line = block.newlines <> 0 in
  let current_line = Region.start_line tok.region in
  let node replace kind pos pad path =
    let parent = Path.top path in
    if starts_line then
      let indent = match pos with
        | A p -> p
        | L   -> parent.indent + if replace then 0 else parent.pad
        | T   -> parent.column + if replace then 0 else parent.pad
      in
      { kind; indent; line_indent=indent; column=indent; pad;
        line = current_line }
    else
      let column = block.toff + tok.offset in
      { kind;
        indent = parent.indent;
        line_indent=parent.line_indent;
        column; pad;
        line = current_line }
  in
  (* Add a new child block *)
  let append kind pos ?(pad=config.i_base) = function
    | {kind = KAttrId (names, _)} ::
      ({kind = KExtendedItem [] | KExtendedExpr [] } as n) :: path ->
        let n = { n with kind = match n.kind with
            | KExtendedItem [] -> KExtendedItem (List.rev names)
            | KExtendedExpr [] -> KExtendedExpr (List.rev names)
            | _ -> assert false
          } in
        let path = {n with pad = config.i_ppx_stritem_ext } :: path in
        node false kind pos pad path :: path
    | path ->
        node false kind pos pad path :: path
  in
  (* replace the current block with a new one *)
  let replace kind pos ?(pad=config.i_base) path = match path with
    | [] | {kind=KInOCamldocCode} :: _ -> node true kind pos pad path :: path
    | _::t -> node true kind pos pad path :: t
  in
  (* Used when expressions are merged together (for example in "3 +" the "+"
     extends the lower-priority expression "3") *)
  let extend kind pos ?(pad=config.i_base) = function
    | [] | {kind=KInOCamldocCode} :: _ as path ->
        node true kind pos pad path :: path
    | h::p ->
        let negative_indent () =
          (* Special negative indent: relative, only at beginning of line,
             and when prio is changed or there is a paren to back-align to *)
          if pad >= 0 || not starts_line then None
          else
            match p with
            | {kind=KParen|KBracket|KBracketBar
                    |KBrace|KBar _|KWith KBrace|KBody _}
              as paren :: _
              when paren.line = h.line
              ->
                let paren_len = match paren.kind with
                  | KParen | KBracket | KBrace | KBar _ | KBody _ -> 1
                  | KBracketBar -> 2
                  | KWith KBrace -> 4
                  | _ -> assert false
                in
                let indent =
                  paren.column + paren_len + 1 (* usually 1 space *) + pad
                in
                Some ({ h with kind; indent; column=indent;
                               line_indent = indent-pad;
                               pad = max h.pad (h.indent-indent)} :: p)
            | _ ->
                match kind,h.kind with
                | KExpr pk, KExpr ph when ph = pk ->
                    (* respect the indent of the above same-priority term, we
                       assume it was already back-indented *)
                    Some ({ h with kind; indent=h.column; column=h.column;
                                   line_indent = h.column;
                                   pad = h.pad } :: p)
                | _ ->
                    let indent = h.column + pad in
                    if indent < 0 then None
                    else Some ({ h with kind; indent; column=indent;
                                        line_indent = indent;
                                        pad = -pad } :: p)
        in
        match negative_indent () with
        | Some p -> p
        | None -> (* normal case *)
            (* change indent to set the starting column of the expression *)
            let pad = max 0 pad in
            let indent,pad =
              if pos = T then h.column, pad
              else
                (* set indent of the whole expr accoring to its parent *)
                Path.indent p + Path.pad p, pad
            in
            let line_indent =
              if starts_line then indent else h.line_indent
            in
            { h with kind; indent; line_indent; pad } :: p
  in
  (* use before appending a new expr_atom: checks if that may cause an
     apply and folds parent exprs accordingly *)
  let fold_expr path =
    match path with
    | {kind=KExpr _} as e :: ({kind=KFun} as fn) :: p ->
        {fn with line_indent = e.line_indent} :: p
    | {kind=KExpr i} as e :: _ when i = prio_max ->
        (* we are appending two expr_atom next to each other:
           this is an apply. *)
        (* this "folds" the left-side of the apply *)
        let p =
          match unwind_while (fun kind -> prio kind >= prio_apply) path with
          | Some({kind=KExpr i} as e1 :: p) when i = prio_apply ->
              {e1 with line_indent = e.line_indent} :: p
          | Some({kind=KExpr _; line} ::
                 {kind=KModule|KInclude|KOpen|KBody KModule} :: _
                 as p) -> (* ignore align_params for functor application *)
              extend (KExpr prio_apply) L (reset_line_indent config line p)
          | Some({kind=KExpr _; line}
              :: {kind=KArrow (KMatch|KTry) | KTry | KMatch;
                  line=arrow_line}::_ as p)
            when config.i_align_params = Auto
              && line = arrow_line ->
              (* Special case: switch to token-aligned (see test js-args) *)
              extend (KExpr prio_apply) T p
          | Some p ->
              extend (KExpr prio_apply)
                (if config.i_align_params = Always then T else L)
                p
          | None -> assert false
        in
        p
    | _ -> path
  in
  let before_append_atom = function
    | {kind=KWith(KTry|KMatch as m)}::parent as path ->
        (* Special case: 'match with' and no bar for the 1st case:
           we append a virtual bar for alignment *)
        let path = match parent with
          | {kind = KExpr i} :: _ when i = prio_flatop -> reset_padding path
          | _ -> path
        in
        let p = append (KBar m) L ~pad:2 path in
        if not starts_line then
          let column = max 0 (block.toff + tok.offset - 2) in
          Path.maptop (fun h -> {h with column}) p
        else p
    | path -> fold_expr path
  in
  let atom path =
    let path = before_append_atom path in
    let pad =
      match path with {kind=KExpr _; pad}::_ -> pad | _ -> config.i_base
    in
    append expr_atom L ~pad path
  in
  let open_paren kind path =
    let path = before_append_atom path in
    let path = match next_offset tok stream with
      | None (* EOL *) -> reset_line_indent config current_line path
      | Some _ -> path
    in
    let p = append kind L path in
    let p = match p with
      (* Special case: paren after arrow has extra indent
         (see test js-begin) *)
      | {kind=KParen|KBegin|KBracket|KBracketBar|KBrace} :: {kind=KArrow _} :: _
        when not starts_line ->
          Path.shift p config.i_base
      | p -> p
    in
    match p with
    | [] -> []
    | h::p as path ->
        match kind with
        | KBegin -> path
        | KParen
          when if not config.i_align_ops then not starts_line else
              match next_token stream with
              | Some(SIG|STRUCT|OBJECT) -> true
              | _ -> false
          -> path
        | _ ->
            (* set alignment for next lines relative to [ *)
            (match next_offset tok stream with
             | Some pad ->
                 let indent =
                   if starts_line then h.indent else block.toff + tok.offset
                 in
                 { h with indent; column=indent; pad } :: p
             | None ->
                 if starts_line then path
                 else {h with column = h.indent + h.pad} :: p)
  in
  let close f path =
    (* Remove the padding for the closing brace/bracket/paren/etc. *)
    Path.maptop (fun h -> {h with kind=expr_atom; pad=0}) (unwind f path)
  in
  let make_infix tok path =
    let op_prio, align, indent = op_prio_align_indent config tok.token in
    let in_record =
        match unwind_while (fun kind -> prio kind >= op_prio) path with
        | Some ({ kind = KExpr _ } :: { kind = KBrace } :: _) -> true
        | _ -> false in
    (* special cases *)
    let indent =
      (* don't back-indent operators when alone on their line
         (except BAR because that would disrupt typing) *)
      if indent < 0 && tok.token <> BAR
         && not (tok.token = SEMI && in_record)
         && next_offset tok stream = None
      then 0 else indent
    in
    match path with
    | {kind=KExpr prio}::_ when prio >= op_prio && prio < prio_max ->
        (* we are just after another operator (should be an atom).
           handle as unary (eg. x + -y) : indented but no effect
           on following expressions *)
        (* append KUnknown L path *)
        append (KExpr prio) L ~pad:(max 0 indent) path
    | _ ->
        match unwind_while (fun kind -> prio kind >= op_prio) path with
        | Some p ->
            extend (KExpr op_prio) align ~pad:indent p
        | None -> (* used as prefix ? Don't apply T indent *)
            append (KExpr op_prio) L ~pad:(max 0 indent) path
  in
  (* KComment/KUnknown nodes correspond to comments or top-level stuff, they
     shouldn't be taken into account when indenting the next token *)
  let block0 = block in
  let block =
    match block.path with
    | { kind = KUnknown } :: path
    | { kind = KInStringIndent } :: path
    | { kind = KInQuotationIndent } :: path
    | { kind = KInCommentIndent } :: path
    | { kind = KOCamldocCode } :: path
    | { kind = KComment } :: path -> { block with path }
    | _ -> block in
  let compute_string_indent tok =
    if Path.is_indented_string block.path && block.newlines < 0 then
      (* Previous line finished with an '\'. *)
      if tok.token = STRING_CLOSE
         || ( String.length tok.substr >= 2
              && tok.substr.[0] = '\\' && tok.substr.[1] = ' ' ) then
        A (Path.top block.path).indent
      else
        L
    else
      A (String.length tok.between) in
  let (>>!) opt f = match opt with Some x -> x | None -> f () in
  handle_dotted block tok >>! fun () ->
  match tok.token with

  (* Comments *)

  | COMMENT_OPEN_EOL | COMMENT_OPEN | COMMENT_OPEN_CLOSE -> begin
      let no_indent =
        tok.token = COMMENT_OPEN_EOL && not config.i_strict_comments in
      let node col =
        if tok.token = COMMENT_OPEN_CLOSE
        then KComment
        else KInComment (tok, col, no_indent, ref true) in
      let s = tok.substr in
      let pad =
        if no_indent then
          0
        else
          let len = String.length s in
          let i = ref 2 in
          while !i < len && s.[!i] = '*' do incr i done;
          while !i < len && s.[!i] = ' ' do incr i done;
          if tok.token = COMMENT_OPEN_EOL then 3 else !i in
      if not starts_line then
        let col = block.toff + tok.offset in
        Path.maptop (fun n -> {n with indent = col})
          (append (node col) L ~pad block.path)
      else
        match block.path with
        | { kind = KExpr i } :: _ when i = prio_max -> begin
            let blocklevel () =
              let p = unwind_top block.path in
              let col = Path.indent p + Path.pad p in
              append
                (node col)
                (A col) ~pad block.path in
            let stream =
              if tok.token = COMMENT_OPEN_CLOSE
              then stream
              else skip_comment stream in
            match next_token_full stream with
            | None -> blocklevel ()
            | Some (* full block-closing tokens + newline *)
                 ({token = SEMISEMI | DONE | END
                         | GREATERRBRACE | GREATERRBRACKET | RBRACE
                         | RBRACKET | RPAREN }, _, _) when block.newlines > 1 ->
                blocklevel ()

            | Some (* semi block-closing tokens *)
                ({ token = SEMISEMI | DONE | END
                         | GREATERRBRACE | GREATERRBRACKET | RBRACE
                         | RBRACKET | RPAREN
                         | THEN | ELSE | IN | EQUAL }, _, _)
              when block.newlines <= 1->
                (* indent as above *)
                let col = (Path.top block0.path).line_indent in
                append
                  (node col)
                  (A col) ~pad block.path
            | next ->
                (* indent like next token, _unless_ we are directly after a
                   case in a sum-type *)
                let align_bar =
                  if block.newlines > 1 || not (is_inside_type block.path)
                  then None
                  else
                    let find_bar =
                      unwind_while
                        (function KBar _ | KExpr _ -> true | _ -> false)
                        block0.path
                    in match find_bar with
                    | Some ({kind=KBar _; column}::_) -> Some column
                    | _ -> None
                in
                match align_bar with
                | Some indent ->
                    append (node indent) (A indent) ~pad block.path
                | None ->
                    (* recursive call to indent like next line *)
                    let col =
                      match next with
                      | Some ({token = EOF }, _, _) | None ->
                          Path.indent []
                      | Some (next, newlines, stream) ->
                          let newlines = newlines + block.newlines in
                          let path =
                            update_path config
                              { block with newlines } stream next in
                          if next.token = COMMENT_CODE_CLOSE then
                            Path.indent path + Path.pad path
                          else
                            Path.indent path in
                    append
                      (node col)
                      (A col) ~pad block.path
          end
        | _ ->
            let col = Path.indent block.path + Path.pad block.path in
            append
              (node col)
              (A col) ~pad block.path
    end

  | COMMENT_CONTENT
  | STRING_OPEN | STRING_CONTENT | STRING_CLOSE
  | PPX_QUOTATION_OPEN | PPX_QUOTATION_CONTENT | PPX_QUOTATION_CLOSE
  | P4_QUOTATION_OPEN | P4_QUOTATION_CONTENT | P4_QUOTATION_CLOSE
    when (Path.in_comment block.path
          || Path.in_ocamldoc_verbatim block.path)
         && block.newlines = 0 ->
      block.path

  | COMMENT_CONTENT
  | STRING_OPEN | STRING_CONTENT | STRING_CLOSE
  | PPX_QUOTATION_OPEN | PPX_QUOTATION_CONTENT | PPX_QUOTATION_CLOSE
  | P4_QUOTATION_OPEN | P4_QUOTATION_CONTENT | P4_QUOTATION_CLOSE
    when Path.in_non_indented_comment block.path ->
      let col = String.length tok.between in
      append KInCommentIndent (A col) ~pad:0 block.path

  | COMMENT_CONTENT
  | STRING_OPEN | STRING_CONTENT | STRING_CLOSE
  | PPX_QUOTATION_OPEN | PPX_QUOTATION_CONTENT | PPX_QUOTATION_CLOSE
  | P4_QUOTATION_OPEN | P4_QUOTATION_CONTENT | P4_QUOTATION_CLOSE
    when Path.in_ocamldoc_verbatim block.path ->
      let col = String.length tok.between in
      append KInCommentIndent (A col) ~pad:0 block.path

  | COMMENT_CONTENT
  | STRING_OPEN | STRING_CONTENT | STRING_CLOSE
  | PPX_QUOTATION_OPEN | PPX_QUOTATION_CONTENT | PPX_QUOTATION_CLOSE
  | P4_QUOTATION_OPEN | P4_QUOTATION_CONTENT | P4_QUOTATION_CLOSE
    when Path.in_comment block.path -> begin
      match block.path with
      | { kind = KInComment ({ region }, _, false, aligned_star);
          indent; pad; column } :: _ ->
          let orig_col = Region.start_column region in
          let col = String.length tok.between in
          let relative_col = col - (orig_col + pad) in
          let append_indent () =
            let col =
              if relative_col > 0 && not config.i_strict_comments then
                indent + pad + relative_col
              else
                column + pad
            in
            aligned_star := false ;
            append KInCommentIndent (A col) block.path in
          if not starts_line then
            block.path
          else if tok.substr <> "" then
            append_indent ()
          else begin
            match Nstream.next stream with
            | None ->
                block.path
            | Some ({ token = COMMENT_CONTENT; substr = "*" }, _)
              when !aligned_star ->
                append KInCommentIndent (A (indent+1)) block.path
            | Some ({ token = COMMENT_VERB_OPEN }, _) ->
                aligned_star := false ;
                append KInCommentIndent T ~pad:0 block.path
            | Some ({ token = EOL }, _) ->
                aligned_star := false ;
                append KInCommentIndent (A 0) block.path
            | Some _ -> append_indent ()
          end
      | _ ->
          Printf.eprintf "Unexpected stack: %s\n%!" (Path.to_string block.path);
          assert false
    end

  | COMMENT_CONTENT ->
      Printf.eprintf "Unexpected stack: %s\n%!" (Path.to_string block.path);
      assert false

  | COMMENT_VERB_OPEN -> begin
      match block.path with
      | { kind = KInComment (tok, _, _, _); indent; pad } :: _ ->
          { kind = KInOCamldocVerbatim;
            line = Region.start_line tok.region;
            indent = indent + pad;
            line_indent = indent + pad;
            column = indent + pad;
            pad = 0 }
          :: block.path
      | _ ->
          Printf.eprintf "Unexpected stack: %s\n%!" (Path.to_string block.path);
          assert false
    end

  | COMMENT_VERB_CLOSE ->
      assert (Path.in_ocamldoc_verbatim block.path);
      List.tl block.path

  | COMMENT_CODE_OPEN ->
      let indent =
        if starts_line then
          Path.indent block0.path +
          Path.pad block0.path
        else
          Path.indent block0.path
      in
      let path =
        { kind = KInOCamldocCode;
          line = Region.start_line tok.region;
          indent = indent;
          line_indent = indent;
          column = indent;
          pad = config.i_base }
        :: block.path in
      path

  | COMMENT_CODE_CLOSE -> begin
      match unwind (fun _ -> false) block.path with
      | { kind = KInOCamldocCode } :: path as path0->
          node true KOCamldocCode T config.i_base path0 :: path
      | _ -> assert false
    end

  | COMMENT_CLOSE
    when block.newlines >= 1 && Path.in_non_indented_comment block.path ->
      let col = String.length tok.between in
      replace KComment ~pad:0 (A col) block.path

  | COMMENT_CLOSE ->
      if not (Path.in_comment block.path) then begin
          Printf.eprintf "Unexpected stack: %s\n%!" (Path.to_string block.path);
          assert false
      end;
      (* TODO config for pad ?? *)
      replace KComment ~pad:0 L block.path

  | _ when Path.in_comment block.path ->
      Printf.eprintf "Unexpected token: %s\n%!"
        (Approx_tokens.string_of_tok tok.token);
      assert false

  (* Strings *)

  | STRING_OPEN ->
      let indent =
        match Nstream.next stream with
        | Some ({ token = ESCAPED_EOL } as tok, _) ->
            String.length tok.between <> 0
        | Some ({ token = STRING_CONTENT }, stream) -> begin
            let stream = skip_string_content stream in
            match Nstream.next stream with
            | Some ({ token = ESCAPED_EOL }, _) -> true
            | _ -> false
          end
        | _ -> false in
      let path = before_append_atom block.path in
      append ~pad:1 (KInString indent) L path

  | STRING_CONTENT ->
      assert (Path.in_string block.path);
      if starts_line then
        let kind = compute_string_indent tok in
        append KInStringIndent kind block.path
      else
        block.path


  | STRING_CLOSE -> begin
      assert (Path.in_string block.path);
      let pad =
        match block.path with
        | _ :: { kind = KExpr _ ; pad } :: _ -> pad
        | _ -> config.i_base in
      let path =
        match replace expr_atom T ~pad block.path with
        | [] -> assert false
        | node :: path ->
            (* Revert node's column to the one of "STRING_OPEN". *)
            { node with column = (Path.top block.path).column } :: path in
      if starts_line then
        let kind = compute_string_indent tok in
        append KInStringIndent kind path
      else
        path
    end

  | _ when Path.in_string block.path ->
      Printf.eprintf "Unexpected token: %s\n%!"
        (Approx_tokens.string_of_tok tok.token);
      assert false

  (* Quotations *)

  | PPX_QUOTATION_OPEN | P4_QUOTATION_OPEN ->
      let path = before_append_atom block.path in
      append KInQuotation L path

  | PPX_QUOTATION_CONTENT | P4_QUOTATION_CONTENT ->
      assert (Path.in_quotation block.path);
      if block.newlines = 0 then
        block.path
      else
        let kind =
          if block.newlines < 0 then T else A (String.length tok.between) in
        append KInQuotationIndent kind block.path

  | PPX_QUOTATION_CLOSE | P4_QUOTATION_CLOSE ->
      assert (Path.in_quotation block.path);
      let pad =
        match block.path with
        | _ :: { kind = KExpr _; pad } :: _ -> pad
        | _ -> config.i_base in
      replace expr_atom L ~pad block.path

  | _ when Path.in_quotation block.path ->
      Printf.eprintf "Unexpected token: %s\n%!"
        (Approx_tokens.string_of_tok tok.token);
      assert false

  (* General cases *)

  | SEMISEMI    -> append KUnknown L ~pad:0 (unwind_top block.path)
  | INCLUDE     -> append KInclude L (unwind_top block.path)
  | EXCEPTION   ->
      (match last_token block with
       | Some LET ->
           append KUnknown L block.path (* let exception *)
       | _ ->
           let p = unwind (function KExpr _ -> false | _ -> true) block.path in
           (match p with
            | {kind=KWith KMatch|KBar KMatch}::_ ->
                append expr_atom L block.path
            | _ -> append KException L (unwind_top block.path)))
  | BEGIN       -> open_paren KBegin block.path
  | OBJECT      -> append KObject L block.path
  | VAL         -> append KVal L (unwind_top block.path)
  | MATCH       ->
      let p = fold_expr block.path in
      if starts_line then append KMatch L p
      else
        let enforce_strict =
          config.i_strict_with = Always
          || config.i_strict_with = Auto
             && match p with
             | {kind=KBegin; indent; column} :: _ -> column = indent
             | _ -> false
        in
        let p, pad =
          if enforce_strict then
            let p = reset_line_indent config current_line p in
            reset_padding p, config.i_base
          else p, Path.pad p + config.i_base
        in
        append KMatch L ~pad p
  | TRY         ->
      let p = fold_expr block.path in
      if starts_line then append KTry L p
      else
        let enforce_strict =
          config.i_strict_with = Always
          || config.i_strict_with = Auto
             && match p with
             | {kind=KBegin; indent; column} :: _ -> column = indent
             | _ -> false
        in
        let p, pad =
          if enforce_strict then
            let p = reset_line_indent config current_line p in
            reset_padding p, config.i_base
          else p, Path.pad p + config.i_base
        in
        append KTry L ~pad p
  | LPAREN -> open_paren KParen block.path
  | LBRACKET | LBRACKETGREATER | LBRACKETLESS ->
      open_paren KBracket block.path
  | LBRACKETPERCENT | LBRACKETAT ->
      let path = before_append_atom block.path in
      append ~pad:4 (KExtendedExpr []) L path
  | LBRACKETATAT ->
      let path =
        (unwind (function KBody k | k -> top_kind k || stritem_kind k)
             block.path)
      in
      let path = match path with
        | {kind = KBody k | k} :: p -> if top_kind k then path else p
        | [] -> []
      in
      append ~pad:4 (KExtendedItem []) L path
  | LBRACKETPERCENTPERCENT | LBRACKETATATAT ->
      append ~pad:4 (KExtendedItem []) L (unwind_top block.path)
  | LBRACKETBAR -> open_paren KBracketBar block.path
  | LBRACE | LBRACELESS ->
      open_paren KBrace block.path
  | FUNCTION ->
      (match fold_expr block.path with
       | l :: _ as p
         when not starts_line
           && l.kind <> KExpr 0
           && (config.i_strict_with = Never
               || config.i_strict_with = Auto && l.kind <> KBegin) ->
           let p = reset_line_indent config current_line p in
           append (KWith KMatch) L
             ~pad:(max (max l.pad config.i_base) config.i_with)
             p
       | p ->
           let p = reset_line_indent config current_line p in
           append (KWith KMatch) L ~pad:config.i_with p)
  | FUN | FUNCTOR ->
      (match block.path with
       | {kind=KArrow KFun}::path ->
           let path = unwind (function KFun -> true | _ -> false) path in
           (match path with
            | {line; column; line_indent}::_ when
                line = current_line || column = line_indent ->
                replace KFun L path
            | _ -> append KFun L block.path)
       | p -> append KFun L (fold_expr p))
  | STRUCT | SIG ->
      let k = match tok.token with
        | STRUCT -> KStruct
        | SIG -> KSig
        | _ -> assert false
      in
      let expr_start =
        unwind (function KParen | KLet | KLetIn | KBody _ -> true | _ -> false)
          block.path
      in
      let indent = match expr_start with
        | {kind=KParen}::{kind=KExpr prio; line; indent}::_
          when prio = prio_apply && line = current_line ->
            indent
        | _ -> Path.indent block.path
      in
      Path.maptop (fun n -> {n with indent})
        (append k L (reset_padding block.path))

  | WHEN ->
      append KWhen L ~pad:(config.i_base + if starts_line then 0 else 2)
        (unwind (function
           | KWith(KTry|KMatch) | KBar(KTry|KMatch) | KFun -> true
           | _ -> false)
           block.path)
  | OPEN ->
      if last_token block = Some LET then
        append KOpen L block.path
      else
        append KOpen L (unwind_top block.path)

  | LET ->
      (* Two ways to detect let vs letin ;
         both seem to work, but need to check which one
         is the most robust (for example w.r.t. unfinished expressions) *)
      (* - it's a top Let if it is after a closed expression *)
      (match block.path with
       | {kind=KExpr i}::p when i = prio_max ->
           append KLet L (unwind_top p)
       | [] | {kind=KInOCamldocCode}::_ as p->
           append KLet L (unwind_top p)
       | _ ->
           append KLetIn L (fold_expr block.path))
      (* - or if after a specific token *)
      (* if close_top_let block.last then *)
      (*   append KLet L config.i_base (unwind_top block.path) *)
      (* else *)
      (*   append KLetIn L config.i_base (fold_expr block.path) *)

  | CLASS ->
      append KLet L (unwind_top block.path)

  | METHOD ->
      append KLet L (unwind_top block.path)

  | INITIALIZER ->
      append (KBody KLet) L (unwind_top block.path)

  | CONSTRAINT ->
      let path =
        unwind
          (function KType | KBody KType | KObject -> true | _ -> false)
          block.path
      in
      append KLet L path

  | AND ->
      let unwind_to = function
        | KLet | KLetIn | KType | KModule -> true
        | _ -> false
      in let path = unwind (unwind_to @* follow) block.path in
      (match path with
       | [] | {kind=KInOCamldocCode}::_ -> append (KAnd KUnknown) L path
       | {kind=KType|KModule|KBody (KType|KModule)}
         :: ({kind=KWith _} as m) :: p ->
           (* hack to align "and" with the 'i' of "with": consider "with" was
              1 column further to the right *)
           let m = if starts_line then {m with column = m.column+1} else m in
           replace (KAnd m.kind) T ~pad:0 (m :: p)
       | {kind=KType|KModule|KBody (KType|KModule)}
         :: ({kind=KAnd (KWith _)} as m) :: p ->
           replace m.kind T ~pad:0 (m :: p)
       | h::_ -> append (KAnd (follow h.kind)) L (parent path))

  | IN ->
      let path =
        unwind ((function KLetIn | KLet -> true | _ -> false) @* follow)
          block.path
      in
      let pad = match next_token stream with
        | Some LET -> 0
        | _ -> config.i_in
      in
      (match unwind_while ((=) KIn) (parent path) with
       | Some p -> extend KIn L ~pad p
       | None -> extend KIn L ~pad path)

  | TYPE ->
      (match last_token block with
       | Some (MODULE | CLASS) -> append KUnknown L block.path (* module type *)
       | Some (WITH|AND)
       | Some COLON (* 'type' inside type decl, for GADTs *)
         -> append KType L block.path
       | _ -> append KType L (unwind_top block.path))

  | MODULE ->
      (match last_token block with
       | Some LET ->
           append KUnknown L block.path (* let module *)
       | Some COLON | Some EQUAL when next_token stream = Some TYPE ->
           append KUnknown L block.path (* : module type of *)
       | Some (WITH|AND) -> append KType L block.path
       | _ -> append KModule L (unwind_top block.path))

  | END ->
      close (function KStruct|KSig|KBegin|KObject -> true | _ -> false)
        block.path

  | WITH ->
      (match next_token_full stream with
       | Some ({token = TYPE|MODULE as tm}, _, _) ->
           let path =
             unwind (function
               | KModule | KOpen | KInclude | KParen
               | KBegin | KColon | KBody KModule ->
                   true
               | _ -> false)
               block.path
           in
           let kind =
             match tm with TYPE -> KType | MODULE -> KModule | _ -> assert false
           in
           append (KWith kind) L path
       | next ->
           let path = unwind (function
               |KTry|KMatch
               |KVal|KType|KBody KType|KException (* type-conv *)
               |KColon
               |KBrace -> true
               |KWith KTry -> (* useful for lwt's try-finally *)
                   tok.substr = "finally"
               | _ -> false
             ) block.path in
           match path with
           | {kind=KBrace; pad} :: _ ->
               (match next with
                | Some (next, _, _)
                  when Region.start_line next.region
                    = Region.end_line tok.region ->
                    Path.maptop (fun n -> {n with indent=n.column})
                      (append (KWith KBrace) L ~pad:next.offset path)
                | _ ->
                    append (KWith KBrace) L ~pad:(pad + config.i_with) path)
           | {kind=KVal|KType|KException as kind}::_ ->
               replace (KWith kind) L path
           | {kind=KTry|KMatch as kind} as n :: parent :: _
             when n.line = current_line
               && n.column <> n.line_indent
               && config.i_strict_with <> Always
             ->
               let path,pad =
                 if parent.line_indent = parent.column
                 then path, max parent.pad config.i_with
                 else
                   reset_line_indent config n.line path,
                   max config.i_with
                     (if parent.pad > 0 then config.i_base else 0)
               in
               replace (KWith kind) L ~pad path
           | {kind=(KTry|KMatch as kind)}::p ->
               if starts_line then
                 append (KWith kind) L ~pad:config.i_with p
               else
                 replace (KWith kind) L ~pad:config.i_with
                   (reset_line_indent config current_line path)
           | {kind=KColon}::_ as p ->
               (* may happen with sexp extension, 'with default' *)
               append expr_atom L p
           | _ -> path)

  | IF ->
      (match last_token block with
       | Some ELSE  -> replace KIf L block.path
       | _ -> append  KIf L (fold_expr block.path))

  | THEN ->
      extend KThen L (unwind ((=) KIf) block.path)

  | ELSE ->
      let pad =
        match config.i_strict_else with
        | Always -> config.i_base
        | Never ->
            if next_offset tok stream <> None then config.i_base
            else 0
        | Auto ->
            if next_offset tok stream <> None then config.i_base
            else match next_token stream with
              | Some (LET|MATCH|TRY|FUN|FUNCTION) -> 0
              | _ -> config.i_base
      in
      extend KElse L ~pad (unwind ((=) KThen) block.path)

  | WHILE | FOR ->
      append KLoop L (fold_expr block.path)

  | TO | DOWNTO ->
      let p =
        Path.maptop (fun n -> { n with indent = n.indent + config.i_base })
          (unwind ((=) KLoop) block.path)
      in
      replace KLoop L p

  | DO ->
      extend KDo L (unwind ((=) KLoop) block.path)

  | DONE ->
      close ((=) KDo) block.path

  | BARRBRACKET -> close ((=) KBracketBar) block.path

  | RPAREN -> close ((=) KParen) block.path

  | RBRACE | GREATERRBRACE -> close ((=) KBrace) block.path

  | RBRACKET ->
      close
        (function
          | KBracket | KExtendedItem _ | KExtendedExpr _ -> true
          | _ -> false)
        block.path

  | GREATERRBRACKET -> close ((=) KBracket) block.path

  | BAR ->
      let path = unwind (function
          | KParen | KBegin | KBracket | KBrace | KBracketBar
          | KWith(KMatch|KTry) | KBar(KMatch|KTry) | KArrow(KMatch|KTry)
          | KLet | KLetIn
          | KBody(KType) -> true
          | _ -> false)
          block.path
      in
      (match path with
       | {kind=KWith m} :: {kind=KExpr i} :: _ when i = prio_flatop ->
           append (KBar m) L (reset_padding path)
       | {kind=KWith m} :: _ -> append (KBar m) L path
       | {kind=KArrow (KMatch|KTry as m)} :: ({kind=KBar _} as h:: _ as p) ->
           Path.maptop (fun x -> {x with column = h.column})
             (replace (KBar m) (A h.column) p)
       | {kind=KArrow m} :: p ->
           append (KBar m) L p
       | _ ->
           match block.path with
           | {kind = KExpr _}::_ -> make_infix tok block.path
           | _ -> append (KBar KType) L block.path)

  | MINUSGREATER ->
      let rec find_parent path =
        let path = unwind (function
            | KParen | KBegin | KBracket | KBrace | KBracketBar
            | KWith(KMatch|KTry) | KBar(KMatch|KTry) | KArrow(KMatch|KTry)
            | KFun
            | KBody(KType|KExternal) | KColon
            | KStruct | KSig | KObject -> true
            | _ -> false)
            path
        in
        match path with
        | {kind=KFun} :: ({kind=KExpr i} as e) :: path when i = prio_flatop ->
            (* eg '>>= fun x ->': indent like the top of the expression *)
            {e with kind = KExpr 0} :: path
        | {kind=KFun; line } :: _
          when next_offset tok stream = None
            && line = current_line
          ->
            (* Special case: [fun ->] at eol, this should be strictly indented
               wrt the above line, independently of the structure *)
            append (KArrow KFun) L (reset_line_indent config line path)
        | {kind=KFun} :: _ ->
            append (KArrow KFun) L path
        | {kind=KBar m}::{kind=KWith _; line}::_ when line = current_line ->
            (* Special case: don't respect match_clause when 'with X ->' is on
              a single line *)
            let pad =
              if next_offset tok stream <> None then config.i_base
              else match next_token stream with
                | Some (MATCH|TRY|FUN|FUNCTION) -> 0
                | _ -> config.i_base
            in
            append (KArrow m) L ~pad (reset_line_indent config line path)
        | {kind=KWith m | KBar m} :: _ ->
            let pad =
              config.i_match_clause
              - if starts_line then config.i_base else 0
            in
            append (KArrow m) L ~pad path
        | {kind=KArrow(KMatch|KTry)} :: p ->
            (* might happen if doing 'when match' for example *)
            (match
              unwind (function
                | KParen | KBegin | KBracket | KBrace | KBracketBar
                | KWith(KMatch|KTry)
                | KFun
                | KBody(KType|KExternal) | KColon
                | KStruct | KSig | KObject -> true
                | _ -> false)
                p
            with
            | {kind=KWith(_)}::p -> find_parent p
            | _ -> make_infix tok block.path)
        | _ -> make_infix tok block.path
      in
      find_parent block.path

  | EQUAL ->
      let unwind_to = function
        | KParen | KBegin | KBrace | KBracket | KBracketBar | KBody _
        | KExternal | KModule | KType | KLet | KLetIn | KException | KVal
        | KBar KType
        | KStruct | KSig | KObject
        | KAnd(KModule|KType|KLet|KLetIn) -> true
        | _ -> false
      in
      let rec find_parent path =
        let path = unwind unwind_to path in
        (match path with
         | [] | {kind=KInOCamldocCode}::_ ->
             make_infix tok block.path
         | {kind=KBody KType}::p -> (* type t = t' = ... *)
             (match p with
              | {kind = KWith (KType|KModule)
                      | KAnd KWith (KType|KModule)}::_ ->
                  find_parent p
              | _ -> replace (KBody KType) L ~pad:config.i_type path)
         | {kind=KBrace}::_ ->
             (match
                unwind_while (fun kind -> prio kind > prio_semi) block.path
              with
              | Some ({kind=KExpr prio}::_) when prio = prio_semi + 1 ->
                  (* already after a field binding: this '=' must be
                     the normal operator *)
                  make_infix tok block.path
              | Some p ->
                  extend (KExpr (prio_semi+1)) T ~pad:config.i_base p
              | None ->
                  make_infix tok block.path)
         | {kind=KParen|KBegin|KBracket|KBracketBar|KBody _|KBar KType}::_ ->
             make_infix tok block.path
         | {kind=KAnd kind | kind} as h::p ->
             let indent = match next_token stream, kind with
               | Some (STRUCT|SIG), _ -> 0
               | _, (KType | KBody KType) -> config.i_type
               | _ -> config.i_base
             in
             if starts_line then
               let h = {h with indent = h.indent + indent; pad = 0} in
               replace (KBody kind) L ~pad:0 (h :: p)
             else
               let h = {h with indent = h.column} in
               replace (KBody kind) T ~pad:indent (h :: p))
      in
      find_parent block.path

  | COLONEQUAL | INFIXOP2 "+=" ->
      (match
        unwind_while (function KExpr _ | KType -> true | _ -> false) block.path
      with
      | Some ({kind=KType}::_ as p) -> (* type t := t' *)
          replace (KBody KType) L p
      | _ ->
          make_infix tok block.path)

  | COLON ->
      let path = unwind (function
          | KParen | KBegin | KBrace | KBracket | KBracketBar | KBody _
          | KModule | KLet | KLetIn | KExternal | KVal
          | KAnd(KModule|KLet|KLetIn) -> true
          | _ -> false)
          block.path
      in
      (match path with
       | {kind = KModule|KLet|KLetIn|KExternal
         | KAnd(KModule|KLet|KLetIn|KExternal)} :: _ ->
           append KColon L path
       | {kind=KVal} :: {kind=KObject} :: _ ->
           make_infix tok path
       | {kind=KVal} as h :: p ->
           let indent = config.i_base in
           if starts_line then
             let h = {h with indent = h.indent + indent; pad = 0} in
             replace (KBody h.kind) L ~pad:0 (h :: p)
           else
             replace (KBody h.kind) L ~pad:indent (h :: p)
       | {kind=KBrace}::_ -> (* record type *)
           (match block.path with
            | {kind=KExpr i}::{kind=KBrace}::_ as p
              when i = prio_max ->
                extend KColon L p
            | {kind=KExpr i}::({kind=KExpr j}::{kind=KBrace}::_ as p)
              when i = prio_max && j = prio_apply -> (* "mutable" *)
                extend KColon L p
            | _ -> make_infix tok block.path)
       | _ -> make_infix tok block.path)

  | SEMI ->
      (match unwind (fun kind -> prio kind < prio_semi) block.path with
       | {kind=KColon}::({kind=KBrace}::_ as p) -> p
       | _ -> make_infix tok block.path)

  (* Some commom preprocessor directives *)
  | UIDENT ("INCLUDE"|"IFDEF"|"THEN"|"ELSE"|"ENDIF"
           |"TEST"|"TEST_UNIT"|"TEST_MODULE"
           |"BENCH"|"BENCH_FUN"|"BENCH_MODULE"|"BENCH_INDEXED"
            as s)
    when starts_line ->
      if
        String.sub s 0 4 = "TEST"
        || (String.length s > 4 && String.sub s 0 5 = "BENCH")
      then
        append KLet L ~pad:(2 * config.i_base) (unwind_top block.path)
      else
        replace KUnknown L (unwind_top block.path)

  | EXTERNAL ->
      append KExternal L (unwind_top block.path)

  | DOT ->
      let last_expr =
        unwind_while (function KExpr _ -> true | _ -> false) block.path
      in
      (match last_expr with
       | Some ({kind=KExpr _} :: {kind=KType} :: ({kind=KColon} :: _ as p)) ->
           (* let f: type t. t -> t = ... *)
           p
       | Some ({kind=KExpr i} :: ({kind=KBrace|KWith KBrace} as h :: p))
         when (i = prio_max || i = prio_dot) && next_offset tok stream = None ->
           (* special case: distributive { Module. field; field } *)
           { h with pad = config.i_base } :: p
       | _ -> make_infix tok block.path)

  | AMPERAMPER | BARBAR ->
      (* back-indented when after if or when and not alone *)
      let op_prio, _align, _indent = op_prio_align_indent config tok.token in
      (match unwind_while (fun kind -> prio kind >= op_prio) block.path with
       | Some ({kind=KExpr _; line}::{kind=KWhen|KIf; line=line_if}::_ as p)
         when line = line_if && next_offset tok stream <> None ->
           extend (KExpr op_prio) T ~pad:(-3) p
       | _ -> make_infix tok block.path)

  | LESS ->
      if is_inside_type block.path then
        (* object type *)
        open_paren KBrace block.path
      else
        make_infix tok block.path

  | GREATER ->
      if is_inside_type block.path then
        match unwind (function
            | KParen | KBegin | KBracket | KBrace | KBracketBar
            | KBody(KType|KExternal) -> true
            | _ -> false)
            block.path
        with
        | {kind=KBrace}::_ as p ->
            close (fun _ -> true) p
        | _ -> append expr_apply L (fold_expr block.path)
      else
        make_infix tok block.path

  | LESSMINUS | COMMA | OR
  | AMPERSAND | INFIXOP0 _ | INFIXOP1 _
  | COLONCOLON | INFIXOP2 _ | PLUSDOT | PLUS | MINUSDOT | MINUS
  | INFIXOP3 _ | STAR | INFIXOP4 _
  | SHARP | AS | COLONGREATER
  | OF ->
      make_infix tok block.path

  | LABEL _ | OPTLABEL _ ->
      (match
        unwind_while (function
          | KExpr _ | KLet | KLetIn | KFun | KAnd(KLet|KLetIn) -> true
          | _ -> false)
          block.path
      with
      | Some ({kind=KExpr _}::_) | None ->
          (* considered as infix, but forcing function application *)
          make_infix tok (fold_expr block.path)
      | _ -> (* in function definition *)
          atom block.path)

  | UIDENT _ ->
      (match block.path with
       | {kind=KBody KType}::_
         when starts_line && next_token stream <> Some DOT ->
           (* type =\nA\n| B : append a virtual bar before A for alignment *)
           let path = append (KBar KType) L ~pad:2 block.path
           in atom path
       | {kind=KBracket} as br::({kind=KBody KType; line}::_ as p)
         when starts_line ->
           (* type = [\n`A\n| `B ]: append a virtual bar before `A *)
           let path =
             if br.line > line then {br with pad = 0} :: p
             else block.path
           in
           let path = append (KBar KType) L ~pad:2 path
           in atom path
       | {kind=KModule | KInclude | KOpen}::_ when not starts_line ->
           (* indent functor parameters as if indent was flushed (like after a
              newline) *)
           Path.maptop (fun n ->
               let indent = n.indent + n.pad in
               {n with indent; line_indent = indent; pad = config.i_base}
             ) (atom block.path)
       | _ -> atom block.path)

  | INT64 _ | INT32 _ | INT _ | LIDENT _
  | FLOAT _ | CHAR _ | TYPEVAR
  | TRUE | FALSE | NATIVEINT _
  | UNDERSCORE | TILDE | QUESTION
  | QUOTE ->
      atom block.path

  | PREFIXOP _ | BANG | QUESTIONQUESTION ->
      (* FIXME: should be highest priority, > atom
         ( append is not right for atoms ) *)
      atom block.path

  | ASSERT | LAZY | NEW | MUTABLE ->
      append expr_apply L (before_append_atom block.path)

  | INHERIT -> append (KExpr 0) L (unwind_top block.path)

  | DOTDOT ->
      (match block.path with
       | {kind = KBody KType} :: _ -> atom block.path
       | _ -> append KUnknown L block.path)

  | VIRTUAL
  | REC
  | PRIVATE | EOF
  | BACKQUOTE | ILLEGAL_CHAR _ | LINE_DIRECTIVE ->
      (* indent the token, but otherwise ignored *)
      append KUnknown L block.path

  | EOL | ESCAPED_EOL -> assert false
  | SPACES -> assert false

let update config block stream tok =

  let starts_line = block.newlines <> 0 in

  let block =
    match block.last with
    | { token = ( COMMENT_CLOSE | COMMENT_CODE_CLOSE ) } :: last ->
        { block with last }
    | _ -> block in

  match tok.token, block.path with

  (* String and quotation *)

  | (EOL | ESCAPED_EOL),
    ( (({ kind = ( KInString _ | KInQuotation ) } as node) :: path)
    | ({ kind = ( KInStringIndent | KInQuotationIndent ) } ::
       ({ kind = _ } as node) :: path) ) ->
      let path =
        if starts_line
           && tok.token = ESCAPED_EOL
           && Path.is_indented_string block.path then
          let indent = node.indent + node.pad in
          { kind = KInStringIndent;
            indent; line_indent=indent; column=indent; pad = 0 ;
            line = node.line } :: node :: path
        else
          { node with indent = node.column;
                      line_indent = node.column } :: path in
      let last = block.last in
      let toff = 0 in
      let orig = Region.start_column tok.region in
      let newlines = if tok.token = ESCAPED_EOL then -1 else 1 in
      let pp_stack = block.pp_stack in
      { path; last; toff; orig; newlines; starts_line; pp_stack }

  | ESCAPED_EOL, { kind = ( KInComment _ | KInCommentIndent ) } :: _
  | EOL, _ ->
      { block with newlines = block.newlines + 1; starts_line }

  | _ ->

      let path = update_path config block stream tok in
      let last =
        match tok.token, block.last with
        | ( COMMENT_OPEN | COMMENT_CODE_OPEN ), last -> tok :: last
        | _, _ :: last -> tok :: last
        | _, [] -> [tok] in
      let toff =
        if block.newlines <> 0
        then Path.indent path
        else block.toff + tok.offset in
      let orig = Region.start_column tok.region in
      let newlines =
        match tok.token with
        | COMMENT_OPEN_EOL -> 1
        | _ -> 0 in
      let pp_stack = block.pp_stack in
      { path; last; toff; orig; newlines; starts_line; pp_stack }


let indent t = Path.indent t.path

let padding t = Path.pad t.path

let set_column t col =
  { t with
    path = Path.maptop (fun n -> {n with indent = col}) t.path;
    toff = col }

let reverse t =
  let col = t.orig in
  let expected = t.toff in
  if col = expected then t
  else match t.last with
    | _ :: _ when t.starts_line ->
        let diff = col - expected in
        let path = match t.path with
          | _ when Path.in_string t.path && last_token t <> Some STRING_OPEN ->
              (* Do not reverse in string except for the opening quote *)
              t.path
          | n::[] ->
              { n with indent = col; column = col } :: []
          | ({kind= KInComment (tok, _, b1, b2)} as n)::r ->
              { n with kind = KInComment (tok, col, b1, b2);
                       indent = col; column = col }
              :: r
          | ({kind= KInOCamldocVerbatim} as n)::r ->
              { n with kind=KInOCamldocVerbatim; indent = col; column = col }
              :: r
          | n1::n2::p ->
              { n1 with indent = col; column = col }
              :: { n2 with pad = n2.pad + diff }
              :: p
          | [] -> []
        in
        { t with path; toff = col }
    | _ -> { t with toff = col }

let guess_indent t =
  let path =
    unwind (function KUnknown -> false | _ -> true)
      t.path
  in
  match path with
  | { kind = KExpr i } :: p
    when i = prio_max && t.newlines > 2 ->
      (* closed expr and newline: we probably want a toplevel block *)
      let p = unwind_top p in
      Path.indent p + Path.pad p
  | path ->
      (* we probably want to write a child of the current node *)
      let path =
        match
          unwind_while (function KExpr p -> p >= prio_apply | _ -> false) path
        with Some p -> p
           | None -> path
      in match path with
      | {indent;pad}::_ -> indent + pad
      | [] -> 0

let is_at_top t = match t.path with
  | [] -> true
  | [{kind}] -> stritem_kind kind
  | _ -> false

let is_declaration t =
  match t.path with
  | [] -> true
  | { kind = KStruct | KSig | KBegin | KObject } :: _ -> true
  | _ -> false

let is_in_comment t = Path.in_comment t.path || Path.in_ocamldoc_verbatim t.path

let is_in_string t = Path.in_string t.path

let starts_line t = t.starts_line
OCaml

Innovation. Community. Security.