]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.osgi/scl/Reflection.scl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.osgi / scl / Reflection.scl
1 import "String"\r
2 import "StringBuilder" as StringBuilder\r
3 import "IterN"\r
4 \r
5 @private\r
6 importJava "org.simantics.scl.compiler.errors.Failable" where\r
7     data Failable a\r
8     \r
9     @JavaName getResult\r
10     resultOfFailable :: Failable a -> Maybe a\r
11 \r
12 @private\r
13 importJava "org.simantics.scl.compiler.module.ImportDeclaration" where\r
14     data ImportDeclaration\r
15 \r
16     @JavaName moduleName\r
17     moduleNameOfImportDeclaration :: ImportDeclaration -> String\r
18     @JavaName localName\r
19     localNameOfImportDeclaration :: ImportDeclaration -> Maybe String\r
20     @JavaName getSpecString\r
21     importSpecOfImportDeclaration :: ImportDeclaration -> String\r
22 \r
23 @private    \r
24 importJava "org.simantics.scl.compiler.module.Module" where\r
25     data Module    \r
26     @JavaName getDependencies\r
27     dependenciesOf :: Module -> [ImportDeclaration]\r
28 \r
29 @private\r
30 importJava "org.simantics.scl.compiler.elaboration.modules.SCLValue" where\r
31     data Value\r
32     @JavaName getName\r
33     nameOfValue :: Value -> Name\r
34     @JavaName getType\r
35     typeOfValue :: Value -> Type\r
36 \r
37 @private\r
38 importJava "org.simantics.scl.compiler.common.names.Name" where\r
39     data Name\r
40     @JavaName name\r
41     nameOfName :: Name -> String\r
42     @JavaName module\r
43     moduleOfName :: Name -> String\r
44 \r
45 @private\r
46 importJava "org.simantics.scl.compiler.module.repository.ModuleRepository" where\r
47     data ModuleRepository\r
48     \r
49     @JavaName getModule\r
50     findModule :: ModuleRepository -> String -> <Proc> Failable Module\r
51     \r
52     @JavaName getValueRef\r
53     findValueRef :: ModuleRepository -> String -> <Proc> Value\r
54     \r
55     @JavaName getValue\r
56     findValue :: ModuleRepository -> String -> <Proc> a\r
57     \r
58     @JavaName getSourceRepository\r
59     sourceRepositoryOf :: ModuleRepository -> ModuleSourceRepository\r
60 \r
61 @private\r
62 importJava "org.simantics.scl.compiler.source.repository.ModuleSourceRepository" where\r
63     data ModuleSourceRepository\r
64     \r
65 @private\r
66 importJava "org.simantics.scl.compiler.module.repository.ModuleRepositories" where\r
67     allValues :: ModuleRepository -> <Proc> [Value]\r
68     allValuesMatching :: ModuleRepository -> String -> <Proc> [Value]\r
69     \r
70 @private    \r
71 importJava "org.simantics.scl.compiler.source.repository.SourceRepositories" where\r
72     @JavaName getModuleNames\r
73     moduleNames :: ModuleSourceRepository -> <Proc> [String]\r
74 \r
75 @private\r
76 importJava "org.simantics.scl.osgi.SCLOsgi" where\r
77     @JavaName MODULE_REPOSITORY\r
78     ModuleRepository :: ModuleRepository\r
79 \r
80 @private\r
81 printTable :: [[String]] -> <Proc> ()\r
82 printTable [] = ()\r
83 printTable rows = iter printRow rows \r
84   where\r
85     columnCount = maximum (map length rows)\r
86     columnWidth i = maximum (map ithColumnWidth rows)\r
87       where\r
88         ithColumnWidth cols | length cols <= i = 0\r
89                             | otherwise = length (cols!i) \r
90     columnWidths = mapN columnWidth columnCount\r
91     printColumn i sb col = foldlN (\sb _ -> sb << " ") (sb << col) (columnWidths!i + 1 - length col)\r
92     printRow row = print $ StringBuilder.toString $ foldlI printColumn StringBuilder.new row \r
93 \r
94 searchValue :: String -> <Proc> ()\r
95 searchValue pattern = printTable $ sort $ map tableRow $ allValuesMatching ModuleRepository pattern\r
96   where\r
97     tableRow value = [\r
98         moduleOfName $ nameOfValue value,\r
99         nameOfName $ nameOfValue value,\r
100         ":: " + (show $ removeForAll $ typeOfValue value)]\r
101 \r
102 moduleDependencyGraph :: <Proc> [(String, [(String, Maybe String, String)])]\r
103 moduleDependencyGraph = mapMaybe\r
104     (\moduleName -> catch \r
105         (match resultOfFailable $ findModule ModuleRepository moduleName with\r
106             Just module -> Just (moduleName, findDependencies module)\r
107             Nothing -> Nothing\r
108         ) \r
109         $ \(_ :: Exception) -> Nothing\r
110     ) \r
111     (moduleNames $ sourceRepositoryOf ModuleRepository)\r
112   where\r
113     mapImportDeclaration decl = (moduleNameOfImportDeclaration decl,\r
114                                  localNameOfImportDeclaration decl,\r
115                                  importSpecOfImportDeclaration decl)\r
116     findDependencies module = map mapImportDeclaration (dependenciesOf module)