This repository was archived by the owner on Apr 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathScopeGraph.hs
More file actions
331 lines (248 loc) · 11.5 KB
/
ScopeGraph.hs
File metadata and controls
331 lines (248 loc) · 11.5 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
-- NOTE: This file needs to be updated to accommodate new AST shapes.
-- A portion of instances have been updated to include the Err functor;
-- remaining instances are to be updated once this is stable.
module Language.Python.ScopeGraph
( scopeGraphModule
) where
import qualified Analysis.Name as Name
import AST.Element
import qualified AST.Parse as Parse
import Control.Effect.ScopeGraph
import qualified Control.Effect.ScopeGraph.Properties.Declaration as Props
import qualified Control.Effect.ScopeGraph.Properties.Function as Props
import qualified Control.Effect.ScopeGraph.Properties.Reference as Props
import Control.Lens (set, (^.))
import Data.Foldable
import Data.List.NonEmpty (NonEmpty (..))
import Data.Maybe
import Data.Monoid
import qualified Data.ScopeGraph as ScopeGraph
import Data.Traversable
import GHC.Records
import GHC.TypeLits
import qualified Language.Python.AST as Py
import Language.Python.Patterns
import Scope.Graph.Convert (Result (..), complete, todo)
import Scope.Types
import Source.Loc (Loc)
import Source.Span (Span, Pos (..), span_, point)
-- This typeclass is internal-only, though it shares the same interface
-- as the one defined in semantic-scope-graph. The somewhat-unconventional
-- quantified constraint is to avoid having to define Show1 instances for
-- every single Python AST type.
class (forall a . Show a => Show (t a)) => ToScopeGraph t where
scopeGraph ::
( ScopeGraphEff sig m
, Monoid (m Result)
)
=> t Loc
-> m Result
instance (ToScopeGraph l, ToScopeGraph r) => ToScopeGraph (l :+: r) where
scopeGraph (L1 l) = scopeGraph l
scopeGraph (R1 r) = scopeGraph r
onField ::
forall (field :: Symbol) syn sig m r .
( ScopeGraphEff sig m
, HasField field (r Loc) (syn Loc)
, ToScopeGraph syn
, Monoid (m Result)
)
=> r Loc
-> m Result
onField
= scopeGraph @syn
. getField @field
onChildren ::
( Traversable t
, ToScopeGraph syn
, ScopeGraphEff sig m
, HasField "extraChildren" (r Loc) (t (syn Loc))
, Monoid (m Result)
)
=> r Loc
-> m Result
onChildren
= fmap fold
. traverse scopeGraph
. getField @"extraChildren"
scopeGraphModule :: ScopeGraphEff sig m => Py.Module Loc -> m Result
scopeGraphModule = getAp . scopeGraph
instance ToScopeGraph Py.AssertStatement where scopeGraph = onChildren
instance ToScopeGraph Py.Assignment where
scopeGraph (Py.Assignment ann (Parse.Success (SingleIdentifier t)) val _typ) = do
declare t Props.Declaration
{ Props.kind = ScopeGraph.Assignment
, Props.relation = ScopeGraph.Default
, Props.associatedScope = Nothing
, Props.span = ann^.span_
}
maybe complete scopeGraph val
scopeGraph x = todo x
instance ToScopeGraph Py.Await where
scopeGraph (Py.Await _ a) = scopeGraph a
instance ToScopeGraph Py.BooleanOperator where
scopeGraph (Py.BooleanOperator _ _ left right) = scopeGraph left <> scopeGraph right
instance ToScopeGraph Py.BinaryOperator where
scopeGraph (Py.BinaryOperator _ _ left right) = scopeGraph left <> scopeGraph right
instance ToScopeGraph Py.AugmentedAssignment where scopeGraph = onField @"right"
instance ToScopeGraph Py.Attribute where scopeGraph = todo
instance ToScopeGraph Py.Block where scopeGraph = onChildren
instance ToScopeGraph Py.BreakStatement where scopeGraph = mempty
instance ToScopeGraph Py.Call where
scopeGraph Py.Call
{ function = Parse.Success f
, arguments = Parse.Success (L1 Py.ArgumentList { extraChildren = args })
} = do
result <- scopeGraph f
let scopeGraphArg = \case
EPrj expr -> scopeGraph @Py.Expression expr
other -> todo other
args <- traverse scopeGraphArg args
pure (result <> mconcat args)
scopeGraph it = todo it
instance ToScopeGraph Py.ClassDefinition where scopeGraph = todo
instance ToScopeGraph Py.ConcatenatedString where scopeGraph = mempty
deriving instance ToScopeGraph Py.CompoundStatement
instance ToScopeGraph Py.ConditionalExpression where scopeGraph = onChildren
instance ToScopeGraph Py.ContinueStatement where scopeGraph = mempty
instance ToScopeGraph Py.DecoratedDefinition where scopeGraph = todo
instance ToScopeGraph Py.ComparisonOperator where scopeGraph = onChildren
instance ToScopeGraph Py.DeleteStatement where scopeGraph = mempty
instance ToScopeGraph Py.Dictionary where scopeGraph = onChildren
instance ToScopeGraph Py.DictionaryComprehension where scopeGraph = todo
instance ToScopeGraph Py.DictionarySplat where scopeGraph = todo
deriving instance ToScopeGraph Py.Expression
instance ToScopeGraph Py.ElseClause where scopeGraph = onField @"body"
instance ToScopeGraph Py.ElifClause where
scopeGraph (Py.ElifClause _ (Parse.Success body) (Parse.Success condition)) = scopeGraph condition <> scopeGraph body
instance ToScopeGraph Py.Ellipsis where scopeGraph = mempty
instance ToScopeGraph Py.ExceptClause where scopeGraph = onChildren
instance ToScopeGraph Py.ExecStatement where scopeGraph = mempty
instance ToScopeGraph Py.ExpressionStatement where scopeGraph = onChildren
instance ToScopeGraph Py.ExpressionList where scopeGraph = onChildren
instance ToScopeGraph Py.False where scopeGraph _ = pure mempty
instance ToScopeGraph Py.FinallyClause where scopeGraph = onField @"extraChildren"
instance ToScopeGraph Py.Float where scopeGraph = mempty
instance ToScopeGraph Py.ForStatement where scopeGraph = todo
instance ToScopeGraph Py.FunctionDefinition where
scopeGraph Py.FunctionDefinition
{ ann
, name = Parse.Success (Py.Identifier _ann1 name)
, parameters = Parse.Success (Py.Parameters _ann2 parameters)
, body = Parse.Success b
} = do
(_, associatedScope) <- declareFunction (Just $ Name.name name) Props.Function
{ Props.kind = ScopeGraph.Function
, Props.span = ann^.span_
}
withScope (CurrentScope associatedScope) $ do
let declProps = Props.Declaration
{ Props.kind = ScopeGraph.Parameter
, Props.relation = ScopeGraph.Default
, Props.associatedScope = Nothing
, Props.span = point (Pos 0 0)
}
let param (Py.Parameter (Prj (Py.Identifier pann pname))) = Just (pann, Name.name pname)
param _ = Nothing
let parameterMs = fmap param parameters
if any isNothing parameterMs
then todo parameterMs
else do
let parameters' = catMaybes parameterMs
paramDeclarations <- for parameters' $ \(pos, parameter) ->
complete <* declare parameter (set span_ (pos^.span_) declProps)
bodyResult <- scopeGraph b
pure (mconcat paramDeclarations <> bodyResult)
instance ToScopeGraph Py.FutureImportStatement where scopeGraph = todo
instance ToScopeGraph Py.GeneratorExpression where scopeGraph = todo
instance ToScopeGraph Py.Identifier where
scopeGraph (Py.Identifier ann name) = do
let refProps = Props.Reference ScopeGraph.Identifier ScopeGraph.Default (ann^.span_ :: Span)
newReference (Name.name name) refProps
complete
instance ToScopeGraph Py.IfStatement where
scopeGraph (Py.IfStatement _ alternative (Parse.Success body) (Parse.Success condition))
= scopeGraph condition
<> scopeGraph body
<> foldMap scopeGraph alternative
instance ToScopeGraph Py.GlobalStatement where scopeGraph = todo
instance ToScopeGraph Py.Integer where scopeGraph = mempty
instance ToScopeGraph Py.ImportStatement where
scopeGraph (Py.ImportStatement _ ((R1 (Py.DottedName _ names@((Py.Identifier ann name) :| _))) :| [])) = do
let toName (Py.Identifier _ name) = Name.name name
newEdge ScopeGraph.Import (toName <$> names)
let referenceProps = Props.Reference ScopeGraph.Identifier ScopeGraph.Default (ann^.span_ :: Span)
newReference (Name.name name) referenceProps
let pairs = zip (toList names) (tail $ toList names)
for_ pairs $ \pair -> do
case pair of
(scopeIdentifier, referenceIdentifier@(Py.Identifier ann2 _)) -> do
withScope (CurrentScope (toName scopeIdentifier)) $ do
let referenceProps = Props.Reference ScopeGraph.Identifier ScopeGraph.Default (ann2^.span_ :: Span)
newReference (toName referenceIdentifier) referenceProps
complete
scopeGraph term = todo (show term)
instance ToScopeGraph Py.ImportFromStatement where
scopeGraph (Py.ImportFromStatement _ [] (L1 (Py.DottedName _ names)) (Just (Py.WildcardImport _ _))) = do
let toName (Py.Identifier _ name) = Name.name name
complete <* newEdge ScopeGraph.Import (toName <$> names)
scopeGraph impossibleTerm@(Py.ImportFromStatement _ [] (L1 (Py.DottedName _ _)) Nothing) =
todo impossibleTerm
scopeGraph term = todo term
instance ToScopeGraph Py.Lambda where scopeGraph = todo
instance ToScopeGraph Py.List where scopeGraph = onChildren
instance ToScopeGraph Py.ListComprehension where scopeGraph = todo
instance ToScopeGraph Py.ListSplat where scopeGraph = onChildren
instance ToScopeGraph Py.NamedExpression where scopeGraph = todo
instance ToScopeGraph Py.None where scopeGraph = mempty
instance ToScopeGraph Py.NonlocalStatement where scopeGraph = todo
instance ToScopeGraph Py.Module where scopeGraph = onChildren
instance ToScopeGraph Py.ReturnStatement where
scopeGraph (Py.ReturnStatement _ mVal) = maybe (pure mempty) scopeGraph mVal
instance ToScopeGraph Py.True where scopeGraph = mempty
instance ToScopeGraph Py.NotOperator where scopeGraph = onField @"argument"
instance ToScopeGraph Py.Pair where
scopeGraph (Py.Pair _ value key) = scopeGraph key <> scopeGraph value
instance ToScopeGraph Py.ParenthesizedExpression where scopeGraph = onField @"extraChildren"
instance ToScopeGraph Py.PassStatement where scopeGraph = mempty
instance ToScopeGraph Py.PrintStatement where
scopeGraph (Py.PrintStatement _ args _chevron) = foldMap scopeGraph args
deriving instance ToScopeGraph Py.PrimaryExpression
deriving instance ToScopeGraph Py.SimpleStatement
instance ToScopeGraph Py.RaiseStatement where scopeGraph = todo
instance ToScopeGraph Py.Set where scopeGraph = onChildren
instance ToScopeGraph Py.SetComprehension where scopeGraph = todo
instance ToScopeGraph Py.String where scopeGraph = mempty
instance ToScopeGraph Py.Subscript where scopeGraph = todo
instance ToScopeGraph Py.Tuple where scopeGraph = onChildren
instance ToScopeGraph Py.TryStatement where
scopeGraph (Py.TryStatement _ body elseClauses)
= scopeGraph body
<> foldMap scopeGraph elseClauses
instance ToScopeGraph Py.UnaryOperator where scopeGraph = onField @"argument"
instance ToScopeGraph Py.WhileStatement where
scopeGraph Py.WhileStatement{ alternative, body, condition }
= scopeGraph condition
<> scopeGraph body
<> foldMap scopeGraph alternative
instance ToScopeGraph Py.WithStatement where
scopeGraph = todo
instance ToScopeGraph Py.Yield where scopeGraph = onChildren