Stabel

Check-in [b65fbd80ac]
Login
Overview
Comment:Strip indentation from multiline strings.
Timelines: family | ancestors | descendants | both | strings
Files: files | file ages | folders
SHA3-256: b65fbd80ac5931523c227c2b0977a6dc71972255ee6e581e583912745f8f6bf7
User & Date: robin.hansen on 2021-09-05 09:54:32
Other Links: branch diff | manifest | tags
Context
2021-09-05
16:10
Most definitions can now have documentation strings attached. Missing for module definitions. check-in: 882b9bc963 user: robin.hansen tags: strings
09:54
Strip indentation from multiline strings. check-in: b65fbd80ac user: robin.hansen tags: strings
09:36
Implemented multiline strings except for indentation handling. check-in: f0c47e6da7 user: robin.hansen tags: strings
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/Parser.elm from [387a3a8a3a] to [f1d2db574c].

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
...
340
341
342
343
344
345
346

















































347
348
349
350
351
352
353
    Parser.chompWhile (\c -> not <| Set.member c whitespaceChars)
        |> Parser.getChompedString


stringParser : Parser String
stringParser =
    Parser.oneOf
        [ Parser.succeed identity
            |. Parser.symbol (Token "\"\"\"" UnknownError)
            |= Parser.loop (Just "") multilineStringParserLoop
        , Parser.succeed identity
            |. Parser.symbol (Token "\"" UnknownError)
            |= Parser.loop (Just "") stringParserLoop
        ]

................................................................................
                    |. Parser.end StringNotTerminated
                , Parser.succeed ()
                    |. Parser.chompWhile (\c -> c /= '"')
                    |> Parser.getChompedString
                    |> Parser.map (\chompedStr -> Parser.Loop <| Just <| str ++ chompedStr)
                ]



















































genericParser : Parser String
genericParser =
    Parser.oneOf
        [ Parser.succeed identity
            |. Parser.symbol (Token "-" UnknownError)
            |> Parser.andThen (\_ -> Parser.problem ExpectedGeneric)







|







 







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







269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
...
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
    Parser.chompWhile (\c -> not <| Set.member c whitespaceChars)
        |> Parser.getChompedString


stringParser : Parser String
stringParser =
    Parser.oneOf
        [ Parser.succeed stripMultilineStringWhitespace
            |. Parser.symbol (Token "\"\"\"" UnknownError)
            |= Parser.loop (Just "") multilineStringParserLoop
        , Parser.succeed identity
            |. Parser.symbol (Token "\"" UnknownError)
            |= Parser.loop (Just "") stringParserLoop
        ]

................................................................................
                    |. Parser.end StringNotTerminated
                , Parser.succeed ()
                    |. Parser.chompWhile (\c -> c /= '"')
                    |> Parser.getChompedString
                    |> Parser.map (\chompedStr -> Parser.Loop <| Just <| str ++ chompedStr)
                ]


stripMultilineStringWhitespace : String -> String
stripMultilineStringWhitespace str =
    let
        noLeadingNewline =
            if String.startsWith "\n" str then
                String.dropLeft 1 str

            else
                str

        noEndingNewline =
            if String.endsWith "\n" noLeadingNewline then
                String.dropRight 1 noLeadingNewline

            else
                noLeadingNewline

        linesWithLeadingWhitespace =
            String.lines noEndingNewline
                |> List.map (\line -> ( countLeadingWhitespace line, line ))

        linesWithStrippedWhitespace =
            case linesWithLeadingWhitespace of
                [] ->
                    []

                ( maxWhitespace, _ ) :: _ ->
                    List.map
                        (stripMaxWhitespace maxWhitespace)
                        linesWithLeadingWhitespace
    in
    String.join "\n" linesWithStrippedWhitespace


countLeadingWhitespace : String -> Int
countLeadingWhitespace str =
    case String.uncons str of
        Just ( ' ', rest ) ->
            1 + countLeadingWhitespace rest

        _ ->
            0


stripMaxWhitespace : Int -> ( Int, String ) -> String
stripMaxWhitespace max ( whitespaceCount, line ) =
    String.dropLeft (min max whitespaceCount) line


genericParser : Parser String
genericParser =
    Parser.oneOf
        [ Parser.succeed identity
            |. Parser.symbol (Token "-" UnknownError)
            |> Parser.andThen (\_ -> Parser.problem ExpectedGeneric)