Merge pull request #486 from JoeyEremondi/dev
Added Pretty instance for Module (3rd time's the charm)
This commit is contained in:
commit
3de2023639
1 changed files with 29 additions and 0 deletions
|
@ -4,12 +4,16 @@ module SourceSyntax.Module where
|
||||||
import Data.Binary
|
import Data.Binary
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Control.Applicative ((<$>), (<*>))
|
import Control.Applicative ((<$>), (<*>))
|
||||||
|
import Text.PrettyPrint as P
|
||||||
|
|
||||||
import SourceSyntax.Expression (LExpr)
|
import SourceSyntax.Expression (LExpr)
|
||||||
import SourceSyntax.Declaration
|
import SourceSyntax.Declaration
|
||||||
import SourceSyntax.Type
|
import SourceSyntax.Type
|
||||||
|
import Data.List (intercalate)
|
||||||
|
|
||||||
import qualified Elm.Internal.Version as Version
|
import qualified Elm.Internal.Version as Version
|
||||||
|
import SourceSyntax.PrettyPrint
|
||||||
|
|
||||||
|
|
||||||
data Module def =
|
data Module def =
|
||||||
Module [String] Exports Imports [def]
|
Module [String] Exports Imports [def]
|
||||||
|
@ -21,6 +25,31 @@ type Imports = [(String, ImportMethod)]
|
||||||
data ImportMethod = As String | Importing [String] | Hiding [String]
|
data ImportMethod = As String | Importing [String] | Hiding [String]
|
||||||
deriving (Eq, Ord, Show)
|
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
|
instance Binary ImportMethod where
|
||||||
put method =
|
put method =
|
||||||
let put' n info = putWord8 n >> put info in
|
let put' n info = putWord8 n >> put info in
|
||||||
|
|
Loading…
Reference in a new issue