1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
module Language.Curry.SourceCodeClassifier
( getDeclarationsInModule, getOperationsInModule
, getTypesInModule, getClassesInModule
) where
import Curry.Comment ( readComments )
import Curry.Files ( readFullAST, readShortAST )
import Curry.Types ( Module(..), Decl(..) )
import Curry.Ident ( idName )
import Curry.SpanInfo ( SpanInfo(..) )
import Curry.Span ( Span(..) )
import Curry.Position ( Position (..) )
import Data.Maybe ( catMaybes )
import Data.List ( sortBy )
import Data.Trie as T ( Trie, empty, insert, lookup )
type LineSpan = (Int, Int)
missing :: LineSpan
missing = (0, 0)
type Occurrence = (String, LineSpan, LineSpan)
getDeclarationsInModule :: String -> IO ([Occurrence], [Occurrence], [Occurrence])
getDeclarationsInModule mn = do
(mdl, comments) <- readModule mn
let ops = collectOperationsInModule mdl comments
let types = collectTypesInModule mdl comments
let classes = collectClassesInModule mdl comments
return (ops, types, classes)
getOperationsInModule :: String -> IO [Occurrence]
getOperationsInModule mn =
uncurry collectOperationsInModule <$> readModule mn
getTypesInModule :: String -> IO [Occurrence]
getTypesInModule mn =
uncurry collectTypesInModule <$> readModule mn
getClassesInModule :: String -> IO [Occurrence]
getClassesInModule mn =
uncurry collectClassesInModule <$> readModule mn
type Entity a = (a, LineSpan)
type SignatureE = Entity [String]
type TypeDeclE = Entity String
type ClassDeclE = Entity String
collectOperationsInModule :: Module a -> [LineSpan] -> [Occurrence]
collectOperationsInModule mdl comments =
let sigs = collectSignatures mdl
ops = collectOperations mdl
in extendWithRules ops $ addComments createOccs comments sigs
where
createOccs :: LineSpan -> SignatureE -> [Occurrence]
createOccs cls (is, sigSpan) = [(i, cls, sigSpan) | i <- is]
collectSignatures :: Module a -> [SignatureE]
collectSignatures (Module _ _ _ _ _ _ ops)
= sortBy lineNumber $ catMaybes $ map collectSignature ops
collectSignature :: Decl a -> Maybe SignatureE
collectSignature decl = case decl of
(TypeSig si is _) -> do
sp <- getLineSpan si
return (map idName is, sp)
_ -> Nothing
collectOperations :: Module a -> T.Trie LineSpan
collectOperations (Module _ _ _ _ _ _ ops) = foldr collect T.empty ops
where
collect :: Decl a -> Trie LineSpan -> Trie LineSpan
collect decl trie = case decl of
(FunctionDecl si _ i _) -> case getLineSpan si of
Nothing -> trie
Just ls -> T.insert (idName i) ls trie
_ -> trie
extendWithRules :: T.Trie LineSpan -> [Occurrence] -> [Occurrence]
extendWithRules ops = map extend
where
extend :: Occurrence -> Occurrence
extend occ@(i, cSpan, sigSpan) = case T.lookup i ops of
Nothing -> occ
Just opSpan -> (i, cSpan, extendSpan sigSpan opSpan)
extendSpan :: LineSpan -> LineSpan -> LineSpan
extendSpan (s1, e1) (s2, e2) = (min s1 s2, max e1 e2)
collectTypesInModule :: Module a -> [LineSpan] -> [Occurrence]
collectTypesInModule mdl comments =
let types = collectTypes mdl
in addComments createOcc comments types
where
collectTypes :: Module a -> [TypeDeclE]
collectTypes (Module _ _ _ _ _ _ decls)
= sortBy lineNumber $ catMaybes $ map collectType decls
collectType :: Decl a -> Maybe (TypeDeclE)
collectType decl = case decl of
(DataDecl si i _ _ _) -> do
sp <- getLineSpan si
return (idName i, sp)
(NewtypeDecl si i _ _ _) -> do
sp <- getLineSpan si
return (idName i, sp)
(TypeDecl si i _ _) -> do
sp <- getLineSpan si
return (idName i, sp)
_ -> Nothing
collectClassesInModule :: Module a -> [LineSpan] -> [Occurrence]
collectClassesInModule mdl comments =
let classes = collectClasses mdl
in addComments createOcc comments classes
where
collectClasses :: Module a -> [ClassDeclE]
collectClasses (Module _ _ _ _ _ _ decls)
= sortBy lineNumber $ catMaybes $ map collectClass decls
collectClass :: Decl a -> Maybe (ClassDeclE)
collectClass decl = case decl of
(ClassDecl si _ _ i _ _ _) -> do
sp <- getLineSpan si
return (idName i, sp)
_ -> Nothing
readModule :: String -> IO (Module (), [LineSpan])
readModule mn = do
comments <- (mergeCommentSpans . getCommentSpans) <$> readComments mn
mdl <- readShortAST mn
return (mdl, comments)
createOcc :: LineSpan -> Entity String -> [Occurrence]
createOcc cls (i, sigSpan) = [(i, cls, sigSpan)]
addComments :: (LineSpan -> (a, LineSpan) -> [Occurrence]) -> [LineSpan] -> [(a, LineSpan)] -> [Occurrence]
addComments f cs ss = case (cs, ss) of
([], _) -> concatMap (f missing) ss
(_, []) -> []
(c:cs', s@(_, sigSpan):ss')
| end c < start sigSpan -> addComments f cs' (s:ss')
| end c == start sigSpan -> f c s ++ addComments f cs' ss'
| otherwise -> f missing s ++ addComments f (c:cs') ss'
where
start = fst
end = snd
getCommentSpans :: [(Span, _)] -> [LineSpan]
getCommentSpans = sortBy startLine . catMaybes . map (getLineSpan . fst)
where
startLine :: LineSpan -> LineSpan -> Bool
startLine (x, _) (y, _) = x <= y
mergeCommentSpans :: [LineSpan] -> [LineSpan]
mergeCommentSpans = foldr merge []
where
merge :: LineSpan -> [LineSpan] -> [LineSpan]
merge (s, e) [] = [(s, e)]
merge (s, e) ((s', e') : xs)
| e == s' = (s, e') : xs
| otherwise = (s, e) : (s', e') : xs
lineNumber :: Entity a -> Entity a -> Bool
lineNumber (_, (s1, _)) (_, (s2, _)) = s1 <= s2
row :: Position -> Maybe Int
row (Position r _) = Just r
row NoPos = Nothing
class HasLineSpan a where
getLineSpan :: a -> Maybe LineSpan
instance HasLineSpan Span where
getLineSpan NoSpan = Nothing
getLineSpan (Span s e) = do
sp <- row s
ep <- row e
return (sp, ep + 1)
instance HasLineSpan SpanInfo where
getLineSpan (SpanInfo s _) = getLineSpan s
getLineSpan NoSpanInfo = Nothing |