on d87affe help sign in
elk/src/Elm/Docs.hs
151 lines 3.6 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2module Elm.Docs where
4import Control.Applicative ((<$>),(<*>))
5import Control.Arrow (second)
6import Control.Monad
7import Data.Aeson ((.:), (.:?), (.=))
8import qualified Data.Aeson as Json
9import qualified Data.ByteString.Lazy.Char8 as BS
10import qualified Data.Text as Text
12import qualified Elm.Compiler.Module as Module
13import qualified Elm.Compiler.Type as Type
16data Documentation = Documentation
17 { moduleName :: Module.Name
18 , comment :: String
19 , aliases :: [Alias]
20 , types :: [Union]
21 , values :: [Value]
22 }
25data Alias = Alias
26 { aliasName :: String
27 , aliasComment :: String
28 , aliasArgs :: [String]
29 , aliasType :: Type.Type
30 }
33data Union = Union
34 { unionName :: String
35 , unionComment :: String
36 , unionArgs :: [String]
37 , unionCases :: [(String, [Type.Type])]
38 }
41data Value = Value
42 { valueName :: String
43 , valueComment :: String
44 , valueType :: Type.Type
45 , valueAssocPrec :: Maybe (String,Int)
46 }
49-- JSON for DOCUMENTATION
51instance Json.ToJSON Documentation where
52 toJSON (Documentation name comment aliases types values) =
53 Json.object
54 [ "name" .= name
55 , "comment" .= comment
56 , "aliases" .= aliases
57 , "types" .= types
58 , "values" .= values
59 ]
61instance Json.FromJSON Documentation where
62 parseJSON (Json.Object obj) =
63 Documentation
64 <$> obj .: "name"
65 <*> obj .: "comment"
66 <*> obj .: "aliases"
67 <*> obj .: "types"
68 <*> obj .: "values"
70 parseJSON value =
71 fail $ "Cannot decode Documentation from: " ++ BS.unpack (Json.encode value)
74-- JSON for ALIAS
76instance Json.ToJSON Alias where
77 toJSON (Alias name comment args tipe) =
78 Json.object
79 [ "name" .= name
80 , "comment" .= comment
81 , "args" .= args
82 , "type" .= tipe
83 ]
85instance Json.FromJSON Alias where
86 parseJSON (Json.Object obj) =
87 Alias
88 <$> obj .: "name"
89 <*> obj .: "comment"
90 <*> obj .: "args"
91 <*> obj .: "type"
93 parseJSON value =
94 fail $ "Cannot decode Alias from: " ++ BS.unpack (Json.encode value)
97-- JSON for UNION
99instance Json.ToJSON Union where
100 toJSON (Union name comment args cases) =
101 Json.object
102 [ "name" .= name
103 , "comment" .= comment
104 , "args" .= args
105 , "cases" .= cases
106 ]
108instance Json.FromJSON Union where
109 parseJSON (Json.Object obj) =
110 Union
111 <$> obj .: "name"
112 <*> obj .: "comment"
113 <*> obj .: "args"
114 <*> obj .: "cases"
116 parseJSON value =
117 fail $ "Cannot decode Union from: " ++ BS.unpack (Json.encode value)
120-- JSON for VALUE
122instance Json.ToJSON Value where
123 toJSON (Value name comment tipe assocPrec) =
124 Json.object (fields ++ possibleFields)
125 where
126 fields =
127 [ "name" .= name
128 , "comment" .= comment
129 , "type" .= tipe
130 ]
132 possibleFields =
133 case assocPrec of
134 Nothing -> []
135 Just (assoc, prec) ->
136 [ "associativity" .= assoc
137 , "precedence" .= prec
138 ]
141instance Json.FromJSON Value where
142 parseJSON (Json.Object obj) =
143 Value
144 <$> obj .: "name"
145 <*> obj .: "comment"
146 <*> obj .: "type"
147 <*> (liftM2 (,) <$> obj .:? "associativity" <*> obj .:? "precedence")
149 parseJSON value =
150 fail $ "Cannot decode Value from: " ++ BS.unpack (Json.encode value)