on 9b0c310 help sign in
elk/src/Elm/Docs.hs
149 lines 3.6 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2module Elm.Docs where
4import Control.Applicative ((<$>),(<*>))
5import Control.Monad
6import Data.Aeson ((.:), (.:?), (.=))
7import qualified Data.Aeson as Json
8import qualified Data.ByteString.Lazy.Char8 as BS
10import qualified Elm.Compiler.Module as Module
11import qualified Elm.Compiler.Type as Type
14data Documentation = Documentation
15 { moduleName :: Module.Name
16 , comment :: String
17 , aliases :: [Alias]
18 , types :: [Union]
19 , values :: [Value]
20 }
23data Alias = Alias
24 { aliasName :: String
25 , aliasComment :: String
26 , aliasArgs :: [String]
27 , aliasType :: Type.Type
28 }
31data Union = Union
32 { unionName :: String
33 , unionComment :: String
34 , unionArgs :: [String]
35 , unionCases :: [(String, [Type.Type])]
36 }
39data Value = Value
40 { valueName :: String
41 , valueComment :: String
42 , valueType :: Type.Type
43 , valueAssocPrec :: Maybe (String,Int)
44 }
47-- JSON for DOCUMENTATION
49instance Json.ToJSON Documentation where
50 toJSON (Documentation name comment aliases types values) =
51 Json.object
52 [ "name" .= name
53 , "comment" .= comment
54 , "aliases" .= aliases
55 , "types" .= types
56 , "values" .= values
57 ]
59instance Json.FromJSON Documentation where
60 parseJSON (Json.Object obj) =
61 Documentation
62 <$> obj .: "name"
63 <*> obj .: "comment"
64 <*> obj .: "aliases"
65 <*> obj .: "types"
66 <*> obj .: "values"
68 parseJSON value =
69 fail $ "Cannot decode Documentation from: " ++ BS.unpack (Json.encode value)
72-- JSON for ALIAS
74instance Json.ToJSON Alias where
75 toJSON (Alias name comment args tipe) =
76 Json.object
77 [ "name" .= name
78 , "comment" .= comment
79 , "args" .= args
80 , "type" .= tipe
81 ]
83instance Json.FromJSON Alias where
84 parseJSON (Json.Object obj) =
85 Alias
86 <$> obj .: "name"
87 <*> obj .: "comment"
88 <*> obj .: "args"
89 <*> obj .: "type"
91 parseJSON value =
92 fail $ "Cannot decode Alias from: " ++ BS.unpack (Json.encode value)
95-- JSON for UNION
97instance Json.ToJSON Union where
98 toJSON (Union name comment args cases) =
99 Json.object
100 [ "name" .= name
101 , "comment" .= comment
102 , "args" .= args
103 , "cases" .= cases
104 ]
106instance Json.FromJSON Union where
107 parseJSON (Json.Object obj) =
108 Union
109 <$> obj .: "name"
110 <*> obj .: "comment"
111 <*> obj .: "args"
112 <*> obj .: "cases"
114 parseJSON value =
115 fail $ "Cannot decode Union from: " ++ BS.unpack (Json.encode value)
118-- JSON for VALUE
120instance Json.ToJSON Value where
121 toJSON (Value name comment tipe assocPrec) =
122 Json.object (fields ++ possibleFields)
123 where
124 fields =
125 [ "name" .= name
126 , "comment" .= comment
127 , "type" .= tipe
128 ]
130 possibleFields =
131 case assocPrec of
132 Nothing -> []
133 Just (assoc, prec) ->
134 [ "associativity" .= assoc
135 , "precedence" .= prec
136 ]
139instance Json.FromJSON Value where
140 parseJSON (Json.Object obj) =
141 Value
142 <$> obj .: "name"
143 <*> obj .: "comment"
144 <*> obj .: "type"
145 <*> (liftM2 (,) <$> obj .:? "associativity" <*> obj .:? "precedence")
147 parseJSON value =
148 fail $ "Cannot decode Value from: " ++ BS.unpack (Json.encode value)