Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Various flatparse helpers and combinators.
Synopsis
- data ParserWarning
- runParserMaybe :: Parser e a -> ByteString -> Maybe a
- runParserEither :: IsString e => Parser e a -> ByteString -> Either e a
- runParserWarn :: Parser ByteString a -> ByteString -> These ParserWarning a
- runParser_ :: Parser String a -> ByteString -> a
- runParser :: Parser e a -> ByteString -> Result e a
- type Parser = ParserT PureMode
- data Result e a
- = OK a !ByteString
- | Fail
- | Err !e
- isWhitespace :: Char -> Bool
- ws_ :: Parser e ()
- ws :: Parser e Char
- wss :: Parser e ByteString
- nota :: Char -> Parser e ByteString
- isa :: (Char -> Bool) -> Parser e ByteString
- sq :: ParserT st e ()
- dq :: ParserT st e ()
- wrappedDq :: Parser b ByteString
- wrappedSq :: Parser b ByteString
- wrappedQ :: Parser e ByteString
- wrappedQNoGuard :: Parser e a -> Parser e a
- eq :: Parser e ()
- sep :: Parser e s -> Parser e a -> Parser e [a]
- bracketed :: Parser e b -> Parser e b -> Parser e a -> Parser e a
- bracketedSB :: Parser e [Char]
- wrapped :: Parser e () -> Parser e a -> Parser e a
- digit :: Parser e Int
- int :: Parser e Int
- double :: Parser e Double
- signed :: Num b => Parser e b -> Parser e b
- byteStringOf' :: Parser e a -> Parser e ByteString
- comma :: Parser e ()
Parsing
data ParserWarning Source #
Warnings covering leftovers, Err
s and Fail
>>>
runParserWarn ws " x"
These (ParserLeftover "x") ' '
>>>
runParserWarn ws "x"
This ParserUncaught
>>>
runParserWarn (ws `cut` "no whitespace") "x"
This (ParserError "no whitespace")
Instances
runParserMaybe :: Parser e a -> ByteString -> Maybe a Source #
runParserEither :: IsString e => Parser e a -> ByteString -> Either e a Source #
runParserWarn :: Parser ByteString a -> ByteString -> These ParserWarning a Source #
Run parser, returning leftovers and errors as ParserWarning
s.
>>>
runParserWarn ws " "
That ' '
>>>
runParserWarn ws "x"
This ParserUncaught
>>>
runParserWarn ws " x"
These (ParserLeftover "x") ' '
runParser_ :: Parser String a -> ByteString -> a Source #
Run parser, discards leftovers & throws an error on failure.
>>>
runParser_ ws " "
' '
>>>
runParser_ ws "x"
*** Exception: uncaught parse error ...
Flatparse re-exports
runParser :: Parser e a -> ByteString -> Result e a #
Run a parser.
Higher-level boxed data type for parsing results.
OK a !ByteString | Contains return value and unconsumed input. |
Fail | Recoverable-by-default failure. |
Err !e | Unrecoverble-by-default error. |
Parsers
isWhitespace :: Char -> Bool Source #
\n \t \f \r and space
Consume whitespace.
>>>
runParser ws_ " \nx"
OK () "x"
>>>
runParser ws_ "x"
OK () "x"
wss :: Parser e ByteString Source #
multiple whitespace
>>>
runParser wss " \nx"
OK " \n" "x"
>>>
runParser wss "x"
Fail
nota :: Char -> Parser e ByteString Source #
Parse whilst not a specific character
>>>
runParser (nota 'x') "abcxyz"
OK "abc" "xyz"
isa :: (Char -> Bool) -> Parser e ByteString Source #
Parse whilst satisfying a predicate.
>>>
runParser (isa (=='x')) "xxxabc"
OK "xxx" "abc"
wrappedDq :: Parser b ByteString Source #
A double-quoted string.
wrappedSq :: Parser b ByteString Source #
A single-quoted string.
wrappedQ :: Parser e ByteString Source #
A single-quoted or double-quoted string.
>>>
runParserMaybe wrappedQ "\"quoted\""
Just "quoted"
>>>
runParserMaybe wrappedQ "'quoted'"
Just "quoted"
wrappedQNoGuard :: Parser e a -> Parser e a Source #
A single-quoted or double-quoted wrapped parser.
>>>
runParser (wrappedQNoGuard (many $ satisfy (/= '"'))) "\"name\""
OK "name" ""
Will consume quotes if the underlying parser does.
>>>
runParser (wrappedQNoGuard (many anyChar)) "\"name\""
Fail
xml production [25]
>>>
runParserMaybe eq " = "
Just ()
>>>
runParserMaybe eq "="
Just ()
sep :: Parser e s -> Parser e a -> Parser e [a] Source #
Some with a separator.
>>>
runParser (sep ws (many (satisfy (/= ' ')))) "a b c"
OK ["a","b","c"] ""
bracketed :: Parser e b -> Parser e b -> Parser e a -> Parser e a Source #
Parser bracketed by two other parsers.
>>>
runParser (bracketed ($(char '[')) ($(char ']')) (many (satisfy (/= ']')))) "[bracketed]"
OK "bracketed" ""
bracketedSB :: Parser e [Char] Source #
Parser bracketed by square brackets.
>>>
runParser bracketedSB "[bracketed]"
OK "bracketed" ""
wrapped :: Parser e () -> Parser e a -> Parser e a Source #
Parser wrapped by another parser.
>>>
runParser (wrapped ($(char '"')) (many (satisfy (/= '"')))) "\"wrapped\""
OK "wrapped" ""
double :: Parser e Double Source #
A Double
parser.
>>>
runParser double "1.234x"
OK 1.234 "x"
>>>
runParser double "."
Fail
>>>
runParser double "123"
OK 123.0 ""
>>>
runParser double ".123"
OK 0.123 ""
>>>
runParser double "123."
OK 123.0 ""
signed :: Num b => Parser e b -> Parser e b Source #
Parser for a signed prefix to a number.
>>>
runParser (signed double) "-1.234x"
OK (-1.234) "x"
byteStringOf' :: Parser e a -> Parser e ByteString Source #
byteStringOf
but using withSpan internally. Doesn't seems faster...