Portability | portable |
---|---|
Stability | experimental |
Maintainer | grantslatton@gmail.com |
Safe Haskell | Safe |
A simple and comprehensive Haskell parsing library.
- class (Eq s, Monoid s) => Stream s t | s -> t where
- data Result s a
- data Parser s a
- justParse :: Stream s t => Parser s a -> s -> Maybe a
- runParser :: Parser s a -> s -> [Result s a]
- finalize :: (Eq s, Monoid s) => [Result s a] -> [Result s a]
- extend :: (Eq s, Monoid s) => Maybe s -> [Result s a] -> [Result s a]
- isDone :: Result s a -> Bool
- isFail :: Result s a -> Bool
- isPartial :: Result s a -> Bool
- rename :: String -> Parser s a -> Parser s a
- (<?>) :: Parser s a -> String -> Parser s a
- test :: Parser s a -> Parser s Bool
- greedy :: Stream s t => Parser s a -> Parser s a
- option :: a -> Parser s a -> Parser s a
- satisfy :: Stream s t => (t -> Bool) -> Parser s t
- mN :: Int -> Int -> Parser s a -> Parser s [a]
- many :: Parser s a -> Parser s [a]
- many1 :: Parser s a -> Parser s [a]
- manyN :: Int -> Parser s a -> Parser s [a]
- atLeast :: Int -> Parser s a -> Parser s [a]
- exactly :: Int -> Parser s a -> Parser s [a]
- eof :: (Eq s, Monoid s) => Parser s ()
- oneOf :: (Eq t, Stream s t) => [t] -> Parser s t
- noneOf :: (Eq t, Stream s t) => [t] -> Parser s t
- anyToken :: Stream s t => Parser s t
- lookAhead :: Parser s a -> Parser s a
- char :: Stream s Char => Char -> Parser s Char
- anyChar :: Stream s Char => Parser s Char
- ascii :: Stream s Char => Parser s Char
- latin1 :: Stream s Char => Parser s Char
- control :: Stream s Char => Parser s Char
- space :: Stream s Char => Parser s Char
- lower :: Stream s Char => Parser s Char
- upper :: Stream s Char => Parser s Char
- alpha :: Stream s Char => Parser s Char
- alphaNum :: Stream s Char => Parser s Char
- print :: Stream s Char => Parser s Char
- digit :: Stream s Char => Parser s Char
- octDigit :: Stream s Char => Parser s Char
- hexDigit :: Stream s Char => Parser s Char
- eol :: Stream s Char => Parser s String
- string :: Stream s Char => String -> Parser s String
- regex :: Stream s Char => String -> Parser s Match
- regex' :: Stream s Char => String -> Parser s String
- data Match = Match {}
Overview
- Allows for parsing arbitrary
Stream
types - Makes extensive use of combinators
- Returns relatively verbose
Fail
ure messages. - Allows one to
rename
aParser
. - Allows for a parser to return a
Partial
Result
- Non-greedy parsing
- Returns a list of all possible parses
- Allows for conversion of a
regex
to a parser
Quickstart Examples
Simple char and string parsing
This parser will only accept the string "hello world"
p = do h <- char 'h' --Parses the character 'h' rest <- string "ello world" --Parses the string "ello world" exp <- char '!' --Parses the character '!' return ([h]++rest++[exp]) --Returns all of the above concatenated together
Basic combinatorial parsing
This parser will accept the string "hello woooooorld"
with any number of o
's.
It returns the number of o
's.
p = do first <- string "hello w" --Parses the string "hello w" os <- many1 (char 'o') --Applies the parser "char 'o'" one or more times second <- string "rld" --Parses the string "rld" return (length os) --Return the number of o's parsed
Recursive combinatorial parsing
This parser will turn a string of comma separated values into a list of them
csv = do v <- greedy (many (noneOf ",")) --Parses as many non-comma characters as possible vs <- option [] (char ',' >> csv) --Optionally parses a comma and a csv, returning the empty list upon failure return (v:vs) --Concatenates and returns the full list
General Parsing
class (Eq s, Monoid s) => Stream s t | s -> t whereSource
A Stream
instance has a stream of type s
, made up of tokens of
type t
, which must be determinable by the stream.
uncons :: Stream s t => s -> Maybe (t, s)Source
uncons
returns Nothing
if the Stream
is empty, otherwise it
returns the first token of the stream, followed by the remainder
of the stream, wrapped in a Just
.
length :: Stream s t => s -> IntSource
The default length
implementation is O(n). If your stream provides
a more efficient method for determining the length, it is wise to
override this. The length
method is only used by the greedy
parser.
Eq t => Stream [t] t | Makes common types such as Strings into a Stream. |
rename :: String -> Parser s a -> Parser s aSource
rename
pushes a new error message onto the stack in case of failure.
This is particularly useful when debugging a complex Parser
.
Generic Parsers
test :: Parser s a -> Parser s BoolSource
Return True
if the Parser
would succeed if one were to apply it,
otherwise, False
.
greedy :: Stream s t => Parser s a -> Parser s aSource
Modifies a Parser
so that it will ony return the most consumptive
succesful results. If there are no successful results, it will only
return the most consumptive failures. One can use greedy
to emulate
parsers from Parsec
or attoparsec
.
option :: a -> Parser s a -> Parser s aSource
Attempts to apply a parser and returns a default value if it fails.
mN :: Int -> Int -> Parser s a -> Parser s [a]Source
Parse from m
to n
occurences of a Parser
. Let n
be negative
if one wishes for no upper bound.
lookAhead :: Parser s a -> Parser s aSource
Applies the parser and returns its result, but resets the leftovers as if it consumed nothing.
Char Parsers
eol :: Stream s Char => Parser s StringSource
Parses until a newline, carriage return + newline, or newline + carriage return.