Skip to content

Instantly share code, notes, and snippets.

@tos-kamiya
Last active December 10, 2015 05:08
Show Gist options
  • Save tos-kamiya/4385141 to your computer and use it in GitHub Desktop.
Save tos-kamiya/4385141 to your computer and use it in GitHub Desktop.
A pretty-printing for Haskell in Haskell. Almost same as http://code.haskell.org/haskell-src-exts/examples/Prettify.hs .
-- references
-- http://code.haskell.org/haskell-src-exts/Test/examples/HaskellParser.hs
-- http://code.haskell.org/haskell-src-exts/examples/Prettify.hs
import qualified Language.Haskell.Exts.Annotated as P
import qualified Language.Haskell.Exts.Extension as Ext
import Language.Haskell.Exts.Pretty (prettyPrint)
import System.Environment (getArgs)
parse originalFileName input = P.parseModuleWithMode parseMode input
where
parseMode :: P.ParseMode
parseMode = P.defaultParseMode
{ P.parseFilename = originalFileName
, P.extensions = Ext.glasgowExts ++
[ Ext.ExplicitForAll
, Ext.DoAndIfThenElse
]
}
main =
do args <- getArgs
case args of
[] -> putStrLn usage
(_:_:_) -> fail "too many command-line arguments"
[fileName] ->
do s <- readFile fileName
let x = parse fileName s
putStrLn $ case x of
P.ParseOk h ->
prettyPrint h
P.ParseFailed srcLoc message ->
unlines [ prettyPrint srcLoc
, message
]
where usage = "Prettify.hs <source.hs>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment