Commit 658fe135 authored by Camil Staps's avatar Camil Staps 🚀

DB documentation; add builtins

parent 4675ccc3
......@@ -28,6 +28,7 @@ from TypeTree import :: TypeTree
, types :: !TypeTree Index
, core :: ![Index]
, apps :: ![Index]
, builtins :: ![Index]
, library_map :: !Map Name [Index]
, module_map :: !Map Name [Index]
}
......@@ -212,9 +213,10 @@ getIndex :: !Index !*CloogleDB -> *(Entry CloogleEntry AnnotationKey Annotation,
getIndices :: ![Index] !*CloogleDB -> *([Entry CloogleEntry AnnotationKey Annotation], *CloogleDB)
filterDB :: (CloogleEntry -> Bool) !*CloogleDB -> *CloogleDB
filterLocations :: (Location -> Bool) !*CloogleDB -> *CloogleDB
excludeCore :: !*CloogleDB -> *CloogleDB
excludeApps :: !*CloogleDB -> *CloogleDB
excludeBuiltins :: !*CloogleDB -> *CloogleDB
includeBuiltins :: !*CloogleDB -> *CloogleDB
filterLibraries :: ![Name] !*CloogleDB -> *CloogleDB
filterModules :: ![Name] !*CloogleDB -> *CloogleDB
filterName :: !String !*CloogleDB -> *CloogleDB
......
......@@ -37,7 +37,7 @@ import Type
import Cloogle
from DB import :: DB, :: Index, :: Entry{..},
from DB import :: DB, :: Index, :: Entry{..}, :: IndexSearchMode(..),
instance == Index, instance < Index
import qualified DB
import Doc
......@@ -225,12 +225,13 @@ syncDB :: !Int !*CloogleDB -> *CloogleDB
syncDB _ db = db
saveDB :: *CloogleDB *File -> *(*CloogleDB, *File)
saveDB wrapper=:{db,name_ngrams,types,core,apps,library_map,module_map} f
saveDB wrapper=:{db,name_ngrams,types,core,apps,builtins,library_map,module_map} f
# (db,f) = 'DB'.saveDB db f
# f = write name_ngrams f
# f = write types f
# f = write core f
# f = write apps f
# f = write builtins f
# f = write library_map f
# f = write module_map f
= ({wrapper & db=db}, f)
......@@ -250,6 +251,8 @@ openDB f
| not ok = (Nothing, f)
# ((ok,apps),f) = appFst isJustU $ read f
| not ok = (Nothing, f)
# ((ok,builtins),f) = appFst isJustU $ read f
| not ok = (Nothing, f)
# ((ok,library_map),f) = appFst isJustU $ read f
| not ok = (Nothing, f)
# ((ok,module_map),f) = appFst isJustU $ read f
......@@ -260,6 +263,7 @@ openDB f
, types=fromJust types
, core=fromJust core
, apps=fromJust apps
, builtins=fromJust builtins
, library_map=fromJust library_map
, module_map=fromJust module_map
}, f)
......@@ -284,14 +288,6 @@ getIndices idxs wrap=:{db}
filterDB :: (CloogleEntry -> Bool) !*CloogleDB -> *CloogleDB
filterDB f db = {db & db = 'DB'.search (\v -> (f v, [])) db.db}
filterLocations :: (Location -> Bool) !*CloogleDB -> *CloogleDB
filterLocations f db =
{ db
& db = 'DB'.search (\v -> case getLocation v of
Nothing -> (True, [])
Just loc -> (f loc, [])) db.db
}
excludeCore :: !*CloogleDB -> *CloogleDB
excludeCore wrap=:{db,core}
# db = 'DB'.unsearchIndices core db
......@@ -302,30 +298,40 @@ excludeApps wrap=:{db,apps}
# db = 'DB'.unsearchIndices apps db
= {wrap & db=db}
excludeBuiltins :: !*CloogleDB -> *CloogleDB
excludeBuiltins wrap=:{db,builtins}
# db = 'DB'.unsearchIndices builtins db
= {wrap & db=db}
includeBuiltins :: !*CloogleDB -> *CloogleDB
includeBuiltins wrap=:{db,builtins}
# db = 'DB'.searchIndices AddExcluded (map (flip tuple []) builtins) db
= {wrap & db=db}
filterLibraries :: ![Name] !*CloogleDB -> *CloogleDB
filterLibraries ss wrap=:{db,library_map}
# db = 'DB'.searchIndices (map (flip tuple []) idxs) db
# db = 'DB'.searchIndices Intersect (map (flip tuple []) idxs) db
= {wrap & db=db}
where
idxs = foldr merge [] $ catMaybes $ map (flip get library_map) ss
filterModules :: ![Name] !*CloogleDB -> *CloogleDB
filterModules ss wrap=:{db,module_map}
# db = 'DB'.searchIndices (map (flip tuple []) idxs) db
# db = 'DB'.searchIndices Intersect (map (flip tuple []) idxs) db
= {wrap & db=db}
where
idxs = foldr merge [] $ catMaybes $ map (flip get module_map) ss
filterName :: !String !*CloogleDB -> *CloogleDB
filterName s wrap=:{db,name_ngrams}
# db = 'DB'.searchIndices indices db
# db = 'DB'.searchIndices Intersect indices db
= {wrap & db=db}
where
indices = [(i,[(MatchingNGrams,IntAnnot n)]) \\ (i,n) <- 'NGrams'.search s name_ngrams]
filterUnifying :: !Type !*CloogleDB -> *CloogleDB
filterUnifying t wrap=:{db,types}
# db = 'DB'.searchIndices idxs db
# db = 'DB'.searchIndices Intersect idxs db
= {wrap & db=db}
where
idxs = sortBy ((<) `on` fst) [(idx,annot)
......
......@@ -9,7 +9,7 @@ import CloogleDB
:: TemporaryDB
newTemporaryDb :: TemporaryDB
finaliseDb :: !TemporaryDB -> *CloogleDB
finaliseDb :: ![CloogleEntry] !TemporaryDB -> *CloogleDB
/**
* Find all modules that could be indexed
......@@ -84,9 +84,9 @@ findModuleContents :: !Bool !String !*World
/**
* Transform the constructors of an algebraic data type into plain functions.
*/
constructor_functions :: TypeDefEntry -> [(Name, FunctionEntry)]
constructor_functions :: TypeDefEntry -> [FunctionEntry]
/**
* Transform the record fields of a record type into plain functions.
*/
record_functions :: TypeDefEntry -> [(Name, FunctionEntry)]
record_functions :: TypeDefEntry -> [FunctionEntry]
......@@ -111,8 +111,8 @@ newTemporaryDb
, temp_modules = []
}
finaliseDb :: !TemporaryDB -> *'CDB'.CloogleDB
finaliseDb tdb
finaliseDb :: ![CloogleEntry] !TemporaryDB -> *'CDB'.CloogleDB
finaliseDb extra tdb
# db = newDB entries
# (names,db) = collectNames db
# name_ngrams = foldr (uncurry index) (newNGramIndex 3 True) names
......@@ -124,6 +124,7 @@ finaliseDb tdb
\\ (i,FunctionEntry fe) <- entridxs, Just t <- [fe.fe_type <|> (docType =<< fe.fe_documentation)]]
, core = coreidxs
, apps = appidxs
, builtins = []
, library_map = libmap
, module_map = modmap
}
......@@ -133,6 +134,7 @@ where
Just loc -> [('CDB'.getName loc, i):ivs]) []
entries =
extra ++
[FunctionEntry fun \\ funs <- tdb.temp_functions, fun <- funs] ++
map TypeDefEntry typedefs ++
[ModuleEntry mod \\ mod <- tdb.temp_modules] ++
......@@ -229,18 +231,17 @@ indexModule :: !Bool !String !'CDB'.Module !'CDB'.Library
indexModule include_locals root mod lib modf db w
#! (functions,macros,generics,typedefs,clss,insts,derivs,clsderivs,(modname,modentry),w)
= findModuleContents include_locals (root </> lib </> mkdir mod) w
#! typedefs = [{td & tde_loc=castLoc modname loc} \\ (loc,td) <- typedefs]
#! lib = lib % (0, size lib - size modname + size mod - 1)
#! db =
{ db
& temp_functions =
[ [{f & fe_loc=castLoc modname loc} \\ (loc,f) <- functions ++ macros ++ generics]
, [{f & fe_loc=castLoc modname {loc & name=Just n}}
\\ (loc,td) <- typedefs
, (n,f) <- constructor_functions td ++ record_functions td]
, [f \\ td <- typedefs, f <- constructor_functions td ++ record_functions td]
: db.temp_functions
]
, temp_classes = [[({ce & ce_loc=castLoc modname loc}, fs) \\ (loc,ce,fs) <- clss]:db.temp_classes]
, temp_types = [[{td & tde_loc=castLoc modname loc} \\ (loc,td) <- typedefs]:db.temp_types]
, temp_types = [typedefs:db.temp_types]
, temp_instances = [castLocThd3 modname insts:db.temp_instances]
, temp_derivations = [map (appSnd (castLocThd3 modname)) derivs:db.temp_derivations]
, temp_class_derivations = [castLocFrth modname clsderivs:db.temp_class_derivations]
......@@ -572,18 +573,18 @@ where
setLine True pos loc = {loc & dcl_line=toLine pos}
setLine False pos loc = {loc & icl_line=toLine pos}
constructor_functions :: 'CDB'.TypeDefEntry -> [('CDB'.Name, 'CDB'.FunctionEntry)]
constructor_functions etd
= [(c,
{ zero
& fe_kind=Constructor
, fe_type=Just f
, fe_representation=Just $ concat $ [c] ++ print_prio p ++ [" :: "] ++ print False f
, fe_priority=p
, fe_documentation=constructorToFunctionDoc <$> doc
})
\\ (c,f,p) <- 'T'.constructorsToFunctions ('CDB'.getTypeDef etd)
& doc <- cons_doc]
constructor_functions :: 'CDB'.TypeDefEntry -> ['CDB'.FunctionEntry]
constructor_functions etd = [
{ zero
& fe_loc='CDB'.setName c etd.tde_loc
, fe_kind=Constructor
, fe_type=Just f
, fe_representation=Just $ concat $ [c] ++ print_prio p ++ [" :: "] ++ print False f
, fe_priority=p
, fe_documentation=constructorToFunctionDoc <$> doc
}
\\ (c,f,p) <- 'T'.constructorsToFunctions ('CDB'.getTypeDef etd)
& doc <- cons_doc]
where
print_prio :: (Maybe 'T'.Priority) -> [String]
print_prio Nothing = []
......@@ -591,17 +592,17 @@ where
cons_doc = fromMaybe [] (docConstructors =<< 'CDB'.getTypeDefDoc etd) ++ repeat Nothing
record_functions :: 'CDB'.TypeDefEntry -> [('CDB'.Name, 'CDB'.FunctionEntry)]
record_functions etd
= [(f,
{ zero
& fe_kind=RecordField
, fe_type=Just t
, fe_representation=Just $ concat [".", f, " :: ":print False t]
, fe_documentation=(\d -> {FunctionDoc | gDefault{|*|} & description=Just d}) <$> doc
})
\\ (f,t) <- 'T'.recordsToFunctions ('CDB'.getTypeDef etd)
& doc <- field_doc]
record_functions :: 'CDB'.TypeDefEntry -> ['CDB'.FunctionEntry]
record_functions etd = [
{ zero
& fe_loc='CDB'.setName f etd.tde_loc
, fe_kind=RecordField
, fe_type=Just t
, fe_representation=Just $ concat [".", f, " :: ":print False t]
, fe_documentation=(\d -> {FunctionDoc | gDefault{|*|} & description=Just d}) <$> doc
}
\\ (f,t) <- 'T'.recordsToFunctions ('CDB'.getTypeDef etd)
& doc <- field_doc]
where
field_doc = fromMaybe [] (docFields =<< 'CDB'.getTypeDefDoc etd) ++ repeat Nothing
......
......@@ -33,16 +33,68 @@ derive JSONDecode Index
instance == Index
instance < Index
/**
* Create a new database from a list of entries.
*/
newDB :: [v] -> *DB v ak a
/**
* Save the database to a file.
*/
saveDB :: *(DB v ak a) *File -> *(*DB v ak a, *File) | JSONEncode{|*|} v
/**
* Open a database from a file.
*/
openDB :: !*File -> *(!Maybe (*DB v ak a), !*File) | JSONDecode{|*|} v
newDB :: [v] -> *DB v ak a
/**
* Return all entries (whether they have been excluded or not).
*/
allEntries :: *(DB v ak a) -> *([v], *DB v ak a)
/**
* Get all entries that are still included, and their annotations.
*/
getEntries :: *(DB v ak a) -> *([(v, Map ak a)], *DB v ak a)
/**
* An in-place map over all entries (also the excluded ones).
*/
mapInPlace :: (v -> v) *(DB v ak a) -> *(DB v ak a)
scan :: (Index v t -> t) t *(DB v ak a) -> *(t, *(DB v ak a))
/**
* Linear search for entries. The search function returns whether the entry
* should be included and which annotations should be added (if any). Excluded
* entries are ignored.
*/
search :: (v -> (Bool, [(ak, a)])) *(DB v ak a) -> *DB v ak a | ==, < ak
searchIndices :: ![(!Index, ![(!ak, !a)])] !*(DB v ak a) -> *DB v ak a | ==, < ak
/**
* When searching on specific indices, two modes are available.
* `Intersect` behaves like {{`search`}}.
*/
:: IndexSearchMode
= Intersect //* Only consider included entries
| AddExcluded //* Re-include matching entries; don't remove non-matching entries
/**
* Like {{`search`}}, but search for specific indices.
*/
searchIndices :: !IndexSearchMode ![(!Index, ![(!ak, !a)])] !*(DB v ak a) -> *DB v ak a | ==, < ak
/**
* Exclude a list of indices.
*/
unsearchIndices :: ![Index] !*(DB v ak a) -> *DB v ak a
/**
* Get an entry and its annotations.
* Also see {{`getIndices`}}.
*/
getIndex :: !Index !*(DB v ak a) -> *(!Entry v ak a, !*(DB v ak a))
/**
* Like {{`getIndex`}}, but for a list of indices.
*/
getIndices :: ![Index] !*(DB v ak a) -> *(![Entry v ak a], !*(DB v ak a))
......@@ -22,6 +22,9 @@ JSONDecode{|Index|} _ l = (Nothing, l)
instance == Index where == (Index a) (Index b) = a == b
instance < Index where < (Index a) (Index b) = a < b
newDB :: [v] -> *DB v ak a
newDB vs = DB {{value=v,included=True,annotations=newMap} \\ v <- vs}
saveDB :: *(DB v ak a) *File -> *(*DB v ak a, *File) | JSONEncode{|*|} v
saveDB (DB db) f
# (s,db) = usize db
......@@ -57,9 +60,6 @@ where
(Nothing, f) -> (Nothing, f)
(Just es, f) -> (Just [e:es], f)
newDB :: [v] -> *DB v ak a
newDB vs = DB {{value=v,included=True,annotations=newMap} \\ v <- vs}
allEntries :: *(DB v ak a) -> *([v], *DB v ak a)
allEntries (DB db)
# (s,db) = usize db
......@@ -96,18 +96,6 @@ where
# (e,es) = es![i]
= upd (i-1) {es & [i]={e & value=f e.value}}
scan :: (Index v t -> t) t *(DB v ak a) -> *(t, *(DB v ak a))
scan f x (DB db)
# (s,db) = usize db
# (x,db) = scan (s-1) x db
= (x,DB db)
where
scan -1 x db = (x,db)
scan i x db
# (e,db) = db![i]
# x = f (Index i) e.value x
= scan (i-1) x db
search :: (v -> (Bool, [(ak, a)])) *(DB v ak a) -> *DB v ak a | ==, < ak
search f (DB db)
# (s,db) = usize db
......@@ -125,8 +113,8 @@ where
, annotations=foldr (uncurry put) e.annotations annotations
}}
searchIndices :: ![(!Index, ![(!ak, !a)])] !*(DB v ak a) -> *DB v ak a | ==, < ak
searchIndices idxs (DB db)
searchIndices :: !IndexSearchMode ![(!Index, ![(!ak, !a)])] !*(DB v ak a) -> *DB v ak a | ==, < ak
searchIndices mode idxs (DB db)
# (s,db) = usize db
# db = upd 0 (s-1) idxs db
= (DB db)
......@@ -136,14 +124,16 @@ where
| i > s = es
upd i s [] es
# (e,es) = es![i]
= upd (i+1) s [] {es & [i]={e & included=False}}
= upd (i+1) s [] {es & [i]={e & included=mode=:AddExcluded}}
upd i s allidxs=:[match=:(Index idx,annots):idxs] es
# (e,es) = es![i]
# e & included = e.included && include
# e & included = case mode of
Intersect -> e.included && match
AddExcluded -> e.included || match
# e & annotations = if e.included (foldr (uncurry put) e.annotations annots) e.annotations
= upd (i+1) s (if include idxs allidxs) {es & [i]=e}
= upd (i+1) s (if match idxs allidxs) {es & [i]=e}
where
include = i == idx
match = i == idx
unsearchIndices :: ![Index] !*(DB v ak a) -> *DB v ak a
unsearchIndices idxs (DB db)
......
......@@ -39,7 +39,7 @@ search {unify,name,className,typeName,modules,libraries,page,include_builtins,in
# cdb = case modules of
Just ms -> filterModules ms cdb
Nothing -> cdb
# cdb = if include_builtins id (filterLocations (not o isBuiltin)) cdb
# cdb = if include_builtins includeBuiltins excludeBuiltins cdb
# cdb = case typeName of
Nothing -> cdb
Just _ -> filterDB (\ce -> ce=:(TypeDefEntry _)) cdb
......@@ -161,7 +161,9 @@ where
, dcl_line = getDclLine =<< mbLoc
, icl_line = getIclLine =<< mbLoc
, distance = distance // TODO
, builtin = Nothing // TODO
, builtin = case mbLoc of
Just (Builtin _ _) -> Just True
_ -> Nothing
, documentation = Nothing // TODO
, langrep_documentation = Nothing // TODO
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment