on 2f8b048 help sign in
elk/compiler/SourceSyntax/PrettyPrint.hs
42 lines 1.2 kB history raw
1{-# LANGUAGE FlexibleInstances #-}
2module SourceSyntax.PrettyPrint where
4import Text.PrettyPrint as P
5import qualified SourceSyntax.Helpers as Help
7class Pretty a where
8 pretty :: a -> Doc
10instance Pretty Doc where
11 pretty doc = doc
13instance Pretty String where
14 pretty = P.text
16renderPretty :: (Pretty a) => a -> String
17renderPretty e = render (pretty e)
19commaCat docs = cat (punctuate comma docs)
20commaSep docs = sep (punctuate comma docs)
22parensIf :: Bool -> Doc -> Doc
23parensIf bool doc = if bool then parens doc else doc
25variable :: String -> Doc
26variable x =
27 if Help.isOp x then parens (text x)
28 else text (reprime x)
30reprime :: String -> String
31reprime = map (\c -> if c == '$' then '\'' else c)
33eightyCharLines :: Int -> String -> String
34eightyCharLines indent message = answer
35 where
36 (answer,_,_) = foldl step (spaces, indent-1, "") chunks
38 chunks = map (\w -> (w, length w)) (words message)
39 spaces = replicate indent ' '
40 step (sentence, slen, space) (word, wlen)
41 | slen + wlen > 79 = (sentence ++ "\n" ++ spaces ++ word, indent + wlen, " ")
42 | otherwise = (sentence ++ space ++ word, slen + wlen + length space, " ")