Play

Check-in [8ef7064bf4]
Login
Overview
Comment:Write test for parsing module definitions, and fix for the discovered bugs.
Timelines: family | ancestors | descendants | both | module-definition
Files: files | file ages | folders
SHA3-256: 8ef7064bf4c5c1e0cb9212f360f67a8df5d575520f40ed21e073e792b61430bc
User & Date: robin.hansen on 2021-04-29 13:10:30
Other Links: branch diff | manifest | tags
Context
2021-05-01
11:28
Parser recognizes that functions can define their own aliases and imports. check-in: 1c4627dc1c user: robin.hansen tags: module-definition
2021-04-29
13:10
Write test for parsing module definitions, and fix for the discovered bugs. check-in: 8ef7064bf4 user: robin.hansen tags: module-definition
2021-04-21
08:26
Add (untested) parser for module definitions. check-in: 6bae4df17f user: robin.hansen tags: module-definition
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Play/Parser.elm from [8d3a0e8e1f] to [a347bc2548].

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
..
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
...
263
264
265
266
267
268
269
270
271















272
273
274
275
276
277
278
...
439
440
441
442
443
444
445

446
447
448
449
450
451
452
...
462
463
464
465
466
467
468
469
470
471
472
473
474
475

476
477
478
479
480
481
482
    , words : Dict String WordDefinition
    }


type alias ModuleDefinition =
    { aliases : Dict String String
    , imports : Dict String (List String)
    , exposes : List String
    }


type TypeDefinition
    = CustomTypeDef SourceLocationRange String (List String) (List ( String, Type ))
    | UnionTypeDef SourceLocationRange String (List String) (List Type)

................................................................................
-- ATOMS


emptyModuleDefinition : ModuleDefinition
emptyModuleDefinition =
    { aliases = Dict.empty
    , imports = Dict.empty
    , exposes = []
    }


validSymbolChar : Char -> Bool
validSymbolChar c =
    not <| Set.member c invalidSymbolChars

................................................................................
        ]
        |. noiseParser
        |> Parser.andThen identity


modulePathStringParser : Parser String
modulePathStringParser =
    Parser.loop [] modulePathParser
        |> Parser.map (\( path, name ) -> String.join "/" (path ++ [ name ]))

















modulePathParser : List String -> Parser (Parser.Step (List String) ( List String, String ))
modulePathParser symbols =
    Parser.oneOf
        [ Parser.succeed (\name -> Parser.Loop (name :: symbols))
            |. Parser.symbol (Token "/" NotMetadata)
................................................................................
        |. Parser.end ExpectedEnd


moduleDefinitionParser : Parser ModuleDefinition
moduleDefinitionParser =
    Parser.succeed identity
        |. Parser.keyword (Token "defmodule:" NoProblem)

        |= Parser.loop emptyModuleDefinition moduleDefinitionMetaParser


moduleDefinitionMetaParser : ModuleDefinition -> Parser (Parser.Step ModuleDefinition ModuleDefinition)
moduleDefinitionMetaParser def =
    Parser.oneOf
        [ Parser.succeed (\alias value -> Parser.Loop { def | aliases = Dict.insert alias value def.aliases })
................................................................................
            |= modulePathStringParser
            |. noiseParser
            |= Parser.loop [] symbolImplListParser
            |. noiseParser
        , Parser.succeed (\exposings -> Parser.Loop { def | exposes = exposings })
            |. Parser.keyword (Token "exposing:" NoProblem)
            |. noiseParser
            |= Parser.loop [] symbolImplListParser
            |. noiseParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser
            |> Parser.andThen Parser.problem
        , Parser.succeed (Parser.Done def)
            |. Parser.keyword (Token ":" NoProblem)

        ]


definitionParser : AST -> Parser (Parser.Step AST AST)
definitionParser ast =
    let
        insertWord wordDef =







|







 







|







 







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







 







>







 







|






>







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
..
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
...
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
...
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
...
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
    , words : Dict String WordDefinition
    }


type alias ModuleDefinition =
    { aliases : Dict String String
    , imports : Dict String (List String)
    , exposes : Set String
    }


type TypeDefinition
    = CustomTypeDef SourceLocationRange String (List String) (List ( String, Type ))
    | UnionTypeDef SourceLocationRange String (List String) (List Type)

................................................................................
-- ATOMS


emptyModuleDefinition : ModuleDefinition
emptyModuleDefinition =
    { aliases = Dict.empty
    , imports = Dict.empty
    , exposes = Set.empty
    }


validSymbolChar : Char -> Bool
validSymbolChar c =
    not <| Set.member c invalidSymbolChars

................................................................................
        ]
        |. noiseParser
        |> Parser.andThen identity


modulePathStringParser : Parser String
modulePathStringParser =
    Parser.oneOf
        [ Parser.succeed identity
            |= symbolImplParser
            |> Parser.andThen (\sym -> Parser.loop sym moduleRefParser)
        , Parser.succeed identity
            |= Parser.loop "" moduleRefParser
        ]


moduleRefParser : String -> Parser (Parser.Step String String)
moduleRefParser path =
    Parser.oneOf
        [ Parser.succeed (\part -> Parser.Loop (path ++ "/" ++ part))
            |. Parser.symbol (Token "/" NotMetadata)
            |= symbolImplParser
        , Parser.succeed (Parser.Done path)
        ]


