Stabel

Check-in [a54cd89678]
Login
Overview
Comment:String parser now understands escape sequences.
Timelines: family | ancestors | descendants | both | strings
Files: files | file ages | folders
SHA3-256: a54cd89678f05161955d7910b8faf5c0b5f03f6dd28461059e591ffa54c9d362
User & Date: robin.hansen on 2021-09-04 10:40:51
Other Links: branch diff | manifest | tags
Context
2021-09-04
11:41
Error handling for string parser. check-in: 3dd9b18af1 user: robin.hansen tags: strings
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/Parser.elm from [cc05849f2a] to [046d437afe].

276
277
278
279
280
281
282












283
284
285
286
287
288
289


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)
        ]









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







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


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


Modified tests/Test/Parser.elm from [5cec99b89a] to [0256c93b49].

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


























            ]
        ]







|
|
|




|
<







 







|






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


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
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
                in
                case compileRetainLocations source of
                    Err _ ->
                        Expect.fail "Did not expect compilation to fail."

                    Ok ast ->
                        Expect.equal expectedAst ast
        , describe "Strings" <|
            let
                expectParseString input output =
                    let
                        source =
                            """
                            def: str
                            : """ ++ "\"" ++ input ++ "\""


                        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 output
                                                ]
                                      }
                                    ]
                            }
                    in
                    expectAst source expectedAst
            in
            [ test "Simple case" <|
                \_ ->
                    expectParseString
                        "This is a test"
                        "This is a test"
            , test "Newline escape sequence" <|
                \_ ->
                    expectParseString
                        "This is a test with\\n a newline"
                        "This is a test with\n a newline"
            , test "Tab escape sequence" <|
                \_ ->
                    expectParseString
                        "This is a test with\\t a tab"
                        "This is a test with\t a tab"
            , test "Backslash escape sequence" <|
                \_ ->
                    expectParseString
                        "This is a test with\\\\ a backslash"
                        "This is a test with\\ a backslash"
            , test "Double-quote escape sequence" <|
                \_ ->
                    expectParseString
                        "This is a \\\"test\\\" with a double quote"
                        "This is a \"test\" with a double quote"
            ]
        ]