2013-07-07 10:56:34 +00:00
|
|
|
|
|
|
|
module SourceSyntax.PrettyPrint where
|
|
|
|
|
|
|
|
import Text.PrettyPrint
|
2013-07-23 13:43:21 +00:00
|
|
|
import qualified SourceSyntax.Helpers as Help
|
2013-07-07 10:56:34 +00:00
|
|
|
|
|
|
|
class Pretty a where
|
|
|
|
pretty :: a -> Doc
|
|
|
|
|
2013-07-10 12:31:57 +00:00
|
|
|
instance Pretty () where
|
|
|
|
pretty () = empty
|
|
|
|
|
2013-07-07 10:56:34 +00:00
|
|
|
commaCat docs = cat (punctuate comma docs)
|
2013-07-19 15:45:16 +00:00
|
|
|
commaSep docs = sep (punctuate comma docs)
|
2013-07-07 10:56:34 +00:00
|
|
|
|
2013-07-23 13:43:21 +00:00
|
|
|
parensIf bool doc = if bool then parens doc else doc
|
|
|
|
|
2013-08-14 07:44:29 +00:00
|
|
|
variable x =
|
|
|
|
if Help.isOp x then parens (text x)
|
|
|
|
else text (reprime x)
|
|
|
|
|
|
|
|
reprime :: String -> String
|
|
|
|
reprime = map (\c -> if c == '$' then '\'' else c)
|
2013-11-20 06:31:38 +00:00
|
|
|
|
|
|
|
eightyCharLines :: Int -> String -> String
|
|
|
|
eightyCharLines indent message = answer
|
|
|
|
where
|
|
|
|
(answer,_,_) = foldl step (replicate (indent-1) ' ', indent-1, "") chunks
|
|
|
|
|
|
|
|
chunks = map (\w -> (w, length w)) (words message)
|
|
|
|
spaces = replicate indent ' '
|
|
|
|
step (sentence, slen, space) (word, wlen)
|
|
|
|
| slen + wlen > 79 = (sentence ++ "\n" ++ spaces ++ word, indent + wlen, " ")
|
|
|
|
| otherwise = (sentence ++ space ++ word, slen + wlen + length space, " ")
|