Parsers convert text sequences into a data structure, for example for handling user input and compiling programming languages.
With this parser combinator you can build parsers in a modular way, building parsers by combining parser functions. Parser functions can either succeed or fail.
{-| Parse a optional sign, succeeds with a -1 if it matches a minus `Char`, otherwise it returns 1 -}sign:ParserIntsign =let
minus =
map (always -1)(symbol '-')
plus =
map (always 1)(symbol '+')in
optional (or plus minus)1{-| Parse a digit -}digit:ParserIntdigit =let
charToInt c =Char.toCode c -Char.toCode '0'in
map charToInt (satisfy Char.isDigit){-| Parse a natural number -}natural:ParserIntnatural =
some digit
|> map (List.foldl (\b a -> a *10+ b)0){-| Parse an integer with optional sign -}integer:ParserIntinteger =
map (*) sign
|> andMap natural