Stabel

Check-in [73872525cb]
Login
Overview
Comment:PackageLoader now parses each module first, then uses new dependency information for finding new modules to parse and total order each module should be qualified in.
Timelines: family | ancestors | descendants | both | module-definition
Files: files | file ages | folders
SHA3-256: 73872525cb6dc062ee473df9b2ef46991eaa8e6c25227f136fd88616e258e4f5
User & Date: robin.hansen on 2021-05-05 08:18:17
Other Links: branch diff | manifest | tags
Context
2021-05-05
08:26
Remove fields from qualifier ast required by previous way of discovering dependencies. Next step is ... check-in: 38b29fb9b2 user: robin.hansen tags: module-definition
08:18
PackageLoader now parses each module first, then uses new dependency information for finding new mod... check-in: 73872525cb user: robin.hansen tags: module-definition
2021-05-03
13:50
Add function that goes through a parser AST and figures out which modules the AST depends on being p... check-in: 7d904176c1 user: robin.hansen tags: module-definition
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Play/PackageLoader.elm from [41ef8f9010] to [cd381958d8].

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
...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
...
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
...
435
436
437
438
439
440
441
442

















443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516





























517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534


535
536

537
538



539
540
541
542
543
544
545
546
547
548
549
550
551
    }


type Model
    = Initializing InitOptions SideEffect
    | LoadingMetadata State (List PackagePath) SideEffect
    | ResolvingModulePaths State (List PackageInfo) SideEffect
    | Compiling State (List ( PackageInfo, ModuleName )) SideEffect
    | Done Qualifier.ExposedAST
    | Failed Problem


type alias State =
    { possibleEntryPoint : Maybe String
    , rootPackage : PackageInfo
    , dependencies : Dict String SemanticVersion
    , dependentPackages : Dict String PackageInfo
    , filePathToModule : Dict String ( PackageName, ModuleName )
    , moduleNameToPackageName : Dict String String
    , absoluteModuleNameToDetails : Dict String ( PackageInfo, ModuleName )
    , inProgressAst : Maybe Qualifier.AST
    , parsedModules : Set String
    }


type alias PackageInfo =
    { path : String
    , metadata : PackageMetadata
    , modules : List ModuleName
    }









emptyState : InitOptions -> PackageInfo -> State
emptyState initOptions rootPackage =
    { possibleEntryPoint = initOptions.possibleEntryPoint
    , rootPackage = rootPackage
    , dependencies = rootPackage.metadata.dependencies
    , dependentPackages = Dict.empty
    , filePathToModule = Dict.empty
    , moduleNameToPackageName = Dict.empty
    , absoluteModuleNameToDetails = Dict.empty
    , inProgressAst = Nothing
    , parsedModules = Set.empty
    }


type Msg
    = FileContents String String String
    | ResolvedDirectories String (List PackagePath)
    | ResolvedPackageModules String (List String)
................................................................................

        LoadingMetadata _ _ sf ->
            Just sf

        ResolvingModulePaths _ _ sf ->
            Just sf

        Compiling _ _ sf ->
            Just sf


init : InitOptions -> Model
init initOptions =
    Initializing initOptions <|
        ReadFile initOptions.projectDirPath "play.json"
................................................................................

        LoadingMetadata state remainingPaths _ ->
            loadingMetadataUpdate msg state remainingPaths

        ResolvingModulePaths state remainingPackages _ ->
            resolvingModulePathsUpdate msg state remainingPackages

        Compiling state remainingModules _ ->
            compilingUpdate msg state remainingModules

        Done _ ->
            model

        Failed _ ->
            model

................................................................................

                    absoluteNameToDetails =
                        allPackages
                            |> List.concatMap (\pInfo -> List.map (\m -> ( pInfo, m )) pInfo.modules)
                            |> List.map (\( pInfo, mName ) -> ( absoluteModuleName pInfo.metadata.name mName, ( pInfo, mName ) ))
                            |> Dict.fromList
                in
                Compiling
                    { state
                        | filePathToModule = pathsToModuleNames
                        , moduleNameToPackageName = moduleNameToPackageName
                        , absoluteModuleNameToDetails = absoluteNameToDetails
                    }
                    (List.map (\m -> ( state.rootPackage, m )) remModules)
                    (ReadFile path fileName)
