@@ -38,7 +38,7 @@ data Package a = Package
3838 { pkgMeta :: PackageMeta
3939 , pkgVersion :: Version
4040 , pkgVersionTag :: String
41− , pkgModules :: [RenderedModule]
41+ , pkgModules :: [Module]
4242 , pkgBookmarks :: [Bookmark]
4343 , pkgResolvedDependencies :: [(PackageName, Version)]
4444 , pkgGithub :: (GithubUser, GithubRepo)
@@ -66,52 +66,100 @@ verifyPackage verifiedUser Package{..} =
6666packageName :: Package a -> PackageName
6767packageName = bowerName . pkgMeta
6868
69−data RenderedModule = RenderedModule
70− { rmName :: String
71− , rmComments :: Maybe String
72− , rmDeclarations :: [RenderedDeclaration]
69+data Module = Module
70+ { modName :: String
71+ , modComments :: Maybe String
72+ , modDeclarations :: [Declaration]
7373 }
7474 deriving (Show, Eq, Ord)
7575
76−data RenderedDeclaration = RenderedDeclaration
77− { rdTitle :: String
78− , rdComments :: Maybe String
79− , rdCode :: RenderedCode
80− , rdSourceSpan :: Maybe P.SourceSpan
81− , rdChildren :: [RenderedChildDeclaration]
82− , rdFixity :: Maybe P.Fixity
76+data Declaration = Declaration
77+ { declTitle :: String
78+ , declComments :: Maybe String
79+ , declSourceSpan :: Maybe P.SourceSpan
80+ , declChildren :: [ChildDeclaration]
81+ , declFixity :: Maybe P.Fixity
82+ , declInfo :: DeclarationInfo
8383 }
8484 deriving (Show, Eq, Ord)
8585
86−data RenderedChildDeclaration = RenderedChildDeclaration
87− { rcdTitle :: String
88− , rcdComments :: Maybe String
89− , rcdSourceSpan :: Maybe P.SourceSpan
90− , rcdInfo :: RenderedChildDeclarationInfo
86+-- |
87+-- A value of this type contains information that is specific to a particular
88+-- kind of declaration (as opposed to information which exists in all kinds of
89+-- declarations, which goes into the 'Declaration' type directly).
90+--
91+-- Many of the constructors are very similar to their equivalents in the real
92+-- PureScript AST, except that they have their name elided, since this is
93+-- already available via the rdTitle field of 'Declaration'.
94+--
95+data DeclarationInfo
96+ -- |
97+ -- A value declaration, with its type.
98+ --
99+ = ValueDeclaration P.Type
100+
101+ -- |
102+ -- A data/newtype declaration, with the kind of declaration (data or
103+ -- newtype) and its type arguments. Constructors are represented as child
104+ -- declarations.
105+ --
106+ | DataDeclaration P.DataDeclType [(String, Maybe P.Kind)]
107+
108+ -- |
109+ -- A data type foreign import, with its kind.
110+ --
111+ | ExternDataDeclaration P.Kind
112+
113+ -- |
114+ -- A type synonym, with its type arguments and its type.
115+ --
116+ | TypeSynonymDeclaration [(String, Maybe P.Kind)] P.Type
117+
118+ -- |
119+ -- A type class, with its type arguments and its superclasses. Instances and
120+ -- members are represented as child declarations.
121+ --
122+ | TypeClassDeclaration [(String, Maybe P.Kind)] [P.Constraint]
123+ deriving (Show, Eq, Ord)
124+
125+declInfoToString :: DeclarationInfo -> String
126+declInfoToString (ValueDeclaration _) = "value"
127+declInfoToString (DataDeclaration _ _) = "data"
128+declInfoToString (ExternDataDeclaration _) = "externData"
129+declInfoToString (TypeSynonymDeclaration _ _) = "typeSynonym"
130+declInfoToString (TypeClassDeclaration _ _) = "typeClass"
131+
132+data ChildDeclaration = ChildDeclaration
133+ { cdeclTitle :: String
134+ , cdeclComments :: Maybe String
135+ , cdeclSourceSpan :: Maybe P.SourceSpan
136+ , cdeclInfo :: ChildDeclarationInfo
91137 }
92138 deriving (Show, Eq, Ord)
93139
94−data RenderedChildDeclarationInfo
95− = ChildInstance RenderedCode
96− -- ^ A type instance declaration. The rendered code value looks like
97− -- `instance showUnit :: Show Unit`
98− | ChildDataConstructor RenderedCode RenderedCode
99− -- ^ A data constructor, with rendered code for both how it appears in the
100− -- data declaration, and the constructor with its actual type. For example,
101− -- the constructor `Just` from the type `Maybe` might have the rendered
102− -- code values being `Just a` and `Just :: forall a. a -> Maybe a`.
103− | ChildTypeClassMember RenderedCode RenderedCode
104− -- ^ A type class member, with rendered code for its type in the context of
105− -- the parent type class declaration (that is, without a constraint), and
106− -- also for its actual type. For example, `add` from `Semiring` might have
107− -- the rendered code values as `add :: a -> a -> a` and
108− -- `add :: forall a. (Semiring a) => a -> a -> a` respectively.
140+data ChildDeclarationInfo
141+ -- |
142+ -- A type instance declaration, with its dependencies and its type.
143+ --
144+ = ChildInstance [P.Constraint] P.Type
145+
146+ -- |
147+ -- A data constructor, with its type arguments.
148+ --
149+ | ChildDataConstructor [P.Type]
150+
151+ -- |
152+ -- A type class member, with its type. Note that the type does not include
153+ -- the type class constraint; this may be added manually if desired. For
154+ -- example, `pure` from `Applicative` would be `forall a. a -> f a`.
155+ --
156+ | ChildTypeClassMember P.Type
109157 deriving (Show, Eq, Ord)
110158
111−childDeclTypeToString :: RenderedChildDeclarationInfo -> String
112−childDeclTypeToString (ChildInstance _) = "instance"
113−childDeclTypeToString (ChildDataConstructor _ _) = "dataConstructor"
114−childDeclTypeToString (ChildTypeClassMember _ _) = "typeClassMember"
159+childDeclInfoToString :: ChildDeclarationInfo -> String
160+childDeclInfoToString (ChildInstance _ _) = "instance"
161+childDeclInfoToString (ChildDataConstructor _) = "dataConstructor"
162+childDeclInfoToString (ChildTypeClassMember _) = "typeClassMember"
115163
116164newtype GithubUser
117165 = GithubUser { runGithubUser :: String }
@@ -124,9 +172,11 @@ newtype GithubRepo
124172data PackageError
125173 = ErrorInPackageMeta BowerError
126174 | InvalidVersion
175+ | InvalidDeclarationType String
127176 | InvalidChildDeclarationType String
128− | InvalidRenderedCode String
129177 | InvalidFixity
178+ | InvalidKind String
179+ | InvalidDataDeclType String
130180 deriving (Show, Eq, Ord)
131181
132182type Bookmark = InPackage (P.ModuleName, String)
@@ -165,7 +215,7 @@ asPackage uploader =
165215 Package <$> key "packageMeta" asPackageMeta .! ErrorInPackageMeta
166216 <*> key "version" asVersion
167217 <*> key "versionTag" asString
168− <*> key "modules" (eachInArray asRenderedModule)
218+ <*> key "modules" (eachInArray asModule)
169219 <*> key "bookmarks" asBookmarks .! ErrorInPackageMeta
170220 <*> key "resolvedDependencies" asResolvedDependencies
171221 <*> key "github" asGithub
@@ -203,20 +253,20 @@ parseVersion' str =
203253 [(vers, "")] -> Just vers
204254 _ -> Nothing
205255
206−asRenderedModule :: Parse PackageError RenderedModule
207−asRenderedModule =
208− RenderedModule <$> key "name" asString
209− <*> key "comments" (perhaps asString)
210− <*> key "declarations" (eachInArray asDeclaration)
256+asModule :: Parse PackageError Module
257+asModule =
258+ Module <$> key "name" asString
259+ <*> key "comments" (perhaps asString)
260+ <*> key "declarations" (eachInArray asDeclaration)
211261
212−asDeclaration :: Parse PackageError RenderedDeclaration
262+asDeclaration :: Parse PackageError Declaration
213263asDeclaration =
214− RenderedDeclaration <$> key "title" asString
215− <*> key "comments" (perhaps asString)
216− <*> key "code" asRenderedCode .! InvalidRenderedCode
217− <*> key "sourceSpan" (perhaps asSourceSpan)
218− <*> key "children" (eachInArray asRenderedChildDeclaration)
219− <*> key "fixity" (perhaps asFixity)
264+ Declaration <$> key "title" asString
265+ <*> key "comments" (perhaps asString)
266+ <*> key "sourceSpan" (perhaps asSourceSpan)
267+ <*> key "children" (eachInArray asChildDeclaration)
268+ <*> key "fixity" (perhaps asFixity)
269+ <*> key "info" asDeclarationInfo
220270
221271asFixity :: Parse PackageError P.Fixity
222272asFixity = P.Fixity <$> key "associativity" asAssociativity
@@ -232,32 +282,76 @@ parseAssociativity str = case str of
232282asAssociativity :: Parse PackageError P.Associativity
233283asAssociativity = withString (maybe (Left InvalidFixity) Right . parseAssociativity)
234284
235−asRenderedChildDeclaration :: Parse PackageError RenderedChildDeclaration
236−asRenderedChildDeclaration =
237− RenderedChildDeclaration <$> key "title" asString
285+asDeclarationInfo :: Parse PackageError DeclarationInfo
286+asDeclarationInfo = do
287+ ty <- key "declType" asString
288+ case ty of
289+ "value" ->
290+ ValueDeclaration <$> key "type" asType
291+ "data" ->
292+ DataDeclaration <$> key "dataDeclType" asDataDeclType
293+ <*> key "typeArguments" asTypeArguments
294+ "externData" ->
295+ ExternDataDeclaration <$> key "kind" asKind
296+ "typeSynonym" ->
297+ TypeSynonymDeclaration <$> key "arguments" asTypeArguments
298+ <*> key "type" asType
299+ "typeClass" ->
300+ TypeClassDeclaration <$> key "arguments" asTypeArguments
301+ <*> key "superclasses" (eachInArray asConstraint)
302+ other ->
303+ throwCustomError (InvalidDeclarationType other)
304+
305+asTypeArguments :: Parse PackageError [(String, Maybe P.Kind)]
306+asTypeArguments = eachInArray asTypeArgument
307+ where
308+ asTypeArgument = (,) <$> nth 0 asString <*> nth 1 (perhaps asKind)
309+
310+asKind :: Parse e P.Kind
311+asKind = fromAesonParser
312+
313+asType :: Parse e P.Type
314+asType = fromAesonParser
315+
316+asDataDeclType :: Parse PackageError P.DataDeclType
317+asDataDeclType =
318+ withString $ \s -> case s of
319+ "data" -> Right P.Data
320+ "newtype" -> Right P.Newtype
321+ other -> Left (InvalidDataDeclType other)
322+
323+asChildDeclaration :: Parse PackageError ChildDeclaration
324+asChildDeclaration =
325+ ChildDeclaration <$> key "title" asString
238326 <*> key "comments" (perhaps asString)
239327 <*> key "sourceSpan" (perhaps asSourceSpan)
240− <*> key "info" asRenderedChildDeclarationInfo
328+ <*> key "info" asChildDeclarationInfo
241329
242−asRenderedChildDeclarationInfo :: Parse PackageError RenderedChildDeclarationInfo
243−asRenderedChildDeclarationInfo = do
330+asChildDeclarationInfo :: Parse PackageError ChildDeclarationInfo
331+asChildDeclarationInfo = do
244332 ty <- key "declType" asString
245333 case ty of
246334 "instance" ->
247− ChildInstance <$> codeKey "code"
335+ ChildInstance <$> key "dependencies" (eachInArray asConstraint)
336+ <*> key "type" asType
248337 "dataConstructor" ->
249− ChildDataConstructor <$> codeKey "signature" <*> codeKey "type"
338+ ChildDataConstructor <$> key "arguments" (eachInArray asType)
250339 "typeClassMember" ->
251− ChildTypeClassMember <$> codeKey "contextualType" <*> codeKey "type"
340+ ChildTypeClassMember <$> key "type" asType
252341 other ->
253342 throwCustomError $ InvalidChildDeclarationType other
254− where
255− codeKey k = key k asRenderedCode .! InvalidRenderedCode
256343
257344asSourcePos :: Parse e P.SourcePos
258345asSourcePos = P.SourcePos <$> nth 0 asIntegral
259346 <*> nth 1 asIntegral
260347
348+asConstraint :: Parse PackageError P.Constraint
349+asConstraint = (,) <$> nth 0 asQualifiedProperName
350+ <*> nth 1 (eachInArray asType)
351+
352+asQualifiedProperName :: Parse e (P.Qualified P.ProperName)
353+asQualifiedProperName = fromAesonParser
354+
261355asBookmarks :: Parse BowerError [Bookmark]
262356asBookmarks = eachInArray asBookmark
263357
@@ -307,38 +401,48 @@ instance A.ToJSON a => A.ToJSON (Package a) where
307401instance A.ToJSON NotYetKnown where
308402 toJSON _ = A.Null
309403
310−instance A.ToJSON RenderedModule where
311− toJSON RenderedModule{..} =
312− A.object [ "name" .= rmName
313− , "comments" .= rmComments
314− , "declarations" .= rmDeclarations
404+instance A.ToJSON Module where
405+ toJSON Module{..} =
406+ A.object [ "name" .= modName
407+ , "comments" .= modComments
408+ , "declarations" .= modDeclarations
315409 ]
316410
317−instance A.ToJSON RenderedDeclaration where
318− toJSON RenderedDeclaration{..} =
319− A.object [ "title" .= rdTitle
320− , "comments" .= rdComments
321− , "code" .= rdCode
322− , "sourceSpan" .= rdSourceSpan
323− , "children" .= rdChildren
324− , "fixity" .= rdFixity
411+instance A.ToJSON Declaration where
412+ toJSON Declaration{..} =
413+ A.object [ "title" .= declTitle
414+ , "comments" .= declComments
415+ , "sourceSpan" .= declSourceSpan
416+ , "children" .= declChildren
417+ , "fixity" .= declFixity
418+ , "info" .= declInfo
325419 ]
326420
327−instance A.ToJSON RenderedChildDeclaration where
328− toJSON RenderedChildDeclaration{..} =
329− A.object [ "title" .= rcdTitle
330− , "comments" .= rcdComments
331− , "sourceSpan" .= rcdSourceSpan
332− , "info" .= rcdInfo
421+instance A.ToJSON ChildDeclaration where
422+ toJSON ChildDeclaration{..} =
423+ A.object [ "title" .= cdeclTitle
424+ , "comments" .= cdeclComments
425+ , "sourceSpan" .= cdeclSourceSpan
426+ , "info" .= cdeclInfo
333427 ]
334428
335−instance A.ToJSON RenderedChildDeclarationInfo where
336− toJSON info = A.object $ "declType" .= childDeclTypeToString info : props
429+instance A.ToJSON DeclarationInfo where
430+ toJSON info = A.object $ "declType" .= declInfoToString info : props
431+ where
432+ props = case info of
433+ ValueDeclaration ty -> ["type" .= ty]
434+ DataDeclaration ty args -> ["dataDeclType" .= ty, "typeArguments" .= args]
435+ ExternDataDeclaration kind -> ["kind" .= kind]
436+ TypeSynonymDeclaration args ty -> ["arguments" .= args, "type" .= ty]
437+ TypeClassDeclaration args super -> ["arguments" .= args, "superclasses" .= super]
438+
439+instance A.ToJSON ChildDeclarationInfo where
440+ toJSON info = A.object $ "declType" .= childDeclInfoToString info : props
337441 where
338442 props = case info of
339− ChildInstance code -> ["code" .= code]
340− ChildDataConstructor sig ty -> ["signature" .= sig, "type" .= ty]
341− ChildTypeClassMember cTy ty -> ["contextualType" .= cTy, "type" .= ty]
443+ ChildInstance deps ty -> ["dependencies" .= deps, "type" .= ty]
444+ ChildDataConstructor args -> ["arguments" .= args]
445+ ChildTypeClassMember ty -> ["type" .= ty]
342446
343447instance A.ToJSON GithubUser where
344448 toJSON = A.toJSON . runGithubUser