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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE NoImplicitPrelude #-}
-- | A freshly resolved physical source graph.
--
-- Nodes are selected canonical files. Import edges retain every source
-- occurrence, including repeated imports that reach the same node.
module Felix.Source.Graph
( SourceNode
, sourceNodeLoaded
, sourceNodeResolved
, sourceNodeFileId
, SourceImportEdge
, sourceImportingNode
, sourceImportReference
, sourceImportedNode
, ResolvedSourceGraph
, sourceGraphRoot
, sourceGraphRootSource
, sourceGraphNodes
, sourceGraphImportEdges
, sourceGraphImportedBeforeImporter
, buildResolvedSourceGraph
) where
import Base
import Felix.Source
import Report.Location
( FileId
, registerFilePathWithDisplay
)
import Syntax.Token (Located(..), gatherImports)
import Control.Monad (unless)
import Control.Monad.State.Strict
( StateT
, get
, gets
, modify'
, runStateT
)
import Control.Monad.Trans.Except (ExceptT, runExceptT, throwE)
import Data.List qualified as List
import Data.List.NonEmpty qualified as NonEmpty
import Data.Map.Strict qualified as Map
import Data.Text qualified as Text
import Text.Megaparsec (errorBundlePretty)
data SourceNode = SourceNode
!LoadedSource
!FileId
deriving stock (Show, Eq)
sourceNodeLoaded :: SourceNode -> LoadedSource
sourceNodeLoaded (SourceNode loaded _fileId) = loaded
sourceNodeResolved :: SourceNode -> ResolvedSource
sourceNodeResolved = loadedSource . sourceNodeLoaded
sourceNodeFileId :: SourceNode -> FileId
sourceNodeFileId (SourceNode _loaded fileId) = fileId
sourceNodeCanonicalPath :: SourceNode -> CanonicalPath
sourceNodeCanonicalPath =
resolvedSourceCanonicalPath . sourceNodeResolved
data SourceImportEdge = SourceImportEdge
!CanonicalPath
!ImportRef
!CanonicalPath
deriving stock (Show, Eq)
sourceImportingNode :: SourceImportEdge -> CanonicalPath
sourceImportingNode (SourceImportEdge importer _reference _imported) =
importer
sourceImportReference :: SourceImportEdge -> ImportRef
sourceImportReference (SourceImportEdge _importer reference _imported) =
reference
sourceImportedNode :: SourceImportEdge -> CanonicalPath
sourceImportedNode (SourceImportEdge _importer _reference imported) =
imported
data ResolvedSourceGraph = ResolvedSourceGraph
!(NonEmpty SourceNode)
![SourceImportEdge]
deriving stock (Show)
sourceGraphRoot :: ResolvedSourceGraph -> CanonicalPath
sourceGraphRoot =
sourceNodeCanonicalPath . NonEmpty.last . sourceGraphNodeSpine
sourceGraphRootSource :: ResolvedSourceGraph -> ResolvedSource
sourceGraphRootSource =
sourceNodeResolved . NonEmpty.last . sourceGraphNodeSpine
sourceGraphNodes :: ResolvedSourceGraph -> [SourceNode]
sourceGraphNodes =
NonEmpty.toList . sourceGraphNodeSpine
sourceGraphImportEdges :: ResolvedSourceGraph -> [SourceImportEdge]
sourceGraphImportEdges (ResolvedSourceGraph _nodes edges) =
edges
-- | Deterministic DFS completion order. Imports are visited in textual
-- occurrence order, so every imported node precedes its importer and sibling
-- imports retain their source order.
sourceGraphImportedBeforeImporter :: ResolvedSourceGraph -> NonEmpty SourceNode
sourceGraphImportedBeforeImporter =
sourceGraphNodeSpine
sourceGraphNodeSpine :: ResolvedSourceGraph -> NonEmpty SourceNode
sourceGraphNodeSpine (ResolvedSourceGraph nodes _edges) =
nodes
data VisitStatus
= Visiting
| Visited
deriving stock (Show, Eq)
data BuildNode = BuildNode
!SourceNode
!VisitStatus
data GraphBuildState = GraphBuildState
{ buildNodes :: !(Map CanonicalPath BuildNode)
, buildEdgesReversed :: ![SourceImportEdge]
, buildOrderReversed :: ![SourceNode]
}
type GraphBuilder = ExceptT SourceError (StateT GraphBuildState IO)
initialGraphBuildState :: GraphBuildState
initialGraphBuildState = GraphBuildState
{ buildNodes = mempty
, buildEdgesReversed = []
, buildOrderReversed = []
}
buildResolvedSourceGraph
:: SourceMounts
-> RootRequest
-> IO (Either SourceError ResolvedSourceGraph)
buildResolvedSourceGraph mounts request = do
resolvedRoot <- resolveRoot mounts request
case resolvedRoot of
Left err ->
pure (Left err)
Right rootSource -> do
loadedRoot <- loadResolvedSource rootSource
case loadedRoot of
Left err ->
pure (Left err)
Right root -> do
(result, finalState) <- runStateT
(runExceptT do
rootNode <- insertFreshNode root
visitNode
mounts
(sourceNodeCanonicalPath rootNode)
[])
initialGraphBuildState
pure case result of
Left err ->
Left err
Right () ->
case NonEmpty.nonEmpty
(reverse (buildOrderReversed finalState)) of
Nothing ->
Left
(SourceGraphInvariantViolation
"source graph has no root node")
Just order ->
Right
(ResolvedSourceGraph
order
(reverse
(buildEdgesReversed
finalState)))
insertFreshNode :: LoadedSource -> GraphBuilder SourceNode
insertFreshNode loaded = do
state <- get
let source = loadedSource loaded
canonical = resolvedSourceCanonicalPath source
case Map.lookup canonical (buildNodes state) of
Just _ ->
throwE
(SourceGraphInvariantViolation
"attempted to allocate a duplicate canonical source node")
Nothing -> do
let identityPath = canonicalPathFilePath canonical
displayPath = resolvedSourceLocationPath source
registration <- liftIO
(registerFilePathWithDisplay identityPath displayPath)
fileId <- either
(throwE . SourceLocationRegistrationFailed source)
pure
registration
let node = SourceNode loaded fileId
modify' \current ->
current
{ buildNodes =
Map.insert
canonical
(BuildNode node Visiting)
(buildNodes current)
}
pure node
visitNode
:: SourceMounts
-> CanonicalPath
-> [SourceCycleStep]
-> GraphBuilder ()
visitNode mounts canonical path = do
node <- lookupBuildNode canonical
references <- discoverNodeImports node
traverse_ (visitImport mounts node path) references
modify' \state ->
state
{ buildNodes =
Map.adjust
(\(BuildNode currentNode _status) ->
BuildNode currentNode Visited)
canonical
(buildNodes state)
, buildOrderReversed =
node : buildOrderReversed state
}
lookupBuildNode :: CanonicalPath -> GraphBuilder SourceNode
lookupBuildNode canonical = do
nodes <- gets buildNodes
case Map.lookup canonical nodes of
Nothing ->
throwE
(SourceGraphInvariantViolation
"source graph contains an unknown canonical path")
Just (BuildNode node _status) ->
pure node
discoverNodeImports :: SourceNode -> GraphBuilder [ImportRef]
discoverNodeImports node = do
let loaded = sourceNodeLoaded node
source = loadedSource loaded
locationPath = resolvedSourceLocationPath source
locatedPaths <- case
gatherImports
(sourceNodeFileId node)
locationPath
(loadedText loaded) of
Left err ->
throwE
(SourceImportDiscoveryFailed
source
(Text.pack (errorBundlePretty err)))
Right paths ->
pure paths
traverse (validateImport source) locatedPaths
validateImport
:: ResolvedSource
-> Located FilePath
-> GraphBuilder ImportRef
validateImport source locatedPath =
case importRef (startPos locatedPath) (unLocated locatedPath) of
Left err ->
throwE
(InvalidImportPath
source
(startPos locatedPath)
(unLocated locatedPath)
err)
Right reference ->
pure reference
visitImport
:: SourceMounts
-> SourceNode
-> [SourceCycleStep]
-> ImportRef
-> GraphBuilder ()
visitImport mounts importerNode path reference = do
let importer = sourceNodeResolved importerNode
importerCanonical = sourceNodeCanonicalPath importerNode
imported <- liftEitherIO (resolveImport mounts importer reference)
let importedCanonical = resolvedSourceCanonicalPath imported
existing <- gets (Map.lookup importedCanonical . buildNodes)
case existing of
Nothing -> do
loaded <- liftEitherIO (loadResolvedSource imported)
importedNode <- insertFreshNode loaded
appendEdge importerCanonical reference importedCanonical
let step = sourceCycleStep importer reference imported
visitNode mounts (sourceNodeCanonicalPath importedNode) (path <> [step])
Just (BuildNode importedNode status) -> do
unless
(sourceNodeResolved importedNode == imported)
(throwE
(SourceGraphInvariantViolation
"canonical source attribution changed within one graph"))
appendEdge importerCanonical reference importedCanonical
case status of
Visited ->
pure ()
Visiting -> do
let step = sourceCycleStep importer reference imported
case cycleSuffix imported (path <> [step]) of
Just cycleSteps ->
throwE (SourceImportCycle cycleSteps)
Nothing ->
throwE
(SourceGraphInvariantViolation
"cycle target is absent from the DFS path")
appendEdge
:: CanonicalPath
-> ImportRef
-> CanonicalPath
-> GraphBuilder ()
appendEdge importer reference imported =
modify' \state ->
state
{ buildEdgesReversed =
SourceImportEdge importer reference imported
: buildEdgesReversed state
}
cycleSuffix
:: ResolvedSource
-> [SourceCycleStep]
-> Maybe (NonEmpty SourceCycleStep)
cycleSuffix repeatedSource path =
NonEmpty.nonEmpty
(List.dropWhile
((/= resolvedSourceCanonicalPath repeatedSource)
. resolvedSourceCanonicalPath
. cycleImporter)
path)
liftEitherIO :: IO (Either SourceError a) -> GraphBuilder a
liftEitherIO action =
liftIO action >>= either throwE pure
|