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 = PPAnnotation String T.Type
26 | PPDef String Expr.LParseExpr
30 = Out String Expr.LExpr T.Type
34type ParseDeclaration = Declaration' ParsePort Expr.ParseDef
35type Declaration = Declaration' Port Expr.Def
37instance Binary Derivation where
38 get = do n <- getWord8
44 _ -> error "Unable to decode Derivation. You may have corrupted binary files,\n\
45 \so please report an issue at <https://github.com/evancz/Elm/issues>"
48 putWord8 $ case derivation of
54instance Show Assoc where
61instance Binary Assoc where
62 get = do n <- getWord8
67 _ -> error "Error reading valid associativity from serialized string"
69 put assoc = putWord8 $ case assoc of { L -> 0 ; N -> 1 ; R -> 2 }
71instance (Pretty port, Pretty def) => Pretty (Declaration' port def) where
74 Definition def -> pretty def
76 Datatype tipe tvars ctors deriveables ->
77 P.hang (P.text "data" <+> P.text tipe <+> P.hsep (map P.text tvars)) 4
78 (P.sep $ zipWith join ("=" : repeat "|") ctors) <+> prettyDeriving deriveables
80 join c ctor = P.text c <+> prettyCtor ctor
81 prettyCtor (name, tipes) =
82 P.hang (P.text name) 2 (P.sep (map T.prettyParens tipes))
84 TypeAlias name tvars tipe deriveables ->
85 alias <+> prettyDeriving deriveables
87 name' = P.text name <+> P.hsep (map P.text tvars)
88 alias = P.hang (P.text "type" <+> name' <+> P.equals) 4 (pretty tipe)
90 Port port -> pretty port
92 Fixity assoc prec op -> P.text "infix" <> assoc' <+> P.int prec <+> P.text op
94 assoc' = case assoc of
99instance Pretty ParsePort where
102 PPAnnotation name tipe -> prettyPort name ":" tipe
103 PPDef name expr -> prettyPort name "=" expr
105instance Pretty Port where
108 In name tipe -> prettyPort name ":" tipe
109 Out name expr tipe -> P.vcat [ prettyPort name ":" tipe
110 , prettyPort name "=" expr ]
113prettyPort :: (Pretty a) => String -> String -> a -> Doc
114prettyPort name op e = P.text "port" <+> P.text name <+> P.text op <+> pretty e
116prettyDeriving :: [Derivation] -> Doc
117prettyDeriving deriveables =
120 [d] -> P.text "deriving" <+> P.text (show d)
121 ds -> P.text "deriving" <+>
122 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