Parsec.hs revision 2cc613eee3aaa5d21c75aa50cdb681ab57069a7d
d9a45a35cd696085be1a038b2cc67bee6819c574cmaederModule : $Header$
8a77240a809197c92c0736c431b4b88947a7bac1Christian MaederDescription : Parsec extensions
adea2e45fa61f1097aadc490a0aeaf4831b729ccChristian MaederCopyright : (c) Christian Maeder, DFKI GmbH 2010
e9458b1a7a19a63aa4c179f9ab20f4d50681c168Jens ElknerLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
09b431a868c79a92ae7c9bd141565f43f9034144Christian MaederMaintainer : Christian.Maeder@dfki.de
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till MossakowskiStability : provisional
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu ProdescuPortability : portable
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowskifrequently useful shortcuts mainly for character parsers
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder-- * monad shortcuts
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder(<<) :: Monad m => m a -> m b -> m a
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder(<<) = liftM2 const
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowskiforget :: Monad m => m a -> m ()
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowskiforget = (>> return ())
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowskipair :: Monad m => m a -> m b -> m (a, b)
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowskipair = liftM2 (,)
1a38107941725211e7c3f051f7a8f5e12199f03acmaederinfixr 5 <:>
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder(<:>) :: Monad m => m a -> m [a] -> m [a]
d9a45a35cd696085be1a038b2cc67bee6819c574cmaeder(<:>) = liftM2 (:)
d3c9318c22fcf44d9135a3b2c64f880b9a785babChristian Maeder(<++>) :: Monad m => m [a] -> m [a] -> m [a]
d9a45a35cd696085be1a038b2cc67bee6819c574cmaeder(<++>) = liftM2 (++)
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski(>->) :: Monad m => m a -> (a -> b) -> m b
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski(>->) p f = liftM f p
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder(>-->) :: Monad m => m (a,b) -> (a -> b -> c) -> m c
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski(>-->) p f = p >-> uncurry f
556f473448dfcceee22afaa89ed7a364489cdbbbChristian Maedersingle :: Monad m => m a -> m [a]
556f473448dfcceee22afaa89ed7a364489cdbbbChristian Maedersingle = liftM return
556f473448dfcceee22afaa89ed7a364489cdbbbChristian Maederflat :: Monad m => m [[a]] -> m [a]
556f473448dfcceee22afaa89ed7a364489cdbbbChristian Maederflat = liftM concat
556f473448dfcceee22afaa89ed7a364489cdbbbChristian MaederenclosedBy :: Monad m => m [a] -> m a -> m [a]
556f473448dfcceee22afaa89ed7a364489cdbbbChristian MaederenclosedBy p q = q <:> p <++> single q
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder-- * parsec shortcuts
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder-- | parse an optional list
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till MossakowskioptionL :: GenParser tok st [a] -> GenParser tok st [a]
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till MossakowskioptionL = option []
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski-- | shortcut for @try . string@
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till MossakowskitryString :: String -> CharParser st String
09b431a868c79a92ae7c9bd141565f43f9034144Christian MaedertryString = try . string
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder-- | nested comments, open and closing strings must have at least two chars
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von SchroedernestedComment :: String -> String -> CharParser st String
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von SchroedernestedComment op cl = case (op, cl) of
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder (oh : ot : _, ch : ct : _) ->
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder tryString op <++>
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder flat (many $ single
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder (noneOf [oh, ch]
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder <|> try (char ch << notFollowedBy (char ct))
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski <|> try (char oh << notFollowedBy (char ot)))
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski <|> nestedComment op cl)
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski <++> string cl
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder _ -> error "nestedComment"
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder-- | a literal enclosed in quotes and a backslash as escape character
09b431a868c79a92ae7c9bd141565f43f9034144Christian MaederquotedLit :: Char -> CharParser st String
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till MossakowskiquotedLit q = enclosedBy (flat $ many $ single (noneOf $ '\\' : [q])
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski <|> char '\\' <:> single anyChar) $ char q
adea2e45fa61f1097aadc490a0aeaf4831b729ccChristian Maeder-- | text in double quotes
556f473448dfcceee22afaa89ed7a364489cdbbbChristian MaederstringLit :: CharParser st String
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till MossakowskistringLit = quotedLit '"'
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski-- | text in single quotes
09b431a868c79a92ae7c9bd141565f43f9034144Christian MaedersQuoted :: CharParser st String
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till MossakowskisQuoted = quotedLit '\''
4ef5e33657aae95850b7e6941f67ac1fb73cd13fChristian Maeder-- | non-nested block
4ef5e33657aae95850b7e6941f67ac1fb73cd13fChristian MaederplainBlock :: String -> String -> CharParser st String
e7d2b3903c7b44db432538b0d720c21062c24823Christian MaederplainBlock op cl = tryString op >> manyTill anyChar (tryString cl)
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowskireserved :: [String] -> CharParser st String -> CharParser st String
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski-- | reject keywords
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maederreserved l p = try $ do
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski if elem s l then unexpected $ show s else return s