@@ -1,12 +1,15 @@
1−{-# OPTIONS_GHC -Wall #-}
1+{-# OPTIONS_GHC -W #-}
22module SourceSyntax.Module where
33
44import Data.Binary
5+import qualified Data.List as List
56import qualified Data.Map as Map
67import Control.Applicative ((<$>), (<*>))
8+import Text.PrettyPrint as P
79
810import SourceSyntax.Expression (LExpr)
911import SourceSyntax.Declaration
12+import SourceSyntax.PrettyPrint
1013import SourceSyntax.Type
1114
1215import qualified Elm.Internal.Version as Version
@@ -21,6 +24,33 @@ type Imports = [(String, ImportMethod)]
2124data ImportMethod = As String | Importing [String] | Hiding [String]
2225 deriving (Eq, Ord, Show)
2326
27+instance (Pretty def) => Pretty (Module def) where
28+ pretty (Module modNames exports imports decls) =
29+ P.vcat [modul, P.text "", prettyImports, P.text "", prettyDecls]
30+ where
31+ prettyDecls = P.sep $ map pretty decls
32+
33+ modul = P.text "module" <+> moduleName <+> where'
34+ moduleName = P.text $ List.intercalate "." modNames
35+ where' =
36+ case exports of
37+ [] -> P.text "where"
38+ _ -> P.parens (commaCat $ map P.text exports) <+> P.text "where"
39+
40+ prettyImports = P.vcat $ map prettyImport imports
41+
42+ prettyImport (name, method) =
43+ P.text "import" <+>
44+ case method of
45+ As alias ->
46+ P.text $ name ++ (if name == alias then "" else " as " ++ alias)
47+
48+ Importing values ->
49+ P.text name <+> P.parens (commaCat (map P.text values))
50+
51+ Hiding [] -> P.text ("open " ++ name)
52+ Hiding _ -> error "invalid import declaration"
53+
2454instance Binary ImportMethod where
2555 put method =
2656 let put' n info = putWord8 n >> put info in