| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Distribution.Compat.Parsec
Contents
Synopsis
- type Parsec s u = ParsecT s u Identity
- data ParsecT s u (m :: * -> *) a :: * -> * -> (* -> *) -> * -> *
- class Monad m => Stream s (m :: * -> *) t | s -> t
- (<?>) :: ParsecT s u m a -> String -> ParsecT s u m a
- runParser :: Stream s Identity t => Parsec s u a -> u -> SourceName -> s -> Either ParseError a
- between :: Stream s m t => ParsecT s u m open -> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a
- option :: Stream s m t => a -> ParsecT s u m a -> ParsecT s u m a
- optional :: Stream s m t => ParsecT s u m a -> ParsecT s u m ()
- optionMaybe :: Stream s m t => ParsecT s u m a -> ParsecT s u m (Maybe a)
- try :: ParsecT s u m a -> ParsecT s u m a
- sepBy :: Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
- sepBy1 :: Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
- choice :: Stream s m t => [ParsecT s u m a] -> ParsecT s u m a
- eof :: (Stream s m t, Show t) => ParsecT s u m ()
- integral :: (Stream s m Char, Integral a) => ParsecT s u m a
- char :: Stream s m Char => Char -> ParsecT s u m Char
- anyChar :: Stream s m Char => ParsecT s u m Char
- satisfy :: Stream s m Char => (Char -> Bool) -> ParsecT s u m Char
- space :: Stream s m Char => ParsecT s u m Char
- spaces :: Stream s m Char => ParsecT s u m ()
- skipSpaces1 :: Stream s m Char => ParsecT s u m ()
- string :: Stream s m Char => String -> ParsecT s u m String
- munch :: Stream s m Char => (Char -> Bool) -> ParsecT s u m String
- munch1 :: Stream s m Char => (Char -> Bool) -> ParsecT s u m String
- oneOf :: Stream s m Char => [Char] -> ParsecT s u m Char
Documentation
data ParsecT s u (m :: * -> *) a :: * -> * -> (* -> *) -> * -> * Source #
ParserT monad transformer and Parser type
ParsecT s u m a is a parser with stream type s, user state type u,
underlying monad m and return type a. Parsec is strict in the user state.
If this is undesirable, simply used a data type like data Box a = Box a and
the state type Box YourStateType to add a level of indirection.
Instances
| MonadState s m => MonadState s (ParsecT s' u m) | |
| MonadReader r m => MonadReader r (ParsecT s u m) | |
| MonadError e m => MonadError e (ParsecT s u m) | |
Methods throwError :: e -> ParsecT s u m a Source # catchError :: ParsecT s u m a -> (e -> ParsecT s u m a) -> ParsecT s u m a Source # | |
| MonadTrans (ParsecT s u) | |
| Monad (ParsecT s u m) | |
| Functor (ParsecT s u m) | |
| MonadFail (ParsecT s u m) | |
| Applicative (ParsecT s u m) | |
Methods pure :: a -> ParsecT s u m a Source # (<*>) :: ParsecT s u m (a -> b) -> ParsecT s u m a -> ParsecT s u m b Source # liftA2 :: (a -> b -> c) -> ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m c Source # (*>) :: ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m b Source # (<*) :: ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m a Source # | |
| MonadIO m => MonadIO (ParsecT s u m) | |
| Alternative (ParsecT s u m) | |
| MonadPlus (ParsecT s u m) | |
| MonadCont m => MonadCont (ParsecT s u m) | |
class Monad m => Stream s (m :: * -> *) t | s -> t Source #
An instance of Stream has stream type s, underlying monad m and token type t determined by the stream
Some rough guidelines for a "correct" instance of Stream:
- unfoldM uncons gives the [t] corresponding to the stream
- A
Streaminstance is responsible for maintaining the "position within the stream" in the stream states. This is trivial unless you are using the monad in a non-trivial way.
Minimal complete definition
Instances
| Monad m => Stream ByteString m Char | |
Methods uncons :: ByteString -> m (Maybe (Char, ByteString)) Source # | |
| Monad m => Stream ByteString m Char | |
Methods uncons :: ByteString -> m (Maybe (Char, ByteString)) Source # | |
| Monad m => Stream Text m Char | |
| Monad m => Stream Text m Char | |
| Monad m => Stream [tok] m tok | |
(<?>) :: ParsecT s u m a -> String -> ParsecT s u m a infix 0 Source #
The parser p <?> msg behaves as parser p, but whenever the
parser p fails without consuming any input, it replaces expect
error messages with the expect error message msg.
This is normally used at the end of a set alternatives where we want
to return an error message in terms of a higher level construct
rather than returning all possible characters. For example, if the
expr parser from the try example would fail, the error
message is: '...: expecting expression'. Without the (<?>)
combinator, the message would be like '...: expecting "let" or
letter', which is less friendly.
runParser :: Stream s Identity t => Parsec s u a -> u -> SourceName -> s -> Either ParseError a Source #
The most general way to run a parser over the Identity monad. runParser p state filePath
input runs parser p on the input list of tokens input,
obtained from source filePath with the initial user state st.
The filePath is only used in error messages and may be the empty
string. Returns either a ParseError (Left) or a
value of type a (Right).
parseFromFile p fname
= do{ input <- readFile fname
; return (runParser p () fname input)
}Combinators
between :: Stream s m t => ParsecT s u m open -> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a Source #
between open close p parses open, followed by p and close.
Returns the value returned by p.
braces = between (symbol "{") (symbol "}")option :: Stream s m t => a -> ParsecT s u m a -> ParsecT s u m a Source #
option x p tries to apply parser p. If p fails without
consuming input, it returns the value x, otherwise the value
returned by p.
priority = option 0 (do{ d <- digit
; return (digitToInt d)
})optional :: Stream s m t => ParsecT s u m a -> ParsecT s u m () Source #
optional p tries to apply parser p. It will parse p or nothing.
It only fails if p fails after consuming input. It discards the result
of p.
try :: ParsecT s u m a -> ParsecT s u m a Source #
The parser try p behaves like parser p, except that it
pretends that it hasn't consumed any input when an error occurs.
This combinator is used whenever arbitrary look ahead is needed.
Since it pretends that it hasn't consumed any input when p fails,
the (<|>) combinator will try its second alternative even when the
first parser failed while consuming input.
The try combinator can for example be used to distinguish
identifiers and reserved words. Both reserved words and identifiers
are a sequence of letters. Whenever we expect a certain reserved
word where we can also expect an identifier we have to use the try
combinator. Suppose we write:
expr = letExpr <|> identifier <?> "expression"
letExpr = do{ string "let"; ... }
identifier = many1 letterIf the user writes "lexical", the parser fails with: unexpected
'x', expecting 't' in "let". Indeed, since the (<|>) combinator
only tries alternatives when the first alternative hasn't consumed
input, the identifier parser is never tried (because the prefix
"le" of the string "let" parser is already consumed). The
right behaviour can be obtained by adding the try combinator:
expr = letExpr <|> identifier <?> "expression"
letExpr = do{ try (string "let"); ... }
identifier = many1 lettersepBy :: Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a] Source #
sepBy p sep parses zero or more occurrences of p, separated
by sep. Returns a list of values returned by p.
commaSep p = p `sepBy` (symbol ",")
sepBy1 :: Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a] Source #
sepBy1 p sep parses one or more occurrences of p, separated
by sep. Returns a list of values returned by p.
choice :: Stream s m t => [ParsecT s u m a] -> ParsecT s u m a Source #
choice ps tries to apply the parsers in the list ps in order,
until one of them succeeds. Returns the value of the succeeding
parser.
eof :: (Stream s m t, Show t) => ParsecT s u m () Source #
This parser only succeeds at the end of the input. This is not a
primitive parser but it is defined using notFollowedBy.
eof = notFollowedBy anyToken <?> "end of input"
Char
char :: Stream s m Char => Char -> ParsecT s u m Char Source #
char c parses a single character c. Returns the parsed
character (i.e. c).
semiColon = char ';'
anyChar :: Stream s m Char => ParsecT s u m Char Source #
This parser succeeds for any character. Returns the parsed character.
satisfy :: Stream s m Char => (Char -> Bool) -> ParsecT s u m Char Source #
The parser satisfy f succeeds for any character for which the
supplied function f returns True. Returns the character that is
actually parsed.
space :: Stream s m Char => ParsecT s u m Char Source #
Parses a white space character (any character which satisfies isSpace)
Returns the parsed character.
spaces :: Stream s m Char => ParsecT s u m () Source #
Skips zero or more white space characters. See also skipMany.
string :: Stream s m Char => String -> ParsecT s u m String Source #
string s parses a sequence of characters given by s. Returns
the parsed string (i.e. s).
divOrMod = string "div"
<|> string "mod"munch :: Stream s m Char => (Char -> Bool) -> ParsecT s u m String Source #
Greedely munch characters while predicate holds. Always succeeds.