................................................................................
                )
                package.modules
                |> Dict.fromList
    in
    Dict.union acc absolutePathsForModule


compilingUpdate : Msg -> State -> List ( PackageInfo, ModuleName ) -> Model
compilingUpdate msg state remainingModules =
    case msg of
        FileContents path fileName content ->
            let
                fullPath =
                    path ++ "/" ++ fileName

                possibleModuleInfo =
................................................................................
            in
            case ( possibleModuleInfo, Parser.run content ) of
                ( _, Err parserError ) ->
                    Failed <| InternalError <| "Parser error: " ++ Debug.toString parserError

                ( Just ( packageName, moduleName ), Ok parserAst ) ->
                    let
                        qualifierResult =

















                            Qualifier.run
                                { packageName = PackageName.toString packageName
                                , modulePath = ModuleName.toString moduleName
                                , ast = parserAst
                                , externalModules = state.moduleNameToPackageName
                                }
                    in
                    case qualifierResult of
                        Err qualifierError ->
                            Failed <| InternalError <| "Qualifier error: " ++ Debug.toString qualifierError

                        Ok qualifiedAST ->
                            let
                                fullModuleName =
                                    absoluteModuleName packageName moduleName

                                mergedQualifiedAst =
                                    state.inProgressAst
                                        |> Maybe.map
                                            (\ipa ->
                                                { additionalModulesRequired =
                                                    Set.union
                                                        ipa.additionalModulesRequired
                                                        qualifiedAST.additionalModulesRequired
                                                , checkForExistingTypes =
                                                    Set.union
                                                        ipa.checkForExistingTypes
                                                        qualifiedAST.checkForExistingTypes
                                                , checkForExistingWords =
                                                    Set.union
                                                        ipa.checkForExistingWords
                                                        qualifiedAST.checkForExistingWords
                                                , types = Dict.union ipa.types qualifiedAST.types
                                                , words = Dict.union ipa.words qualifiedAST.words
                                                }
                                            )
                                        |> Maybe.withDefault qualifiedAST

                                updatedParsedModules =
                                    Set.insert fullModuleName state.parsedModules

                                modulesQueuedForOrAlreadyParsed =
                                    remainingModules
                                        |> List.map (\( pInfo, mName ) -> absoluteModuleName pInfo.metadata.name mName)
                                        |> Set.fromList
                                        |> Set.union updatedParsedModules

                                missingModulesInParseQueue =
                                    Set.diff mergedQualifiedAst.additionalModulesRequired modulesQueuedForOrAlreadyParsed
                                        |> Set.toList
                                        |> List.filterMap (\absName -> Dict.get absName state.absoluteModuleNameToDetails)

                                updatedRemainingModules =
                                    missingModulesInParseQueue ++ remainingModules
                            in
                            nextCompileStep updatedParsedModules mergedQualifiedAst updatedRemainingModules state

                ( Nothing, _ ) ->
                    Failed <| InternalError <| "Don't know why we read file: " ++ fullPath

        _ ->
            Failed <| InternalError <| "Unknown message for compile stage: " ++ Debug.toString msg


absoluteModuleName : PackageName -> ModuleName -> String
absoluteModuleName packageName moduleName =
    "/" ++ PackageName.toString packageName ++ "/" ++ ModuleName.toString moduleName


nextCompileStep : Set String -> Qualifier.AST -> List ( PackageInfo, ModuleName ) -> State -> Model
nextCompileStep parsedModules inProgressAst remainingModules state =
    case remainingModules of
        [] ->
            let





























                wordsWithEntryPoint =
                    state.possibleEntryPoint
                        |> Maybe.map (setEntryPoint inProgressAst.words)
                        |> Maybe.withDefault inProgressAst.words

                setEntryPoint words entryPointName =
                    Dict.update
                        entryPointName
                        (\maybeWord ->
                            case maybeWord of
                                Nothing ->
                                    Nothing

                                Just word ->
                                    Just { word | metadata = Metadata.asEntryPoint word.metadata }
                        )
                        words
            in


            Done
                { types = inProgressAst.types

                , words = wordsWithEntryPoint
                }




        ( packageInfo, moduleName ) :: otherModules ->
            let
                ( path, fileName ) =
                    readModuleFromDisk packageInfo.path moduleName
            in
            Compiling
                { state
                    | parsedModules = parsedModules
                    , inProgressAst = Just inProgressAst
                }
                otherModules
                (ReadFile path fileName)







|












|
|









>
>
>
>
>
>
>










|
|







 







|







 







|
|







 







|







 







|
|







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|





<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|
|

|
|
|
|

|
|
|
|













|
|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|
|




<
<
<
<
<
<
|
<


>
>
|
<
>
|
|
>
>
>






<
<
<
<
<
<
|
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
...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
...
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
...
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
...
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472




473






























474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543






544

545
546
547
548
549

550
551
552
553
554
555
556
557
558
559
560
561






562
    }


type Model
    = Initializing InitOptions SideEffect
    | LoadingMetadata State (List PackagePath) SideEffect
    | ResolvingModulePaths State (List PackageInfo) SideEffect
    | Parsing State (List ( PackageInfo, ModuleName )) SideEffect
    | Done Qualifier.ExposedAST
    | Failed Problem


type alias State =
    { possibleEntryPoint : Maybe String
    , rootPackage : PackageInfo
    , dependencies : Dict String SemanticVersion
    , dependentPackages : Dict String PackageInfo
    , filePathToModule : Dict String ( PackageName, ModuleName )
    , moduleNameToPackageName : Dict String String
    , absoluteModuleNameToDetails : Dict String ( PackageInfo, ModuleName )
    , parsedModuleNames : Set String
    , parsedModules : List ParsedModuleInfo
    }


type alias PackageInfo =
    { path : String
    , metadata : PackageMetadata
    , modules : List ModuleName
    }


type alias ParsedModuleInfo =
    { packageName : PackageName
    , modulePath : ModuleName
    , ast : Parser.AST
    }


emptyState : InitOptions -> PackageInfo -> State
emptyState initOptions rootPackage =
    { possibleEntryPoint = initOptions.possibleEntryPoint
    , rootPackage = rootPackage
    , dependencies = rootPackage.metadata.dependencies
    , dependentPackages = Dict.empty
    , filePathToModule = Dict.empty
    , moduleNameToPackageName = Dict.empty
    , absoluteModuleNameToDetails = Dict.empty
    , parsedModuleNames = Set.empty
    , parsedModules = []
    }


type Msg
    = FileContents String String String
    | ResolvedDirectories String (List PackagePath)
    | ResolvedPackageModules String (List String)
................................................................................

        LoadingMetadata _ _ sf ->
            Just sf

        ResolvingModulePaths _ _ sf ->
            Just sf

        Parsing _ _ sf ->
            Just sf


init : InitOptions -> Model
init initOptions =
    Initializing initOptions <|
        ReadFile initOptions.projectDirPath "play.json"
................................................................................

        LoadingMetadata state remainingPaths _ ->
            loadingMetadataUpdate msg state remainingPaths

        ResolvingModulePaths state remainingPackages _ ->
            resolvingModulePathsUpdate msg state remainingPackages

        Parsing state remainingModules _ ->
            parsingUpdate msg state remainingModules

        Done _ ->
            model

        Failed _ ->
            model

................................................................................

                    absoluteNameToDetails =
                        allPackages
                            |> List.concatMap (\pInfo -> List.map (\m -> ( pInfo, m )) pInfo.modules)
                            |> List.map (\( pInfo, mName ) -> ( absoluteModuleName pInfo.metadata.name mName, ( pInfo, mName ) ))
                            |> Dict.fromList
                in
                Parsing
                    { state
                        | filePathToModule = pathsToModuleNames
                        , moduleNameToPackageName = moduleNameToPackageName
                        , absoluteModuleNameToDetails = absoluteNameToDetails
                    }
                    (List.map (\m -> ( state.rootPackage, m )) remModules)
                    (ReadFile path fileName)
................................................................................
                )
                package.modules
                |> Dict.fromList
    in
    Dict.union acc absolutePathsForModule


parsingUpdate : Msg -> State -> List ( PackageInfo, ModuleName ) -> Model
parsingUpdate msg state remainingModules =
    case msg of
        FileContents path fileName content ->
            let
                fullPath =
                    path ++ "/" ++ fileName

                possibleModuleInfo =
