{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
module Pretty (
Doc, TextDetails(..),
char, text, ftext, ptext, ztext, sizedText, zeroWidthText,
int, integer, float, double, rational, hex,
semi, comma, colon, space, equals,
lparen, rparen, lbrack, rbrack, lbrace, rbrace,
parens, brackets, braces, quotes, quote, doubleQuotes,
maybeParens,
empty,
(<>), (<+>), hcat, hsep,
($$), ($+$), vcat,
sep, cat,
fsep, fcat,
nest,
hang, hangNotEmpty, punctuate,
isEmpty,
Style(..),
style,
renderStyle,
Mode(..),
fullRender, txtPrinter,
printDoc, printDoc_,
bufLeftRender
) where
import GhcPrelude hiding (error)
import BufWrite
import FastString
import PlainPanic
import System.IO
import Numeric (showHex)
import GHC.Base ( unpackCString#, unpackNBytes#, Int(..) )
import GHC.Ptr ( Ptr(..) )
infixl 6 <>
infixl 6 <+>
infixl 5 $$, $+$
data Doc
= Empty
| NilAbove Doc
| TextBeside !TextDetails {-# UNPACK #-} !Int Doc
| Nest {-# UNPACK #-} !Int Doc
| Union Doc Doc
| NoDoc
| Beside Doc Bool Doc
| Above Doc Bool Doc
type RDoc = Doc
data TextDetails = Chr {-# UNPACK #-} !Char
| Str String
| PStr FastString
| ZStr FastZString
| LStr {-# UNPACK #-} !PtrString
| RStr {-# UNPACK #-} !Int {-# UNPACK #-} !Char
instance Show Doc where
showsPrec :: Int -> Doc -> ShowS
showsPrec _ doc :: Doc
doc cont :: String
cont = Mode
-> Int
-> Float
-> (TextDetails -> ShowS)
-> String
-> Doc
-> String
forall a.
Mode -> Int -> Float -> (TextDetails -> a -> a) -> a -> Doc -> a
fullRender (Style -> Mode
mode Style
style) (Style -> Int
lineLength Style
style)
(Style -> Float
ribbonsPerLine Style
style)
TextDetails -> ShowS
txtPrinter String
cont Doc
doc
char :: Char -> Doc
char :: Char -> Doc
char c :: Char
c = TextDetails -> Int -> Doc -> Doc
textBeside_ (Char -> TextDetails
Chr Char
c) 1 Doc
Empty
text :: String -> Doc
text :: String -> Doc
text s :: String
s = TextDetails -> Int -> Doc -> Doc
textBeside_ (String -> TextDetails
Str String
s) (String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s) Doc
Empty
{-# NOINLINE [0] text #-}
{-# RULES "text/str"
forall a. text (unpackCString# a) = ptext (mkPtrString# a)
#-}
{-# RULES "text/unpackNBytes#"
forall p n. text (unpackNBytes# p n) = ptext (PtrString (Ptr p) (I# n))
#-}
ftext :: FastString -> Doc
ftext :: FastString -> Doc
ftext s :: FastString
s = TextDetails -> Int -> Doc -> Doc
textBeside_ (FastString -> TextDetails
PStr FastString
s) (FastString -> Int
lengthFS FastString
s) Doc
Empty
ptext :: PtrString -> Doc
ptext :: PtrString -> Doc
ptext s :: PtrString
s = TextDetails -> Int -> Doc -> Doc
textBeside_ (PtrString -> TextDetails
LStr PtrString
s) (PtrString -> Int
lengthPS PtrString
s) Doc
Empty
ztext :: FastZString -> Doc
ztext :: FastZString -> Doc
ztext s :: FastZString
s = TextDetails -> Int -> Doc -> Doc
textBeside_ (FastZString -> TextDetails
ZStr FastZString
s) (FastZString -> Int
lengthFZS FastZString
s) Doc
Empty
sizedText :: Int -> String -> Doc
sizedText :: Int -> String -> Doc
sizedText l :: Int
l s :: String
s = TextDetails -> Int -> Doc -> Doc
textBeside_ (String -> TextDetails
Str String
s) Int
l Doc
Empty
zeroWidthText :: String -> Doc
zeroWidthText :: String -> Doc
zeroWidthText = Int -> String -> Doc
sizedText 0
empty :: Doc
empty :: Doc
empty = Doc
Empty
isEmpty :: Doc -> Bool
isEmpty :: Doc -> Bool
isEmpty Empty = Bool
True
isEmpty _ = Bool
False
semi :: Doc
comma :: Doc
colon :: Doc
space :: Doc
equals :: Doc
lparen :: Doc
rparen :: Doc
lbrack :: Doc
rbrack :: Doc
lbrace :: Doc
rbrace :: Doc
semi :: Doc
semi = Char -> Doc
char ';'
comma :: Doc
comma = Char -> Doc
char ','
colon :: Doc
colon = Char -> Doc
char ':'
space :: Doc
space = Char -> Doc
char ' '
equals :: Doc
equals = Char -> Doc
char '='
lparen :: Doc
lparen = Char -> Doc
char '('
rparen :: Doc
rparen = Char -> Doc
char ')'
lbrack :: Doc
lbrack = Char -> Doc
char '['
rbrack :: Doc
rbrack = Char -> Doc
char ']'
lbrace :: Doc
lbrace = Char -> Doc
char '{'
rbrace :: Doc
rbrace = Char -> Doc
char '}'
spaceText, nlText :: TextDetails
spaceText :: TextDetails
spaceText = Char -> TextDetails
Chr ' '
nlText :: TextDetails
nlText = Char -> TextDetails
Chr '\n'
int :: Int -> Doc
integer :: Integer -> Doc
float :: Float -> Doc
double :: Double -> Doc
rational :: Rational -> Doc
hex :: Integer -> Doc
int :: Int -> Doc
int n :: Int
n = String -> Doc
text (Int -> String
forall a. Show a => a -> String
show Int
n)
integer :: Integer -> Doc
integer n :: Integer
n = String -> Doc
text (Integer -> String
forall a. Show a => a -> String
show Integer
n)
float :: Float -> Doc
float n :: Float
n = String -> Doc
text (Float -> String
forall a. Show a => a -> String
show Float
n)
double :: Double -> Doc
double n :: Double
n = String -> Doc
text (Double -> String
forall a. Show a => a -> String
show Double
n)
rational :: Rational -> Doc
rational n :: Rational
n = String -> Doc
text (Rational -> String
forall a. Show a => a -> String
show Rational
n)
hex :: Integer -> Doc
hex n :: Integer
n = String -> Doc
text ('0' Char -> ShowS
forall a. a -> [a] -> [a]
: 'x' Char -> ShowS
forall a. a -> [a] -> [a]
: String
padded)
where
str :: String
str = Integer -> ShowS
forall a. (Integral a, Show a) => a -> ShowS
showHex Integer
n ""
strLen :: Int
strLen = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max 1 (String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
str)
len :: Int
len = 2 Int -> Int -> Int
forall a b. (Num a, Integral b) => a -> b -> a
^ (Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Double -> Double -> Double
forall a. Floating a => a -> a -> a
logBase 2 (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
strLen :: Double)) :: Int)
padded :: String
padded = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
strLen) '0' String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
str
parens :: Doc -> Doc
brackets :: Doc -> Doc
braces :: Doc -> Doc
quotes :: Doc -> Doc
quote :: Doc -> Doc
doubleQuotes :: Doc -> Doc
quotes :: Doc -> Doc
quotes p :: Doc
p = Char -> Doc
char '`' Doc -> Doc -> Doc
<> Doc
p Doc -> Doc -> Doc
<> Char -> Doc
char '\''
quote :: Doc -> Doc
quote p :: Doc
p = Char -> Doc
char '\'' Doc -> Doc -> Doc
<> Doc
p
doubleQuotes :: Doc -> Doc
doubleQuotes p :: Doc
p = Char -> Doc
char '"' Doc -> Doc -> Doc
<> Doc
p Doc -> Doc -> Doc
<> Char -> Doc
char '"'
parens :: Doc -> Doc
parens p :: Doc
p = Char -> Doc
char '(' Doc -> Doc -> Doc
<> Doc
p Doc -> Doc -> Doc
<> Char -> Doc
char ')'
brackets :: Doc -> Doc
brackets p :: Doc
p = Char -> Doc
char '[' Doc -> Doc -> Doc
<> Doc
p Doc -> Doc -> Doc
<> Char -> Doc
char ']'
braces :: Doc -> Doc
braces p :: Doc
p = Char -> Doc
char '{' Doc -> Doc -> Doc
<> Doc
p Doc -> Doc -> Doc
<> Char -> Doc
char '}'
maybeParens :: Bool -> Doc -> Doc
maybeParens :: Bool -> Doc -> Doc
maybeParens False = Doc -> Doc
forall a. a -> a
id
maybeParens True = Doc -> Doc
parens
reduceDoc :: Doc -> RDoc
reduceDoc :: Doc -> Doc
reduceDoc (Beside p :: Doc
p g :: Bool
g q :: Doc
q) = Doc
p Doc -> Doc -> Doc
forall a b. a -> b -> b
`seq` Bool
g Bool -> Doc -> Doc
forall a b. a -> b -> b
`seq` (Doc -> Bool -> Doc -> Doc
beside Doc
p Bool
g (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$! Doc -> Doc
reduceDoc Doc
q)
reduceDoc (Above p :: Doc
p g :: Bool
g q :: Doc
q) = Doc
p Doc -> Doc -> Doc
forall a b. a -> b -> b
`seq` Bool
g Bool -> Doc -> Doc
forall a b. a -> b -> b
`seq` (Doc -> Bool -> Doc -> Doc
above Doc
p Bool
g (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$! Doc -> Doc
reduceDoc Doc
q)
reduceDoc p :: Doc
p = Doc
p
hcat :: [Doc] -> Doc
hcat :: [Doc] -> Doc
hcat = Doc -> Doc
reduceAB (Doc -> Doc) -> ([Doc] -> Doc) -> [Doc] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Doc -> Doc -> Doc) -> Doc -> [Doc] -> Doc
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Bool -> Doc -> Doc -> Doc
beside_' Bool
False) Doc
empty
hsep :: [Doc] -> Doc
hsep :: [Doc] -> Doc
hsep = Doc -> Doc
reduceAB (Doc -> Doc) -> ([Doc] -> Doc) -> [Doc] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Doc -> Doc -> Doc) -> Doc -> [Doc] -> Doc
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Bool -> Doc -> Doc -> Doc
beside_' Bool
True) Doc
empty
vcat :: [Doc] -> Doc
vcat :: [Doc] -> Doc
vcat = Doc -> Doc
reduceAB (Doc -> Doc) -> ([Doc] -> Doc) -> [Doc] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Doc -> Doc -> Doc) -> Doc -> [Doc] -> Doc
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Bool -> Doc -> Doc -> Doc
above_' Bool
False) Doc
empty
nest :: Int -> Doc -> Doc
nest :: Int -> Doc -> Doc
nest k :: Int
k p :: Doc
p = Int -> Doc -> Doc
mkNest Int
k (Doc -> Doc
reduceDoc Doc
p)
hang :: Doc -> Int -> Doc -> Doc
hang :: Doc -> Int -> Doc -> Doc
hang d1 :: Doc
d1 n :: Int
n d2 :: Doc
d2 = [Doc] -> Doc
sep [Doc
d1, Int -> Doc -> Doc
nest Int
n Doc
d2]
hangNotEmpty :: Doc -> Int -> Doc -> Doc
hangNotEmpty :: Doc -> Int -> Doc -> Doc
hangNotEmpty d1 :: Doc
d1 n :: Int
n d2 :: Doc
d2 = if Doc -> Bool
isEmpty Doc
d1
then Doc
d2
else Doc -> Int -> Doc -> Doc
hang Doc
d1 Int
n Doc
d2
punctuate :: Doc -> [Doc] -> [Doc]
punctuate :: Doc -> [Doc] -> [Doc]
punctuate _ [] = []
punctuate p :: Doc
p (x :: Doc
x:xs :: [Doc]
xs) = Doc -> [Doc] -> [Doc]
go Doc
x [Doc]
xs
where go :: Doc -> [Doc] -> [Doc]
go y :: Doc
y [] = [Doc
y]
go y :: Doc
y (z :: Doc
z:zs :: [Doc]
zs) = (Doc
y Doc -> Doc -> Doc
<> Doc
p) Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
: Doc -> [Doc] -> [Doc]
go Doc
z [Doc]
zs
mkNest :: Int -> Doc -> Doc
mkNest :: Int -> Doc -> Doc
mkNest k :: Int
k _ | Int
k Int -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = Doc
forall a. HasCallStack => a
undefined
mkNest k :: Int
k (Nest k1 :: Int
k1 p :: Doc
p) = Int -> Doc -> Doc
mkNest (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
k1) Doc
p
mkNest _ NoDoc = Doc
NoDoc
mkNest _ Empty = Doc
Empty
mkNest 0 p :: Doc
p = Doc
p
mkNest k :: Int
k p :: Doc
p = Int -> Doc -> Doc
nest_ Int
k Doc
p
mkUnion :: Doc -> Doc -> Doc
mkUnion :: Doc -> Doc -> Doc
mkUnion Empty _ = Doc
Empty
mkUnion p :: Doc
p q :: Doc
q = Doc
p Doc -> Doc -> Doc
`union_` Doc
q
beside_' :: Bool -> Doc -> Doc -> Doc
beside_' :: Bool -> Doc -> Doc -> Doc
beside_' _ p :: Doc
p Empty = Doc
p
beside_' g :: Bool
g p :: Doc
p q :: Doc
q = Doc -> Bool -> Doc -> Doc
Beside Doc
p Bool
g Doc
q
above_' :: Bool -> Doc -> Doc -> Doc
above_' :: Bool -> Doc -> Doc -> Doc
above_' _ p :: Doc
p Empty = Doc
p
above_' g :: Bool
g p :: Doc
p q :: Doc
q = Doc -> Bool -> Doc -> Doc
Above Doc
p Bool
g Doc
q
reduceAB :: Doc -> Doc
reduceAB :: Doc -> Doc
reduceAB (Above Empty _ q :: Doc
q) = Doc
q
reduceAB (Beside Empty _ q :: Doc
q) = Doc
q
reduceAB doc :: Doc
doc = Doc
doc
nilAbove_ :: RDoc -> RDoc
nilAbove_ :: Doc -> Doc
nilAbove_ = Doc -> Doc
NilAbove
textBeside_ :: TextDetails -> Int -> RDoc -> RDoc
textBeside_ :: TextDetails -> Int -> Doc -> Doc
textBeside_ = TextDetails -> Int -> Doc -> Doc
TextBeside
nest_ :: Int -> RDoc -> RDoc
nest_ :: Int -> Doc -> Doc
nest_ = Int -> Doc -> Doc
Nest
union_ :: RDoc -> RDoc -> RDoc
union_ :: Doc -> Doc -> Doc
union_ = Doc -> Doc -> Doc
Union
($$) :: Doc -> Doc -> Doc
p :: Doc
p $$ :: Doc -> Doc -> Doc
$$ q :: Doc
q = Doc -> Bool -> Doc -> Doc
above_ Doc
p Bool
False Doc
q
($+$) :: Doc -> Doc -> Doc
p :: Doc
p $+$ :: Doc -> Doc -> Doc
$+$ q :: Doc
q = Doc -> Bool -> Doc -> Doc
above_ Doc
p Bool
True Doc
q
above_ :: Doc -> Bool -> Doc -> Doc
above_ :: Doc -> Bool -> Doc -> Doc
above_ p :: Doc
p _ Empty = Doc
p
above_ Empty _ q :: Doc
q = Doc
q
above_ p :: Doc
p g :: Bool
g q :: Doc
q = Doc -> Bool -> Doc -> Doc
Above Doc
p Bool
g Doc
q
above :: Doc -> Bool -> RDoc -> RDoc
above :: Doc -> Bool -> Doc -> Doc
above (Above p :: Doc
p g1 :: Bool
g1 q1 :: Doc
q1) g2 :: Bool
g2 q2 :: Doc
q2 = Doc -> Bool -> Doc -> Doc
above Doc
p Bool
g1 (Doc -> Bool -> Doc -> Doc
above Doc
q1 Bool
g2 Doc
q2)
above p :: Doc
p@(Beside{}) g :: Bool
g q :: Doc
q = Doc -> Bool -> Int -> Doc -> Doc
aboveNest (Doc -> Doc
reduceDoc Doc
p) Bool
g 0 (Doc -> Doc
reduceDoc Doc
q)
above p :: Doc
p g :: Bool
g q :: Doc
q = Doc -> Bool -> Int -> Doc -> Doc
aboveNest Doc
p Bool
g 0 (Doc -> Doc
reduceDoc Doc
q)
aboveNest :: RDoc -> Bool -> Int -> RDoc -> RDoc
aboveNest :: Doc -> Bool -> Int -> Doc -> Doc
aboveNest _ _ k :: Int
k _ | Int
k Int -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = Doc
forall a. HasCallStack => a
undefined
aboveNest NoDoc _ _ _ = Doc
NoDoc
aboveNest (p1 :: Doc
p1 `Union` p2 :: Doc
p2) g :: Bool
g k :: Int
k q :: Doc
q = Doc -> Bool -> Int -> Doc -> Doc
aboveNest Doc
p1 Bool
g Int
k Doc
q Doc -> Doc -> Doc
`union_`
Doc -> Bool -> Int -> Doc -> Doc
aboveNest Doc
p2 Bool
g Int
k Doc
q
aboveNest Empty _ k :: Int
k q :: Doc
q = Int -> Doc -> Doc
mkNest Int
k Doc
q
aboveNest (Nest k1 :: Int
k1 p :: Doc
p) g :: Bool
g k :: Int
k q :: Doc
q = Int -> Doc -> Doc
nest_ Int
k1 (Doc -> Bool -> Int -> Doc -> Doc
aboveNest Doc
p Bool
g (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
k1) Doc
q)
aboveNest (NilAbove p :: Doc
p) g :: Bool
g k :: Int
k q :: Doc
q = Doc -> Doc
nilAbove_ (Doc -> Bool -> Int -> Doc -> Doc
aboveNest Doc
p Bool
g Int
k Doc
q)
aboveNest (TextBeside s :: TextDetails
s sl :: Int
sl p :: Doc
p) g :: Bool
g k :: Int
k q :: Doc
q = TextDetails -> Int -> Doc -> Doc
textBeside_ TextDetails
s Int
sl Doc
rest
where
!k1 :: Int
k1 = Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sl
rest :: Doc
rest = case Doc
p of
Empty -> Bool -> Int -> Doc -> Doc
nilAboveNest Bool
g Int
k1 Doc
q
_ -> Doc -> Bool -> Int -> Doc -> Doc
aboveNest Doc
p Bool
g Int
k1 Doc
q
aboveNest (Above {}) _ _ _ = String -> Doc
forall a. String -> a
error "aboveNest Above"
aboveNest (Beside {}) _ _ _ = String -> Doc
forall a. String -> a
error "aboveNest Beside"
nilAboveNest :: Bool -> Int -> RDoc -> RDoc
nilAboveNest :: Bool -> Int -> Doc -> Doc
nilAboveNest _ k :: Int
k _ | Int
k Int -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = Doc
forall a. HasCallStack => a
undefined
nilAboveNest _ _ Empty = Doc
Empty
nilAboveNest g :: Bool
g k :: Int
k (Nest k1 :: Int
k1 q :: Doc
q) = Bool -> Int -> Doc -> Doc
nilAboveNest Bool
g (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
k1) Doc
q
nilAboveNest g :: Bool
g k :: Int
k q :: Doc
q | Bool -> Bool
not Bool
g Bool -> Bool -> Bool
&& Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0
= TextDetails -> Int -> Doc -> Doc
textBeside_ (Int -> Char -> TextDetails
RStr Int
k ' ') Int
k Doc
q
| Bool
otherwise
= Doc -> Doc
nilAbove_ (Int -> Doc -> Doc
mkNest Int
k Doc
q)
(<>) :: Doc -> Doc -> Doc
p :: Doc
p <> :: Doc -> Doc -> Doc
<> q :: Doc
q = Doc -> Bool -> Doc -> Doc
beside_ Doc
p Bool
False Doc
q
(<+>) :: Doc -> Doc -> Doc
p :: Doc
p <+> :: Doc -> Doc -> Doc
<+> q :: Doc
q = Doc -> Bool -> Doc -> Doc
beside_ Doc
p Bool
True Doc
q
beside_ :: Doc -> Bool -> Doc -> Doc
beside_ :: Doc -> Bool -> Doc -> Doc
beside_ p :: Doc
p _ Empty = Doc
p
beside_ Empty _ q :: Doc
q = Doc
q
beside_ p :: Doc
p g :: Bool
g q :: Doc
q = Doc -> Bool -> Doc -> Doc
Beside Doc
p Bool
g Doc
q
beside :: Doc -> Bool -> RDoc -> RDoc
beside :: Doc -> Bool -> Doc -> Doc
beside NoDoc _ _ = Doc
NoDoc
beside (p1 :: Doc
p1 `Union` p2 :: Doc
p2) g :: Bool
g q :: Doc
q = Doc -> Bool -> Doc -> Doc
beside Doc
p1 Bool
g Doc
q Doc -> Doc -> Doc
`union_` Doc -> Bool -> Doc -> Doc
beside Doc
p2 Bool
g Doc
q
beside Empty _ q :: Doc
q = Doc
q
beside (Nest k :: Int
k p :: Doc
p) g :: Bool
g q :: Doc
q = Int -> Doc -> Doc
nest_ Int
k (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$! Doc -> Bool -> Doc -> Doc
beside Doc
p Bool
g Doc
q
beside p :: Doc
p@(Beside p1 :: Doc
p1 g1 :: Bool
g1 q1 :: Doc
q1) g2 :: Bool
g2 q2 :: Doc
q2
| Bool
g1 Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool
g2 = Doc -> Bool -> Doc -> Doc
beside Doc
p1 Bool
g1 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$! Doc -> Bool -> Doc -> Doc
beside Doc
q1 Bool
g2 Doc
q2
| Bool
otherwise = Doc -> Bool -> Doc -> Doc
beside (Doc -> Doc
reduceDoc Doc
p) Bool
g2 Doc
q2
beside p :: Doc
p@(Above{}) g :: Bool
g q :: Doc
q = let !d :: Doc
d = Doc -> Doc
reduceDoc Doc
p in Doc -> Bool -> Doc -> Doc
beside Doc
d Bool
g Doc
q
beside (NilAbove p :: Doc
p) g :: Bool
g q :: Doc
q = Doc -> Doc
nilAbove_ (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$! Doc -> Bool -> Doc -> Doc
beside Doc
p Bool
g Doc
q
beside (TextBeside s :: TextDetails
s sl :: Int
sl p :: Doc
p) g :: Bool
g q :: Doc
q = TextDetails -> Int -> Doc -> Doc
textBeside_ TextDetails
s Int
sl Doc
rest
where
rest :: Doc
rest = case Doc
p of
Empty -> Bool -> Doc -> Doc
nilBeside Bool
g Doc
q
_ -> Doc -> Bool -> Doc -> Doc
beside Doc
p Bool
g Doc
q
nilBeside :: Bool -> RDoc -> RDoc
nilBeside :: Bool -> Doc -> Doc
nilBeside _ Empty = Doc
Empty
nilBeside g :: Bool
g (Nest _ p :: Doc
p) = Bool -> Doc -> Doc
nilBeside Bool
g Doc
p
nilBeside g :: Bool
g p :: Doc
p | Bool
g = TextDetails -> Int -> Doc -> Doc
textBeside_ TextDetails
spaceText 1 Doc
p
| Bool
otherwise = Doc
p
sep :: [Doc] -> Doc
sep :: [Doc] -> Doc
sep = Bool -> [Doc] -> Doc
sepX Bool
True
cat :: [Doc] -> Doc
cat :: [Doc] -> Doc
cat = Bool -> [Doc] -> Doc
sepX Bool
False
sepX :: Bool -> [Doc] -> Doc
sepX :: Bool -> [Doc] -> Doc
sepX _ [] = Doc
empty
sepX x :: Bool
x (p :: Doc
p:ps :: [Doc]
ps) = Bool -> Doc -> Int -> [Doc] -> Doc
sep1 Bool
x (Doc -> Doc
reduceDoc Doc
p) 0 [Doc]
ps
sep1 :: Bool -> RDoc -> Int -> [Doc] -> RDoc
sep1 :: Bool -> Doc -> Int -> [Doc] -> Doc
sep1 _ _ k :: Int
k _ | Int
k Int -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = Doc
forall a. HasCallStack => a
undefined
sep1 _ NoDoc _ _ = Doc
NoDoc
sep1 g :: Bool
g (p :: Doc
p `Union` q :: Doc
q) k :: Int
k ys :: [Doc]
ys = Bool -> Doc -> Int -> [Doc] -> Doc
sep1 Bool
g Doc
p Int
k [Doc]
ys Doc -> Doc -> Doc
`union_`
Doc -> Bool -> Int -> Doc -> Doc
aboveNest Doc
q Bool
False Int
k (Doc -> Doc
reduceDoc ([Doc] -> Doc
vcat [Doc]
ys))
sep1 g :: Bool
g Empty k :: Int
k ys :: [Doc]
ys = Int -> Doc -> Doc
mkNest Int
k (Bool -> [Doc] -> Doc
sepX Bool
g [Doc]
ys)
sep1 g :: Bool
g (Nest n :: Int
n p :: Doc
p) k :: Int
k ys :: [Doc]
ys = Int -> Doc -> Doc
nest_ Int
n (Bool -> Doc -> Int -> [Doc] -> Doc
sep1 Bool
g Doc
p (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) [Doc]
ys)
sep1 _ (NilAbove p :: Doc
p) k :: Int
k ys :: [Doc]
ys = Doc -> Doc
nilAbove_
(Doc -> Bool -> Int -> Doc -> Doc
aboveNest Doc
p Bool
False Int
k (Doc -> Doc
reduceDoc ([Doc] -> Doc
vcat [Doc]
ys)))
sep1 g :: Bool
g (TextBeside s :: TextDetails
s sl :: Int
sl p :: Doc
p) k :: Int
k ys :: [Doc]
ys = TextDetails -> Int -> Doc -> Doc
textBeside_ TextDetails
s Int
sl (Bool -> Doc -> Int -> [Doc] -> Doc
sepNB Bool
g Doc
p (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sl) [Doc]
ys)
sep1 _ (Above {}) _ _ = String -> Doc
forall a. String -> a
error "sep1 Above"
sep1 _ (Beside {}) _ _ = String -> Doc
forall a. String -> a
error "sep1 Beside"
sepNB :: Bool -> Doc -> Int -> [Doc] -> Doc
sepNB :: Bool -> Doc -> Int -> [Doc] -> Doc
sepNB g :: Bool
g (Nest _ p :: Doc
p) k :: Int
k ys :: [Doc]
ys
= Bool -> Doc -> Int -> [Doc] -> Doc
sepNB Bool
g Doc
p Int
k [Doc]
ys
sepNB g :: Bool
g Empty k :: Int
k ys :: [Doc]
ys
= Doc -> Doc
oneLiner (Bool -> Doc -> Doc
nilBeside Bool
g (Doc -> Doc
reduceDoc Doc
rest)) Doc -> Doc -> Doc
`mkUnion`
Bool -> Int -> Doc -> Doc
nilAboveNest Bool
False Int
k (Doc -> Doc
reduceDoc ([Doc] -> Doc
vcat [Doc]
ys))
where
rest :: Doc
rest | Bool
g = [Doc] -> Doc
hsep [Doc]
ys
| Bool
otherwise = [Doc] -> Doc
hcat [Doc]
ys
sepNB g :: Bool
g p :: Doc
p k :: Int
k ys :: [Doc]
ys
= Bool -> Doc -> Int -> [Doc] -> Doc
sep1 Bool
g Doc
p Int
k [Doc]
ys
fcat :: [Doc] -> Doc
fcat :: [Doc] -> Doc
fcat = Bool -> [Doc] -> Doc
fill Bool
False
fsep :: [Doc] -> Doc
fsep :: [Doc] -> Doc
fsep = Bool -> [Doc] -> Doc
fill Bool
True
fill :: Bool -> [Doc] -> RDoc
fill :: Bool -> [Doc] -> Doc
fill _ [] = Doc
empty
fill g :: Bool
g (p :: Doc
p:ps :: [Doc]
ps) = Bool -> Doc -> Int -> [Doc] -> Doc
fill1 Bool
g (Doc -> Doc
reduceDoc Doc
p) 0 [Doc]
ps
fill1 :: Bool -> RDoc -> Int -> [Doc] -> Doc
fill1 :: Bool -> Doc -> Int -> [Doc] -> Doc
fill1 _ _ k :: Int
k _ | Int
k Int -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = Doc
forall a. HasCallStack => a
undefined
fill1 _ NoDoc _ _ = Doc
NoDoc
fill1 g :: Bool
g (p :: Doc
p `Union` q :: Doc
q) k :: Int
k ys :: [Doc]
ys = Bool -> Doc -> Int -> [Doc] -> Doc
fill1 Bool
g Doc
p Int
k [Doc]
ys Doc -> Doc -> Doc
`union_`
Doc -> Bool -> Int -> Doc -> Doc
aboveNest Doc
q Bool
False Int
k (Bool -> [Doc] -> Doc
fill Bool
g [Doc]
ys)
fill1 g :: Bool
g Empty k :: Int
k ys :: [Doc]
ys = Int -> Doc -> Doc
mkNest Int
k (Bool -> [Doc] -> Doc
fill Bool
g [Doc]
ys)
fill1 g :: Bool
g (Nest n :: Int
n p :: Doc
p) k :: Int
k ys :: [Doc]
ys = Int -> Doc -> Doc
nest_ Int
n (Bool -> Doc -> Int -> [Doc] -> Doc
fill1 Bool
g Doc
p (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) [Doc]
ys)
fill1 g :: Bool
g (NilAbove p :: Doc
p) k :: Int
k ys :: [Doc]
ys = Doc -> Doc
nilAbove_ (Doc -> Bool -> Int -> Doc -> Doc
aboveNest Doc
p Bool
False Int
k (Bool -> [Doc] -> Doc
fill Bool
g [Doc]
ys))
fill1 g :: Bool
g (TextBeside s :: TextDetails
s sl :: Int
sl p :: Doc
p) k :: Int
k ys :: [Doc]
ys = TextDetails -> Int -> Doc -> Doc
textBeside_ TextDetails
s Int
sl (Bool -> Doc -> Int -> [Doc] -> Doc
fillNB Bool
g Doc
p (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sl) [Doc]
ys)
fill1 _ (Above {}) _ _ = String -> Doc
forall a. String -> a
error "fill1 Above"
fill1 _ (Beside {}) _ _ = String -> Doc
forall a. String -> a
error "fill1 Beside"
fillNB :: Bool -> Doc -> Int -> [Doc] -> Doc
fillNB :: Bool -> Doc -> Int -> [Doc] -> Doc
fillNB _ _ k :: Int
k _ | Int
k Int -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = Doc
forall a. HasCallStack => a
undefined
fillNB g :: Bool
g (Nest _ p :: Doc
p) k :: Int
k ys :: [Doc]
ys = Bool -> Doc -> Int -> [Doc] -> Doc
fillNB Bool
g Doc
p Int
k [Doc]
ys
fillNB _ Empty _ [] = Doc
Empty
fillNB g :: Bool
g Empty k :: Int
k (Empty:ys :: [Doc]
ys) = Bool -> Doc -> Int -> [Doc] -> Doc
fillNB Bool
g Doc
Empty Int
k [Doc]
ys
fillNB g :: Bool
g Empty k :: Int
k (y :: Doc
y:ys :: [Doc]
ys) = Bool -> Int -> Doc -> [Doc] -> Doc
fillNBE Bool
g Int
k Doc
y [Doc]
ys
fillNB g :: Bool
g p :: Doc
p k :: Int
k ys :: [Doc]
ys = Bool -> Doc -> Int -> [Doc] -> Doc
fill1 Bool
g Doc
p Int
k [Doc]
ys
fillNBE :: Bool -> Int -> Doc -> [Doc] -> Doc
fillNBE :: Bool -> Int -> Doc -> [Doc] -> Doc
fillNBE g :: Bool
g k :: Int
k y :: Doc
y ys :: [Doc]
ys
= Bool -> Doc -> Doc
nilBeside Bool
g (Bool -> Doc -> Int -> [Doc] -> Doc
fill1 Bool
g ((Doc -> Doc
elideNest (Doc -> Doc) -> (Doc -> Doc) -> Doc -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Doc
oneLiner (Doc -> Doc) -> (Doc -> Doc) -> Doc -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Doc
reduceDoc) Doc
y) Int
k' [Doc]
ys)
Doc -> Doc -> Doc
`mkUnion` Bool -> Int -> Doc -> Doc
nilAboveNest Bool
False Int
k (Bool -> [Doc] -> Doc
fill Bool
g (Doc
yDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ys))
where k' :: Int
k' = if Bool
g then Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1 else Int
k
elideNest :: Doc -> Doc
elideNest :: Doc -> Doc
elideNest (Nest _ d :: Doc
d) = Doc
d
elideNest d :: Doc
d = Doc
d
best :: Int
-> Int
-> RDoc
-> RDoc
best :: Int -> Int -> Doc -> Doc
best w0 :: Int
w0 r :: Int
r = Int -> Doc -> Doc
get Int
w0
where
get :: Int
-> Doc -> Doc
get :: Int -> Doc -> Doc
get w :: Int
w _ | Int
w Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0 Bool -> Bool -> Bool
&& Bool
False = Doc
forall a. HasCallStack => a
undefined
get _ Empty = Doc
Empty
get _ NoDoc = Doc
NoDoc
get w :: Int
w (NilAbove p :: Doc
p) = Doc -> Doc
nilAbove_ (Int -> Doc -> Doc
get Int
w Doc
p)
get w :: Int
w (TextBeside s :: TextDetails
s sl :: Int
sl p :: Doc
p) = TextDetails -> Int -> Doc -> Doc
textBeside_ TextDetails
s Int
sl (Int -> Int -> Doc -> Doc
get1 Int
w Int
sl Doc
p)
get w :: Int
w (Nest k :: Int
k p :: Doc
p) = Int -> Doc -> Doc
nest_ Int
k (Int -> Doc -> Doc
get (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
k) Doc
p)
get w :: Int
w (p :: Doc
p `Union` q :: Doc
q) = Int -> Int -> Doc -> Doc -> Doc
nicest Int
w Int
r (Int -> Doc -> Doc
get Int
w Doc
p) (Int -> Doc -> Doc
get Int
w Doc
q)
get _ (Above {}) = String -> Doc
forall a. String -> a
error "best get Above"
get _ (Beside {}) = String -> Doc
forall a. String -> a
error "best get Beside"
get1 :: Int
-> Int
-> Doc
-> Doc
get1 :: Int -> Int -> Doc -> Doc
get1 w :: Int
w _ _ | Int
w Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0 Bool -> Bool -> Bool
&& Bool
False = Doc
forall a. HasCallStack => a
undefined
get1 _ _ Empty = Doc
Empty
get1 _ _ NoDoc = Doc
NoDoc
get1 w :: Int
w sl :: Int
sl (NilAbove p :: Doc
p) = Doc -> Doc
nilAbove_ (Int -> Doc -> Doc
get (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sl) Doc
p)
get1 w :: Int
w sl :: Int
sl (TextBeside t :: TextDetails
t tl :: Int
tl p :: Doc
p) = TextDetails -> Int -> Doc -> Doc
textBeside_ TextDetails
t Int
tl (Int -> Int -> Doc -> Doc
get1 Int
w (Int
sl Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
tl) Doc
p)
get1 w :: Int
w sl :: Int
sl (Nest _ p :: Doc
p) = Int -> Int -> Doc -> Doc
get1 Int
w Int
sl Doc
p
get1 w :: Int
w sl :: Int
sl (p :: Doc
p `Union` q :: Doc
q) = Int -> Int -> Int -> Doc -> Doc -> Doc
nicest1 Int
w Int
r Int
sl (Int -> Int -> Doc -> Doc
get1 Int
w Int
sl Doc
p)
(Int -> Int -> Doc -> Doc
get1 Int
w Int
sl Doc
q)
get1 _ _ (Above {}) = String -> Doc
forall a. String -> a
error "best get1 Above"
get1 _ _ (Beside {}) = String -> Doc
forall a. String -> a
error "best get1 Beside"
nicest :: Int -> Int -> Doc -> Doc -> Doc
nicest :: Int -> Int -> Doc -> Doc -> Doc
nicest !Int
w !Int
r = Int -> Int -> Int -> Doc -> Doc -> Doc
nicest1 Int
w Int
r 0
nicest1 :: Int -> Int -> Int -> Doc -> Doc -> Doc
nicest1 :: Int -> Int -> Int -> Doc -> Doc -> Doc
nicest1 !Int
w !Int
r !Int
sl p :: Doc
p q :: Doc
q | Int -> Doc -> Bool
fits ((Int
w Int -> Int -> Int
forall a. Ord a => a -> a -> a
`min` Int
r) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sl) Doc
p = Doc
p
| Bool
otherwise = Doc
q
fits :: Int
-> Doc
-> Bool
fits :: Int -> Doc -> Bool
fits n :: Int
n _ | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0 = Bool
False
fits _ NoDoc = Bool
False
fits _ Empty = Bool
True
fits _ (NilAbove _) = Bool
True
fits n :: Int
n (TextBeside _ sl :: Int
sl p :: Doc
p) = Int -> Doc -> Bool
fits (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sl) Doc
p
fits _ (Above {}) = String -> Bool
forall a. String -> a
error "fits Above"
fits _ (Beside {}) = String -> Bool
forall a. String -> a
error "fits Beside"
fits _ (Union {}) = String -> Bool
forall a. String -> a
error "fits Union"
fits _ (Nest {}) = String -> Bool
forall a. String -> a
error "fits Nest"
first :: Doc -> Doc -> Doc
first :: Doc -> Doc -> Doc
first p :: Doc
p q :: Doc
q | Doc -> Bool
nonEmptySet Doc
p = Doc
p
| Bool
otherwise = Doc
q
nonEmptySet :: Doc -> Bool
nonEmptySet :: Doc -> Bool
nonEmptySet NoDoc = Bool
False
nonEmptySet (_ `Union` _) = Bool
True
nonEmptySet Empty = Bool
True
nonEmptySet (NilAbove _) = Bool
True
nonEmptySet (TextBeside _ _ p :: Doc
p) = Doc -> Bool
nonEmptySet Doc
p
nonEmptySet (Nest _ p :: Doc
p) = Doc -> Bool
nonEmptySet Doc
p
nonEmptySet (Above {}) = String -> Bool
forall a. String -> a
error "nonEmptySet Above"
nonEmptySet (Beside {}) = String -> Bool
forall a. String -> a
error "nonEmptySet Beside"
oneLiner :: Doc -> Doc
oneLiner :: Doc -> Doc
oneLiner NoDoc = Doc
NoDoc
oneLiner Empty = Doc
Empty
oneLiner (NilAbove _) = Doc
NoDoc
oneLiner (TextBeside s :: TextDetails
s sl :: Int
sl p :: Doc
p) = TextDetails -> Int -> Doc -> Doc
textBeside_ TextDetails
s Int
sl (Doc -> Doc
oneLiner Doc
p)
oneLiner (Nest k :: Int
k p :: Doc
p) = Int -> Doc -> Doc
nest_ Int
k (Doc -> Doc
oneLiner Doc
p)
oneLiner (p :: Doc
p `Union` _) = Doc -> Doc
oneLiner Doc
p
oneLiner (Above {}) = String -> Doc
forall a. String -> a
error "oneLiner Above"
oneLiner (Beside {}) = String -> Doc
forall a. String -> a
error "oneLiner Beside"
data Style
= Style { Style -> Mode
mode :: Mode
, Style -> Int
lineLength :: Int
, Style -> Float
ribbonsPerLine :: Float
}
style :: Style
style :: Style
style = Style :: Mode -> Int -> Float -> Style
Style { lineLength :: Int
lineLength = 100, ribbonsPerLine :: Float
ribbonsPerLine = 1.5, mode :: Mode
mode = Mode
PageMode }
data Mode = PageMode
| ZigZagMode
| LeftMode
| OneLineMode
renderStyle :: Style -> Doc -> String
renderStyle :: Style -> Doc -> String
renderStyle s :: Style
s = Mode
-> Int
-> Float
-> (TextDetails -> ShowS)
-> String
-> Doc
-> String
forall a.
Mode -> Int -> Float -> (TextDetails -> a -> a) -> a -> Doc -> a
fullRender (Style -> Mode
mode Style
s) (Style -> Int
lineLength Style
s) (Style -> Float
ribbonsPerLine Style
s)
TextDetails -> ShowS
txtPrinter ""
txtPrinter :: TextDetails -> String -> String
txtPrinter :: TextDetails -> ShowS
txtPrinter (Chr c :: Char
c) s :: String
s = Char
cChar -> ShowS
forall a. a -> [a] -> [a]
:String
s
txtPrinter (Str s1 :: String
s1) s2 :: String
s2 = String
s1 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s2
txtPrinter (PStr s1 :: FastString
s1) s2 :: String
s2 = FastString -> String
unpackFS FastString
s1 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s2
txtPrinter (ZStr s1 :: FastZString
s1) s2 :: String
s2 = FastZString -> String
zString FastZString
s1 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s2
txtPrinter (LStr s1 :: PtrString
s1) s2 :: String
s2 = PtrString -> String
unpackPtrString PtrString
s1 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s2
txtPrinter (RStr n :: Int
n c :: Char
c) s2 :: String
s2 = Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
n Char
c String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s2
fullRender :: Mode
-> Int
-> Float
-> (TextDetails -> a -> a)
-> a
-> Doc
-> a
fullRender :: Mode -> Int -> Float -> (TextDetails -> a -> a) -> a -> Doc -> a
fullRender OneLineMode _ _ txt :: TextDetails -> a -> a
txt end :: a
end doc :: Doc
doc
= TextDetails
-> (Doc -> Doc -> Doc) -> (TextDetails -> a -> a) -> a -> Doc -> a
forall a.
TextDetails
-> (Doc -> Doc -> Doc) -> (TextDetails -> a -> a) -> a -> Doc -> a
easyDisplay TextDetails
spaceText (\_ y :: Doc
y -> Doc
y) TextDetails -> a -> a
txt a
end (Doc -> Doc
reduceDoc Doc
doc)
fullRender LeftMode _ _ txt :: TextDetails -> a -> a
txt end :: a
end doc :: Doc
doc
= TextDetails
-> (Doc -> Doc -> Doc) -> (TextDetails -> a -> a) -> a -> Doc -> a
forall a.
TextDetails
-> (Doc -> Doc -> Doc) -> (TextDetails -> a -> a) -> a -> Doc -> a
easyDisplay TextDetails
nlText Doc -> Doc -> Doc
first TextDetails -> a -> a
txt a
end (Doc -> Doc
reduceDoc Doc
doc)
fullRender m :: Mode
m lineLen :: Int
lineLen ribbons :: Float
ribbons txt :: TextDetails -> a -> a
txt rest :: a
rest doc :: Doc
doc
= Mode -> Int -> Int -> (TextDetails -> a -> a) -> a -> Doc -> a
forall a.
Mode -> Int -> Int -> (TextDetails -> a -> a) -> a -> Doc -> a
display Mode
m Int
lineLen Int
ribbonLen TextDetails -> a -> a
txt a
rest Doc
doc'
where
doc' :: Doc
doc' = Int -> Int -> Doc -> Doc
best Int
bestLineLen Int
ribbonLen (Doc -> Doc
reduceDoc Doc
doc)
bestLineLen, ribbonLen :: Int
ribbonLen :: Int
ribbonLen = Float -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round (Int -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
lineLen Float -> Float -> Float
forall a. Fractional a => a -> a -> a
/ Float
ribbons)
bestLineLen :: Int
bestLineLen = case Mode
m of
ZigZagMode -> Int
forall a. Bounded a => a
maxBound
_ -> Int
lineLen
easyDisplay :: TextDetails
-> (Doc -> Doc -> Doc)
-> (TextDetails -> a -> a)
-> a
-> Doc
-> a
easyDisplay :: TextDetails
-> (Doc -> Doc -> Doc) -> (TextDetails -> a -> a) -> a -> Doc -> a
easyDisplay nlSpaceText :: TextDetails
nlSpaceText choose :: Doc -> Doc -> Doc
choose txt :: TextDetails -> a -> a
txt end :: a
end
= Doc -> a
lay
where
lay :: Doc -> a
lay NoDoc = String -> a
forall a. String -> a
error "easyDisplay: NoDoc"
lay (Union p :: Doc
p q :: Doc
q) = Doc -> a
lay (Doc -> Doc -> Doc
choose Doc
p Doc
q)
lay (Nest _ p :: Doc
p) = Doc -> a
lay Doc
p
lay Empty = a
end
lay (NilAbove p :: Doc
p) = TextDetails
nlSpaceText TextDetails -> a -> a
`txt` Doc -> a
lay Doc
p
lay (TextBeside s :: TextDetails
s _ p :: Doc
p) = TextDetails
s TextDetails -> a -> a
`txt` Doc -> a
lay Doc
p
lay (Above {}) = String -> a
forall a. String -> a
error "easyDisplay Above"
lay (Beside {}) = String -> a
forall a. String -> a
error "easyDisplay Beside"
display :: Mode -> Int -> Int -> (TextDetails -> a -> a) -> a -> Doc -> a
display :: Mode -> Int -> Int -> (TextDetails -> a -> a) -> a -> Doc -> a
display m :: Mode
m !Int
page_width !Int
ribbon_width txt :: TextDetails -> a -> a
txt end :: a
end doc :: Doc
doc
= case Int
page_width Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
ribbon_width of { gap_width :: Int
gap_width ->
case Int
gap_width Int -> Int -> Int
forall a. Integral a => a -> a -> a
`quot` 2 of { shift :: Int
shift ->
let
lay :: Int -> Doc -> a
lay k :: Int
k _ | Int
k Int -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = a
forall a. HasCallStack => a
undefined
lay k :: Int
k (Nest k1 :: Int
k1 p :: Doc
p) = Int -> Doc -> a
lay (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
k1) Doc
p
lay _ Empty = a
end
lay k :: Int
k (NilAbove p :: Doc
p) = TextDetails
nlText TextDetails -> a -> a
`txt` Int -> Doc -> a
lay Int
k Doc
p
lay k :: Int
k (TextBeside s :: TextDetails
s sl :: Int
sl p :: Doc
p)
= case Mode
m of
ZigZagMode | Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
gap_width
-> TextDetails
nlText TextDetails -> a -> a
`txt` (
String -> TextDetails
Str (Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
shift '/') TextDetails -> a -> a
`txt` (
TextDetails
nlText TextDetails -> a -> a
`txt`
Int -> TextDetails -> Int -> Doc -> a
lay1 (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
shift) TextDetails
s Int
sl Doc
p ))
| Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0
-> TextDetails
nlText TextDetails -> a -> a
`txt` (
String -> TextDetails
Str (Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
shift '\\') TextDetails -> a -> a
`txt` (
TextDetails
nlText TextDetails -> a -> a
`txt`
Int -> TextDetails -> Int -> Doc -> a
lay1 (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
shift) TextDetails
s Int
sl Doc
p ))
_ -> Int -> TextDetails -> Int -> Doc -> a
lay1 Int
k TextDetails
s Int
sl Doc
p
lay _ (Above {}) = String -> a
forall a. String -> a
error "display lay Above"
lay _ (Beside {}) = String -> a
forall a. String -> a
error "display lay Beside"
lay _ NoDoc = String -> a
forall a. String -> a
error "display lay NoDoc"
lay _ (Union {}) = String -> a
forall a. String -> a
error "display lay Union"
lay1 :: Int -> TextDetails -> Int -> Doc -> a
lay1 !Int
k s :: TextDetails
s !Int
sl p :: Doc
p = let !r :: Int
r = Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sl
in Int -> a -> a
indent Int
k (TextDetails
s TextDetails -> a -> a
`txt` Int -> Doc -> a
lay2 Int
r Doc
p)
lay2 :: Int -> Doc -> a
lay2 k :: Int
k _ | Int
k Int -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = a
forall a. HasCallStack => a
undefined
lay2 k :: Int
k (NilAbove p :: Doc
p) = TextDetails
nlText TextDetails -> a -> a
`txt` Int -> Doc -> a
lay Int
k Doc
p
lay2 k :: Int
k (TextBeside s :: TextDetails
s sl :: Int
sl p :: Doc
p) = TextDetails
s TextDetails -> a -> a
`txt` Int -> Doc -> a
lay2 (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sl) Doc
p
lay2 k :: Int
k (Nest _ p :: Doc
p) = Int -> Doc -> a
lay2 Int
k Doc
p
lay2 _ Empty = a
end
lay2 _ (Above {}) = String -> a
forall a. String -> a
error "display lay2 Above"
lay2 _ (Beside {}) = String -> a
forall a. String -> a
error "display lay2 Beside"
lay2 _ NoDoc = String -> a
forall a. String -> a
error "display lay2 NoDoc"
lay2 _ (Union {}) = String -> a
forall a. String -> a
error "display lay2 Union"
indent :: Int -> a -> a
indent !Int
n r :: a
r = Int -> Char -> TextDetails
RStr Int
n ' ' TextDetails -> a -> a
`txt` a
r
in
Int -> Doc -> a
lay 0 Doc
doc
}}
printDoc :: Mode -> Int -> Handle -> Doc -> IO ()
printDoc :: Mode -> Int -> Handle -> Doc -> IO ()
printDoc mode :: Mode
mode cols :: Int
cols hdl :: Handle
hdl doc :: Doc
doc = Mode -> Int -> Handle -> Doc -> IO ()
printDoc_ Mode
mode Int
cols Handle
hdl (Doc
doc Doc -> Doc -> Doc
$$ String -> Doc
text "")
printDoc_ :: Mode -> Int -> Handle -> Doc -> IO ()
printDoc_ :: Mode -> Int -> Handle -> Doc -> IO ()
printDoc_ LeftMode _ hdl :: Handle
hdl doc :: Doc
doc
= do { Handle -> Doc -> IO ()
printLeftRender Handle
hdl Doc
doc; Handle -> IO ()
hFlush Handle
hdl }
printDoc_ mode :: Mode
mode pprCols :: Int
pprCols hdl :: Handle
hdl doc :: Doc
doc
= do { Mode
-> Int
-> Float
-> (TextDetails -> IO () -> IO ())
-> IO ()
-> Doc
-> IO ()
forall a.
Mode -> Int -> Float -> (TextDetails -> a -> a) -> a -> Doc -> a
fullRender Mode
mode Int
pprCols 1.5 TextDetails -> IO () -> IO ()
forall b. TextDetails -> IO b -> IO b
put IO ()
done Doc
doc ;
Handle -> IO ()
hFlush Handle
hdl }
where
put :: TextDetails -> IO b -> IO b
put (Chr c :: Char
c) next :: IO b
next = Handle -> Char -> IO ()
hPutChar Handle
hdl Char
c IO () -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO b
next
put (Str s :: String
s) next :: IO b
next = Handle -> String -> IO ()
hPutStr Handle
hdl String
s IO () -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO b
next
put (PStr s :: FastString
s) next :: IO b
next = Handle -> String -> IO ()
hPutStr Handle
hdl (FastString -> String
unpackFS FastString
s) IO () -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO b
next
put (ZStr s :: FastZString
s) next :: IO b
next = Handle -> FastZString -> IO ()
hPutFZS Handle
hdl FastZString
s IO () -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO b
next
put (LStr s :: PtrString
s) next :: IO b
next = Handle -> PtrString -> IO ()
hPutPtrString Handle
hdl PtrString
s IO () -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO b
next
put (RStr n :: Int
n c :: Char
c) next :: IO b
next = Handle -> String -> IO ()
hPutStr Handle
hdl (Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
n Char
c) IO () -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO b
next
done :: IO ()
done = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
hPutPtrString :: Handle -> PtrString -> IO ()
hPutPtrString :: Handle -> PtrString -> IO ()
hPutPtrString _handle :: Handle
_handle (PtrString _ 0) = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
hPutPtrString handle :: Handle
handle (PtrString a :: Ptr Word8
a l :: Int
l) = Handle -> Ptr Word8 -> Int -> IO ()
forall a. Handle -> Ptr a -> Int -> IO ()
hPutBuf Handle
handle Ptr Word8
a Int
l
printLeftRender :: Handle -> Doc -> IO ()
printLeftRender :: Handle -> Doc -> IO ()
printLeftRender hdl :: Handle
hdl doc :: Doc
doc = do
BufHandle
b <- Handle -> IO BufHandle
newBufHandle Handle
hdl
BufHandle -> Doc -> IO ()
bufLeftRender BufHandle
b Doc
doc
BufHandle -> IO ()
bFlush BufHandle
b
bufLeftRender :: BufHandle -> Doc -> IO ()
bufLeftRender :: BufHandle -> Doc -> IO ()
bufLeftRender b :: BufHandle
b doc :: Doc
doc = BufHandle -> Doc -> IO ()
layLeft BufHandle
b (Doc -> Doc
reduceDoc Doc
doc)
layLeft :: BufHandle -> Doc -> IO ()
layLeft :: BufHandle -> Doc -> IO ()
layLeft b :: BufHandle
b _ | BufHandle
b BufHandle -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = IO ()
forall a. HasCallStack => a
undefined
layLeft _ NoDoc = String -> IO ()
forall a. String -> a
error "layLeft: NoDoc"
layLeft b :: BufHandle
b (Union p :: Doc
p q :: Doc
q) = BufHandle -> Doc -> IO ()
layLeft BufHandle
b (Doc -> IO ()) -> Doc -> IO ()
forall a b. (a -> b) -> a -> b
$! Doc -> Doc -> Doc
first Doc
p Doc
q
layLeft b :: BufHandle
b (Nest _ p :: Doc
p) = BufHandle -> Doc -> IO ()
layLeft BufHandle
b (Doc -> IO ()) -> Doc -> IO ()
forall a b. (a -> b) -> a -> b
$! Doc
p
layLeft b :: BufHandle
b Empty = BufHandle -> Char -> IO ()
bPutChar BufHandle
b '\n'
layLeft b :: BufHandle
b (NilAbove p :: Doc
p) = Doc
p Doc -> IO () -> IO ()
forall a b. a -> b -> b
`seq` (BufHandle -> Char -> IO ()
bPutChar BufHandle
b '\n' IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> BufHandle -> Doc -> IO ()
layLeft BufHandle
b Doc
p)
layLeft b :: BufHandle
b (TextBeside s :: TextDetails
s _ p :: Doc
p) = TextDetails
s TextDetails -> IO () -> IO ()
forall a b. a -> b -> b
`seq` (BufHandle -> TextDetails -> IO ()
put BufHandle
b TextDetails
s IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> BufHandle -> Doc -> IO ()
layLeft BufHandle
b Doc
p)
where
put :: BufHandle -> TextDetails -> IO ()
put b :: BufHandle
b _ | BufHandle
b BufHandle -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False = IO ()
forall a. HasCallStack => a
undefined
put b :: BufHandle
b (Chr c :: Char
c) = BufHandle -> Char -> IO ()
bPutChar BufHandle
b Char
c
put b :: BufHandle
b (Str s :: String
s) = BufHandle -> String -> IO ()
bPutStr BufHandle
b String
s
put b :: BufHandle
b (PStr s :: FastString
s) = BufHandle -> FastString -> IO ()
bPutFS BufHandle
b FastString
s
put b :: BufHandle
b (ZStr s :: FastZString
s) = BufHandle -> FastZString -> IO ()
bPutFZS BufHandle
b FastZString
s
put b :: BufHandle
b (LStr s :: PtrString
s) = BufHandle -> PtrString -> IO ()
bPutPtrString BufHandle
b PtrString
s
put b :: BufHandle
b (RStr n :: Int
n c :: Char
c) = BufHandle -> Int -> Char -> IO ()
bPutReplicate BufHandle
b Int
n Char
c
layLeft _ _ = String -> IO ()
forall a. String -> a
panic "layLeft: Unhandled case"
error :: String -> a
error :: String -> a
error = String -> a
forall a. String -> a
panic