1{-# OPTIONS_GHC -Wall #-}
2module SourceSyntax.Declaration where
5import qualified SourceSyntax.Expression as Expr
6import qualified SourceSyntax.Type as T
7import SourceSyntax.PrettyPrint
8import Text.PrettyPrint as P
10data Declaration' port def
12 | Datatype String [String] [(String,[T.Type])] [Derivation]
13 | TypeAlias String [String] T.Type [Derivation]
15 | Fixity Assoc Int String
21data Derivation = Json | JS | Binary | New
25 = PortAnnotation String T.Type
26 | SendDefinition String Expr.LParseExpr
27 | RecvDefinition String Expr.LParseExpr
31 = Send String Expr.LExpr T.Type
32 | Recv String Expr.LExpr T.Type
35type ParseDeclaration = Declaration' ParsePort Expr.ParseDef
36type Declaration = Declaration' Port Expr.Def
38instance Binary Derivation where
39 get = do n <- getWord8
45 _ -> error "Unable to decode Derivation. You may have corrupted binary files,\n\
46 \so please report an issue at <https://github.com/evancz/Elm/issues>"
49 putWord8 $ case derivation of
55instance Show Assoc where
62instance (Pretty port, Pretty def) => Pretty (Declaration' port def) where
65 Definition def -> pretty def
67 Datatype tipe tvars ctors deriveables ->
68 P.hang (P.text "data" <+> P.text tipe <+> P.hsep (map P.text tvars)) 4
69 (P.sep $ zipWith join ("=" : repeat "|") ctors) <+> prettyDeriving deriveables
71 join c ctor = P.text c <+> prettyCtor ctor
72 prettyCtor (name, tipes) =
73 P.hang (P.text name) 2 (P.sep (map T.prettyParens tipes))
75 TypeAlias name tvars tipe deriveables ->
76 alias <+> prettyDeriving deriveables
78 name' = P.text name <+> P.hsep (map P.text tvars)
79 alias = P.hang (P.text "type" <+> name' <+> P.equals) 4 (pretty tipe)
81 Port port -> pretty port
83 Fixity assoc prec op -> P.text "infix" <> assoc' <+> P.int prec <+> P.text op
85 assoc' = case assoc of
90instance Pretty ParsePort where
93 PortAnnotation name tipe -> prettyPort name ":" tipe
94 SendDefinition name expr -> prettyPort name "<-" expr
95 RecvDefinition name expr -> prettyPort name "->" expr
97instance Pretty Port where
100 Send name expr tipe -> mkPort "<-" name expr tipe
101 Recv name expr tipe -> mkPort "->" name expr tipe
103 mkPort arrow name expr tipe =
104 P.vcat [ prettyPort name ":" tipe
105 , prettyPort name arrow expr ]
107prettyPort :: (Pretty a) => String -> String -> a -> Doc
108prettyPort name op e = P.text "port" <+> P.text name <+> P.text op <+> pretty e
110prettyDeriving :: [Derivation] -> Doc
111prettyDeriving deriveables =
114 [d] -> P.text "deriving" <+> P.text (show d)
115 ds -> P.text "deriving" <+>
116 P.parens (P.hsep $ P.punctuate P.comma $ map (P.text . show) ds)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list