on master help sign in
Merge pull request #486 from JoeyEremondi/dev

Added Pretty instance for Module (3rd time's the charm)

commit3de202363901e277711c49eeec9078d4a58184d1 authorEvan Czaplicki <info@elm-lang.org> dateThu 6 Feb 2014, 19:41 (13 years ago) parent90613118594f6a
1 file changed+29
compiler/SourceSyntax/Module.hs+29
compiler/SourceSyntax/Module.hs+29
@@ -4,12 +4,16 @@ module SourceSyntax.Module where
44import Data.Binary
55import qualified Data.Map as Map
66import Control.Applicative ((<$>), (<*>))
7+import Text.PrettyPrint as P
78
89import SourceSyntax.Expression (LExpr)
910import SourceSyntax.Declaration
1011import SourceSyntax.Type
12+import Data.List (intercalate)
1113
1214import qualified Elm.Internal.Version as Version
15+import SourceSyntax.PrettyPrint
16+
1317
1418data Module def =
1519 Module [String] Exports Imports [def]
@@ -21,6 +25,31 @@ type Imports = [(String, ImportMethod)]
2125data ImportMethod = As String | Importing [String] | Hiding [String]
2226 deriving (Eq, Ord, Show)
2327
28+instance (Pretty def ) => Pretty (Module def) where
29+ pretty (Module modNames exportList importList decs) =
30+ let
31+ exportPret = case exportList of
32+ [] -> P.text " "
33+ _ -> P.parens $ commaCat $ map P.text exportList
34+
35+ decPret = P.sep $ map pretty decs
36+ modName = P.text $ intercalate "." modNames
37+ modPret = (P.text "module" <+> modName <+> exportPret <+> P.text "where")
38+
39+
40+ importPret = P.vcat $ map prettyImport importList
41+
42+ prettyImport (name, method) =
43+ case method of
44+ As s -> if name == s
45+ then P.text $ "import " ++ name
46+ else P.text $ "import " ++ name ++ " as " ++ s
47+ Importing strs -> (P.text $ "import " ++ name ++ " ") <+> (commaCat $ map P.text strs)
48+ Hiding [] -> (P.text $ "import open " ++ name ++ " ")
49+ Hiding strs -> (P.text $ "import open " ++ name ++ " ") <+> (commaCat $ map P.text strs)
50+
51+ in P.sep [modPret, importPret, decPret]
52+
2453instance Binary ImportMethod where
2554 put method =
2655 let put' n info = putWord8 n >> put info in