-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a modules() method to the cryptol python api for listing modules …
…and their documentation (#1755) * Add a modules() method to the cryptol python api for listing modules and their documentation * Changelog! * update rpc docs * Additional changelog * Add modules() to the many layers of the API and add a test case * Put the space back into the manually line-wrapped documentation
- Loading branch information
Showing
14 changed files
with
222 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
cryptol-remote-api/python/tests/cryptol/test-files/Modules.cry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* A top-level | ||
* docstring | ||
*/ | ||
module Modules where | ||
|
||
/** A functor docstring */ | ||
submodule F where | ||
parameter | ||
type N : # | ||
|
||
/** A submodule in a functor docstring */ | ||
submodule Q where | ||
q = () | ||
|
||
/** A submodule docstring */ | ||
submodule M = submodule F where | ||
type N = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import unittest | ||
from pathlib import Path | ||
import cryptol | ||
from cryptol.single_connection import * | ||
|
||
class TestModules(unittest.TestCase): | ||
def test_modules(self): | ||
connect(verify=False) | ||
load_file(str(Path('tests','cryptol','test-files', 'Modules.cry'))) | ||
|
||
expected = [ | ||
{'module': 'Cryptol', 'parameterized': False}, | ||
{'module': 'Modules', 'parameterized': False, 'documentation': ['A top-level\ndocstring\n']}, | ||
{'module': 'Modules::`where` argument of M', 'parameterized': False}, | ||
{'module': 'Modules::M', 'parameterized': False, 'documentation': ['A submodule docstring\n', 'A functor docstring\n']}, | ||
{'module': 'Modules::M::Q', 'parameterized': False, 'documentation': ['A submodule in a functor docstring\n']}, | ||
{'module': 'Modules::F', 'parameterized': True, 'documentation': ['A functor docstring\n']}, | ||
{'module': 'Modules::F::Q', 'parameterized': True, 'documentation': ['A submodule in a functor docstring\n']}, | ||
] | ||
|
||
names_to_check = modules() | ||
|
||
self.assertCountEqual(expected, names_to_check) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module CryptolServer.Modules | ||
( visibleModules | ||
, visibleModulesDescr | ||
) where | ||
|
||
import qualified Argo.Doc as Doc | ||
import qualified Data.Aeson as JSON | ||
import Data.Aeson ((.=)) | ||
import qualified Data.Map as Map | ||
import Data.Text (unpack) | ||
|
||
import Cryptol.ModuleSystem.Env (loadedModules) | ||
import Cryptol.ModuleSystem.Interface (ifsDoc) | ||
import Cryptol.TypeCheck.AST as T | ||
import Cryptol.Utils.PP (PP, pp) | ||
|
||
import CryptolServer (CryptolCommand, getModuleEnv) | ||
|
||
|
||
data VisibleModulesParams = VisibleNamesParams | ||
|
||
instance JSON.FromJSON VisibleModulesParams where | ||
parseJSON _ = pure VisibleNamesParams | ||
|
||
instance Doc.DescribedMethod VisibleModulesParams [VisibleModuleInfo] where | ||
parameterFieldDescription = [] | ||
|
||
resultFieldDescription = | ||
[ ("module", | ||
Doc.Paragraph [ Doc.Text "A human-readable representation of the module's name"]) | ||
, ("documentation", | ||
Doc.Paragraph [ Doc.Text "An optional field containing documentation strings for the module, " | ||
, Doc.Text "if it is documented"]) | ||
, ("parameterized", | ||
Doc.Paragraph [ Doc.Text "A Boolean value indicating whether the focused module is " | ||
, Doc.Text "parameterized" | ||
]) | ||
] | ||
|
||
visibleModulesDescr :: Doc.Block | ||
visibleModulesDescr = | ||
Doc.Paragraph [Doc.Text "List the currently visible (i.e., in scope) module names."] | ||
|
||
visibleModules :: VisibleModulesParams -> CryptolCommand [VisibleModuleInfo] | ||
visibleModules _ = concatMap (moduleToInfos False) . loadedModules <$> getModuleEnv | ||
|
||
|
||
moduleToInfos :: PP a => Bool -> T.ModuleG a -> [VisibleModuleInfo] | ||
moduleToInfos p m = | ||
ModuleInfo | ||
{ name = show (pp (T.mName m)) | ||
, parameterized = p | ||
, docs = map unpack (T.mDoc m) | ||
} : | ||
[ ModuleInfo | ||
{ name = show (pp k) | ||
, parameterized = p | ||
, docs = map unpack (ifsDoc (T.smIface v)) | ||
} | ||
| (k,v) <- Map.assocs (T.mSubmodules m) | ||
] ++ | ||
[ x | ||
| v <- Map.elems (T.mFunctors m) | ||
, x <- moduleToInfos True v | ||
] | ||
|
||
data VisibleModuleInfo = | ||
ModuleInfo | ||
{ name :: String | ||
, parameterized :: Bool | ||
, docs :: [String] | ||
} | ||
|
||
instance JSON.ToJSON VisibleModuleInfo where | ||
toJSON ModuleInfo{..} = JSON.object ( | ||
[ "module" .= name | ||
, "parameterized" .= parameterized] ++ | ||
["documentation" .= docs | not (null docs)]) |