@@ -38,7 +38,7 @@ data Package a = Package
38 { pkgMeta :: PackageMeta
38 { pkgMeta :: PackageMeta
39 , pkgVersion :: Version
39 , pkgVersion :: Version
40 , pkgVersionTag :: String
40 , pkgVersionTag :: String
41− , pkgModules :: [RenderedModule]
41+ , pkgModules :: [Module]
42 , pkgBookmarks :: [Bookmark]
42 , pkgBookmarks :: [Bookmark]
43 , pkgResolvedDependencies :: [(PackageName, Version)]
43 , pkgResolvedDependencies :: [(PackageName, Version)]
44 , pkgGithub :: (GithubUser, GithubRepo)
44 , pkgGithub :: (GithubUser, GithubRepo)
@@ -66,52 +66,100 @@ verifyPackage verifiedUser Package{..} =
66packageName :: Package a -> PackageName
66packageName :: Package a -> PackageName
67packageName = bowerName . pkgMeta
67packageName = bowerName . pkgMeta
69−data RenderedModule = RenderedModule
69+data Module = Module
70− { rmName :: String
70+ { modName :: String
71− , rmComments :: Maybe String
71+ , modComments :: Maybe String
72− , rmDeclarations :: [RenderedDeclaration]
72+ , modDeclarations :: [Declaration]
74 deriving (Show, Eq, Ord)
74 deriving (Show, Eq, Ord)
76−data RenderedDeclaration = RenderedDeclaration
76+data Declaration = Declaration
77− { rdTitle :: String
77+ { declTitle :: String
78− , rdComments :: Maybe String
78+ , declComments :: Maybe String
79− , rdCode :: RenderedCode
80− , rdSourceSpan :: Maybe P.SourceSpan
79+ , declSourceSpan :: Maybe P.SourceSpan
81− , rdChildren :: [RenderedChildDeclaration]
80+ , declChildren :: [ChildDeclaration]
82− , rdFixity :: Maybe P.Fixity
81+ , declFixity :: Maybe P.Fixity
82+ , declInfo :: DeclarationInfo
84 deriving (Show, Eq, Ord)
84 deriving (Show, Eq, Ord)
86−data RenderedChildDeclaration = RenderedChildDeclaration
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).
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'.
97+ -- A value declaration, with its type.
99+ = ValueDeclaration P.Type
102+ -- A data/newtype declaration, with the kind of declaration (data or
103+ -- newtype) and its type arguments. Constructors are represented as child
106+ | DataDeclaration P.DataDeclType [(String, Maybe P.Kind)]
109+ -- A data type foreign import, with its kind.
111+ | ExternDataDeclaration P.Kind
114+ -- A type synonym, with its type arguments and its type.
116+ | TypeSynonymDeclaration [(String, Maybe P.Kind)] P.Type
119+ -- A type class, with its type arguments and its superclasses. Instances and
120+ -- members are represented as child declarations.
122+ | TypeClassDeclaration [(String, Maybe P.Kind)] [P.Constraint]
123+ deriving (Show, Eq, Ord)
125+declInfoToString :: DeclarationInfo -> String
126+declInfoToString (ValueDeclaration _) = "value"
127+declInfoToString (DataDeclaration _ _) = "data"
128+declInfoToString (ExternDataDeclaration _) = "externData"
129+declInfoToString (TypeSynonymDeclaration _ _) = "typeSynonym"
130+declInfoToString (TypeClassDeclaration _ _) = "typeClass"
132+data ChildDeclaration = ChildDeclaration
87− { rcdTitle :: String
133+ { cdeclTitle :: String
88− , rcdComments :: Maybe String
134+ , cdeclComments :: Maybe String
89− , rcdSourceSpan :: Maybe P.SourceSpan
135+ , cdeclSourceSpan :: Maybe P.SourceSpan
90− , rcdInfo :: RenderedChildDeclarationInfo
136+ , cdeclInfo :: ChildDeclarationInfo
92 deriving (Show, Eq, Ord)
138 deriving (Show, Eq, Ord)
94−data RenderedChildDeclarationInfo
140+data ChildDeclarationInfo
142+ -- A type instance declaration, with its dependencies and its type.
95− = ChildInstance RenderedCode
144+ = ChildInstance [P.Constraint] P.Type
96− -- ^ A type instance declaration. The rendered code value looks like
145+
97− -- `instance showUnit :: Show Unit`
146+ -- |
147+ -- A data constructor, with its type arguments.
98− | ChildDataConstructor RenderedCode RenderedCode
149+ | ChildDataConstructor [P.Type]
99− -- ^ A data constructor, with rendered code for both how it appears in the
150+
100− -- data declaration, and the constructor with its actual type. For example,
151+ -- |
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
152+ -- A type class member, with its type. Note that the type does not include
105− -- the parent type class declaration (that is, without a constraint), and
153+ -- the type class constraint; this may be added manually if desired. For
106− -- also for its actual type. For example, `add` from `Semiring` might have
154+ -- example, `pure` from `Applicative` would be `forall a. a -> f a`.
107− -- the rendered code values as `add :: a -> a -> a` and
155+ --
108− -- `add :: forall a. (Semiring a) => a -> a -> a` respectively.
156+ | ChildTypeClassMember P.Type
109 deriving (Show, Eq, Ord)
157 deriving (Show, Eq, Ord)
111−childDeclTypeToString :: RenderedChildDeclarationInfo -> String
159+childDeclInfoToString :: ChildDeclarationInfo -> String
112−childDeclTypeToString (ChildInstance _) = "instance"
160+childDeclInfoToString (ChildInstance _ _) = "instance"
113−childDeclTypeToString (ChildDataConstructor _ _) = "dataConstructor"
161+childDeclInfoToString (ChildDataConstructor _) = "dataConstructor"
114−childDeclTypeToString (ChildTypeClassMember _ _) = "typeClassMember"
162+childDeclInfoToString (ChildTypeClassMember _) = "typeClassMember"
116newtype GithubUser
164newtype GithubUser
117 = GithubUser { runGithubUser :: String }
165 = GithubUser { runGithubUser :: String }
@@ -124,9 +172,11 @@ newtype GithubRepo
124data PackageError
172data PackageError
125 = ErrorInPackageMeta BowerError
173 = ErrorInPackageMeta BowerError
126 | InvalidVersion
174 | InvalidVersion
175+ | InvalidDeclarationType String
127 | InvalidChildDeclarationType String
176 | InvalidChildDeclarationType String
128− | InvalidRenderedCode String
129 | InvalidFixity
177 | InvalidFixity
178+ | InvalidKind String
179+ | InvalidDataDeclType String
130 deriving (Show, Eq, Ord)
180 deriving (Show, Eq, Ord)
132type Bookmark = InPackage (P.ModuleName, String)
182type Bookmark = InPackage (P.ModuleName, String)
@@ -165,7 +215,7 @@ asPackage uploader =
165 Package <$> key "packageMeta" asPackageMeta .! ErrorInPackageMeta
215 Package <$> key "packageMeta" asPackageMeta .! ErrorInPackageMeta
166 <*> key "version" asVersion
216 <*> key "version" asVersion
167 <*> key "versionTag" asString
217 <*> key "versionTag" asString
168− <*> key "modules" (eachInArray asRenderedModule)
218+ <*> key "modules" (eachInArray asModule)
169 <*> key "bookmarks" asBookmarks .! ErrorInPackageMeta
219 <*> key "bookmarks" asBookmarks .! ErrorInPackageMeta
170 <*> key "resolvedDependencies" asResolvedDependencies
220 <*> key "resolvedDependencies" asResolvedDependencies
171 <*> key "github" asGithub
221 <*> key "github" asGithub
@@ -203,20 +253,20 @@ parseVersion' str =
203 [(vers, "")] -> Just vers
253 [(vers, "")] -> Just vers
204 _ -> Nothing
254 _ -> Nothing
206−asRenderedModule :: Parse PackageError RenderedModule
256+asModule :: Parse PackageError Module
207−asRenderedModule =
257+asModule =
208− RenderedModule <$> key "name" asString
258+ Module <$> key "name" asString
209− <*> key "comments" (perhaps asString)
259+ <*> key "comments" (perhaps asString)
210− <*> key "declarations" (eachInArray asDeclaration)
260+ <*> key "declarations" (eachInArray asDeclaration)
212−asDeclaration :: Parse PackageError RenderedDeclaration
262+asDeclaration :: Parse PackageError Declaration
213asDeclaration =
263asDeclaration =
214− RenderedDeclaration <$> key "title" asString
264+ Declaration <$> key "title" asString
215− <*> key "comments" (perhaps asString)
265+ <*> key "comments" (perhaps asString)
216− <*> key "code" asRenderedCode .! InvalidRenderedCode
217− <*> key "sourceSpan" (perhaps asSourceSpan)
266+ <*> key "sourceSpan" (perhaps asSourceSpan)
218− <*> key "children" (eachInArray asRenderedChildDeclaration)
267+ <*> key "children" (eachInArray asChildDeclaration)
219− <*> key "fixity" (perhaps asFixity)
268+ <*> key "fixity" (perhaps asFixity)
269+ <*> key "info" asDeclarationInfo
221asFixity :: Parse PackageError P.Fixity
271asFixity :: Parse PackageError P.Fixity
222asFixity = P.Fixity <$> key "associativity" asAssociativity
272asFixity = P.Fixity <$> key "associativity" asAssociativity
@@ -232,32 +282,76 @@ parseAssociativity str = case str of
232asAssociativity :: Parse PackageError P.Associativity
282asAssociativity :: Parse PackageError P.Associativity
233asAssociativity = withString (maybe (Left InvalidFixity) Right . parseAssociativity)
283asAssociativity = withString (maybe (Left InvalidFixity) Right . parseAssociativity)
235−asRenderedChildDeclaration :: Parse PackageError RenderedChildDeclaration
285+asDeclarationInfo :: Parse PackageError DeclarationInfo
236−asRenderedChildDeclaration =
286+asDeclarationInfo = do
287+ ty <- key "declType" asString
290+ ValueDeclaration <$> key "type" asType
292+ DataDeclaration <$> key "dataDeclType" asDataDeclType
293+ <*> key "typeArguments" asTypeArguments
295+ ExternDataDeclaration <$> key "kind" asKind
297+ TypeSynonymDeclaration <$> key "arguments" asTypeArguments
298+ <*> key "type" asType
300+ TypeClassDeclaration <$> key "arguments" asTypeArguments
301+ <*> key "superclasses" (eachInArray asConstraint)
303+ throwCustomError (InvalidDeclarationType other)
305+asTypeArguments :: Parse PackageError [(String, Maybe P.Kind)]
306+asTypeArguments = eachInArray asTypeArgument
308+ asTypeArgument = (,) <$> nth 0 asString <*> nth 1 (perhaps asKind)
310+asKind :: Parse e P.Kind
311+asKind = fromAesonParser
313+asType :: Parse e P.Type
314+asType = fromAesonParser
316+asDataDeclType :: Parse PackageError P.DataDeclType
318+ withString $ \s -> case s of
319+ "data" -> Right P.Data
320+ "newtype" -> Right P.Newtype
321+ other -> Left (InvalidDataDeclType other)
323+asChildDeclaration :: Parse PackageError ChildDeclaration
237− RenderedChildDeclaration <$> key "title" asString
325+ ChildDeclaration <$> key "title" asString
238 <*> key "comments" (perhaps asString)
326 <*> key "comments" (perhaps asString)
239 <*> key "sourceSpan" (perhaps asSourceSpan)
327 <*> key "sourceSpan" (perhaps asSourceSpan)
240− <*> key "info" asRenderedChildDeclarationInfo
328+ <*> key "info" asChildDeclarationInfo
242−asRenderedChildDeclarationInfo :: Parse PackageError RenderedChildDeclarationInfo
330+asChildDeclarationInfo :: Parse PackageError ChildDeclarationInfo
243−asRenderedChildDeclarationInfo = do
331+asChildDeclarationInfo = do
244 ty <- key "declType" asString
332 ty <- key "declType" asString
245 case ty of
333 case ty of
246 "instance" ->
334 "instance" ->
247− ChildInstance <$> codeKey "code"
335+ ChildInstance <$> key "dependencies" (eachInArray asConstraint)
336+ <*> key "type" asType
248 "dataConstructor" ->
337 "dataConstructor" ->
249− ChildDataConstructor <$> codeKey "signature" <*> codeKey "type"
338+ ChildDataConstructor <$> key "arguments" (eachInArray asType)
250 "typeClassMember" ->
339 "typeClassMember" ->
251− ChildTypeClassMember <$> codeKey "contextualType" <*> codeKey "type"
340+ ChildTypeClassMember <$> key "type" asType
253 throwCustomError $ InvalidChildDeclarationType other
342 throwCustomError $ InvalidChildDeclarationType other
255− codeKey k = key k asRenderedCode .! InvalidRenderedCode
257asSourcePos :: Parse e P.SourcePos
344asSourcePos :: Parse e P.SourcePos
258asSourcePos = P.SourcePos <$> nth 0 asIntegral
345asSourcePos = P.SourcePos <$> nth 0 asIntegral
259 <*> nth 1 asIntegral
346 <*> nth 1 asIntegral
348+asConstraint :: Parse PackageError P.Constraint
349+asConstraint = (,) <$> nth 0 asQualifiedProperName
350+ <*> nth 1 (eachInArray asType)
352+asQualifiedProperName :: Parse e (P.Qualified P.ProperName)
353+asQualifiedProperName = fromAesonParser
261asBookmarks :: Parse BowerError [Bookmark]
355asBookmarks :: Parse BowerError [Bookmark]
262asBookmarks = eachInArray asBookmark
356asBookmarks = eachInArray asBookmark
@@ -307,38 +401,48 @@ instance A.ToJSON a => A.ToJSON (Package a) where
307instance A.ToJSON NotYetKnown where
401instance A.ToJSON NotYetKnown where
308 toJSON _ = A.Null
402 toJSON _ = A.Null
310−instance A.ToJSON RenderedModule where
404+instance A.ToJSON Module where
311− toJSON RenderedModule{..} =
405+ toJSON Module{..} =
312− A.object [ "name" .= rmName
406+ A.object [ "name" .= modName
313− , "comments" .= rmComments
407+ , "comments" .= modComments
314− , "declarations" .= rmDeclarations
408+ , "declarations" .= modDeclarations
317−instance A.ToJSON RenderedDeclaration where
411+instance A.ToJSON Declaration where
318− toJSON RenderedDeclaration{..} =
412+ toJSON Declaration{..} =
319− A.object [ "title" .= rdTitle
413+ A.object [ "title" .= declTitle
320− , "comments" .= rdComments
414+ , "comments" .= declComments
322− , "sourceSpan" .= rdSourceSpan
415+ , "sourceSpan" .= declSourceSpan
323− , "children" .= rdChildren
416+ , "children" .= declChildren
324− , "fixity" .= rdFixity
417+ , "fixity" .= declFixity
418+ , "info" .= declInfo
327−instance A.ToJSON RenderedChildDeclaration where
421+instance A.ToJSON ChildDeclaration where
328− toJSON RenderedChildDeclaration{..} =
422+ toJSON ChildDeclaration{..} =
329− A.object [ "title" .= rcdTitle
423+ A.object [ "title" .= cdeclTitle
330− , "comments" .= rcdComments
424+ , "comments" .= cdeclComments
331− , "sourceSpan" .= rcdSourceSpan
425+ , "sourceSpan" .= cdeclSourceSpan
332− , "info" .= rcdInfo
426+ , "info" .= cdeclInfo
335−instance A.ToJSON RenderedChildDeclarationInfo where
429+instance A.ToJSON DeclarationInfo where
336− toJSON info = A.object $ "declType" .= childDeclTypeToString info : props
430+ toJSON info = A.object $ "declType" .= declInfoToString info : props
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]
439+instance A.ToJSON ChildDeclarationInfo where
440+ toJSON info = A.object $ "declType" .= childDeclInfoToString info : props
338 props = case info of
442 props = case info of
339− ChildInstance code -> ["code" .= code]
443+ ChildInstance deps ty -> ["dependencies" .= deps, "type" .= ty]
340− ChildDataConstructor sig ty -> ["signature" .= sig, "type" .= ty]
444+ ChildDataConstructor args -> ["arguments" .= args]
341− ChildTypeClassMember cTy ty -> ["contextualType" .= cTy, "type" .= ty]
445+ ChildTypeClassMember ty -> ["type" .= ty]
343instance A.ToJSON GithubUser where
447instance A.ToJSON GithubUser where
344 toJSON = A.toJSON . runGithubUser
448 toJSON = A.toJSON . runGithubUser