on f73a6ff help sign in
elk/compiler/SourceSyntax/PrettyPrint.hs
34 lines 1.0 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
13commaCat docs = cat (punctuate comma docs)
14commaSep docs = sep (punctuate comma docs)
16parensIf bool doc = if bool then parens doc else doc
18variable x =
19 if Help.isOp x then parens (text x)
20 else text (reprime x)
22reprime :: String -> String
23reprime = map (\c -> if c == '$' then '\'' else c)
25eightyCharLines :: Int -> String -> String
26eightyCharLines indent message = answer
27 where
28 (answer,_,_) = foldl step (replicate (indent-1) ' ', indent-1, "") chunks
30 chunks = map (\w -> (w, length w)) (words message)
31 spaces = replicate indent ' '
32 step (sentence, slen, space) (word, wlen)
33 | slen + wlen > 79 = (sentence ++ "\n" ++ spaces ++ word, indent + wlen, " ")
34 | otherwise = (sentence ++ space ++ word, slen + wlen + length space, " ")