on 577fc17 help sign in
elk/compiler/AST/PrettyPrint.hs
42 lines 1.2 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
8class Pretty a where
9 pretty :: a -> Doc
11instance Pretty Doc where
12 pretty doc = doc
14instance Pretty String where
15 pretty = P.text
17renderPretty :: (Pretty a) => a -> String
18renderPretty e = render (pretty e)
20commaCat docs = cat (punctuate comma docs)
21commaSep docs = sep (punctuate comma docs)
23parensIf :: Bool -> Doc -> Doc
24parensIf bool doc = if bool then parens doc else doc
26variable :: String -> Doc
27variable x =
28 if Help.isOp x then parens (text x) else text x
30eightyCharLines :: Int -> String -> String
31eightyCharLines indent message = answer
32 where
33 (answer,_,_) = foldl step (spaces, indent-1, "") chunks
35 chunks = map (\w -> (w, length w)) (words message)
36 spaces = replicate indent ' '
37 step (sentence, slen, space) (word, wlen)
38 | slen + wlen > 79 = (sentence ++ "\n" ++ spaces ++ word, indent + wlen, " ")
39 | otherwise = (sentence ++ space ++ word, slen + wlen + length space, " ")
41instance ErrorList Doc where
42 listMsg str = [ P.text str ]