modulePathParser : List String -> Parser (Parser.Step (List String) ( List String, String ))
modulePathParser symbols =
    Parser.oneOf
        [ Parser.succeed (\name -> Parser.Loop (name :: symbols))
            |. Parser.symbol (Token "/" NotMetadata)
................................................................................
        |. Parser.end ExpectedEnd


moduleDefinitionParser : Parser ModuleDefinition
moduleDefinitionParser =
    Parser.succeed identity
        |. Parser.keyword (Token "defmodule:" NoProblem)
        |. noiseParser
        |= Parser.loop emptyModuleDefinition moduleDefinitionMetaParser


moduleDefinitionMetaParser : ModuleDefinition -> Parser (Parser.Step ModuleDefinition ModuleDefinition)
moduleDefinitionMetaParser def =
    Parser.oneOf
        [ Parser.succeed (\alias value -> Parser.Loop { def | aliases = Dict.insert alias value def.aliases })
................................................................................
            |= modulePathStringParser
            |. noiseParser
            |= Parser.loop [] symbolImplListParser
            |. noiseParser
        , Parser.succeed (\exposings -> Parser.Loop { def | exposes = exposings })
            |. Parser.keyword (Token "exposing:" NoProblem)
            |. noiseParser
            |= (Parser.loop [] symbolImplListParser |> Parser.map Set.fromList)
            |. noiseParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser
            |> Parser.andThen Parser.problem
        , Parser.succeed (Parser.Done def)
            |. Parser.keyword (Token ":" NoProblem)
            |. noiseParser
        ]


definitionParser : AST -> Parser (Parser.Step AST AST)
definitionParser ast =
    let
        insertWord wordDef =

Modified tests/Test/Parser.elm from [64a5198555] to [33d6843342].

3
4
5
6
7
8
9

10
11
12
13
14
15
16
....
1040
1041
1042
1043
1044
1045
1046














1047























































import Dict
import Dict.Extra as Dict
import Expect
import Play.Data.Metadata as Metadata
import Play.Data.SourceLocation exposing (SourceLocation, SourceLocationRange, emptyRange)
import Play.Data.Type as Type
import Play.Parser as AST exposing (..)

import Test exposing (Test, describe, test)
import Test.Parser.Util exposing (addFunctionsForStructs, compile, compileRetainLocations, expectCompiles)


suite : Test
suite =
    describe "Parser"
................................................................................
                in
                case compileRetainLocations source of
                    Err _ ->
                        Expect.fail "Did not expect compilation to fail."

                    Ok ast ->
                        Expect.equal expectedAst ast














        ]






























































>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
....
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
import Dict
import Dict.Extra as Dict
import Expect
import Play.Data.Metadata as Metadata
import Play.Data.SourceLocation exposing (SourceLocation, SourceLocationRange, emptyRange)
import Play.Data.Type as Type
import Play.Parser as AST exposing (..)
import Set
import Test exposing (Test, describe, test)
import Test.Parser.Util exposing (addFunctionsForStructs, compile, compileRetainLocations, expectCompiles)


suite : Test
suite =
    describe "Parser"
................................................................................
                in
                case compileRetainLocations source of
                    Err _ ->
                        Expect.fail "Did not expect compilation to fail."

                    Ok ast ->
                        Expect.equal expectedAst ast
        , describe "Modules"
            [ test "Module definition" <|
                \_ ->
                    let
                        source =
                            """
                            defmodule:
                            alias: other /some/mod
                            alias: moar local/mod
                            import: /some/other/mod test1 word2
                            import: internals foo
                            import: internal/mod
                            exposing: inc
                            :

                            defstruct: Pair a b
                            : first a
                            : second b

                            def: inc
                            : 1 +
                            """

                        expectedAst =
                            { moduleDefinition =
                                { aliases =
                                    Dict.fromList
                                        [ ( "other", "/some/mod" )
                                        , ( "moar", "local/mod" )
                                        ]
                                , imports =
                                    Dict.fromList
                                        [ ( "/some/other/mod", [ "test1", "word2" ] )
                                        , ( "internals", [ "foo" ] )
                                        , ( "internal/mod", [] )
                                        ]
                                , exposes = Set.fromList [ "inc" ]
                                }
                            , types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ CustomTypeDef
                                        emptyRange
                                        "Pair"
                                        [ "a", "b" ]
                                        [ ( "first", Type.Generic "a" )
                                        , ( "second", Type.Generic "b" )
                                        ]
                                    ]
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "inc"
                                      , metadata = Metadata.default
                                      , implementation =
                                            SoloImpl
                                                [ AST.Integer emptyRange 1
                                                , AST.Word emptyRange "+"
                                                ]
                                      }
                                    ]
                            }
                                |> addFunctionsForStructs
                    in
                    case compile source of
                        Err err ->
                            Expect.fail <| "Did not expect parsing to fail: " ++ Debug.toString err

                        Ok ast ->
                            Expect.equal expectedAst ast
            ]
        ]

Modified tests/Test/Parser/Util.elm from [d299e71bfb] to [6ce6ed116d].

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
compileRetainLocations str =
    String.unindent str
        |> run


stripLocations : AST -> AST
stripLocations ast =
    { moduleDefinition = emptyModuleDefinition
    , types = Dict.map (\_ t -> stripTypeLocation t) ast.types
    , words = Dict.map (\_ d -> stripWordLocation d) ast.words
    }


stripTypeLocation : TypeDefinition -> TypeDefinition
stripTypeLocation typeDef =







|







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
compileRetainLocations str =
    String.unindent str
        |> run


stripLocations : AST -> AST
stripLocations ast =
    { moduleDefinition = ast.moduleDefinition
    , types = Dict.map (\_ t -> stripTypeLocation t) ast.types
    , words = Dict.map (\_ d -> stripWordLocation d) ast.words
    }


stripTypeLocation : TypeDefinition -> TypeDefinition
stripTypeLocation typeDef =