-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathExactPrint.hs
More file actions
2171 lines (2021 loc) · 78.3 KB
/
ExactPrint.hs
File metadata and controls
2171 lines (2021 loc) · 78.3 KB
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
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE CPP #-}
-----------------------------------------------------------------------------
-- |
-- Module : Language.Haskell.Exts.Annotated.ExactPrint
-- Copyright : (c) Niklas Broberg 2009
-- License : BSD-style (see the file LICENSE.txt)
--
-- Maintainer : Niklas Broberg, d00nibro@chalmers.se
-- Stability : stable
-- Portability : portable
--
-- Exact-printer for Haskell abstract syntax. The input is a (semi-concrete)
-- abstract syntax tree, annotated with exact source information to enable
-- printing the tree exactly as it was parsed.
--
-----------------------------------------------------------------------------
module Language.Haskell.Exts.ExactPrint
( exactPrint
, ExactP
) where
import Language.Haskell.Exts.Syntax
import Language.Haskell.Exts.SrcLoc
import Language.Haskell.Exts.Comments
import Control.Monad (when, liftM, ap, unless)
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative (Applicative(..))
#endif
import Control.Arrow ((***), (&&&))
import Prelude hiding (exp)
import Data.List (intersperse)
-- import Debug.Trace (trace)
------------------------------------------------------
-- The EP monad and basic combinators
type Pos = (Int,Int)
pos :: (SrcInfo loc) => loc -> Pos
pos ss = (startLine ss, startColumn ss)
newtype EP x = EP (Pos -> [Comment] -> (x, Pos, [Comment], ShowS))
instance Functor EP where
fmap = liftM
instance Applicative EP where
pure = return
(<*>) = ap
instance Monad EP where
return x = EP $ \l cs -> (x, l, cs, id)
EP m >>= k = EP $ \l0 c0 -> let
(a, l1, c1, s1) = m l0 c0
EP f = k a
(b, l2, c2, s2) = f l1 c1
in (b, l2, c2, s1 . s2)
runEP :: EP () -> [Comment] -> String
runEP (EP f) cs = let (_,_,_,s) = f (1,1) cs in s ""
getPos :: EP Pos
getPos = EP (\l cs -> (l,l,cs,id))
setPos :: Pos -> EP ()
setPos l = EP (\_ cs -> ((),l,cs,id))
printString :: String -> EP ()
printString str =
EP (\(l,c) cs -> let (l', c') = foldl go (l, c) str
go (cl, _) '\n' = (cl + 1, 1)
go (cl, cc) _ = (cl, cc + 1)
in ((), (l', c'), cs, showString str))
getComment :: EP (Maybe Comment)
getComment = EP $ \l cs ->
let x = case cs of
c:_ -> Just c
_ -> Nothing
in (x, l, cs, id)
dropComment :: EP ()
dropComment = EP $ \l cs ->
let cs' = case cs of
(_:cs1) -> cs1
_ -> cs
in ((), l, cs', id)
newLine :: EP ()
newLine = do
(l,_) <- getPos
printString "\n"
setPos (l+1,1)
padUntil :: Pos -> EP ()
padUntil (l,c) = do
(l1,c1) <- getPos
case {- trace (show ((l,c), (l1,c1))) -} () of
_ {-()-} | l1 >= l && c1 <= c -> printString $ replicate (c - c1) ' '
| l1 < l -> newLine >> padUntil (l,c)
| otherwise -> return ()
mPrintComments :: Pos -> EP ()
mPrintComments p = do
mc <- getComment
case mc of
Nothing -> return ()
Just (Comment multi s str) ->
when (pos s < p) $ do
dropComment
padUntil (pos s)
printComment multi str
setPos (srcSpanEndLine s, srcSpanEndColumn s)
mPrintComments p
printComment :: Bool -> String -> EP ()
printComment b str
| b = printString $ "{-" ++ str ++ "-}"
| otherwise = printString $ "--" ++ str
printWhitespace :: Pos -> EP ()
printWhitespace p = mPrintComments p >> padUntil p
printStringAt :: Pos -> String -> EP ()
printStringAt p str = printWhitespace p >> printString str
errorEP :: String -> EP a
errorEP = fail
------------------------------------------------------------------------------
-- Printing of source elements
-- | Print an AST exactly as specified by the annotations on the nodes in the tree.
exactPrint :: (ExactP ast) => ast SrcSpanInfo -> [Comment] -> String
exactPrint ast = runEP (exactPC ast)
exactPC :: (ExactP ast) => ast SrcSpanInfo -> EP ()
exactPC ast = let p = pos (ann ast) in mPrintComments p >> padUntil p >> exactP ast
printSeq :: [(Pos, EP ())] -> EP ()
printSeq [] = return ()
printSeq ((p,pr):xs) = printWhitespace p >> pr >> printSeq xs
printStrs :: SrcInfo loc => [(loc, String)] -> EP ()
printStrs = printSeq . map (pos *** printString)
printPoints :: SrcSpanInfo -> [String] -> EP ()
printPoints l = printStrs . zip (srcInfoPoints l)
printInterleaved, printInterleaved' :: (ExactP ast, SrcInfo loc) => [(loc, String)] -> [ast SrcSpanInfo] -> EP ()
printInterleaved sistrs asts = printSeq $
interleave (map (pos *** printString ) sistrs)
(map (pos . ann &&& exactP) asts)
printInterleaved' sistrs (a:asts) = exactPC a >> printInterleaved sistrs asts
printInterleaved' _ _ = internalError "printInterleaved'"
printStreams :: [(Pos, EP ())] -> [(Pos, EP ())] -> EP ()
printStreams [] ys = printSeq ys
printStreams xs [] = printSeq xs
printStreams (x@(p1,ep1):xs) (y@(p2,ep2):ys)
| p1 <= p2 = printWhitespace p1 >> ep1 >> printStreams xs (y:ys)
| otherwise = printWhitespace p2 >> ep2 >> printStreams (x:xs) ys
interleave :: [a] -> [a] -> [a]
interleave [] ys = ys
interleave xs [] = xs
interleave (x:xs) (y:ys) = x:y: interleave xs ys
maybeEP :: (a -> EP ()) -> Maybe a -> EP ()
maybeEP = maybe (return ())
bracketList :: (ExactP ast) => (String, String, String) -> [SrcSpan] -> [ast SrcSpanInfo] -> EP ()
bracketList (a,b,c) poss asts = printInterleaved (pList poss (a,b,c)) asts
pList :: [a] -> (b, b, b) -> [(a, b)]
pList (p:ps) (a,b,c) = (p,a) : pList' ps (b,c)
pList _ _ = internalError "pList"
pList' :: [a] -> (b, b) -> [(a, b)]
pList' [] _ = []
pList' [p] (_,c) = [(p,c)]
pList' (p:ps) (b,c) = (p, b) : pList' ps (b,c)
parenList, squareList, squareColonList, curlyList, parenHashList :: (ExactP ast) => [SrcSpan] -> [ast SrcSpanInfo] -> EP ()
parenList = bracketList ("(",",",")")
squareList = bracketList ("[",",","]")
squareColonList = bracketList ("[:",",",":]")
curlyList = bracketList ("{",",","}")
parenHashList = bracketList ("(#",",","#)")
layoutList :: (ExactP ast) => [SrcSpan] -> [ast SrcSpanInfo] -> EP ()
layoutList poss asts = printStreams
(map (pos *** printString) $ lList poss)
(map (pos . ann &&& exactP) asts)
lList :: [SrcSpan] -> [(SrcSpan, String)]
lList (p:ps) = (if isNullSpan p then (p,"") else (p,"{")) : lList' ps
lList _ = internalError "lList"
lList' :: [SrcSpan] -> [(SrcSpan, String)]
lList' [] = []
lList' [p] = [if isNullSpan p then (p,"") else (p,"}")]
lList' (p:ps) = (if isNullSpan p then (p,"") else (p,";")) : lList' ps
printSemi :: SrcSpan -> EP ()
printSemi p = do
printWhitespace (pos p)
unless (isNullSpan p) $ printString ";"
--------------------------------------------------
-- Exact printing
class Annotated ast => ExactP ast where
exactP :: ast SrcSpanInfo -> EP ()
instance ExactP Literal where
exactP lit = case lit of
Char _ _ rw -> printString ('\'':rw ++ "\'")
String _ _ rw -> printString ('\"':rw ++ "\"")
Int _ _ rw -> printString rw
Frac _ _ rw -> printString rw
PrimInt _ _ rw -> printString (rw ++ "#" )
PrimWord _ _ rw -> printString (rw ++ "##")
PrimFloat _ _ rw -> printString (rw ++ "#" )
PrimDouble _ _ rw -> printString (rw ++ "##")
PrimChar _ _ rw -> printString ('\'':rw ++ "\'#" )
PrimString _ _ rw -> printString ('\"':rw ++ "\"#" )
instance ExactP Sign where
exactP sg = case sg of
Signless _ -> return ()
Negative l -> printStringAt (pos l) "-"
instance ExactP ModuleName where
exactP (ModuleName _ str) = printString str
instance ExactP SpecialCon where
exactP sc = case sc of
UnitCon l -> printPoints l ["(",")"]
ListCon l -> printPoints l ["[","]"]
FunCon l -> case srcInfoPoints l of
[_,b,_] -> printStringAt (pos b) "->"
_ -> errorEP "ExactP: SpecialCon is given wrong number of srcInfoPoints"
TupleCon l b n -> printPoints l $
case b of
Unboxed -> "(#": replicate (n-1) "," ++ ["#)"]
_ -> "(" : replicate (n-1) "," ++ [")"]
Cons _ -> printString ":"
UnboxedSingleCon l -> printPoints l ["(#","#)"]
isSymbolName :: Name l -> Bool
isSymbolName (Symbol _ _) = True
isSymbolName _ = False
isSymbolQName :: QName l -> Bool
isSymbolQName (UnQual _ n) = isSymbolName n
isSymbolQName (Qual _ _ n) = isSymbolName n
isSymbolQName (Special _ Cons{}) = True
isSymbolQName (Special _ FunCon{}) = True
isSymbolQName _ = False
instance ExactP QName where
exactP qn
| isSymbolQName qn =
case srcInfoPoints (ann qn) of
[_,b,c] -> do
printString "("
printWhitespace (pos b)
epQName qn
printStringAt (pos c) ")"
_ -> errorEP "ExactP: QName is given wrong number of srcInfoPoints"
| otherwise = epQName qn
epQName :: QName SrcSpanInfo -> EP ()
epQName qn = case qn of
Qual _ mn n -> exactP mn >> printString "." >> exactP n
UnQual _ n -> exactP n
Special _ sc -> exactP sc
epInfixQName :: QName SrcSpanInfo -> EP ()
epInfixQName qn
| isSymbolQName qn = printWhitespace (pos (ann qn)) >> epQName qn
| otherwise =
case srcInfoPoints (ann qn) of
[a,b,c] -> do
printStringAt (pos a) "`"
printWhitespace (pos b)
epQName qn
printStringAt (pos c) "`"
_ -> errorEP "ExactP: QName (epInfixName) is given wrong number of srcInfoPoints"
instance ExactP Name where
exactP n = case n of
Ident _ str -> printString str
Symbol l str ->
case srcInfoPoints l of
[_,b,c] -> do
printString "("
printWhitespace (pos b)
printString str
printStringAt (pos c) ")"
[] -> printString str
_ -> errorEP "ExactP: Name is given wrong number of srcInfoPoints"
epInfixName :: Name SrcSpanInfo -> EP ()
epInfixName n
| isSymbolName n = printWhitespace (pos (ann n)) >> exactP n
| otherwise =
case srcInfoPoints (ann n) of
[a,b,c] -> do
printStringAt (pos a) "`"
printWhitespace (pos b)
exactP n
printStringAt (pos c) "`"
_ -> errorEP "ExactP: Name (epInfixName) is given wrong number of srcInfoPoints"
instance ExactP IPName where
exactP ipn = case ipn of
IPDup _ str -> printString $ '?':str
IPLin _ str -> printString $ '%':str
instance ExactP QOp where
exactP qop = case qop of
QVarOp _ qn -> epInfixQName qn
QConOp _ qn -> epInfixQName qn
instance ExactP Op where
exactP op = case op of
VarOp _ n -> epInfixName n
ConOp _ n -> epInfixName n
instance ExactP CName where
exactP cn = case cn of
VarName _ n -> exactP n
ConName _ n -> exactP n
instance ExactP Namespace where
exactP ns = case ns of
NoNamespace _ -> return ()
TypeNamespace l -> printStringAt (pos l) "type"
PatternNamespace l -> printStringAt (pos l) "pattern"
instance ExactP ExportSpec where
exactP espec = case espec of
EVar _ qn -> exactPC qn
EAbs _ ns qn -> exactP ns >> exactPC qn
EThingWith l wc qn cns ->
let names = case wc of
NoWildcard {} -> cns
EWildcard wcl n ->
let (before,after) = splitAt n cns
wildcardName = VarName wcl (Ident wcl "..")
in before ++ [wildcardName] ++ after
k = length (srcInfoPoints l)
in exactP qn >> printInterleaved (zip (srcInfoPoints l) $ "(":replicate (k-2) "," ++ [")"]) names
EModuleContents _ mn -> printString "module" >> exactPC mn
instance ExactP ExportSpecList where
exactP (ExportSpecList l ess) =
let k = length (srcInfoPoints l)
in printInterleaved (zip (srcInfoPoints l) $ "(": replicate (k-2) "," ++ [")"]) ess
instance ExactP ImportSpecList where
exactP (ImportSpecList l hid ispecs) = do
let pts = srcInfoPoints l
pts1 <- if hid then do
let (x:pts') = pts
printStringAt (pos x) "hiding"
return pts'
else return pts
let k = length pts1
printInterleaved (zip pts1 $ "(": replicate (k-2) "," ++ [")"]) ispecs
instance ExactP ImportSpec where
exactP ispec = case ispec of
IVar _ qn -> exactPC qn
IAbs _ ns n -> exactP ns >> exactPC n
IThingAll l n -> exactP n >> printPoints l ["(","..",")"]
IThingWith l n cns ->
let k = length (srcInfoPoints l)
in exactP n >> printInterleaved (zip (srcInfoPoints l) $ "(":replicate (k-2) "," ++ [")"]) cns
instance ExactP ImportDecl where
exactP (ImportDecl l mn qf src safe mpkg mas mispecs) = do
printString "import"
case srcInfoPoints l of
(_:pts) -> do
pts1 <- if src then
case pts of
x:y:pts' -> do
printStringAt (pos x) "{-# SOURCE"
printStringAt (pos y) "#-}"
return pts'
_ -> errorEP "ExactP: ImportDecl is given too few srcInfoPoints"
else return pts
pts2 <- if safe then
case pts1 of
x:pts' -> do
printStringAt (pos x) "safe"
return pts'
_ -> errorEP "ExactP: ImportDecl is given too few srcInfoPoints"
else return pts1
pts3 <- if qf then
case pts2 of
x:pts' -> do
printStringAt (pos x) "qualified"
return pts'
_ -> errorEP "ExactP: ImportDecl is given too few srcInfoPoints"
else return pts2
pts4 <- case mpkg of
Just pkg ->
case pts3 of
x:pts' -> do
printStringAt (pos x) $ show pkg
return pts'
_ -> errorEP "ExactP: ImportDecl is given too few srcInfoPoints"
_ -> return pts3
exactPC mn
_ <- case mas of
Just as ->
case pts4 of
x:pts' -> do
printStringAt (pos x) "as"
exactPC as
return pts'
_ -> errorEP "ExactP: ImportDecl is given too few srcInfoPoints"
_ -> return pts4
case mispecs of
Nothing -> return ()
Just ispecs -> exactPC ispecs
_ -> errorEP "ExactP: ImportDecl is given too few srcInfoPoints"
instance ExactP Module where
exactP mdl = case mdl of
Module l mmh oss ids decls -> do
let (oPts, pts) = splitAt (max (length oss + 1) 2) (srcInfoPoints l)
layoutList oPts oss
maybeEP exactPC mmh
printStreams (map (pos *** printString) $ lList pts)
(map (pos . ann &&& exactPC) ids ++ map (pos . ann &&& exactPC) (sepFunBinds decls))
XmlPage l _mn oss xn attrs mat es -> do
let (oPts, pPts) = splitAt (max (length oss + 1) 2) $ srcInfoPoints l
case pPts of
[a,b,c,d,e] -> do
layoutList oPts oss
printStringAt (pos a) "<"
exactPC xn
mapM_ exactPC attrs
maybeEP exactPC mat
printStringAt (pos b) ">"
mapM_ exactPC es
printStringAt (pos c) "</"
printWhitespace (pos d)
exactP xn
printStringAt (pos e) ">"
_ -> errorEP "ExactP: Module: XmlPage is given wrong number of srcInfoPoints"
XmlHybrid l mmh oss ids decls xn attrs mat es -> do
let (oPts, pts) = splitAt (max (length oss + 1) 2) (srcInfoPoints l)
layoutList oPts oss
maybeEP exactPC mmh
let (dPts, pPts) = splitAt (length pts - 5) pts
case pPts of
[a,b,c,d,e] -> do
printStreams (map (\(p,s) -> (pos p, printString s)) $ lList dPts)
(map (\i -> (pos $ ann i, exactPC i)) ids ++ map (\d' -> (pos $ ann d', exactPC d')) (sepFunBinds decls))
printStringAt (pos a) "<"
exactPC xn
mapM_ exactPC attrs
maybeEP exactPC mat
printStringAt (pos b) ">"
mapM_ exactPC es
printStringAt (pos c) "</"
printWhitespace (pos d)
exactP xn
printStringAt (pos e) ">"
_ -> errorEP "ExactP: Module: XmlHybrid is given wrong number of srcInfoPoints"
instance ExactP ModuleHead where
exactP (ModuleHead l mn mwt mess) =
case srcInfoPoints l of
[a,b] -> do
printStringAt (pos a) "module"
exactPC mn
maybeEP exactPC mwt
maybeEP exactPC mess
printStringAt (pos b) "where"
_ -> errorEP "ExactP: ModuleHead is given wrong number of srcInfoPoints"
instance ExactP ModulePragma where
exactP op = case op of
LanguagePragma l ns ->
let pts = srcInfoPoints l
k = length ns - 1 -- number of commas
m = length pts - k - 2 -- number of virtual semis, likely 0
in printInterleaved (zip pts ("{-# LANGUAGE":replicate k "," ++ replicate m "" ++ ["#-}"])) ns
OptionsPragma l mt str ->
let k = length (srcInfoPoints l)
-- We strip out a leading space in the lexer unless the pragma
-- starts with a newline.
addSpace xs@('\n':_) = xs
addSpace xs = ' ':xs
opstr = "{-# OPTIONS" ++ case mt of { Just t -> "_" ++ show t ; _ -> "" } ++ addSpace str
in printPoints l $ opstr : replicate (k-2) "" ++ ["#-}"]
AnnModulePragma l ann' ->
case srcInfoPoints l of
[_,b] -> do
printString "{-# ANN"
exactPC ann'
printStringAt (pos b) "#-}"
_ -> errorEP "ExactP: ModulePragma: AnnPragma is given wrong number of srcInfoPoints"
instance ExactP WarningText where
exactP (DeprText l str) = printPoints l ["{-# DEPRECATED", str, "#-}"]
exactP (WarnText l str) = printPoints l ["{-# WARNING", str, "#-}"]
instance ExactP Assoc where
exactP a = case a of
AssocNone _ -> printString "infix"
AssocLeft _ -> printString "infixl"
AssocRight _ -> printString "infixr"
instance ExactP DataOrNew where
exactP (DataType _) = printString "data"
exactP (NewType _) = printString "newtype"
instance ExactP TypeEqn where
exactP (TypeEqn l t1 t2) =
case srcInfoPoints l of
[a] -> do
exactPC t1
printStringAt (pos a) "="
exactPC t2
_ -> errorEP "ExactP: TypeEqn is given wrong number of srcInfoPoints"
instance ExactP InjectivityInfo where
exactP (InjectivityInfo l to from) =
case srcInfoPoints l of
a:b:_ -> do
printStringAt (pos a) "|"
exactPC to
printStringAt (pos b) "->"
mapM_ exactPC from
_ -> errorEP "ExactP: InjectivityInfo given wrong number of srcInfoPoints"
instance ExactP ResultSig where
exactP (KindSig l k) =
case srcInfoPoints l of
a:_ -> do
printStringAt (pos a) "::"
exactPC k
_ -> errorEP "ExactP: ResultSig given wrong number of srcInfoPoints"
exactP (TyVarSig l tv) =
case srcInfoPoints l of
a:_ -> do
printStringAt (pos a) "="
exactPC tv
_ -> errorEP "ExactP: ResultSig given wrong number of srcInfoPoints"
instance ExactP Decl where
exactP decl = case decl of
TypeDecl l dh t ->
case srcInfoPoints l of
[a,b] -> do
printStringAt (pos a) "type"
exactPC dh
printStringAt (pos b) "="
exactPC t
_ -> errorEP "ExactP: Decl: TypeDecl is given wrong number of srcInfoPoints"
TypeFamDecl l dh mk mi ->
case srcInfoPoints l of
a:b:_ -> do
printStringAt (pos a) "type"
printStringAt (pos b) "family"
exactPC dh
maybeEP exactPC mk
maybeEP exactPC mi
_ -> errorEP "ExactP: Decl: TypeFamDecl is given wrong number of srcInfoPoints"
ClosedTypeFamDecl l dh mk mi eqns ->
case srcInfoPoints l of
a:b:c:_ -> do
printStringAt (pos a) "type"
printStringAt (pos b) "family"
exactPC dh
maybeEP exactPC mk
maybeEP exactPC mi
printStringAt (pos c) "where"
mapM_ exactP eqns
_ -> errorEP "ExactP: Decl: ClosedTypeFamDecl is given wrong number of srcInfoPoints"
DataDecl l dn mctxt dh constrs mder -> do
exactP dn
maybeEP exactPC mctxt
exactPC dh
-- the next line works for empty data types since the srcInfoPoints will be empty then
printInterleaved (zip (srcInfoPoints l) ("=": repeat "|")) constrs
maybeEP exactPC mder
GDataDecl l dn mctxt dh mk gds mder -> do
let pts = srcInfoPoints l
exactP dn
maybeEP exactPC mctxt
exactPC dh
pts1 <- case mk of
Nothing -> return pts
Just kd -> case pts of
p:pts' -> do
printStringAt (pos p) "::"
exactPC kd
return pts'
_ -> errorEP "ExactP: Decl: GDataDecl is given too few srcInfoPoints"
case pts1 of
x:pts' -> do
printStringAt (pos x) "where"
layoutList pts' gds
maybeEP exactPC mder
_ -> errorEP "ExactP: Decl: GDataDecl is given too few srcInfoPoints"
DataFamDecl l mctxt dh mk -> do
printString "data"
maybeEP exactPC mctxt
exactPC dh
maybeEP (\kd -> printStringAt (pos (head (srcInfoPoints l))) "::" >> exactPC kd) mk
TypeInsDecl l t1 t2 ->
case srcInfoPoints l of
[_,b,c] -> do
printString "type"
printStringAt (pos b) "instance"
exactPC t1
printStringAt (pos c) "="
exactPC t2
_ -> errorEP "ExactP: Decl: TypeInsDecl is given wrong number of srcInfoPoints"
DataInsDecl l dn t constrs mder ->
case srcInfoPoints l of
p:pts -> do
exactP dn
printStringAt (pos p) "instance"
exactPC t
printInterleaved (zip pts ("=": repeat "|")) constrs
maybeEP exactPC mder
_ -> errorEP "ExactP: Decl: DataInsDecl is given too few srcInfoPoints"
GDataInsDecl l dn t mk gds mder ->
case srcInfoPoints l of
p:pts -> do
exactP dn
printStringAt (pos p) "instance"
exactPC t
pts1 <- case mk of
Nothing -> return pts
Just kd -> case pts of
p':pts' -> do
printStringAt (pos p') "::"
exactPC kd
return pts'
_ -> errorEP "ExactP: Decl: GDataInsDecl is given too few srcInfoPoints"
case pts1 of
x:pts' -> do
printStringAt (pos x) "where"
layoutList pts' gds
maybeEP exactPC mder
_ -> errorEP "ExactP: Decl: GDataInsDecl is given too few srcInfoPoints"
_ -> errorEP "ExactP: Decl: GDataInsDecl is given too few srcInfoPoints"
ClassDecl l mctxt dh fds mcds ->
case srcInfoPoints l of
_:pts -> do
printString "class"
maybeEP exactPC mctxt
exactPC dh
_ <- case fds of
[] -> return pts
_ -> do
let (pts1, pts2) = splitAt (length fds) pts
printInterleaved (zip pts1 ("|":repeat ",")) fds
return pts2
maybeEP (\cds ->
case pts of
p:pts' -> do
printStringAt (pos p) "where"
layoutList pts' $ sepClassFunBinds cds
_ -> errorEP "ExactP: Decl: ClassDecl is given too few srcInfoPoints"
) mcds
_ -> errorEP "ExactP: Decl: ClassDecl is given too few srcInfoPoints"
InstDecl l movlp ih mids ->
case srcInfoPoints l of
_:pts -> do
printString "instance"
maybeEP exactPC movlp
exactPC ih
maybeEP (\ids -> do
let (p:pts') = pts
printStringAt (pos p) "where"
layoutList pts' $ sepInstFunBinds ids
) mids
_ -> errorEP "ExactP: Decl: InstDecl is given too few srcInfoPoints"
DerivDecl l movlp ih ->
case srcInfoPoints l of
[_,b] -> do
printString "deriving"
printStringAt (pos b) "instance"
maybeEP exactPC movlp
exactPC ih
_ -> errorEP "ExactP: Decl: DerivDecl is given wrong number of srcInfoPoints"
InfixDecl l assoc mprec ops -> do
let pts = srcInfoPoints l
exactP assoc
pts1 <- case mprec of
Nothing -> return pts
Just prec ->
case pts of
p:pts' -> do
printStringAt (pos p) (show prec)
return pts'
_ -> errorEP "ExactP: Decl: InfixDecl is given too few srcInfoPoints"
printInterleaved' (zip pts1 (repeat ",")) ops
DefaultDecl l ts ->
case srcInfoPoints l of
_:pts -> do
printString "default"
printInterleaved (zip (init pts) ("(":repeat ",")) ts
printStringAt (pos (last pts)) ")"
_ -> errorEP "ExactP: Decl: DefaultDecl is given too few srcInfoPoints"
SpliceDecl _ spl -> exactP spl
TypeSig l ns t -> do
let pts = srcInfoPoints l
printInterleaved' (zip pts (replicate (length pts - 1) "," ++ ["::"])) ns
exactPC t
PatSynSig l n dh c1 c2 t -> do
case srcInfoPoints l of
(pat:dc:pts1) -> do
printStringAt (pos pat) "pattern"
exactPC n
printStringAt (pos dc) "::"
case dh of
Nothing -> return ()
Just tvs ->
case pts1 of
(a:b:_) -> do
printStringAt (pos a) "forall"
mapM_ exactPC tvs
printStringAt (pos b) "."
_ -> errorEP "ExactP: Decl: PatSynSig: Forall: is given too few srcInfoPoints"
maybeEP exactPC c1
maybeEP exactPC c2
exactPC t
_ -> errorEP "ExactP: Decl: PatSynSig: is given too few srcInfoPoints"
FunBind _ ms -> mapM_ exactPC ms
PatBind l p rhs mbs -> do
let pts = srcInfoPoints l
exactP p
exactPC rhs
maybeEP (\bs -> printStringAt (pos (head pts)) "where" >> exactPC bs) mbs
PatSyn l lhs rhs dir ->
case srcInfoPoints l of
[pat,sepPos] -> do
let sep = case dir of
ImplicitBidirectional -> "="
ExplicitBidirectional _ _ -> "<-"
Unidirectional -> "<-"
printStringAt (pos pat) "pattern"
exactPC lhs
printStringAt (pos sepPos) sep
exactPC rhs
case dir of
ExplicitBidirectional bl ds -> do
case srcInfoPoints bl of
(w:pts) -> do
printStringAt (pos w) "where"
layoutList pts ds
_ -> errorEP "ExactP: Decl: PaySyn: ExplicitBidirectional is given too few srcInfoPoints"
_ -> return ()
_ -> errorEP "ExactP: Decl: PatSyn is given too few srcInfoPoints"
ForImp l cc msf mstr n t ->
case srcInfoPoints l of
_:b:pts -> do
printString "foreign"
printStringAt (pos b) "import"
exactPC cc
maybeEP exactPC msf
pts1 <- case mstr of
Nothing -> return pts
Just str -> case pts of
x:pts' -> do
printStringAt (pos x) (show str)
return pts'
_ -> errorEP "ExactP: Decl: ForImp is given too few srcInfoPoints"
case pts1 of
y:_ -> do
exactPC n
printStringAt (pos y) "::"
exactPC t
_ -> errorEP "ExactP: Decl: ForImp is given too few srcInfoPoints"
_ -> errorEP "ExactP: Decl: ForImp is given too few srcInfoPoints"
ForExp l cc mstr n t ->
case srcInfoPoints l of
_:b:pts -> do
printString "foreign"
printStringAt (pos b) "export"
exactPC cc
pts1 <- case mstr of
Nothing -> return pts
Just str -> case pts of
x:pts' -> do
printStringAt (pos x) (show str)
return pts'
_ -> errorEP "ExactP: Decl: ForExp is given too few srcInfoPoints"
case pts1 of
y:_ -> do
exactPC n
printStringAt (pos y) "::"
exactPC t
_ -> errorEP "ExactP: Decl: ForExp is given too few srcInfoPoints"
_ -> errorEP "ExactP: Decl: ForExp is given too few srcInfoPoints"
RulePragmaDecl l rs ->
case srcInfoPoints l of
[_,b] -> do
printString "{-# RULES"
mapM_ exactPC rs
printStringAt (pos b) "#-}"
_ -> errorEP "ExactP: Decl: RulePragmaDecl is given too few srcInfoPoints"
DeprPragmaDecl l nstrs ->
case srcInfoPoints l of
_:pts -> do
printString "{-# DEPRECATED"
printWarndeprs (map pos (init pts)) nstrs
printStringAt (pos (last pts)) "#-}"
_ -> errorEP "ExactP: Decl: DeprPragmaDecl is given too few srcInfoPoints"
WarnPragmaDecl l nstrs ->
case srcInfoPoints l of
_:pts -> do
printString "{-# WARNING"
printWarndeprs (map pos (init pts)) nstrs
printStringAt (pos (last pts)) "#-}"
_ -> errorEP "ExactP: Decl: WarnPragmaDecl is given too few srcInfoPoints"
InlineSig l inl mact qn ->
case srcInfoPoints l of
[_,b] -> do
printString $ if inl then "{-# INLINE" else "{-# NOINLINE"
maybeEP exactPC mact
exactPC qn
printStringAt (pos b) "#-}"
_ -> errorEP "ExactP: Decl: InlineSig is given wrong number of srcInfoPoints"
InlineConlikeSig l mact qn ->
case srcInfoPoints l of
[_,b] -> do
printString "{-# INLINE CONLIKE"
maybeEP exactPC mact
exactPC qn
printStringAt (pos b) "#-}"
_ -> errorEP "ExactP: Decl: InlineConlikeSig is given wrong number of srcInfoPoints"
SpecSig l mact qn ts ->
case srcInfoPoints l of
_:pts -> do
printString "{-# SPECIALISE"
maybeEP exactPC mact
exactPC qn
printInterleaved (zip pts ("::" : replicate (length pts - 2) "," ++ ["#-}"])) ts
_ -> errorEP "ExactP: Decl: SpecSig is given too few srcInfoPoints"
SpecInlineSig l b mact qn ts ->
case srcInfoPoints l of
_:pts -> do
printString $ "{-# SPECIALISE " ++ if b then "INLINE" else "NOINLINE"
maybeEP exactPC mact
exactPC qn
printInterleaved (zip pts ("::" : replicate (length pts - 2) "," ++ ["#-}"])) ts
_ -> errorEP "ExactP: Decl: SpecInlineSig is given too few srcInfoPoints"
InstSig l ih ->
case srcInfoPoints l of
[_,b,c] -> do
printString "{-# SPECIALISE"
printStringAt (pos b) "instance"
exactPC ih
printStringAt (pos c) "#-}"
_ -> errorEP "ExactP: Decl: InstSig is given wrong number of srcInfoPoints"
AnnPragma l ann' ->
case srcInfoPoints l of
[_,b] -> do
printString "{-# ANN"
exactPC ann'
printStringAt (pos b) "#-}"
_ -> errorEP "ExactP: Decl: AnnPragma is given wrong number of srcInfoPoints"
MinimalPragma l b ->
case srcInfoPoints l of
[_,b'] -> do
printString "{-# MINIMAL"
maybeEP exactPC b
printStringAt (pos b') "#-}"
_ -> errorEP "ExactP: Decl: MinimalPragma is given wrong number of srcInfoPoints"
RoleAnnotDecl l ty roles ->
case srcInfoPoints l of
(t:r:_) -> do
printStringAt (pos t) "type"
printStringAt (pos r) "role"
exactPC ty
mapM_ exactPC roles
_ -> errorEP "ExactP: Decl: RoleAnnotDecl is given wrong number of srcInfoPoints"
instance ExactP Role where
exactP r =
case r of
RoleWildcard l -> printStringAt (pos l) "_"
Representational l -> printStringAt (pos l) "representational"
Phantom l -> printStringAt (pos l) "phantom"
Nominal l -> printStringAt (pos l) "nominal"
instance ExactP Annotation where
exactP ann' = case ann' of
Ann _ n e -> do
exactP n
exactPC e
TypeAnn _ n e -> do
printString "type"
exactPC n
exactPC e
ModuleAnn _ e -> do
printString "module"
exactPC e
instance ExactP BooleanFormula where
exactP b' = case b' of
VarFormula _ n -> exactPC n
AndFormula l bs ->
let pts = srcInfoPoints l
in printStreams (zip (map pos pts) (repeat $ printString ",")) (map (pos . ann &&& exactPC) bs)
OrFormula l bs ->
let pts = srcInfoPoints l
in printStreams (zip (map pos pts) (repeat $ printString "|")) (map (pos . ann &&& exactPC) bs)
ParenFormula l b ->
case srcInfoPoints l of
[a'',b''] -> printStringAt (pos a'') "(" >> exactPC b >> printStringAt (pos b'') ")"
_ -> errorEP "ExactP: BooleanFormula: ParenFormula is given wrong number of srcInfoPoints"
printWarndeprs :: [Pos] -> [([Name SrcSpanInfo], String)] -> EP ()
printWarndeprs _ [] = return ()
printWarndeprs ps' ((ns',str'):nsts') = printWd ps' ns' str' nsts'
where printWd :: [Pos] -> [Name SrcSpanInfo] -> String -> [([Name SrcSpanInfo], String)] -> EP ()
printWd (p:ps) [] str nsts = printStringAt p (show str) >> printWarndeprs ps nsts
printWd ps [n] str nsts = exactPC n >> printWd ps [] str nsts
printWd (p:ps) (n:ns) str nsts = exactPC n >> printStringAt p "," >> printWd ps ns str nsts
printWd _ _ _ _ = internalError "printWd"
sepFunBinds :: [Decl SrcSpanInfo] -> [Decl SrcSpanInfo]
sepFunBinds [] = []
sepFunBinds (FunBind _ ms:ds) = map (\m -> FunBind (ann m) [m]) ms ++ sepFunBinds ds
sepFunBinds (d:ds) = d : sepFunBinds ds
sepClassFunBinds :: [ClassDecl SrcSpanInfo] -> [ClassDecl SrcSpanInfo]
sepClassFunBinds [] = []
sepClassFunBinds (ClsDecl _ (FunBind _ ms):ds) = map (\m -> ClsDecl (ann m) $ FunBind (ann m) [m]) ms ++ sepClassFunBinds ds
sepClassFunBinds (d:ds) = d : sepClassFunBinds ds
sepInstFunBinds :: [InstDecl SrcSpanInfo] -> [InstDecl SrcSpanInfo]
sepInstFunBinds [] = []
sepInstFunBinds (InsDecl _ (FunBind _ ms):ds) = map (\m -> InsDecl (ann m) $ FunBind (ann m) [m]) ms ++ sepInstFunBinds ds
sepInstFunBinds (d:ds) = d : sepInstFunBinds ds
instance ExactP DeclHead where
exactP dh' = case dh' of
DHead _ n -> exactP n
DHInfix _ tva n -> exactP tva >> epInfixName n
DHParen l dh ->
case srcInfoPoints l of
[_,b] -> printString "(" >> exactPC dh >> printStringAt (pos b) ")"
_ -> errorEP "ExactP: DeclHead: DeclParen is given wrong number of srcInfoPoints"
DHApp _ dh t -> exactP dh >> exactPC t
instance ExactP InstRule where
exactP ih' = case ih' of
IRule l mtvs mctxt qn -> do
let pts = srcInfoPoints l
_ <- case mtvs of
Nothing -> return pts
Just tvs ->
case pts of
[a,b] -> do
printStringAt (pos a) "forall"
mapM_ exactPC tvs
printStringAt (pos b) "."
return pts
_ -> errorEP "ExactP: InstRule: IRule is given too few srcInfoPoints"
maybeEP exactPC mctxt
exactPC qn
IParen l ih ->
case srcInfoPoints l of
[a,b] -> printStringAt (pos a) "(" >> exactPC ih >> printStringAt (pos b) ")"
_ -> errorEP "ExactP: InstRule: IParen is given wrong number of srcInfoPoints"
instance ExactP InstHead where
exactP doih' = case doih' of
IHCon _ qn -> exactPC qn
IHInfix _ ta qn -> exactPC ta >> epInfixQName qn
IHParen l doih ->