Merge pull request #486 from JoeyEremondi/dev

Added Pretty instance for Module (3rd time's the charm)
This commit is contained in:
Evan Czaplicki 2014-02-06 19:41:03 +01:00
commit 3de2023639

View file

@ -4,12 +4,16 @@ module SourceSyntax.Module where
import Data.Binary
import qualified Data.Map as Map
import Control.Applicative ((<$>), (<*>))
import Text.PrettyPrint as P
import SourceSyntax.Expression (LExpr)
import SourceSyntax.Declaration
import SourceSyntax.Type
import Data.List (intercalate)
import qualified Elm.Internal.Version as Version
import SourceSyntax.PrettyPrint
data Module def =
Module [String] Exports Imports [def]
@ -21,6 +25,31 @@ type Imports = [(String, ImportMethod)]
data ImportMethod = As String | Importing [String] | Hiding [String]
deriving (Eq, Ord, Show)
instance (Pretty def ) => Pretty (Module def) where
pretty (Module modNames exportList importList decs) =
let
exportPret = case exportList of
[] -> P.text " "
_ -> P.parens $ commaCat $ map P.text exportList
decPret = P.sep $ map pretty decs
modName = P.text $ intercalate "." modNames
modPret = (P.text "module" <+> modName <+> exportPret <+> P.text "where")
importPret = P.vcat $ map prettyImport importList
prettyImport (name, method) =
case method of
As s -> if name == s
then P.text $ "import " ++ name
else P.text $ "import " ++ name ++ " as " ++ s
Importing strs -> (P.text $ "import " ++ name ++ " ") <+> (commaCat $ map P.text strs)
Hiding [] -> (P.text $ "import open " ++ name ++ " ")
Hiding strs -> (P.text $ "import open " ++ name ++ " ") <+> (commaCat $ map P.text strs)
in P.sep [modPret, importPret, decPret]
instance Binary ImportMethod where
put method =
let put' n info = putWord8 n >> put info in