module Text.PrettyPrint.Annotated.Leijen (
Doc, putDoc, hPutDoc,
empty, char, text, (<>), nest, line, linebreak, group, softline,
softbreak,
align, hang, indent, encloseSep, list, tupled, semiBraces,
(<+>), (<$>), (</>), (<$$>), (<//>),
hsep, vsep, fillSep, sep, hcat, vcat, fillCat, cat, punctuate,
fill, fillBreak,
enclose, squotes, dquotes, parens, angles, braces, brackets,
lparen, rparen, langle, rangle, lbrace, rbrace, lbracket, rbracket,
squote, dquote, semi, colon, comma, space, dot, backslash, equals,
pipe,
string, int, integer, float, double, rational, bool,
annotate, noAnnotate,
SimpleDoc(..), renderPretty, renderCompact, displayDecorated, displayDecoratedA, display, displayS, displayIO,
SpanList(..), displaySpans
, column, nesting, width
) where
import System.IO (Handle,hPutStr,hPutChar,stdout)
import Data.String
import Prelude ((.), ($), (/=), (<), (<=), (>), (>=), (-), (*), (+), (++),
Bool(..), Char, Double, Float, Functor, Int, Integer, IO, Rational, Show, ShowS,
id, error, flip, foldr1, fromIntegral, length, max, min, otherwise, repeat, replicate,
return, round, seq, show, showChar, showString, showsPrec, span, zipWith)
import Control.Applicative (Applicative(..), liftA2)
import Data.Monoid (Monoid(..))
infixr 5 </>,<//>,<$>,<$$>
infixr 6 <>,<+>
instance IsString (Doc a) where
fromString :: String -> Doc a
fromString = forall a. String -> Doc a
text
list :: [Doc a] -> Doc a
list :: forall a. [Doc a] -> Doc a
list = forall a. Doc a -> Doc a -> Doc a -> [Doc a] -> Doc a
encloseSep forall a. Doc a
lbracket forall a. Doc a
rbracket forall a. Doc a
comma
tupled :: [Doc a] -> Doc a
tupled :: forall a. [Doc a] -> Doc a
tupled = forall a. Doc a -> Doc a -> Doc a -> [Doc a] -> Doc a
encloseSep forall a. Doc a
lparen forall a. Doc a
rparen forall a. Doc a
comma
semiBraces :: [Doc a] -> Doc a
semiBraces :: forall a. [Doc a] -> Doc a
semiBraces = forall a. Doc a -> Doc a -> Doc a -> [Doc a] -> Doc a
encloseSep forall a. Doc a
lbrace forall a. Doc a
rbrace forall a. Doc a
semi
encloseSep :: Doc a -> Doc a -> Doc a -> [Doc a] -> Doc a
encloseSep :: forall a. Doc a -> Doc a -> Doc a -> [Doc a] -> Doc a
encloseSep Doc a
left Doc a
right Doc a
sep [Doc a]
ds
= case [Doc a]
ds of
[] -> Doc a
left forall a. Doc a -> Doc a -> Doc a
<> Doc a
right
[Doc a
d] -> Doc a
left forall a. Doc a -> Doc a -> Doc a
<> Doc a
d forall a. Doc a -> Doc a -> Doc a
<> Doc a
right
[Doc a]
_ -> forall a. Doc a -> Doc a
align (forall a. [Doc a] -> Doc a
cat (forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall a. Doc a -> Doc a -> Doc a
(<>) (Doc a
left forall a. a -> [a] -> [a]
: forall a. a -> [a]
repeat Doc a
sep) [Doc a]
ds) forall a. Doc a -> Doc a -> Doc a
<> Doc a
right)
punctuate :: Doc a -> [Doc a] -> [Doc a]
punctuate :: forall a. Doc a -> [Doc a] -> [Doc a]
punctuate Doc a
p [] = []
punctuate Doc a
p [Doc a
d] = [Doc a
d]
punctuate Doc a
p (Doc a
d:[Doc a]
ds) = (Doc a
d forall a. Doc a -> Doc a -> Doc a
<> Doc a
p) forall a. a -> [a] -> [a]
: forall a. Doc a -> [Doc a] -> [Doc a]
punctuate Doc a
p [Doc a]
ds
sep :: [Doc a] -> Doc a
sep :: forall a. [Doc a] -> Doc a
sep = forall a. Doc a -> Doc a
group forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [Doc a] -> Doc a
vsep
fillSep :: [Doc a] -> Doc a
fillSep :: forall a. [Doc a] -> Doc a
fillSep = forall {a}. (Doc a -> Doc a -> Doc a) -> [Doc a] -> Doc a
fold forall a. Doc a -> Doc a -> Doc a
(</>)
hsep :: [Doc a] -> Doc a
hsep :: forall a. [Doc a] -> Doc a
hsep = forall {a}. (Doc a -> Doc a -> Doc a) -> [Doc a] -> Doc a
fold forall a. Doc a -> Doc a -> Doc a
(<+>)
vsep :: [Doc a] -> Doc a
vsep :: forall a. [Doc a] -> Doc a
vsep = forall {a}. (Doc a -> Doc a -> Doc a) -> [Doc a] -> Doc a
fold forall a. Doc a -> Doc a -> Doc a
(<$>)
cat :: [Doc a] -> Doc a
cat :: forall a. [Doc a] -> Doc a
cat = forall a. Doc a -> Doc a
group forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [Doc a] -> Doc a
vcat
fillCat :: [Doc a] -> Doc a
fillCat :: forall a. [Doc a] -> Doc a
fillCat = forall {a}. (Doc a -> Doc a -> Doc a) -> [Doc a] -> Doc a
fold forall a. Doc a -> Doc a -> Doc a
(<//>)
hcat :: [Doc a] -> Doc a
hcat :: forall a. [Doc a] -> Doc a
hcat = forall {a}. (Doc a -> Doc a -> Doc a) -> [Doc a] -> Doc a
fold forall a. Doc a -> Doc a -> Doc a
(<>)
vcat :: [Doc a] -> Doc a
vcat :: forall a. [Doc a] -> Doc a
vcat = forall {a}. (Doc a -> Doc a -> Doc a) -> [Doc a] -> Doc a
fold forall a. Doc a -> Doc a -> Doc a
(<$$>)
fold :: (Doc a -> Doc a -> Doc a) -> [Doc a] -> Doc a
fold Doc a -> Doc a -> Doc a
f [] = forall a. Doc a
empty
fold Doc a -> Doc a -> Doc a
f [Doc a]
ds = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 Doc a -> Doc a -> Doc a
f [Doc a]
ds
(<>) :: Doc a -> Doc a -> Doc a
Doc a
x <> :: forall a. Doc a -> Doc a -> Doc a
<> Doc a
y = Doc a
x forall a. Doc a -> Doc a -> Doc a
`beside` Doc a
y
(<+>) :: Doc a -> Doc a -> Doc a
Doc a
x <+> :: forall a. Doc a -> Doc a -> Doc a
<+> Doc a
y = Doc a
x forall a. Doc a -> Doc a -> Doc a
<> forall a. Doc a
space forall a. Doc a -> Doc a -> Doc a
<> Doc a
y
(</>) :: Doc a -> Doc a -> Doc a
Doc a
x </> :: forall a. Doc a -> Doc a -> Doc a
</> Doc a
y = Doc a
x forall a. Doc a -> Doc a -> Doc a
<> forall a. Doc a
softline forall a. Doc a -> Doc a -> Doc a
<> Doc a
y
(<//>) :: Doc a -> Doc a -> Doc a
Doc a
x <//> :: forall a. Doc a -> Doc a -> Doc a
<//> Doc a
y = Doc a
x forall a. Doc a -> Doc a -> Doc a
<> forall a. Doc a
softbreak forall a. Doc a -> Doc a -> Doc a
<> Doc a
y
(<$>) :: Doc a -> Doc a -> Doc a
Doc a
x <$> :: forall a. Doc a -> Doc a -> Doc a
<$> Doc a
y = Doc a
x forall a. Doc a -> Doc a -> Doc a
<> forall a. Doc a
line forall a. Doc a -> Doc a -> Doc a
<> Doc a
y
(<$$>) :: Doc a -> Doc a -> Doc a
Doc a
x <$$> :: forall a. Doc a -> Doc a -> Doc a
<$$> Doc a
y = Doc a
x forall a. Doc a -> Doc a -> Doc a
<> forall a. Doc a
linebreak forall a. Doc a -> Doc a -> Doc a
<> Doc a
y
softline :: Doc a
softline :: forall a. Doc a
softline = forall a. Doc a -> Doc a
group forall a. Doc a
line
softbreak :: Doc a
softbreak :: forall a. Doc a
softbreak = forall a. Doc a -> Doc a
group forall a. Doc a
linebreak
squotes :: Doc a -> Doc a
squotes :: forall a. Doc a -> Doc a
squotes = forall a. Doc a -> Doc a -> Doc a -> Doc a
enclose forall a. Doc a
squote forall a. Doc a
squote
dquotes :: Doc a -> Doc a
dquotes :: forall a. Doc a -> Doc a
dquotes = forall a. Doc a -> Doc a -> Doc a -> Doc a
enclose forall a. Doc a
dquote forall a. Doc a
dquote
braces :: Doc a -> Doc a
braces :: forall a. Doc a -> Doc a
braces = forall a. Doc a -> Doc a -> Doc a -> Doc a
enclose forall a. Doc a
lbrace forall a. Doc a
rbrace
parens :: Doc a -> Doc a
parens :: forall a. Doc a -> Doc a
parens = forall a. Doc a -> Doc a -> Doc a -> Doc a
enclose forall a. Doc a
lparen forall a. Doc a
rparen
angles :: Doc a -> Doc a
angles :: forall a. Doc a -> Doc a
angles = forall a. Doc a -> Doc a -> Doc a -> Doc a
enclose forall a. Doc a
langle forall a. Doc a
rangle
brackets :: Doc a -> Doc a
brackets :: forall a. Doc a -> Doc a
brackets = forall a. Doc a -> Doc a -> Doc a -> Doc a
enclose forall a. Doc a
lbracket forall a. Doc a
rbracket
enclose :: Doc a -> Doc a -> Doc a -> Doc a
enclose :: forall a. Doc a -> Doc a -> Doc a -> Doc a
enclose Doc a
l Doc a
r Doc a
x = Doc a
l forall a. Doc a -> Doc a -> Doc a
<> Doc a
x forall a. Doc a -> Doc a -> Doc a
<> Doc a
r
lparen :: Doc a
lparen :: forall a. Doc a
lparen = forall a. Char -> Doc a
char Char
'('
rparen :: Doc a
rparen :: forall a. Doc a
rparen = forall a. Char -> Doc a
char Char
')'
langle :: Doc a
langle :: forall a. Doc a
langle = forall a. Char -> Doc a
char Char
'<'
rangle :: Doc a
rangle :: forall a. Doc a
rangle = forall a. Char -> Doc a
char Char
'>'
lbrace :: Doc a
lbrace :: forall a. Doc a
lbrace = forall a. Char -> Doc a
char Char
'{'
rbrace :: Doc a
rbrace :: forall a. Doc a
rbrace = forall a. Char -> Doc a
char Char
'}'
lbracket :: Doc a
lbracket :: forall a. Doc a
lbracket = forall a. Char -> Doc a
char Char
'['
rbracket :: Doc a
rbracket :: forall a. Doc a
rbracket = forall a. Char -> Doc a
char Char
']'
squote :: Doc a
squote :: forall a. Doc a
squote = forall a. Char -> Doc a
char Char
'\''
dquote :: Doc a
dquote :: forall a. Doc a
dquote = forall a. Char -> Doc a
char Char
'"'
semi :: Doc a
semi :: forall a. Doc a
semi = forall a. Char -> Doc a
char Char
';'
colon :: Doc a
colon :: forall a. Doc a
colon = forall a. Char -> Doc a
char Char
':'
comma :: Doc a
comma :: forall a. Doc a
comma = forall a. Char -> Doc a
char Char
','
space :: Doc a
space :: forall a. Doc a
space = forall a. Char -> Doc a
char Char
' '
dot :: Doc a
dot :: forall a. Doc a
dot = forall a. Char -> Doc a
char Char
'.'
backslash :: Doc a
backslash :: forall a. Doc a
backslash = forall a. Char -> Doc a
char Char
'\\'
equals :: Doc a
equals :: forall a. Doc a
equals = forall a. Char -> Doc a
char Char
'='
pipe :: Doc a
pipe :: forall a. Doc a
pipe = forall a. Char -> Doc a
char Char
'|'
string :: String -> Doc a
string :: forall a. String -> Doc a
string String
"" = forall a. Doc a
empty
string (Char
'\n':String
s) = forall a. Doc a
line forall a. Doc a -> Doc a -> Doc a
<> forall a. String -> Doc a
string String
s
string String
s = case (forall a. (a -> Bool) -> [a] -> ([a], [a])
span (forall a. Eq a => a -> a -> Bool
/=Char
'\n') String
s) of
(String
xs,String
ys) -> forall a. String -> Doc a
text String
xs forall a. Doc a -> Doc a -> Doc a
<> forall a. String -> Doc a
string String
ys
bool :: Bool -> Doc a
bool :: forall a. Bool -> Doc a
bool Bool
b = forall a. String -> Doc a
text (forall a. Show a => a -> String
show Bool
b)
int :: Int -> Doc a
int :: forall a. Int -> Doc a
int Int
i = forall a. String -> Doc a
text (forall a. Show a => a -> String
show Int
i)
integer :: Integer -> Doc a
integer :: forall a. Integer -> Doc a
integer Integer
i = forall a. String -> Doc a
text (forall a. Show a => a -> String
show Integer
i)
float :: Float -> Doc a
float :: forall a. Float -> Doc a
float Float
f = forall a. String -> Doc a
text (forall a. Show a => a -> String
show Float
f)
double :: Double -> Doc a
double :: forall a. Double -> Doc a
double Double
d = forall a. String -> Doc a
text (forall a. Show a => a -> String
show Double
d)
rational :: Rational -> Doc a
rational :: forall a. Rational -> Doc a
rational Rational
r = forall a. String -> Doc a
text (forall a. Show a => a -> String
show Rational
r)
fillBreak :: Int -> Doc a -> Doc a
fillBreak :: forall a. Int -> Doc a -> Doc a
fillBreak Int
f Doc a
x = forall a. Doc a -> (Int -> Doc a) -> Doc a
width Doc a
x (\Int
w ->
if (Int
w forall a. Ord a => a -> a -> Bool
> Int
f) then forall a. Int -> Doc a -> Doc a
nest Int
f forall a. Doc a
linebreak
else forall a. String -> Doc a
text (Int -> String
spaces (Int
f forall a. Num a => a -> a -> a
- Int
w)))
fill :: Int -> Doc a -> Doc a
fill :: forall a. Int -> Doc a -> Doc a
fill Int
f Doc a
d = forall a. Doc a -> (Int -> Doc a) -> Doc a
width Doc a
d (\Int
w ->
if (Int
w forall a. Ord a => a -> a -> Bool
>= Int
f) then forall a. Doc a
empty
else forall a. String -> Doc a
text (Int -> String
spaces (Int
f forall a. Num a => a -> a -> a
- Int
w)))
width :: Doc a -> (Int -> Doc a) -> Doc a
width :: forall a. Doc a -> (Int -> Doc a) -> Doc a
width Doc a
d Int -> Doc a
f = forall a. (Int -> Doc a) -> Doc a
column (\Int
k1 -> Doc a
d forall a. Doc a -> Doc a -> Doc a
<> forall a. (Int -> Doc a) -> Doc a
column (\Int
k2 -> Int -> Doc a
f (Int
k2 forall a. Num a => a -> a -> a
- Int
k1)))
indent :: Int -> Doc a -> Doc a
indent :: forall a. Int -> Doc a -> Doc a
indent Int
i Doc a
d = forall a. Int -> Doc a -> Doc a
hang Int
i (forall a. String -> Doc a
text (Int -> String
spaces Int
i) forall a. Doc a -> Doc a -> Doc a
<> Doc a
d)
hang :: Int -> Doc a -> Doc a
hang :: forall a. Int -> Doc a -> Doc a
hang Int
i Doc a
d = forall a. Doc a -> Doc a
align (forall a. Int -> Doc a -> Doc a
nest Int
i Doc a
d)
align :: Doc a -> Doc a
align :: forall a. Doc a -> Doc a
align Doc a
d = forall a. (Int -> Doc a) -> Doc a
column (\Int
k ->
forall a. (Int -> Doc a) -> Doc a
nesting (\Int
i -> forall a. Int -> Doc a -> Doc a
nest (Int
k forall a. Num a => a -> a -> a
- Int
i) Doc a
d))
data Doc a = Empty
| Char Char
| Text !Int String
| Line !Bool
| Cat (Doc a) (Doc a)
| Nest !Int (Doc a)
| Union (Doc a) (Doc a)
| Column (Int -> Doc a)
| Nesting (Int -> Doc a)
| Annotate a (Doc a)
| AnnotEnd
deriving forall a b. a -> Doc b -> Doc a
forall a b. (a -> b) -> Doc a -> Doc b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Doc b -> Doc a
$c<$ :: forall a b. a -> Doc b -> Doc a
fmap :: forall a b. (a -> b) -> Doc a -> Doc b
$cfmap :: forall a b. (a -> b) -> Doc a -> Doc b
Functor
type SpanList a = [(Int, Int, a)]
data SimpleDoc a = SEmpty
| SChar Char (SimpleDoc a)
| SText !Int String (SimpleDoc a)
| SLine !Int (SimpleDoc a)
| SAnnotStart a (SimpleDoc a)
| SAnnotStop (SimpleDoc a)
deriving forall a b. a -> SimpleDoc b -> SimpleDoc a
forall a b. (a -> b) -> SimpleDoc a -> SimpleDoc b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> SimpleDoc b -> SimpleDoc a
$c<$ :: forall a b. a -> SimpleDoc b -> SimpleDoc a
fmap :: forall a b. (a -> b) -> SimpleDoc a -> SimpleDoc b
$cfmap :: forall a b. (a -> b) -> SimpleDoc a -> SimpleDoc b
Functor
empty :: Doc a
empty :: forall a. Doc a
empty = forall a. Doc a
Empty
char :: Char -> Doc a
char :: forall a. Char -> Doc a
char Char
'\n' = forall a. Doc a
line
char Char
c = forall a. Char -> Doc a
Char Char
c
text :: String -> Doc a
text :: forall a. String -> Doc a
text String
"" = forall a. Doc a
Empty
text String
s = forall a. Int -> String -> Doc a
Text (forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s) String
s
line :: Doc a
line :: forall a. Doc a
line = forall a. Bool -> Doc a
Line Bool
False
linebreak :: Doc a
linebreak :: forall a. Doc a
linebreak = forall a. Bool -> Doc a
Line Bool
True
beside :: Doc a -> Doc a -> Doc a
beside Doc a
x Doc a
y = forall a. Doc a -> Doc a -> Doc a
Cat Doc a
x Doc a
y
nest :: Int -> Doc a -> Doc a
nest :: forall a. Int -> Doc a -> Doc a
nest Int
i Doc a
x = forall a. Int -> Doc a -> Doc a
Nest Int
i Doc a
x
column, nesting :: (Int -> Doc a) -> Doc a
column :: forall a. (Int -> Doc a) -> Doc a
column Int -> Doc a
f = forall a. (Int -> Doc a) -> Doc a
Column Int -> Doc a
f
nesting :: forall a. (Int -> Doc a) -> Doc a
nesting Int -> Doc a
f = forall a. (Int -> Doc a) -> Doc a
Nesting Int -> Doc a
f
group :: Doc a -> Doc a
group :: forall a. Doc a -> Doc a
group Doc a
x = forall a. Doc a -> Doc a -> Doc a
Union (forall a. Doc a -> Doc a
flatten Doc a
x) Doc a
x
flatten :: Doc a -> Doc a
flatten :: forall a. Doc a -> Doc a
flatten (Cat Doc a
x Doc a
y) = forall a. Doc a -> Doc a -> Doc a
Cat (forall a. Doc a -> Doc a
flatten Doc a
x) (forall a. Doc a -> Doc a
flatten Doc a
y)
flatten (Nest Int
i Doc a
x) = forall a. Int -> Doc a -> Doc a
Nest Int
i (forall a. Doc a -> Doc a
flatten Doc a
x)
flatten (Line Bool
break) = if Bool
break then forall a. Doc a
Empty else forall a. Int -> String -> Doc a
Text Int
1 String
" "
flatten (Union Doc a
x Doc a
y) = forall a. Doc a -> Doc a
flatten Doc a
x
flatten (Column Int -> Doc a
f) = forall a. (Int -> Doc a) -> Doc a
Column (forall a. Doc a -> Doc a
flatten forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc a
f)
flatten (Nesting Int -> Doc a
f) = forall a. (Int -> Doc a) -> Doc a
Nesting (forall a. Doc a -> Doc a
flatten forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc a
f)
flatten Doc a
other = Doc a
other
annotate :: a -> Doc a -> Doc a
annotate :: forall a. a -> Doc a -> Doc a
annotate = forall a. a -> Doc a -> Doc a
Annotate
noAnnotate :: Doc a -> Doc a
noAnnotate :: forall a. Doc a -> Doc a
noAnnotate (Cat Doc a
x Doc a
y) = forall a. Doc a -> Doc a -> Doc a
Cat (forall a. Doc a -> Doc a
noAnnotate Doc a
x) (forall a. Doc a -> Doc a
noAnnotate Doc a
y)
noAnnotate (Nest Int
i Doc a
x) = forall a. Int -> Doc a -> Doc a
Nest Int
i (forall a. Doc a -> Doc a
noAnnotate Doc a
x)
noAnnotate (Union Doc a
x Doc a
y) = forall a. Doc a -> Doc a -> Doc a
Union (forall a. Doc a -> Doc a
noAnnotate Doc a
x) (forall a. Doc a -> Doc a
noAnnotate Doc a
y)
noAnnotate (Column Int -> Doc a
f) = forall a. (Int -> Doc a) -> Doc a
Column (forall a. Doc a -> Doc a
noAnnotate forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc a
f)
noAnnotate (Nesting Int -> Doc a
f) = forall a. (Int -> Doc a) -> Doc a
Nesting (forall a. Doc a -> Doc a
noAnnotate forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc a
f)
noAnnotate (Annotate a
_ Doc a
x) = forall a. Doc a -> Doc a
noAnnotate Doc a
x
noAnnotate Doc a
other = Doc a
other
data Docs a = Nil
| Cons !Int (Doc a) (Docs a)
renderPretty :: Float -> Int -> Doc a -> SimpleDoc a
renderPretty :: forall a. Float -> Int -> Doc a -> SimpleDoc a
renderPretty Float
rfrac Int
w Doc a
x
= forall {a}. Int -> Int -> Docs a -> SimpleDoc a
best Int
0 Int
0 (forall a. Int -> Doc a -> Docs a -> Docs a
Cons Int
0 Doc a
x forall a. Docs a
Nil)
where
r :: Int
r = forall a. Ord a => a -> a -> a
max Int
0 (forall a. Ord a => a -> a -> a
min Int
w (forall a b. (RealFrac a, Integral b) => a -> b
round (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
w forall a. Num a => a -> a -> a
* Float
rfrac)))
best :: Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k Docs a
Nil = forall a. SimpleDoc a
SEmpty
best Int
n Int
k (Cons Int
i Doc a
d Docs a
ds)
= case Doc a
d of
Doc a
Empty -> Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k Docs a
ds
Char Char
c -> let k' :: Int
k' = Int
kforall a. Num a => a -> a -> a
+Int
1 in seq :: forall a b. a -> b -> b
seq Int
k' (forall a. Char -> SimpleDoc a -> SimpleDoc a
SChar Char
c (Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k' Docs a
ds))
Text Int
l String
s -> let k' :: Int
k' = Int
kforall a. Num a => a -> a -> a
+Int
l in seq :: forall a b. a -> b -> b
seq Int
k' (forall a. Int -> String -> SimpleDoc a -> SimpleDoc a
SText Int
l String
s (Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k' Docs a
ds))
Line Bool
_ -> forall a. Int -> SimpleDoc a -> SimpleDoc a
SLine Int
i (Int -> Int -> Docs a -> SimpleDoc a
best Int
i Int
i Docs a
ds)
Cat Doc a
x Doc a
y -> Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k (forall a. Int -> Doc a -> Docs a -> Docs a
Cons Int
i Doc a
x (forall a. Int -> Doc a -> Docs a -> Docs a
Cons Int
i Doc a
y Docs a
ds))
Nest Int
j Doc a
x -> let i' :: Int
i' = Int
iforall a. Num a => a -> a -> a
+Int
j in seq :: forall a b. a -> b -> b
seq Int
i' (Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k (forall a. Int -> Doc a -> Docs a -> Docs a
Cons Int
i' Doc a
x Docs a
ds))
Union Doc a
x Doc a
y -> forall {a}. Int -> Int -> SimpleDoc a -> SimpleDoc a -> SimpleDoc a
nicest Int
n Int
k (Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k (forall a. Int -> Doc a -> Docs a -> Docs a
Cons Int
i Doc a
x Docs a
ds))
(Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k (forall a. Int -> Doc a -> Docs a -> Docs a
Cons Int
i Doc a
y Docs a
ds))
Column Int -> Doc a
f -> Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k (forall a. Int -> Doc a -> Docs a -> Docs a
Cons Int
i (Int -> Doc a
f Int
k) Docs a
ds)
Nesting Int -> Doc a
f -> Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k (forall a. Int -> Doc a -> Docs a -> Docs a
Cons Int
i (Int -> Doc a
f Int
i) Docs a
ds)
Annotate a
a Doc a
d' -> forall a. a -> SimpleDoc a -> SimpleDoc a
SAnnotStart a
a (Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k (forall a. Int -> Doc a -> Docs a -> Docs a
Cons Int
i Doc a
d' (forall a. Int -> Doc a -> Docs a -> Docs a
Cons Int
i forall a. Doc a
AnnotEnd Docs a
ds)))
Doc a
AnnotEnd -> forall a. SimpleDoc a -> SimpleDoc a
SAnnotStop (Int -> Int -> Docs a -> SimpleDoc a
best Int
n Int
k Docs a
ds)
nicest :: Int -> Int -> SimpleDoc a -> SimpleDoc a -> SimpleDoc a
nicest Int
n Int
k SimpleDoc a
x SimpleDoc a
y | forall {a}. Int -> SimpleDoc a -> Bool
fits Int
width SimpleDoc a
x = SimpleDoc a
x
| Bool
otherwise = SimpleDoc a
y
where
width :: Int
width = forall a. Ord a => a -> a -> a
min (Int
w forall a. Num a => a -> a -> a
- Int
k) (Int
r forall a. Num a => a -> a -> a
- Int
k forall a. Num a => a -> a -> a
+ Int
n)
fits :: Int -> SimpleDoc a -> Bool
fits Int
w SimpleDoc a
x | Int
w forall a. Ord a => a -> a -> Bool
< Int
0 = Bool
False
fits Int
w SimpleDoc a
SEmpty = Bool
True
fits Int
w (SChar Char
c SimpleDoc a
x) = Int -> SimpleDoc a -> Bool
fits (Int
w forall a. Num a => a -> a -> a
- Int
1) SimpleDoc a
x
fits Int
w (SText Int
l String
s SimpleDoc a
x) = Int -> SimpleDoc a -> Bool
fits (Int
w forall a. Num a => a -> a -> a
- Int
l) SimpleDoc a
x
fits Int
w (SLine Int
i SimpleDoc a
x) = Bool
True
fits Int
w (SAnnotStart a
_ SimpleDoc a
x) = Int -> SimpleDoc a -> Bool
fits Int
w SimpleDoc a
x
fits Int
w (SAnnotStop SimpleDoc a
x) = Int -> SimpleDoc a -> Bool
fits Int
w SimpleDoc a
x
renderCompact :: Doc a -> SimpleDoc a
renderCompact :: forall a. Doc a -> SimpleDoc a
renderCompact Doc a
x
= forall {a}. Int -> [Doc a] -> SimpleDoc a
scan Int
0 [Doc a
x]
where
scan :: Int -> [Doc a] -> SimpleDoc a
scan Int
k [] = forall a. SimpleDoc a
SEmpty
scan Int
k (Doc a
d:[Doc a]
ds) = case Doc a
d of
Doc a
Empty -> Int -> [Doc a] -> SimpleDoc a
scan Int
k [Doc a]
ds
Char Char
c -> let k' :: Int
k' = Int
kforall a. Num a => a -> a -> a
+Int
1 in seq :: forall a b. a -> b -> b
seq Int
k' (forall a. Char -> SimpleDoc a -> SimpleDoc a
SChar Char
c (Int -> [Doc a] -> SimpleDoc a
scan Int
k' [Doc a]
ds))
Text Int
l String
s -> let k' :: Int
k' = Int
kforall a. Num a => a -> a -> a
+Int
l in seq :: forall a b. a -> b -> b
seq Int
k' (forall a. Int -> String -> SimpleDoc a -> SimpleDoc a
SText Int
l String
s (Int -> [Doc a] -> SimpleDoc a
scan Int
k' [Doc a]
ds))
Line Bool
_ -> forall a. Int -> SimpleDoc a -> SimpleDoc a
SLine Int
0 (Int -> [Doc a] -> SimpleDoc a
scan Int
0 [Doc a]
ds)
Cat Doc a
x Doc a
y -> Int -> [Doc a] -> SimpleDoc a
scan Int
k (Doc a
xforall a. a -> [a] -> [a]
:Doc a
yforall a. a -> [a] -> [a]
:[Doc a]
ds)
Nest Int
j Doc a
x -> Int -> [Doc a] -> SimpleDoc a
scan Int
k (Doc a
xforall a. a -> [a] -> [a]
:[Doc a]
ds)
Union Doc a
x Doc a
y -> Int -> [Doc a] -> SimpleDoc a
scan Int
k (Doc a
yforall a. a -> [a] -> [a]
:[Doc a]
ds)
Column Int -> Doc a
f -> Int -> [Doc a] -> SimpleDoc a
scan Int
k (Int -> Doc a
f Int
kforall a. a -> [a] -> [a]
:[Doc a]
ds)
Nesting Int -> Doc a
f -> Int -> [Doc a] -> SimpleDoc a
scan Int
k (Int -> Doc a
f Int
0forall a. a -> [a] -> [a]
:[Doc a]
ds)
Annotate a
a Doc a
d' -> forall a. a -> SimpleDoc a -> SimpleDoc a
SAnnotStart a
a (Int -> [Doc a] -> SimpleDoc a
scan Int
k (Doc a
d'forall a. a -> [a] -> [a]
:forall a. Doc a
AnnotEndforall a. a -> [a] -> [a]
:[Doc a]
ds))
Doc a
AnnotEnd -> forall a. SimpleDoc a -> SimpleDoc a
SAnnotStop forall a b. (a -> b) -> a -> b
$ Int -> [Doc a] -> SimpleDoc a
scan Int
k [Doc a]
ds
display :: SimpleDoc a -> String
display :: forall a. SimpleDoc a -> String
display = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. SimpleDoc a -> ShowS
displayS String
""
displayS :: SimpleDoc a -> ShowS
displayS :: forall a. SimpleDoc a -> ShowS
displayS SimpleDoc a
SEmpty = forall a. a -> a
id
displayS (SChar Char
c SimpleDoc a
x) = Char -> ShowS
showChar Char
c forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SimpleDoc a -> ShowS
displayS SimpleDoc a
x
displayS (SText Int
l String
s SimpleDoc a
x) = String -> ShowS
showString String
s forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SimpleDoc a -> ShowS
displayS SimpleDoc a
x
displayS (SLine Int
i SimpleDoc a
x) = String -> ShowS
showString (Char
'\n'forall a. a -> [a] -> [a]
:Int -> String
indentation Int
i) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SimpleDoc a -> ShowS
displayS SimpleDoc a
x
displayS (SAnnotStart a
_ SimpleDoc a
x) = forall a. SimpleDoc a -> ShowS
displayS SimpleDoc a
x
displayS (SAnnotStop SimpleDoc a
x) = forall a. SimpleDoc a -> ShowS
displayS SimpleDoc a
x
displayIO :: Handle -> SimpleDoc a -> IO ()
displayIO :: forall a. Handle -> SimpleDoc a -> IO ()
displayIO Handle
handle SimpleDoc a
simpleDoc
= forall {a}. SimpleDoc a -> IO ()
display SimpleDoc a
simpleDoc
where
display :: SimpleDoc a -> IO ()
display SimpleDoc a
SEmpty = forall (m :: * -> *) a. Monad m => a -> m a
return ()
display (SChar Char
c SimpleDoc a
x) = do{ Handle -> Char -> IO ()
hPutChar Handle
handle Char
c; SimpleDoc a -> IO ()
display SimpleDoc a
x}
display (SText Int
l String
s SimpleDoc a
x) = do{ Handle -> String -> IO ()
hPutStr Handle
handle String
s; SimpleDoc a -> IO ()
display SimpleDoc a
x}
display (SLine Int
i SimpleDoc a
x) = do{ Handle -> String -> IO ()
hPutStr Handle
handle (Char
'\n'forall a. a -> [a] -> [a]
:Int -> String
indentation Int
i); SimpleDoc a -> IO ()
display SimpleDoc a
x}
display (SAnnotStart a
_ SimpleDoc a
x) = SimpleDoc a -> IO ()
display SimpleDoc a
x
display (SAnnotStop SimpleDoc a
x) = SimpleDoc a -> IO ()
display SimpleDoc a
x
displaySpans :: SimpleDoc a -> (String, SpanList a)
displaySpans :: forall a. SimpleDoc a -> (String, SpanList a)
displaySpans SimpleDoc a
sd = forall a. Int -> [(Int, a)] -> SimpleDoc a -> (String, SpanList a)
display Int
0 [] SimpleDoc a
sd
where display :: Int -> [(Int, a)] -> SimpleDoc a -> (String, SpanList a)
display :: forall a. Int -> [(Int, a)] -> SimpleDoc a -> (String, SpanList a)
display Int
i [] SimpleDoc a
SEmpty = (String
"", [])
display Int
i [(Int, a)]
stk (SChar Char
c SimpleDoc a
x) = let (String
str, SpanList a
spans) = forall a. Int -> [(Int, a)] -> SimpleDoc a -> (String, SpanList a)
display (Int
iforall a. Num a => a -> a -> a
+Int
1) [(Int, a)]
stk SimpleDoc a
x
in (Char
cforall a. a -> [a] -> [a]
:String
str, SpanList a
spans)
display Int
i [(Int, a)]
stk (SText Int
l String
s SimpleDoc a
x) = forall a b c. (a -> b) -> (a, c) -> (b, c)
mapFst (String
sforall a. [a] -> [a] -> [a]
++) (forall a. Int -> [(Int, a)] -> SimpleDoc a -> (String, SpanList a)
display (Int
i forall a. Num a => a -> a -> a
+ Int
l) [(Int, a)]
stk SimpleDoc a
x)
display Int
i [(Int, a)]
stk (SLine Int
ind SimpleDoc a
x) = forall a b c. (a -> b) -> (a, c) -> (b, c)
mapFst ((Char
'\n'forall a. a -> [a] -> [a]
:Int -> String
indentation Int
ind)forall a. [a] -> [a] -> [a]
++) (forall a. Int -> [(Int, a)] -> SimpleDoc a -> (String, SpanList a)
display (Int
1forall a. Num a => a -> a -> a
+Int
iforall a. Num a => a -> a -> a
+Int
ind) [(Int, a)]
stk SimpleDoc a
x)
display Int
i [(Int, a)]
stk (SAnnotStart a
ann SimpleDoc a
x) = forall a. Int -> [(Int, a)] -> SimpleDoc a -> (String, SpanList a)
display Int
i ((Int
i, a
ann)forall a. a -> [a] -> [a]
:[(Int, a)]
stk) SimpleDoc a
x
display Int
i ((Int
start, a
ann):[(Int, a)]
stk) (SAnnotStop SimpleDoc a
x) = forall a b c. (a -> b) -> (c, a) -> (c, b)
mapSnd ((Int
start, Int
iforall a. Num a => a -> a -> a
-Int
start, a
ann)forall a. a -> [a] -> [a]
:) (forall a. Int -> [(Int, a)] -> SimpleDoc a -> (String, SpanList a)
display Int
i [(Int, a)]
stk SimpleDoc a
x)
display Int
_ [] (SAnnotStop SimpleDoc a
_) = forall a. HasCallStack => String -> a
error String
"stack underflow"
display Int
_ [(Int, a)]
stk SimpleDoc a
SEmpty = forall a. HasCallStack => String -> a
error String
"Stack not consumed by rendering"
mapFst :: (a -> b) -> (a, c) -> (b, c)
mapFst :: forall a b c. (a -> b) -> (a, c) -> (b, c)
mapFst a -> b
f (a
x, c
y) = (a -> b
f a
x, c
y)
mapSnd :: (a -> b) -> (c, a) -> (c, b)
mapSnd :: forall a b c. (a -> b) -> (c, a) -> (c, b)
mapSnd a -> b
f (c
x, a
y) = (c
x, a -> b
f a
y)
displayDecorated :: (a -> String -> String) -> SimpleDoc a -> String
displayDecorated :: forall a. (a -> ShowS) -> SimpleDoc a -> String
displayDecorated a -> ShowS
decor SimpleDoc a
sd = ShowS -> ShowS -> [(ShowS, ShowS)] -> SimpleDoc a -> ShowS
display forall a. a -> a
id forall a. a -> a
id [] SimpleDoc a
sd String
""
where display :: ShowS -> ShowS -> [(ShowS, ShowS)] -> SimpleDoc a -> ShowS
display ShowS
s ShowS
d [] SimpleDoc a
SEmpty = ShowS
d forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
s
display ShowS
s ShowS
d [(ShowS, ShowS)]
stk (SChar Char
c SimpleDoc a
x) = ShowS -> ShowS -> [(ShowS, ShowS)] -> SimpleDoc a -> ShowS
display (ShowS
s forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
c) ShowS
d [(ShowS, ShowS)]
stk SimpleDoc a
x
display ShowS
s ShowS
d [(ShowS, ShowS)]
stk (SText Int
l String
str SimpleDoc a
x) = ShowS -> ShowS -> [(ShowS, ShowS)] -> SimpleDoc a -> ShowS
display (ShowS
s forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
str) ShowS
d [(ShowS, ShowS)]
stk SimpleDoc a
x
display ShowS
s ShowS
d [(ShowS, ShowS)]
stk (SLine Int
ind SimpleDoc a
x) = ShowS -> ShowS -> [(ShowS, ShowS)] -> SimpleDoc a -> ShowS
display (ShowS
s forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString (Char
'\n'forall a. a -> [a] -> [a]
:Int -> String
indentation Int
ind)) ShowS
d [(ShowS, ShowS)]
stk SimpleDoc a
x
display ShowS
s ShowS
d [(ShowS, ShowS)]
stk (SAnnotStart a
ann SimpleDoc a
x) = ShowS -> ShowS -> [(ShowS, ShowS)] -> SimpleDoc a -> ShowS
display forall a. a -> a
id (a -> ShowS
decor a
ann) ((ShowS
s, ShowS
d)forall a. a -> [a] -> [a]
:[(ShowS, ShowS)]
stk) SimpleDoc a
x
display ShowS
s ShowS
d ((ShowS
sf', ShowS
d'):[(ShowS, ShowS)]
stk) (SAnnotStop SimpleDoc a
x) = let formatted :: String
formatted = ShowS
d (ShowS
s String
"")
in ShowS -> ShowS -> [(ShowS, ShowS)] -> SimpleDoc a -> ShowS
display (ShowS
sf' forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
formatted) ShowS
d' [(ShowS, ShowS)]
stk SimpleDoc a
x
display ShowS
_ ShowS
_ [] (SAnnotStop SimpleDoc a
_) = forall a. HasCallStack => String -> a
error String
"stack underflow"
display ShowS
_ ShowS
_ [(ShowS, ShowS)]
stk SimpleDoc a
SEmpty = forall a. HasCallStack => String -> a
error String
"stack not consumed by rendering"
displayDecoratedA :: (Applicative f, Monoid b)
=> (String -> f b) -> (a -> f b) -> (a -> f b)
-> SimpleDoc a -> f b
displayDecoratedA :: forall (f :: * -> *) b a.
(Applicative f, Monoid b) =>
(String -> f b) -> (a -> f b) -> (a -> f b) -> SimpleDoc a -> f b
displayDecoratedA String -> f b
str a -> f b
start a -> f b
end SimpleDoc a
sd = [a] -> SimpleDoc a -> f b
display [] SimpleDoc a
sd
where display :: [a] -> SimpleDoc a -> f b
display [] SimpleDoc a
SEmpty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty
display [a]
stk (SChar Char
c SimpleDoc a
x) = (String -> f b
str [Char
c]) f b -> f b -> f b
<++> ([a] -> SimpleDoc a -> f b
display [a]
stk SimpleDoc a
x)
display [a]
stk (SText Int
l String
s SimpleDoc a
x) = (String -> f b
str String
s) f b -> f b -> f b
<++> ([a] -> SimpleDoc a -> f b
display [a]
stk SimpleDoc a
x)
display [a]
stk (SLine Int
ind SimpleDoc a
x) = (String -> f b
str (Char
'\n'forall a. a -> [a] -> [a]
:Int -> String
indentation Int
ind)) f b -> f b -> f b
<++> ([a] -> SimpleDoc a -> f b
display [a]
stk SimpleDoc a
x)
display [a]
stk (SAnnotStart a
ann SimpleDoc a
x) = (a -> f b
start a
ann) f b -> f b -> f b
<++> ([a] -> SimpleDoc a -> f b
display (a
annforall a. a -> [a] -> [a]
:[a]
stk) SimpleDoc a
x)
display (a
ann:[a]
stk) (SAnnotStop SimpleDoc a
x) = (a -> f b
end a
ann) f b -> f b -> f b
<++> ([a] -> SimpleDoc a -> f b
display [a]
stk SimpleDoc a
x)
display [] (SAnnotStop SimpleDoc a
_) = forall a. HasCallStack => String -> a
error String
"stack underflow"
display [a]
stk SimpleDoc a
SEmpty = forall a. HasCallStack => String -> a
error String
"stack not consumed by rendering"
<++> :: f b -> f b -> f b
(<++>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Monoid a => a -> a -> a
mappend
instance Show (Doc a) where
showsPrec :: Int -> Doc a -> ShowS
showsPrec Int
d Doc a
doc = forall a. SimpleDoc a -> ShowS
displayS (forall a. Float -> Int -> Doc a -> SimpleDoc a
renderPretty Float
0.4 Int
80 Doc a
doc)
putDoc :: Doc a -> IO ()
putDoc :: forall a. Doc a -> IO ()
putDoc Doc a
doc = forall a. Handle -> Doc a -> IO ()
hPutDoc Handle
stdout Doc a
doc
hPutDoc :: Handle -> Doc a -> IO ()
hPutDoc :: forall a. Handle -> Doc a -> IO ()
hPutDoc Handle
handle Doc a
doc = forall a. Handle -> SimpleDoc a -> IO ()
displayIO Handle
handle (forall a. Float -> Int -> Doc a -> SimpleDoc a
renderPretty Float
0.4 Int
80 Doc a
doc)
spaces :: Int -> String
spaces Int
n | Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = String
""
| Bool
otherwise = forall a. Int -> a -> [a]
replicate Int
n Char
' '
indentation :: Int -> String
indentation Int
n = Int -> String
spaces Int
n