mcsp-algorithms-0.1.0: Algorithms for Minimum Common String Partition (MCSP) in Haskell.
Safe HaskellSafe-Inferred
LanguageGHC2021

MCSP.Text.ReadP

Description

Utilities for the ReadP parser.

Synopsis

Construction and application

readP :: Read a => ReadP a Source #

ReadP parser using the Read instance.

>>> import Data.Int
>>> readP_to_S (readP @Int) "12"
[(12,"")]
>>> readP_to_S (readP @Int) "33XY"
[(33,"XY")]
>>> readP_to_S (readP @Int) "ZX"
[]

maybeP :: Maybe a -> ReadP a Source #

Lift a Maybe to the ReadP monad.

Nothing causes a parse error.

>>> readP_to_S (maybeP $ Just 12) ""
[(12,"")]
>>> readP_to_S (maybeP $ Nothing) ""
[]

readEitherP :: ReadP a -> String -> Either String a Source #

Parse a string using the given ReadP parser instance.

Succeeds if there is exactly one valid result. A Left value indicates a parse error.

>>> import Data.Int
>>> readEitherP (readP @Int) "123"
Right 123
>>> readEitherP (readP @Int) "hello"
Left "no parse on \"hello\""

readMaybeP :: ReadP a -> String -> Maybe a Source #

Parse a string using the given ReadP parser instance.

Succeeds if there is exactly one valid result. A Nothing value indicates a parse error.

>>> import Data.Int
>>> readMaybeP (readP @Int) "123"
Just 123
>>> readMaybeP (readP @Int) "hello"
Nothing

Additional operations

next :: (Char -> Bool) -> ReadP () Source #

Succeeds iff the next character matches the given predicate, without consuming it.

>>> readP_to_S (next (== 'x')) "x"
[((),"x")]
>>> readP_to_S (next (== 'x')) "y"
[]
>>> readP_to_S (next (== 'x')) ""
[]

most :: ReadP a -> ReadP [a] Source #

Parses zero or more occurrences of the given parser.

Like many, but succeds only once, with as many matches as possible.

>>> readP_to_S (most word) " abc def ghi "
[(["abc","def","ghi"],"")]
>>> readP_to_S (many word) " abc def ghi "
[([]," abc def ghi "),(["abc"],"def ghi "),(["abc","def"],"ghi "),(["abc","def","ghi"],"")]

Whitespace adapters

skipInLine :: ReadP () Source #

Skip whitespace, but not the end of line.

>>> readP_to_S skipInLine "   "
[((),"")]
>>> readP_to_S (skipInLine *> satisfy (/= '\n')) "  A"
[('A',"")]
>>> readP_to_S skipInLine "  \n"
[((),"\n")]

trimmed :: ReadP a -> ReadP a Source #

Updates the parser so it guarantees that no whitespace precedes or succedes the matched text.

>>> import Data.Int
>>> readP_to_S (readP @Int) " 12 "
[(12," ")]
>>> readP_to_S (trimmed $ readP @Int) " 12 "
[]

trim :: ReadP a -> ReadP a Source #

Updates the parser so it used only in a trimmed part of the text, removing outer whitespace.

>>> import Data.Int
>>> readP_to_S (readP @Int) " 12 "
[(12," ")]
>>> readP_to_S (trim $ readP @Int) " 12 "
[(12,"")]

eol :: ReadP () Source #

Matches the end-of-line.

End-of-line can be eof or 'n'.

>>> readP_to_S eol ""
[((),"")]
>>> readP_to_S eol "a"
[]
>>> readP_to_S eol "\na"
[((),"\na")]

word :: ReadP String Source #

Matches a single word.

>>> readP_to_S word "  abc "
[("abc","")]
>>> readP_to_S word "  abc def "
[("abc","def ")]
>>> readP_to_S word "  "
[]

words :: ReadP [String] Source #

Matches a list of words separated by whitespaces.

>>> readP_to_S words "12 3 4"
[([],"12 3 4"),(["12"],"3 4"),(["12","3"],"4"),(["12","3","4"],"")]

Re-export