Copyright | (c) Artem Mavrin 2021 |
---|---|
License | BSD3 |
Maintainer | artemvmavrin@gmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | Safe |
Language | Haskell2010 |
Provides a lightweight, general-purpose parser type and some related combinators.
The API is deliberately nearly a subset of the parsec
API. One big
difference is the behavior of the Alternative
instace;
our instance always backtracks, so there is no need for a try
function.
Synopsis
- data Parser a
- parse :: Parser a -> String -> Maybe a
- charIf :: (Char -> Bool) -> Parser Char
- char :: Char -> Parser Char
- anyChar :: Parser Char
- oneOf :: [Char] -> Parser Char
- string :: String -> Parser String
- spaces :: Parser String
- between :: Parser b -> Parser c -> Parser a -> Parser a
- sepBy :: Parser a -> Parser b -> Parser [a]
- sepBy1 :: Parser a -> Parser b -> Parser [a]
- chainl1 :: Parser a -> Parser (a -> a -> a) -> Parser a
- chainr1 :: Parser a -> Parser (a -> a -> a) -> Parser a
- notFollowedBy :: Parser a -> Parser ()
- eof :: Parser ()
The Parser type
Running a parser
parse :: Parser a -> String -> Maybe a Source #
Run a parser on a string, discarding any unparsed suffix
Parser constructors
charIf :: (Char -> Bool) -> Parser Char Source #
Parse a single character if it satisfies a predicate
Examples
>>>
runParser (charIf isDigit) "123"
Just ("23",'1')
>>>
runParser (charIf isDigit) "abc"
Nothing
char :: Char -> Parser Char Source #
Parse a specific character.
Examples
>>>
runParser (char 'a') "abc"
Just ("bc",'a')
>>>
runParser (char 'a') "def"
Nothing
anyChar :: Parser Char Source #
Parse any character.
Examples
>>>
runParser anyChar "abc"
Just ("bc",'a')
>>>
runParser anyChar ""
Nothing
oneOf :: [Char] -> Parser Char Source #
Parse one out of a list of characters.
Examples
>>>
runParser (oneOf ['a', 'b']) "bar"
Just ("ar",'b')
>>>
runParser (oneOf ['a', 'b']) "foo"
Nothing
string :: String -> Parser String Source #
Parse a specific string.
Examples
>>>
runParser (string "foo") "foobar"
Just ("bar","foo")
>>>
runParser (string "foo") "fobar"
Nothing
spaces :: Parser String Source #
Parse zero or more spaces.
Examples
>>>
runParser spaces " 123"
Just ("123"," ")
>>>
runParser spaces "123"
Just ("123","")
>>>
runParser spaces ""
Just ("","")
Parser combinators
between :: Parser b -> Parser c -> Parser a -> Parser a Source #
(
runs between
open close p)open
, then p
, then close
, returning
the value parsed by p
.
sepBy :: Parser a -> Parser b -> Parser [a] Source #
(
runs zero or more occurrences of sepBy
p sep)p
, separated by
sep
, returning the list of values parsed by p
.
sepBy1 :: Parser a -> Parser b -> Parser [a] Source #
(
runs one or more occurrences of sepBy1
p sep)p
, separated by
sep
, returning the list of values parsed by p
.
chainl1 :: Parser a -> Parser (a -> a -> a) -> Parser a Source #
(
runs one or more occurrences of chainl1
p op)p
, separated by
op
, returning the value obtained by a left-associative application of the
functions parsed by op
to the values parsed by p
.
chainr1 :: Parser a -> Parser (a -> a -> a) -> Parser a Source #
(
runs one or more occurrences of chainr1
p op)p
, separated by
op
, returning the value obtained by a right-associative application of the
functions parsed by op
to the values parsed by p
.
notFollowedBy :: Parser a -> Parser () Source #
(
succeeds if and only if notFollowedBy
p)p
fails.