hackport-0.7.2.2: Hackage and Portage integration tool
Copyright(c) Edward Kmett 2011
LicenseBSD3
Maintainerekmett@gmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Distribution.Compat.CharParsing

Description

Parsers for character streams

Originally in parsers package.

Synopsis

Combinators

oneOf :: CharParsing m => [Char] -> 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"

noneOf :: CharParsing m => [Char] -> m Char Source #

As the dual of oneOf, noneOf cs succeeds if the current character is not in the supplied list of characters cs. Returns the parsed character.

 consonant = noneOf "aeiou"

spaces :: CharParsing m => m () Source #

Skips zero or more white space characters. See also skipMany.

space :: CharParsing m => m Char Source #

Parses a white space character (any character which satisfies isSpace) Returns the parsed character.

newline :: CharParsing m => m Char Source #

Parses a newline character ('\n'). Returns a newline character.

tab :: CharParsing m => m Char Source #

Parses a tab character ('\t'). Returns a tab character.

upper :: CharParsing m => m Char Source #

Parses an upper case letter. Returns the parsed character.

lower :: CharParsing m => m Char Source #

Parses a lower case character. Returns the parsed character.

alphaNum :: CharParsing m => m Char Source #

Parses a letter or digit. Returns the parsed character.

letter :: CharParsing m => m Char Source #

Parses a letter (an upper case or lower case character). Returns the parsed character.

digit :: CharParsing m => m Char Source #

Parses a digit. Returns the parsed character.

hexDigit :: CharParsing m => m Char Source #

Parses a hexadecimal digit (a digit or a letter between 'a' and 'f' or 'A' and 'F'). Returns the parsed character.

octDigit :: CharParsing m => m Char Source #

Parses an octal digit (a character between '0' and '7'). Returns the parsed character.

Class

class Parsing m => CharParsing m where Source #

Additional functionality needed to parse character streams.

Minimal complete definition

satisfy

Methods

satisfy :: (Char -> Bool) -> m Char Source #

Parse a single character of the input, with UTF-8 decoding

char :: Char -> m Char Source #

char c parses a single character c. Returns the parsed character (i.e. c).

e.g.

semiColon = char ';'

notChar :: Char -> m Char Source #

notChar c parses any single character other than c. Returns the parsed character.

anyChar :: m Char Source #

This parser succeeds for any character. Returns the parsed character.

string :: String -> 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"

text :: Text -> m Text Source #

text t parses a sequence of characters determined by the text t Returns the parsed text fragment (i.e. t).

Using OverloadedStrings:

 divOrMod    =   text "div"
             <|> text "mod"

Instances

Instances details
CharParsing ParsecParser Source # 
Instance details

Defined in Distribution.Parsec

(CharParsing m, MonadPlus m) => CharParsing (IdentityT m) Source # 
Instance details

Defined in Distribution.Compat.CharParsing

(CharParsing m, MonadPlus m) => CharParsing (ReaderT e m) Source # 
Instance details

Defined in Distribution.Compat.CharParsing

(CharParsing m, MonadPlus m) => CharParsing (StateT s m) Source # 
Instance details

Defined in Distribution.Compat.CharParsing

(CharParsing m, MonadPlus m) => CharParsing (StateT s m) Source # 
Instance details

Defined in Distribution.Compat.CharParsing

(CharParsing m, MonadPlus m, Monoid w) => CharParsing (WriterT w m) Source # 
Instance details

Defined in Distribution.Compat.CharParsing

(CharParsing m, MonadPlus m, Monoid w) => CharParsing (WriterT w m) Source # 
Instance details

Defined in Distribution.Compat.CharParsing

Stream s m Char => CharParsing (ParsecT s u m) Source # 
Instance details

Defined in Distribution.Compat.CharParsing

(CharParsing m, MonadPlus m, Monoid w) => CharParsing (RWST r w s m) Source # 
Instance details

Defined in Distribution.Compat.CharParsing

Methods

satisfy :: (Char -> Bool) -> RWST r w s m Char Source #

char :: Char -> RWST r w s m Char Source #

notChar :: Char -> RWST r w s m Char Source #

anyChar :: RWST r w s m Char Source #

string :: String -> RWST r w s m String Source #

text :: Text -> RWST r w s m Text Source #

(CharParsing m, MonadPlus m, Monoid w) => CharParsing (RWST r w s m) Source # 
Instance details

Defined in Distribution.Compat.CharParsing

Methods

satisfy :: (Char -> Bool) -> RWST r w s m Char Source #

char :: Char -> RWST r w s m Char Source #

notChar :: Char -> RWST r w s m Char Source #

anyChar :: RWST r w s m Char Source #

string :: String -> RWST r w s m String Source #

text :: Text -> RWST r w s m Text Source #

Cabal additions

signedIntegral :: (CharParsing m, Integral a) => m a Source #

Accepts negative (starting with -) and positive (without sign) integral numbers.

Since: 3.4.0.0

munch1 :: CharParsing m => (Char -> Bool) -> m String Source #

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

