Cabal-2.1.0.0: A framework for packaging Haskell software

Safe HaskellNone
LanguageHaskell2010

Distribution.Compat.Parsec

Contents

Synopsis

Documentation

type Parsec s u = ParsecT s u Identity Source #

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) 
Instance details

Methods

get :: ParsecT s' u m s Source #

put :: s -> ParsecT s' u m () Source #

state :: (s -> (a, s)) -> ParsecT s' u m a Source #

MonadReader r m => MonadReader r (ParsecT s u m) 
Instance details

Methods

ask :: ParsecT s u m r Source #

local :: (r -> r) -> ParsecT s u m a -> ParsecT s u m a Source #

reader :: (r -> a) -> ParsecT s u m a Source #

MonadError e m => MonadError e (ParsecT s u m) 
Instance details

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) 
Instance details

Methods

lift :: Monad m => m a -> ParsecT s u m a Source #

Monad (ParsecT s u m) 
Instance details

Methods

(>>=) :: ParsecT s u m a -> (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 b Source #

return :: a -> ParsecT s u m a Source #

fail :: String -> ParsecT s u m a Source #

Functor (ParsecT s u m) 
Instance details

Methods

fmap :: (a -> b) -> ParsecT s u m a -> ParsecT s u m b Source #

(<$) :: a -> ParsecT s u m b -> ParsecT s u m a Source #

MonadFail (ParsecT s u m) 
Instance details

Methods

fail :: String -> ParsecT s u m a Source #

Applicative (ParsecT s u m) 
Instance details

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) 
Instance details

Methods

liftIO :: IO a -> ParsecT s u m a Source #

Alternative (ParsecT s u m) 
Instance details

Methods

empty :: ParsecT s u m a Source #

(<|>) :: ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a Source #

some :: ParsecT s u m a -> ParsecT s u m [a] Source #

many :: ParsecT s u m a -> ParsecT s u m [a] Source #

MonadPlus (ParsecT s u m) 
Instance details

Methods

mzero :: ParsecT s u m a Source #

mplus :: ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a Source #

MonadCont m => MonadCont (ParsecT s u m) 
Instance details

Methods

callCC :: ((a -> ParsecT s u m b) -> ParsecT s u m a) -> ParsecT s u m a Source #

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 Stream instance is responsible for maintaining the "position within the stream" in the stream state s. This is trivial unless you are using the monad in a non-trivial way.

Minimal complete definition

uncons

Instances
Monad m => Stream ByteString m Char 
Instance details
Monad m => Stream ByteString m Char 
Instance details
Monad m => Stream Text m Char 
Instance details

Methods

uncons :: Text -> m (Maybe (Char, Text)) Source #

Monad m => Stream Text m Char 
Instance details

Methods

uncons :: Text -> m (Maybe (Char, Text)) Source #

Monad m => Stream [tok] m tok 
Instance details

Methods

uncons :: [tok] -> m (Maybe (tok, [tok])) Source #

(<?>) :: 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.

optionMaybe :: Stream s m t => ParsecT s u m a -> ParsecT s u m (Maybe a) Source #

optionMaybe p tries to apply parser p. If p fails without consuming input, it return Nothing, otherwise it returns Just the value returned by 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 letter

If 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 letter

sepBy :: 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

integral :: (Stream s m Char, Integral a) => ParsecT s u m a Source #

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.

skipSpaces1 :: Stream s m Char => ParsecT s u m () Source #

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.

munch1 :: Stream s m Char => (Char -> Bool) -> ParsecT s u m String Source #

Greedily munch characters while predicate holds. Require at least one character.

oneOf :: Stream s m Char => [Char] -> ParsecT s u m Char Source #

oneOf cs succeeds if the current character is in the supplied list of characters cs. Returns the parsed character. See also satisfy.

  vowel  = oneOf "aeiou"