................................................................................
            in
            case ( possibleModuleInfo, Parser.run content ) of
                ( _, Err parserError ) ->
                    Failed <| InternalError <| "Parser error: " ++ Debug.toString parserError

                ( Just ( packageName, moduleName ), Ok parserAst ) ->
                    let
                        fullModuleName =
                            absoluteModuleName packageName moduleName

                        updatedParsedModules =
                            Set.insert fullModuleName state.parsedModuleNames

                        updatedState =
                            { state
                                | parsedModuleNames = updatedParsedModules
                                , parsedModules =
                                    { packageName = packageName
                                    , modulePath = moduleName
                                    , ast = parserAst
                                    }
                                        :: state.parsedModules
                            }

                        requiredModules =
                            Qualifier.requiredModules
                                { packageName = PackageName.toString packageName
                                , modulePath = ModuleName.toString moduleName
                                , ast = parserAst
                                , externalModules = state.moduleNameToPackageName
                                }



































                        modulesQueuedForOrAlreadyParsed =
                            remainingModules
                                |> List.map (\( pInfo, mName ) -> absoluteModuleName pInfo.metadata.name mName)
                                |> Set.fromList
                                |> Set.union updatedParsedModules

                        missingModulesInParseQueue =
                            Set.diff requiredModules modulesQueuedForOrAlreadyParsed
                                |> Set.toList
                                |> List.filterMap (\absName -> Dict.get absName state.absoluteModuleNameToDetails)

                        updatedRemainingModules =
                            remainingModules ++ missingModulesInParseQueue
                    in
                    nextCompileStep updatedRemainingModules updatedState

                ( Nothing, _ ) ->
                    Failed <| InternalError <| "Don't know why we read file: " ++ fullPath

        _ ->
            Failed <| InternalError <| "Unknown message for compile stage: " ++ Debug.toString msg


absoluteModuleName : PackageName -> ModuleName -> String
absoluteModuleName packageName moduleName =
    "/" ++ PackageName.toString packageName ++ "/" ++ ModuleName.toString moduleName


nextCompileStep : List ( PackageInfo, ModuleName ) -> State -> Model
nextCompileStep remainingModules state =
    case remainingModules of
        [] ->
            let
                ( qualifiedAst, errs ) =
                    state.parsedModules
                        |> List.foldl qualifyAst ( { types = Dict.empty, words = Dict.empty }, [] )

                qualifyAst parsedModInfo ( qast, es ) =
                    let
                        qualifierResult =
                            Qualifier.run
                                { packageName = PackageName.toString parsedModInfo.packageName
                                , modulePath = ModuleName.toString parsedModInfo.modulePath
                                , ast = parsedModInfo.ast
                                , externalModules = state.moduleNameToPackageName
                                }
                    in
                    case qualifierResult of
                        Err qualifierError ->
                            ( qast
                            , (InternalError <| "Qualifier error: " ++ Debug.toString qualifierError) :: es
                            )

                        Ok qualifiedAST ->
                            let
                                mergedQualifiedAst =
                                    { types = Dict.union qast.types qualifiedAST.types
                                    , words = Dict.union qast.words qualifiedAST.words
                                    }
                            in
                            ( mergedQualifiedAst, es )

                wordsWithEntryPoint =
                    state.possibleEntryPoint
                        |> Maybe.map (setEntryPoint qualifiedAst.words)
                        |> Maybe.withDefault qualifiedAst.words

                setEntryPoint words entryPointName =
                    Dict.update
                        entryPointName






                        (Maybe.map (\w -> { w | metadata = Metadata.asEntryPoint w.metadata }))

                        words
            in
            case errs of
                [] ->
                    Done

                        { types = qualifiedAst.types
                        , words = wordsWithEntryPoint
                        }

                _ ->
                    Failed <| InternalError <| "Qualification failed with error: " ++ Debug.toString errs

        ( packageInfo, moduleName ) :: otherModules ->
            let
                ( path, fileName ) =
                    readModuleFromDisk packageInfo.path moduleName
            in






            Parsing state otherModules (ReadFile path fileName)