@@ -2,40 +2,219 @@
2module Elm.Docs where
2module Elm.Docs where
4import Control.Applicative ((<$>),(<*>))
4import Control.Applicative ((<$>),(<*>))
5+import Control.Arrow (second)
5import Control.Monad
6import Control.Monad
6−import Data.Aeson
7+import Data.Aeson ((.:), (.:?), (.=))
7−
8+import qualified Data.Aeson as Json
8−data Document = Doc
9+import qualified Data.ByteString.Lazy.Char8 as BS
9− { moduleName :: String
10+import qualified Data.Text as Text
10− , structure :: String
18− , assocPrec :: Maybe (String,Int)
21−instance FromJSON Document where
22− parseJSON (Object v) =
25− <*> (concat <$> sequence [ v .: "aliases", v .: "datatypes", v .: "values" ])
27− parseJSON _ = fail "Conversion to Document was expecting an object"
29−instance FromJSON Entry where
30− parseJSON (Object v) =
31− Entry <$> v .: "name"
34− <*> (liftM2 (,) <$> v .:? "associativity"
35− <*> v .:? "precedence")
37− parseJSON _ = fail "Conversion to Entry was expecting an object"
39−data Content = Markdown String | Value String
12+import qualified Elm.Compiler.Module as Module
15+data Documentation = Documentation
16+ { moduleName :: Module.Name
25+ { aliasName :: String
26+ , aliasComment :: String
27+ , aliasArgs :: [String]
33+ { unionName :: String
34+ , unionComment :: String
35+ , unionArgs :: [String]
36+ , unionCases :: [(String, [Type])]
41+ { valueName :: String
42+ , valueComment :: String
44+ , valueAssocPrec :: Maybe (String,Int)
53+ | Record [(String, Type)] (Maybe Type)
56+-- JSON for DOCUMENTATION
58+instance Json.ToJSON Documentation where
59+ toJSON (Documentation name comment aliases types values) =
62+ , "comment" .= comment
63+ , "aliases" .= aliases
68+instance Json.FromJSON Documentation where
69+ parseJSON (Json.Object obj) =
78+ fail $ "Cannot decode Documentation from: " ++ BS.unpack (Json.encode value)
83+instance Json.ToJSON Alias where
84+ toJSON (Alias name comment args tipe) =
87+ , "comment" .= comment
92+instance Json.FromJSON Alias where
93+ parseJSON (Json.Object obj) =
101+ fail $ "Cannot decode Alias from: " ++ BS.unpack (Json.encode value)
106+instance Json.ToJSON Union where
107+ toJSON (Union name comment args cases) =
110+ , "comment" .= comment
115+instance Json.FromJSON Union where
116+ parseJSON (Json.Object obj) =
119+ <*> obj .: "comment"
124+ fail $ "Cannot decode Union from: " ++ BS.unpack (Json.encode value)
129+instance Json.ToJSON Value where
130+ toJSON (Value name comment tipe assocPrec) =
131+ Json.object (fields ++ possibleFields)
135+ , "comment" .= comment
142+ Just (assoc, prec) ->
143+ [ "associativity" .= assoc
144+ , "precedence" .= prec
148+instance Json.FromJSON Value where
149+ parseJSON (Json.Object obj) =
152+ <*> obj .: "comment"
154+ <*> (liftM2 (,) <$> obj .:? "associativity" <*> obj .:? "precedence")
157+ fail $ "Cannot decode Value from: " ++ BS.unpack (Json.encode value)
162+instance Json.ToJSON Type where
164+ Json.object (getFields tipe)
169+ [ "tag" .= ("lambda" :: Text.Text)
170+ , "in" .= Json.toJSON t1
171+ , "out" .= Json.toJSON t2
175+ [ "tag" .= ("var" :: Text.Text)
176+ , "name" .= Json.toJSON x
180+ [ "tag" .= ("type" :: Text.Text)
181+ , "name" .= Json.toJSON name
185+ [ "tag" .= ("app" :: Text.Text)
186+ , "func" .= Json.toJSON t
187+ , "args" .= Json.toJSON ts
190+ Record fields ext ->
191+ [ "tag" .= ("record" :: Text.Text)
192+ , "fields" .= Json.toJSON (map (Json.toJSON . second Json.toJSON) fields)
193+ , "extension" .= Json.toJSON ext
196+instance Json.FromJSON Type where
197+ parseJSON (Json.Object obj) =
198+ do tag <- obj .: "tag"
199+ case (tag :: String) of
201+ Lambda <$> obj .: "in" <*> obj .: "out"
204+ Var <$> obj .: "name"
207+ Type <$> obj .: "name"
210+ App <$> obj .: "func" <*> obj .: "arg"
213+ Record <$> obj .: "fields" <*> obj .: "extension"
216+ fail $ "Error when decoding type with tag: " ++ tag
220+ fail $ "Cannot decode Value from: " ++ BS.unpack (Json.encode value)