Copyright | Copyright Waived |
---|---|
License | PublicDomain |
Maintainer | grantslatton@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | Safe |
Language | Haskell98 |
- runParser :: Parser s a -> s -> [Result s a]
- parseOnly :: Stream s t => Parser s a -> s -> Maybe a
- extend :: (Eq s, Monoid s) => Maybe s -> [Result s a] -> [Result s a]
- finalize :: (Eq s, Monoid s) => [Result s a] -> [Result s a]
- data Parser s a
- data Result s a
- class (Eq s, Monoid s) => Stream s t | s -> t where
Overview
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" ex <- char '!' --Parses the character '!' return ([h]++rest++[ex]) --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 string "hello w" --Parses the string "hello w" os <- many1 (char 'o') --Applies the parser "char 'o'" one or more times 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. Note that we could use the sepBy
parser, but this demonstrates a
recursive parser.
csv = do v <- 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
class (Eq s, Monoid s) => Stream s t | s -> t where Source
A Stream
instance has a stream of type s
, made up of tokens of
type t
, which must be determinable by the stream. A minimal complete
definition only needs to define uncons
.
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 -> Int Source
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.