elm/compiler/SourceSyntax/PrettyPrint.hs
Evan Czaplicki 9dd5dff279 Make AST more general and try to give its phases better names
Also change the constructors for the Pattern ADT
2014-02-10 00:17:33 +01:00

39 lines
1.1 KiB
Haskell

module SourceSyntax.PrettyPrint where
import Text.PrettyPrint
import qualified SourceSyntax.Helpers as Help
class Pretty a where
pretty :: a -> Doc
instance Pretty () where
pretty () = empty
renderPretty :: (Pretty a) => a -> String
renderPretty e = render (pretty e)
commaCat docs = cat (punctuate comma docs)
commaSep docs = sep (punctuate comma docs)
parensIf :: Bool -> Doc -> Doc
parensIf bool doc = if bool then parens doc else doc
variable :: String -> Doc
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)
eightyCharLines :: Int -> String -> String
eightyCharLines indent message = answer
where
(answer,_,_) = foldl step (spaces, 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, " ")