This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

Parser.String

Pre-made parsers and helpers to create parsers for strings

string : Parser Char -> Parser String

Like oneOrMore, but convets the resulting list of characters to a string

word : String -> Parser String

Parse an exact word

module Parser.String exposing (string, word)

{-| Pre-made parsers and helpers to create parsers for strings

@docs string, word

-}

import Parser as P exposing (Parser, Problem(..), (|=), (|*))
import Parser.Char as PChar
import Parser.Combinator as PComb
import String


{-| Like `oneOrMore`, but convets the resulting list of characters to a string
-}
string : Parser Char -> Parser String
string parser =
    P.succeed String.fromList
        |= PComb.oneOrMore parser


{-| Parse an exact word
-}
word : String -> Parser String
word s =
    wordHelper s s


wordHelper : String -> String -> Parser String
wordHelper base s =
    case String.uncons s of
        Just ( c, subS ) ->
            P.succeed String.cons
                |= PChar.charCustomError c
                    (Bad <|
                        "In the word '"
                            ++ base
                            ++ "', expected '"
                            ++ (String.fromChar c)
                            ++ "'"
                    )
                |= wordHelper base subS

        Nothing ->
            P.succeed ""