1 -----------------------------------------------------------------------------
3 -- Module : Text.ParserCombinators.Parsec.Combinator
4 -- Copyright : (c) Daan Leijen 1999-2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : daan@cs.uu.nl
8 -- Stability : provisional
9 -- Portability : portable
11 -- Commonly used generic combinators
13 -----------------------------------------------------------------------------
15 module Text.ParserCombinators.Parsec.Combinator
30 , manyTill, lookAhead, anyToken
34 import Text.ParserCombinators.Parsec.Prim
37 ----------------------------------------------------------------
39 ----------------------------------------------------------------
40 choice :: [GenParser tok st a] -> GenParser tok st a
41 choice ps = foldr (<|>) mzero ps
43 option :: a -> GenParser tok st a -> GenParser tok st a
44 option x p = p <|> return x
46 optional :: GenParser tok st a -> GenParser tok st ()
47 optional p = do{ p; return ()} <|> return ()
49 between :: GenParser tok st open -> GenParser tok st close
50 -> GenParser tok st a -> GenParser tok st a
52 = do{ open; x <- p; close; return x }
55 skipMany1 :: GenParser tok st a -> GenParser tok st ()
56 skipMany1 p = do{ p; skipMany p }
60 scan = do{ p; scan } <|> return ()
63 many1 :: GenParser tok st a -> GenParser tok st [a]
64 many1 p = do{ x <- p; xs <- many p; return (x:xs) }
69 ; scan (\tail -> f (x:tail))
74 sepBy1,sepBy :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [a]
75 sepBy p sep = sepBy1 p sep <|> return []
76 sepBy1 p sep = do{ x <- p
77 ; xs <- many (sep >> p)
81 sepEndBy1, sepEndBy :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [a]
82 sepEndBy1 p sep = do{ x <- p
84 ; xs <- sepEndBy p sep
90 sepEndBy p sep = sepEndBy1 p sep <|> return []
93 endBy1,endBy :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [a]
94 endBy1 p sep = many1 (do{ x <- p; sep; return x })
95 endBy p sep = many (do{ x <- p; sep; return x })
97 count :: Int -> GenParser tok st a -> GenParser tok st [a]
98 count n p | n <= 0 = return []
99 | otherwise = sequence (replicate n p)
102 chainr p op x = chainr1 p op <|> return x
103 chainl p op x = chainl1 p op <|> return x
105 chainr1,chainl1 :: GenParser tok st a -> GenParser tok st (a -> a -> a) -> GenParser tok st a
106 chainl1 p op = do{ x <- p; rest x }
116 scan = do{ x <- p; rest x }
124 -----------------------------------------------------------
125 -- Tricky combinators
126 -----------------------------------------------------------
127 anyToken :: Show tok => GenParser tok st tok
128 anyToken = tokenPrim show (\pos tok toks -> pos) Just
130 eof :: Show tok => GenParser tok st ()
131 eof = notFollowedBy anyToken <?> "end of input"
133 notFollowedBy :: Show tok => GenParser tok st tok -> GenParser tok st ()
134 notFollowedBy p = try (do{ c <- p; unexpected (show [c]) }
138 manyTill :: GenParser tok st a -> GenParser tok st end -> GenParser tok st [a]
139 manyTill p end = scan
141 scan = do{ end; return [] }
143 do{ x <- p; xs <- scan; return (x:xs) }
146 lookAhead :: GenParser tok st a -> GenParser tok st a
147 lookAhead p = do{ state <- getParserState
149 ; setParserState state