on 06503fe help sign in
elk/compiler/SourceSyntax/Declaration.hs
115 lines 3.7 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module SourceSyntax.Declaration where
4import Data.Binary
5import qualified SourceSyntax.Expression as Expr
6import qualified SourceSyntax.Type as T
7import SourceSyntax.PrettyPrint
8import Text.PrettyPrint as P
10data Declaration' port def
11 = Definition def
12 | Datatype String [String] [(String,[T.Type])] [Derivation]
13 | TypeAlias String [String] T.Type [Derivation]
14 | Port port
15 | Fixity Assoc Int String
16 deriving (Eq, Show)
18data Assoc = L | N | R
19 deriving (Eq)
21data Derivation = Json | JS | Binary | New
22 deriving (Eq, Show)
24data ParsePort
25 = PortAnnotation String T.Type
26 | SendDefinition String Expr.LParseExpr
27 | RecvDefinition String Expr.LParseExpr
29data Port
30 = Send String Expr.LExpr T.Type
31 | Recv String Expr.LExpr T.Type
32 deriving (Eq,Show)
34type ParseDeclaration = Declaration' ParsePort Expr.ParseDef
35type Declaration = Declaration' Port Expr.Def
37instance Binary Derivation where
38 get = do n <- getWord8
39 return $ case n of
40 0 -> Json
41 1 -> JS
42 2 -> Binary
43 3 -> New
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>"
47 put derivation =
48 putWord8 $ case derivation of
49 Json -> 0
50 JS -> 1
51 Binary -> 2
52 New -> 3
54instance Show Assoc where
55 show assoc =
56 case assoc of
57 L -> "left"
58 N -> "non"
59 R -> "right"
61instance (Pretty port, Pretty def) => Pretty (Declaration' port def) where
62 pretty decl =
63 case decl of
64 Definition def -> pretty def
66 Datatype tipe tvars ctors deriveables ->
67 P.hang (P.text "data" <+> P.text tipe <+> P.hsep (map P.text tvars)) 4
68 (P.sep $ zipWith join ("=" : repeat "|") ctors) <+> prettyDeriving deriveables
69 where
70 join c ctor = P.text c <+> prettyCtor ctor
71 prettyCtor (name, tipes) =
72 P.hang (P.text name) 2 (P.sep (map T.prettyParens tipes))
74 TypeAlias name tvars tipe deriveables ->
75 alias <+> prettyDeriving deriveables
76 where
77 name' = P.text name <+> P.hsep (map P.text tvars)
78 alias = P.hang (P.text "type" <+> name' <+> P.equals) 4 (pretty tipe)
80 Port port -> pretty port
82 Fixity assoc prec op -> P.text "infix" <> assoc' <+> P.int prec <+> P.text op
83 where
84 assoc' = case assoc of
85 L -> P.text "l"
86 N -> P.empty
87 R -> P.text "r"
89instance Pretty ParsePort where
90 pretty port =
91 case port of
92 PortAnnotation name tipe -> prettyPort name ":" tipe
93 SendDefinition name expr -> prettyPort name "<-" expr
94 RecvDefinition name expr -> prettyPort name "->" expr
96instance Pretty Port where
97 pretty port =
98 case port of
99 Send name expr tipe -> mkPort "<-" name expr tipe
100 Recv name expr tipe -> mkPort "->" name expr tipe
101 where
102 mkPort arrow name expr tipe =
103 P.vcat [ prettyPort name ":" tipe
104 , prettyPort name arrow expr ]
106prettyPort :: (Pretty a) => String -> String -> a -> Doc
107prettyPort name op e = P.text "port" <+> P.text name <+> P.text op <+> pretty e
109prettyDeriving :: [Derivation] -> Doc
110prettyDeriving deriveables =
111 case deriveables of
112 [] -> P.empty
113 [d] -> P.text "deriving" <+> P.text (show d)
114 ds -> P.text "deriving" <+>
115 P.parens (P.hsep $ P.punctuate P.comma $ map (P.text . show) ds)