{-# LANGUAGE Rank2Types #-}
module Lambdabot.Util.Parser
( withParsed
, prettyPrintInLine
) where
import Data.Generics
import Language.Haskell.Exts.Simple
withParsed :: (forall a. (Data a, Eq a) => a -> a) -> String -> String
withParsed _ "" = "Error: expected a Haskell expression or declaration"
withParsed f s = case (parseExp s, parseDecl s) of
(ParseOk a, _) -> prettyPrintInLine $ f a
(_, ParseOk a) -> prettyPrintInLine $ f a
(ParseFailed l e, _) -> prettyPrint l ++ ':' : e
prettyPrintInLine :: Pretty a => a -> String
prettyPrintInLine = prettyPrintWithMode (defaultMode { layout = PPInLine })