on b412396 help sign in
elk/compiler/SourceSyntax/PrettyPrint.hs
39 lines 1.1 kB history raw
2module SourceSyntax.PrettyPrint where
4import Text.PrettyPrint
5import qualified SourceSyntax.Helpers as Help
7class Pretty a where
8 pretty :: a -> Doc
10instance Pretty () where
11 pretty () = empty
13renderPretty :: (Pretty a) => a -> String
14renderPretty e = render (pretty e)
16commaCat docs = cat (punctuate comma docs)
17commaSep docs = sep (punctuate comma docs)
19parensIf :: Bool -> Doc -> Doc
20parensIf bool doc = if bool then parens doc else doc
22variable :: String -> Doc
23variable x =
24 if Help.isOp x then parens (text x)
25 else text (reprime x)
27reprime :: String -> String
28reprime = map (\c -> if c == '$' then '\'' else c)
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, " ")