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