on aeb02cd help sign in
elk/src/AST/PrettyPrint.hs
64 lines 1.3 kB history raw
1{-# LANGUAGE FlexibleInstances #-}
2module AST.PrettyPrint where
4import Control.Monad.Trans.Error
5import Text.PrettyPrint as P
6import qualified AST.Helpers as Help
9class Pretty a where
10 pretty :: a -> Doc
13instance Pretty Doc where
14 pretty doc = doc
17instance Pretty String where
18 pretty = P.text
21renderPretty :: (Pretty a) => a -> String
22renderPretty e =
23 render (pretty e)
26commaCat :: [Doc] -> Doc
27commaCat docs =
28 cat (punctuate comma docs)
31commaSep :: [Doc] -> Doc
32commaSep docs =
33 sep (punctuate comma docs)
36parensIf :: Bool -> Doc -> Doc
37parensIf bool doc =
38 if bool then parens doc else doc
41variable :: String -> Doc
42variable x =
43 if Help.isOp x then parens (text x) else text x
46eightyCharLines :: Int -> String -> String
47eightyCharLines indent message = answer
48 where
49 (answer,_,_) = foldl step (spaces, indent-1, "") chunks
51 chunks = map (\w -> (w, length w)) (words message)
52 spaces = replicate indent ' '
53 step (sentence, slen, space) (word, wlen)
54 | slen + wlen > 79 = (sentence ++ "\n" ++ spaces ++ word, indent + wlen, " ")
55 | otherwise = (sentence ++ space ++ word, slen + wlen + length space, " ")
58instance Error Doc where
59 noMsg = P.empty
60 strMsg = P.text
63instance ErrorList Doc where
64 listMsg str = [ P.text str ]