Play

Check-in [1c4627dc1c]
Login
Overview
Comment:Parser recognizes that functions can define their own aliases and imports.
Timelines: family | ancestors | descendants | both | module-definition
Files: files | file ages | folders
SHA3-256: 1c4627dc1cd0276fafc78f3e811429411fd9426ef73a0fdf3a4a0f87a856d88d
User & Date: robin.hansen on 2021-05-01 11:28:26
Other Links: branch diff | manifest | tags
Context
2021-05-01
11:49
Removed entry: true syntax from function definition. Fixes [48cf587779]. check-in: 52304dc684 user: robin.hansen tags: module-definition
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Play/Data/Metadata.elm from [66ac0fbdbc] to [3efee805a7].

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
..
46
47
48
49
50
51
52










module Play.Data.Metadata exposing (..)


import Play.Data.SourceLocation exposing (SourceLocationRange)
import Play.Data.Type exposing (Type)
import Play.Data.TypeSignature exposing (TypeSignature(..))


type alias Metadata =
    { isEntryPoint : Bool
    , type_ : TypeSignature
    , isQuoted : Bool
    , sourceLocationRange : Maybe SourceLocationRange


    }


default : Metadata
default =
    { isEntryPoint = False
    , type_ = NotProvided
    , isQuoted = False
    , sourceLocationRange = Nothing


    }


asEntryPoint : Metadata -> Metadata
asEntryPoint meta =
    { meta | isEntryPoint = True }

................................................................................
withSourceLocationRange range meta =
    { meta | sourceLocationRange = Just range }


clearSourceLocationRange : Metadata -> Metadata
clearSourceLocationRange meta =
    { meta | sourceLocationRange = Nothing }












>










>
>









>
>







 







>
>
>
>
>
>
>
>
>
>
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
..
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Play.Data.Metadata exposing (..)

import Dict exposing (Dict)
import Play.Data.SourceLocation exposing (SourceLocationRange)
import Play.Data.Type exposing (Type)
import Play.Data.TypeSignature exposing (TypeSignature(..))


type alias Metadata =
    { isEntryPoint : Bool
    , type_ : TypeSignature
    , isQuoted : Bool
    , sourceLocationRange : Maybe SourceLocationRange
    , aliases : Dict String String
    , imports : Dict String (List String)
    }


default : Metadata
default =
    { isEntryPoint = False
    , type_ = NotProvided
    , isQuoted = False
    , sourceLocationRange = Nothing
    , aliases = Dict.empty
    , imports = Dict.empty
    }


asEntryPoint : Metadata -> Metadata
asEntryPoint meta =
    { meta | isEntryPoint = True }

................................................................................
withSourceLocationRange range meta =
    { meta | sourceLocationRange = Just range }


clearSourceLocationRange : Metadata -> Metadata
clearSourceLocationRange meta =
    { meta | sourceLocationRange = Nothing }


withAlias : String -> String -> Metadata -> Metadata
withAlias ali val meta =
    { meta | aliases = Dict.insert ali val meta.aliases }


withImport : String -> List String -> Metadata -> Metadata
withImport ns vals meta =
    { meta | imports = Dict.insert ns vals meta.imports }

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

665
666
667
668
669
670
671














672
673
674
675
676
677
678
            |. Parser.keyword (Token "type:" NoProblem)
            |. noiseParser
            |= typeSignatureParser
        , Parser.succeed (Parser.Loop { def | metadata = { metadata | isEntryPoint = True } })
            |. Parser.keyword (Token "entry:" NoProblem)
            |. noiseParser
            |. Parser.keyword (Token "true" NoProblem)














            |. noiseParser
        , Parser.succeed (\impl -> Parser.Loop { def | implementation = SoloImpl impl })
            |. Parser.keyword (Token ":" NoProblem)
            |. noiseParser
            |= implementationParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser







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







665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
            |. Parser.keyword (Token "type:" NoProblem)
            |. noiseParser
            |= typeSignatureParser
        , Parser.succeed (Parser.Loop { def | metadata = { metadata | isEntryPoint = True } })
            |. Parser.keyword (Token "entry:" NoProblem)
            |. noiseParser
            |. Parser.keyword (Token "true" NoProblem)
            |. noiseParser
        , Parser.succeed (\alias value -> Parser.Loop { def | metadata = Metadata.withAlias alias value def.metadata })
            |. Parser.keyword (Token "alias:" NoProblem)
            |. noiseParser
            |= symbolParser
            |. noiseParser
            |= modulePathStringParser
            |. noiseParser
        , Parser.succeed (\mod vals -> Parser.Loop { def | metadata = Metadata.withImport mod vals def.metadata })
            |. Parser.keyword (Token "import:" NoProblem)
            |. noiseParser
            |= modulePathStringParser
            |. noiseParser
            |= Parser.loop [] symbolImplListParser
            |. noiseParser
        , Parser.succeed (\impl -> Parser.Loop { def | implementation = SoloImpl impl })
            |. Parser.keyword (Token ":" NoProblem)
            |. noiseParser
            |= implementationParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser

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

1094
1095
1096
1097
1098
1099
1100

















































1101
1102
1103
1104
1105
1106
1107
                                        ]
                                    ]
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "inc"
                                      , metadata = Metadata.default
                                      , implementation =

















































                                            SoloImpl
                                                [ AST.Integer emptyRange 1
                                                , AST.Word emptyRange "+"
                                                ]
                                      }
                                    ]
                            }







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







1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
                                        ]
                                    ]
                            , 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
            , test "Functions can have its own aliases and imports" <|
                \_ ->
                    let
                        source =
                            """
                            def: inc
                            alias: other /some/mod
                            alias: moar local/mod
                            import: /some/other/mod test1 word2
                            import: internals foo
                            import: internal/mod
                            : 1 +
                            """

                        expectedAst =
                            { moduleDefinition = AST.emptyModuleDefinition
                            , types = Dict.empty
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "inc"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.withAlias "other" "/some/mod"
                                                |> Metadata.withAlias "moar" "local/mod"
                                                |> Metadata.withImport
                                                    "/some/other/mod"
                                                    [ "test1", "word2" ]
                                                |> Metadata.withImport
                                                    "internals"
                                                    [ "foo" ]
                                                |> Metadata.withImport
                                                    "internal/mod"
                                                    []
                                      , implementation =
                                            SoloImpl
                                                [ AST.Integer emptyRange 1
                                                , AST.Word emptyRange "+"
                                                ]
                                      }
                                    ]
                            }