munch :: CharParsing m => (Char -> Bool) -> m String Source #

Greedely munch characters while predicate holds. Always succeeds.

Parsing Combinators

choice :: Alternative m => [m a] -> 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.

option :: Alternative m => a -> m a -> 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 (digitToInt <$> digit)

optional :: Alternative f => f a -> f (Maybe a) #

One or none.

skipOptional :: Alternative m => m a -> m () Source #

skipOptional 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. (Plays the role of parsec's optional, which conflicts with Applicative's optional)

between :: Applicative m => m bra -> m ket -> m a -> m a Source #

between open close p parses open, followed by p and close. Returns the value returned by p.

 braces  = between (symbol "{") (symbol "}")

some :: Alternative f => f a -> f [a] #

One or more.

many :: Alternative f => f a -> f [a] #

Zero or more.

sepBy :: Alternative m => m a -> m sep -> 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 ",")

sepByNonEmpty :: Alternative m => m a -> m sep -> m (NonEmpty a) Source #

sepByNonEmpty p sep parses one or more occurrences of p, separated by sep. Returns a non-empty list of values returned by p.

sepEndByNonEmpty :: Alternative m => m a -> m sep -> m (NonEmpty a) Source #

sepEndByNonEmpty p sep parses one or more occurrences of p, separated and optionally ended by sep. Returns a non-empty list of values returned by p.

sepEndBy :: Alternative m => m a -> m sep -> m [a] Source #

sepEndBy p sep parses zero or more occurrences of p, separated and optionally ended by sep, ie. haskell style statements. Returns a list of values returned by p.

 haskellStatements  = haskellStatement `sepEndBy` semi

endByNonEmpty :: Alternative m => m a -> m sep -> m (NonEmpty a) Source #

endByNonEmpty p sep parses one or more occurrences of p, separated and ended by sep. Returns a non-empty list of values returned by p.

endBy :: Alternative m => m a -> m sep -> m [a] Source #

endBy p sep parses zero or more occurrences of p, separated and ended by sep. Returns a list of values returned by p.

  cStatements  = cStatement `endBy` semi

count :: Applicative m => Int -> m a -> m [a] Source #

count n p parses n occurrences of p. If n is smaller or equal to zero, the parser equals to return []. Returns a list of n values returned by p.

chainl :: Alternative m => m a -> m (a -> a -> a) -> a -> m a Source #

chainl p op x parses zero or more occurrences of p, separated by op. Returns a value obtained by a left associative application of all functions returned by op to the values returned by p. If there are zero occurrences of p, the value x is returned.

chainr :: Alternative m => m a -> m (a -> a -> a) -> a -> m a Source #

chainr p op x parses zero or more occurrences of p, separated by op Returns a value obtained by a right associative application of all functions returned by op to the values returned by p. If there are no occurrences of p, the value x is returned.

chainl1 :: Alternative m => m a -> m (a -> a -> a) -> m a Source #

chainl1 p op x parses one or more occurrences of p, separated by op Returns a value obtained by a left associative application of all functions returned by op to the values returned by p. . This parser can for example be used to eliminate left recursion which typically occurs in expression grammars.

 expr   = term   `chainl1` addop
 term   = factor `chainl1` mulop
 factor = parens expr <|> integer

 mulop  = (*) <$ symbol "*"
      <|> div <$ symbol "/"

 addop  = (+) <$ symbol "+"
      <|> (-) <$ symbol "-"

chainr1 :: Alternative m => m a -> m (a -> a -> a) -> m a Source #

chainr1 p op x parses one or more occurrences of p, separated by op Returns a value obtained by a right associative application of all functions returned by op to the values returned by p.

manyTill :: Alternative m => m a -> m end -> m [a] Source #

manyTill p end applies parser p zero or more times until parser end succeeds. Returns the list of values returned by p. This parser can be used to scan comments:

 simpleComment   = do{ string "<!--"
                     ; manyTill anyChar (try (string "-->"))
                     }

Note the overlapping parsers anyChar and string "-->", and therefore the use of the try combinator.

Parsing Class

class Alternative m => Parsing m where Source #

Additional functionality needed to describe parsers independent of input type.

Minimal complete definition

try, (<?>), unexpected, eof, notFollowedBy

Methods

try :: m a -> m a Source #

Take a parser that may consume input, and on failure, go back to where we started and fail as if we didn't consume input.

(<?>) :: m a -> String -> m a infixr 0 Source #

Give a parser a name

skipMany :: m a -> m () Source #

A version of many that discards its input. Specialized because it can often be implemented more cheaply.

skipSome :: m a -> m () Source #

skipSome p applies the parser p one or more times, skipping its result. (aka skipMany1 in parsec)

unexpected :: String -> m a Source #

Used to emit an error on an unexpected token

eof :: 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 anyChar <?> "end of input"

notFollowedBy :: Show a => m a -> m () Source #

