cf90116dc8cf7f25ec1975c6dcad71598c45a6d2
[ghc-hetmet.git] / ghc / compiler / utils / Unpretty.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[Unpretty]{Unpretty-printing data type}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module Unpretty (
10         Unpretty(..),
11
12         uppNil, uppStr, uppPStr, uppChar, uppInt, uppInteger,
13         uppSP, upp'SP, uppLbrack, uppRbrack, uppLparen, uppRparen,
14         uppSemi, uppComma, uppEquals,
15
16         uppBracket, uppParens,
17         uppCat, uppBeside, uppBesides, uppAbove, uppAboves,
18         uppNest, uppSep, uppInterleave, uppIntersperse,
19         uppShow,
20         uppAppendFile,
21
22         -- abstract type, to complete the interface...
23         CSeq
24    ) where
25
26 CHK_Ubiq() -- debugging consistency check
27
28 import CharSeq
29 \end{code}
30
31 Same interface as @Pretty@, but doesn't do anything.
32
33 The pretty type is redefined here:
34 \begin{code}
35 type Unpretty = CSeq
36 \end{code}
37
38 %************************************************
39 %*                                              *
40         \subsection{The interface}
41 %*                                              *
42 %************************************************
43
44 \begin{code}
45 uppNil          :: Unpretty
46 uppSP, upp'SP, uppLbrack, uppRbrack, uppLparen, uppRparen, uppSemi, uppComma, uppEquals :: Unpretty
47
48 uppStr          :: [Char] -> Unpretty
49 uppPStr         :: FAST_STRING -> Unpretty
50 uppChar         :: Char -> Unpretty
51 uppInt          :: Int -> Unpretty
52 uppInteger      :: Integer -> Unpretty
53
54 uppBracket      :: Unpretty -> Unpretty -- put brackets around it
55 uppParens       :: Unpretty -> Unpretty -- put parens   around it
56
57 uppBeside       :: Unpretty -> Unpretty -> Unpretty
58 uppBesides      :: [Unpretty] -> Unpretty
59 ppBesideSP      :: Unpretty -> Unpretty -> Unpretty
60 uppCat          :: [Unpretty] -> Unpretty               -- i.e., ppBesidesSP
61
62 uppAbove        :: Unpretty -> Unpretty -> Unpretty
63 uppAboves       :: [Unpretty] -> Unpretty
64
65 uppInterleave   :: Unpretty -> [Unpretty] -> Unpretty
66 uppIntersperse  :: Unpretty -> [Unpretty] -> Unpretty   -- no spaces between
67 uppSep          :: [Unpretty] -> Unpretty
68 uppNest         :: Int -> Unpretty -> Unpretty
69
70 uppShow         :: Int -> Unpretty -> [Char]
71
72 uppAppendFile   :: _FILE -> Int -> Unpretty -> IO ()
73 \end{code}
74
75 %************************************************
76 %*                                              *
77         \subsection{The representation}
78 %*                                              *
79 %************************************************
80
81 \begin{code}
82 uppShow _ p     = cShow p
83
84 uppAppendFile f _ p = cAppendFile f p
85
86 uppNil          = cNil
87 uppStr s        = cStr s
88 uppPStr s       = cPStr s
89 uppChar c       = cCh c
90 uppInt n        = cInt n
91
92 uppInteger n    = cStr (show n)
93
94 uppSP           = cCh ' '
95 upp'SP{-'-}     = cPStr SLIT(", ")
96 uppLbrack       = cCh '['
97 uppRbrack       = cCh ']'
98 uppLparen       = cCh '('
99 uppRparen       = cCh ')'
100 uppSemi         = cCh ';'
101 uppComma        = cCh ','
102 uppEquals       = cCh '='
103
104 uppBracket p = uppBeside uppLbrack (uppBeside p uppRbrack)
105 uppParens  p = uppBeside uppLparen (uppBeside p uppRparen)
106
107 uppInterleave sep ps = uppSep (pi ps)
108   where
109    pi []        = []
110    pi [x]       = [x]
111    pi (x:xs)    = (cAppend{-uppBeside-} x sep) : pi xs
112 \end{code}
113
114 \begin{code}
115 uppIntersperse sep ps = uppBesides (pi ps)
116   where
117    pi []        = []
118    pi [x]       = [x]
119    pi (x:xs)    = (cAppend{-uppBeside-} x sep) : pi xs
120 \end{code}
121
122 \begin{code}
123 uppBeside p1 p2  = p1 `cAppend` p2
124
125 uppBesides []     = cNil{-uppNil-}
126 uppBesides [p]    = p
127 uppBesides (p:ps) = p `cAppend`{-uppBeside-} uppBesides ps
128 \end{code}
129
130 \begin{code}
131 ppBesideSP p1 p2 = p1 `cAppend` (cCh ' ') `cAppend` p2
132 \end{code}
133
134 @uppCat@ is the name I (WDP) happen to have been using for @ppBesidesSP@.
135
136 \begin{code}
137 uppCat []     = cNil{-uppNil-}
138 uppCat [p]    = p
139 uppCat (p:ps) = ppBesideSP p (uppCat ps)
140
141 uppAbove p1 p2 = p1 `cAppend` (cCh '\n') `cAppend` p2
142
143 uppAboves []     = cNil{-uppNil-}
144 uppAboves [p]    = p
145 uppAboves (p:ps) = p `cAppend` (cCh '\n') `cAppend` (uppAboves ps)
146
147 uppNest n p = p
148
149 uppSep ps = uppBesides ps
150 \end{code}