Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
MCSP.Text.ReadP
Description
Utilities for the ReadP
parser.
Synopsis
- readP :: Read a => ReadP a
- maybeP :: Maybe a -> ReadP a
- readEitherP :: ReadP a -> String -> Either String a
- readMaybeP :: ReadP a -> String -> Maybe a
- next :: (Char -> Bool) -> ReadP ()
- most :: ReadP a -> ReadP [a]
- skipInLine :: ReadP ()
- trimmed :: ReadP a -> ReadP a
- trim :: ReadP a -> ReadP a
- eol :: ReadP ()
- word :: ReadP String
- words :: ReadP [String]
- module Text.ParserCombinators.ReadP
Construction and application
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,"")]
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")]
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
module Text.ParserCombinators.ReadP