notFollowedBy p only succeeds when parser p fails. This parser does not consume any input. This parser can be used to implement the 'longest match' rule. For example, when recognizing keywords (for example let), we want to make sure that a keyword is not followed by a legal identifier character, in which case the keyword is actually an identifier (for example lets). We can program this behaviour as follows:

 keywordLet  = try $ string "let" <* notFollowedBy alphaNum

Instances

Instances details
Parsing ParsecParser Source # 
Instance details

Defined in Distribution.Parsec

(Parsing m, Monad m) => Parsing (IdentityT m) Source # 
Instance details

Defined in Distribution.Compat.Parsing

(Parsing m, MonadPlus m) => Parsing (ReaderT e m) Source # 
Instance details

Defined in Distribution.Compat.Parsing

Methods

try :: ReaderT e m a -> ReaderT e m a Source #

(<?>) :: ReaderT e m a -> String -> ReaderT e m a Source #

skipMany :: ReaderT e m a -> ReaderT e m () Source #

skipSome :: ReaderT e m a -> ReaderT e m () Source #

unexpected :: String -> ReaderT e m a Source #

eof :: ReaderT e m () Source #

notFollowedBy :: Show a => ReaderT e m a -> ReaderT e m () Source #

(Parsing m, MonadPlus m) => Parsing (StateT s m) Source # 
Instance details

Defined in Distribution.Compat.Parsing

Methods

try :: StateT s m a -> StateT s m a Source #

(<?>) :: StateT s m a -> String -> StateT s m a Source #

skipMany :: StateT s m a -> StateT s m () Source #

skipSome :: StateT s m a -> StateT s m () Source #

unexpected :: String -> StateT s m a Source #

eof :: StateT s m () Source #

notFollowedBy :: Show a => StateT s m a -> StateT s m () Source #

(Parsing m, MonadPlus m) => Parsing (StateT s m) Source # 
Instance details

Defined in Distribution.Compat.Parsing

Methods

try :: StateT s m a -> StateT s m a Source #

(<?>) :: StateT s m a -> String -> StateT s m a Source #

skipMany :: StateT s m a -> StateT s m () Source #

skipSome :: StateT s m a -> StateT s m () Source #

unexpected :: String -> StateT s m a Source #

eof :: StateT s m () Source #

notFollowedBy :: Show a => StateT s m a -> StateT s m () Source #

(Parsing m, MonadPlus m, Monoid w) => Parsing (WriterT w m) Source # 
Instance details

Defined in Distribution.Compat.Parsing

Methods

try :: WriterT w m a -> WriterT w m a Source #

(<?>) :: WriterT w m a -> String -> WriterT w m a Source #

skipMany :: WriterT w m a -> WriterT w m () Source #

skipSome :: WriterT w m a -> WriterT w m () Source #

unexpected :: String -> WriterT w m a Source #

eof :: WriterT w m () Source #

notFollowedBy :: Show a => WriterT w m a -> WriterT w m () Source #

(Parsing m, MonadPlus m, Monoid w) => Parsing (WriterT w m) Source # 
Instance details

Defined in Distribution.Compat.Parsing

Methods

try :: WriterT w m a -> WriterT w m a Source #

(<?>) :: WriterT w m a -> String -> WriterT w m a Source #

skipMany :: WriterT w m a -> WriterT w m () Source #

skipSome :: WriterT w m a -> WriterT w m () Source #

unexpected :: String -> WriterT w m a Source #

eof :: WriterT w m () Source #

notFollowedBy :: Show a => WriterT w m a -> WriterT w m () Source #

(Stream s m t, Show t) => Parsing (ParsecT s u m) Source # 
Instance details

Defined in Distribution.Compat.Parsing

Methods

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

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

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

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

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

eof :: ParsecT s u m () Source #

notFollowedBy :: Show a => ParsecT s u m a -> ParsecT s u m () Source #

(Parsing m, MonadPlus m, Monoid w) => Parsing (RWST r w s m) Source # 
Instance details

Defined in Distribution.Compat.Parsing

Methods

try :: RWST r w s m a -> RWST r w s m a Source #

(<?>) :: RWST r w s m a -> String -> RWST r w s m a Source #

skipMany :: RWST r w s m a -> RWST r w s m () Source #

skipSome :: RWST r w s m a -> RWST r w s m () Source #

unexpected :: String -> RWST r w s m a Source #

eof :: RWST r w s m () Source #

notFollowedBy :: Show a => RWST r w s m a -> RWST r w s m () Source #

(Parsing m, MonadPlus m, Monoid w) => Parsing (RWST r w s m) Source # 
Instance details

Defined in Distribution.Compat.Parsing

Methods

try :: RWST r w s m a -> RWST r w s m a Source #

(<?>) :: RWST r w s m a -> String -> RWST r w s m a Source #

skipMany :: RWST r w s m a -> RWST r w s m () Source #

skipSome :: RWST r w s m a -> RWST r w s m () Source #

unexpected :: String -> RWST r w s m a Source #

eof :: RWST r w s m () Source #

notFollowedBy :: Show a => RWST r w s m a -> RWST r w s m () Source #