Stabel

Check-in [70148502b3]
Login
Overview
Comment:Parser now understands basic strings.
Timelines: family | ancestors | descendants | both | strings
Files: files | file ages | folders
SHA3-256: 70148502b355587df8a055432ff5a733db571d76cfafa9e8a1deccdda15acabb
User & Date: robin.hansen on 2021-09-03 18:23:43
Other Links: branch diff | manifest | tags
Context
2021-09-04
10:40
String parser now understands escape sequences. check-in: a54cd89678 user: robin.hansen tags: strings
2021-09-03
18:23
Parser now understands basic strings. check-in: 70148502b3 user: robin.hansen tags: strings
2021-09-01
05:19
Fix issues found with elm-review. check-in: f7ed530f30 user: robin.hansen tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/Parser.elm from [040268e985] to [cc05849f2a].

82
83
84
85
86
87
88

89
90
91
92
93
94
95
...
116
117
118
119
120
121
122


123
124
125
126
127
128
129
...
262
263
264
265
266
267
268

















269
270
271
272
273
274
275
....
1053
1054
1055
1056
1057
1058
1059







1060
1061
1062
1063
1064
1065
1066
    | PackageFunction SourceLocationRange (List String) String
    | ExternalFunction SourceLocationRange (List String) String
    | InlineFunction SourceLocationRange (List AstNode)
    | ConstructType String
    | GetMember String String
    | SetMember String String
    | ArrayLiteral SourceLocationRange (List AstNode)



run : String -> String -> Result (List (Parser.DeadEnd Context Problem)) AST
run ref sourceCode =
    Parser.run (parser ref) sourceCode


................................................................................
        , '['
        , ']'
        , '('
        , ')'
        , '.'
        , '#'
        , '/'


        ]


whitespaceChars : Set Char
whitespaceChars =
    Set.fromList
        [ ' '
................................................................................


textParser : Parser String
textParser =
    Parser.chompWhile (\c -> not <| Set.member c whitespaceChars)
        |> Parser.getChompedString



















genericParser : Parser String
genericParser =
    Parser.oneOf
        [ Parser.succeed identity
            |. Parser.symbol (Token "-" UnknownError)
            |> Parser.andThen (\_ -> Parser.problem ExpectedGeneric)
................................................................................
        , Parser.succeed (\startLoc arrContent endLoc -> Parser.Loop (ArrayLiteral (SourceLocationRange startLoc endLoc) arrContent :: nodes))
            |= sourceLocationParser
            |. Parser.symbol (Token "{" ExpectedLeftCurly)
            |. noiseParser
            |= implementationParser
            |. Parser.symbol (Token "}" ExpectedRightCurly)
            |= sourceLocationParser







            |. noiseParser
        , Parser.succeed (Parser.Done (List.reverse nodes))
        ]


nodeParser : Parser AstNode
nodeParser =







>







 







>
>







 







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







 







>
>
>
>
>
>
>







82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
...
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
....
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
    | PackageFunction SourceLocationRange (List String) String
    | ExternalFunction SourceLocationRange (List String) String
    | InlineFunction SourceLocationRange (List AstNode)
    | ConstructType String
    | GetMember String String
    | SetMember String String
    | ArrayLiteral SourceLocationRange (List AstNode)
    | StringLiteral SourceLocationRange String


run : String -> String -> Result (List (Parser.DeadEnd Context Problem)) AST
run ref sourceCode =
    Parser.run (parser ref) sourceCode


................................................................................
        , '['
        , ']'
        , '('
        , ')'
        , '.'
        , '#'
        , '/'
        , '\''
        , '"'
        ]


whitespaceChars : Set Char
whitespaceChars =
    Set.fromList
        [ ' '
................................................................................


textParser : Parser String
textParser =
    Parser.chompWhile (\c -> not <| Set.member c whitespaceChars)
        |> Parser.getChompedString


stringParser : Parser String
stringParser =
    Parser.loop "" stringParserLoop


stringParserLoop : String -> Parser (Parser.Step String String)
stringParserLoop str =
    Parser.oneOf
        [ Parser.succeed (Parser.Done str)
            |. Parser.symbol (Token "\"" UnknownError)
        , Parser.succeed ()
            |. Parser.chompIf (always True) UnknownError
            |> Parser.getChompedString
            |> Parser.map (\chompedStr -> Parser.Loop <| str ++ chompedStr)
        ]


genericParser : Parser String
genericParser =
    Parser.oneOf
        [ Parser.succeed identity
            |. Parser.symbol (Token "-" UnknownError)
            |> Parser.andThen (\_ -> Parser.problem ExpectedGeneric)
................................................................................
        , Parser.succeed (\startLoc arrContent endLoc -> Parser.Loop (ArrayLiteral (SourceLocationRange startLoc endLoc) arrContent :: nodes))
            |= sourceLocationParser
            |. Parser.symbol (Token "{" ExpectedLeftCurly)
            |. noiseParser
            |= implementationParser
            |. Parser.symbol (Token "}" ExpectedRightCurly)
            |= sourceLocationParser
            |. noiseParser
        , Parser.succeed (\startLoc strContent endLoc -> Parser.Loop (StringLiteral (SourceLocationRange startLoc endLoc) strContent :: nodes))
            |= sourceLocationParser
            |. Parser.symbol (Token "\"" UnknownError)
            |= stringParser
            -- stringParser chomps the final "
            |= sourceLocationParser
            |. noiseParser
        , Parser.succeed (Parser.Done (List.reverse nodes))
        ]


nodeParser : Parser AstNode
nodeParser =

Modified src/Stabel/Qualifier.elm from [39463bf45b] to [65bd5476f4].

1464
1465
1466
1467
1468
1469
1470




1471
1472
1473
1474
1475
1476
1477
                        | qualifiedNodes =
                            Ok (ArrayLiteral (mapLoc loc) qualifiedNodes)
                                :: acc.qualifiedNodes
                    }

                Err err ->
                    { acc | qualifiedNodes = Err err :: acc.qualifiedNodes }






isMultiFunction : Parser.FunctionDefinition -> Bool
isMultiFunction def =
    case def.implementation of
        Parser.SoloImpl _ ->
            False







>
>
>
>







1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
                        | qualifiedNodes =
                            Ok (ArrayLiteral (mapLoc loc) qualifiedNodes)
                                :: acc.qualifiedNodes
                    }

                Err err ->
                    { acc | qualifiedNodes = Err err :: acc.qualifiedNodes }

        Parser.StringLiteral _ _ ->
            -- TODO
            acc


isMultiFunction : Parser.FunctionDefinition -> Bool
isMultiFunction def =
    case def.implementation of
        Parser.SoloImpl _ ->
            False

Modified tests/Test/Parser.elm from [7f783df7cb] to [5cec99b89a].

1175
1176
1177
1178
1179
1180
1181









1182






















                in
                case compileRetainLocations source of
                    Err _ ->
                        Expect.fail "Did not expect compilation to fail."

                    Ok ast ->
                        Expect.equal expectedAst ast









        ]





























>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
                in
                case compileRetainLocations source of
                    Err _ ->
                        Expect.fail "Did not expect compilation to fail."

                    Ok ast ->
                        Expect.equal expectedAst ast
        , describe "Strings"
            [ test "Simple case" <|
                \_ ->
                    let
                        source =
                            """
                            def: str
                            : "This is a test"
                            """

                        expectedAst =
                            { sourceReference = ""
                            , moduleDefinition = ModuleDefinition.Undefined
                            , types = Dict.empty
                            , functions =
                                Dict.fromListBy .name
                                    [ { name = "str"
                                      , typeSignature = AssociatedFunctionSignature.NotProvided
                                      , sourceLocationRange = Nothing
                                      , aliases = Dict.empty
                                      , imports = Dict.empty
                                      , implementation =
                                            SoloImpl
                                                [ AST.StringLiteral emptyRange "This is a test"
                                                ]
                                      }
                                    ]
                            }
                    in
                    expectAst source expectedAst
            ]
        ]

Modified tests/Test/Parser/Util.elm from [38c3a75d10] to [69e6ba697c].

81
82
83
84
85
86
87



88
89
90
91
92
93
94

        AST.InlineFunction _ val ->
            AST.InlineFunction emptyRange (List.map stripNodeLocation val)

        AST.ArrayLiteral _ val ->
            AST.ArrayLiteral emptyRange (List.map stripNodeLocation val)




        _ ->
            node


stripMultiWordBranchLocation : ( TypeMatch, List AstNode ) -> ( TypeMatch, List AstNode )
stripMultiWordBranchLocation ( typeMatch, nodes ) =
    ( stripTypeMatchLocation typeMatch







>
>
>







81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

        AST.InlineFunction _ val ->
            AST.InlineFunction emptyRange (List.map stripNodeLocation val)

        AST.ArrayLiteral _ val ->
            AST.ArrayLiteral emptyRange (List.map stripNodeLocation val)

        AST.StringLiteral _ val ->
            AST.StringLiteral emptyRange val

        _ ->
            node


stripMultiWordBranchLocation : ( TypeMatch, List AstNode ) -> ( TypeMatch, List AstNode )
stripMultiWordBranchLocation ( typeMatch, nodes ) =
    ( stripTypeMatchLocation typeMatch