1 -----------------------------------------------------------------------------
3 -- Module : Text.Parsec.Combinator
4 -- Copyright : (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
5 -- License : BSD-style (see the LICENSE file)
7 -- Maintainer : derek.a.elkins@gmail.com
8 -- Stability : provisional
9 -- Portability : portable
11 -- Commonly used generic combinators
13 -----------------------------------------------------------------------------
15 module Text.Parsec.Combinator
19 , option, optionMaybe, optional
29 , manyTill, lookAhead, anyToken
33 import Text.Parsec.Prim
35 -- | @choice ps@ tries to apply the parsers in the list @ps@ in order,
36 -- until one of them succeeds. Returns the value of the succeeding
39 choice :: (Stream s m t) => [ParsecT s u m a] -> ParsecT s u m a
40 choice ps = foldr (<|>) mzero ps
42 -- | @option x p@ tries to apply parser @p@. If @p@ fails without
43 -- consuming input, it returns the value @x@, otherwise the value
46 -- > priority = option 0 (do{ d <- digit
47 -- > ; return (digitToInt d)
50 option :: (Stream s m t) => a -> ParsecT s u m a -> ParsecT s u m a
51 option x p = p <|> return x
53 -- | @option p@ tries to apply parser @p@. If @p@ fails without
54 -- consuming input, it return 'Nothing', otherwise it returns
55 -- 'Just' the value returned by @p@.
57 optionMaybe :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m (Maybe a)
58 optionMaybe p = option Nothing (liftM Just p)
60 -- | @optional p@ tries to apply parser @p@. It will parse @p@ or nothing.
61 -- It only fails if @p@ fails after consuming input. It discards the result
64 optional :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m ()
65 optional p = do{ p; return ()} <|> return ()
67 -- | @between open close p@ parses @open@, followed by @p@ and @close@.
68 -- Returns the value returned by @p@.
70 -- > braces = between (symbol "{") (symbol "}")
72 between :: (Stream s m t) => ParsecT s u m open -> ParsecT s u m close
73 -> ParsecT s u m a -> ParsecT s u m a
75 = do{ open; x <- p; close; return x }
77 -- | @skipMany1 p@ applies the parser @p@ /one/ or more times, skipping
80 skipMany1 :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m ()
81 skipMany1 p = do{ p; skipMany p }
85 scan = do{ p; scan } <|> return ()
88 -- | @many p@ applies the parser @p@ /one/ or more times. Returns a
89 -- list of the returned values of @p@.
91 -- > word = many1 letter
93 many1 :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m [a]
94 many1 p = do{ x <- p; xs <- many p; return (x:xs) }
99 ; scan (\tail -> f (x:tail))
105 -- | @sepBy p sep@ parses /zero/ or more occurrences of @p@, separated
106 -- by @sep@. Returns a list of values returned by @p@.
108 -- > commaSep p = p `sepBy` (symbol ",")
110 sepBy :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
111 sepBy p sep = sepBy1 p sep <|> return []
113 -- | @sepBy1 p sep@ parses /one/ or more occurrences of @p@, separated
114 -- by @sep@. Returns a list of values returned by @p@.
116 sepBy1 :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
117 sepBy1 p sep = do{ x <- p
118 ; xs <- many (sep >> p)
123 -- | @sepEndBy1 p sep@ parses /one/ or more occurrences of @p@,
124 -- separated and optionally ended by @sep@. Returns a list of values
127 sepEndBy1 :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
128 sepEndBy1 p sep = do{ x <- p
130 ; xs <- sepEndBy p sep
136 -- | @sepEndBy p sep@ parses /zero/ or more occurrences of @p@,
137 -- separated and optionally ended by @sep@, ie. haskell style
138 -- statements. Returns a list of values returned by @p@.
140 -- > haskellStatements = haskellStatement `sepEndBy` semi
142 sepEndBy :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
143 sepEndBy p sep = sepEndBy1 p sep <|> return []
146 -- | @endBy1 p sep@ parses /one/ or more occurrences of @p@, seperated
147 -- and ended by @sep@. Returns a list of values returned by @p@.
149 endBy1 :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
150 endBy1 p sep = many1 (do{ x <- p; sep; return x })
152 -- | @endBy p sep@ parses /zero/ or more occurrences of @p@, seperated
153 -- and ended by @sep@. Returns a list of values returned by @p@.
155 -- > cStatements = cStatement `endBy` semi
157 endBy :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
158 endBy p sep = many (do{ x <- p; sep; return x })
160 -- | @count n p@ parses @n@ occurrences of @p@. If @n@ is smaller or
161 -- equal to zero, the parser equals to @return []@. Returns a list of
162 -- @n@ values returned by @p@.
164 count :: (Stream s m t) => Int -> ParsecT s u m a -> ParsecT s u m [a]
165 count n p | n <= 0 = return []
166 | otherwise = sequence (replicate n p)
168 -- | @chainr p op x@ parser /zero/ or more occurrences of @p@,
169 -- separated by @op@ Returns a value obtained by a /right/ associative
170 -- application of all functions returned by @op@ to the values returned
171 -- by @p@. If there are no occurrences of @p@, the value @x@ is
174 chainr :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> a -> ParsecT s u m a
175 chainr p op x = chainr1 p op <|> return x
177 -- | @chainl p op x@ parser /zero/ or more occurrences of @p@,
178 -- separated by @op@. Returns a value obtained by a /left/ associative
179 -- application of all functions returned by @op@ to the values returned
180 -- by @p@. If there are zero occurrences of @p@, the value @x@ is
183 chainl :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> a -> ParsecT s u m a
184 chainl p op x = chainl1 p op <|> return x
186 -- | @chainl1 p op x@ parser /one/ or more occurrences of @p@,
187 -- separated by @op@ Returns a value obtained by a /left/ associative
188 -- application of all functions returned by @op@ to the values returned
189 -- by @p@. . This parser can for example be used to eliminate left
190 -- recursion which typically occurs in expression grammars.
192 -- > expr = term `chainl1` mulop
193 -- > term = factor `chainl1` addop
194 -- > factor = parens expr <|> integer
196 -- > mulop = do{ symbol "*"; return (*) }
197 -- > <|> do{ symbol "/"; return (div) }
199 -- > addop = do{ symbol "+"; return (+) }
200 -- > <|> do{ symbol "-"; return (-) }
202 chainl1 :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> ParsecT s u m a
203 chainl1 p op = do{ x <- p; rest x }
211 -- | @chainr1 p op x@ parser /one/ or more occurrences of |p|,
212 -- separated by @op@ Returns a value obtained by a /right/ associative
213 -- application of all functions returned by @op@ to the values returned
216 chainr1 :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> ParsecT s u m a
219 scan = do{ x <- p; rest x }
227 -----------------------------------------------------------
228 -- Tricky combinators
229 -----------------------------------------------------------
230 -- | The parser @anyToken@ accepts any kind of token. It is for example
231 -- used to implement 'eof'. Returns the accepted token.
233 anyToken :: (Stream s m t, Show t) => ParsecT s u m t
234 anyToken = tokenPrim show (\pos _tok _toks -> pos) Just
236 -- | This parser only succeeds at the end of the input. This is not a
237 -- primitive parser but it is defined using 'notFollowedBy'.
239 -- > eof = notFollowedBy anyToken <?> "end of input"
241 eof :: (Stream s m t, Show t) => ParsecT s u m ()
242 eof = notFollowedBy anyToken <?> "end of input"
244 -- | @notFollowedBy p@ only succeeds when parser @p@ fails. This parser
245 -- does not consume any input. This parser can be used to implement the
246 -- \'longest match\' rule. For example, when recognizing keywords (for
247 -- example @let@), we want to make sure that a keyword is not followed
248 -- by a legal identifier character, in which case the keyword is
249 -- actually an identifier (for example @lets@). We can program this
250 -- behaviour as follows:
252 -- > keywordLet = try (do{ string "let"
253 -- > ; notFollowedBy alphaNum
256 notFollowedBy :: (Stream s m t, Show t) => ParsecT s u m t -> ParsecT s u m ()
257 notFollowedBy p = try (do{ c <- p; unexpected (show [c]) }
261 -- | @manyTill p end@ applies parser @p@ /zero/ or more times until
262 -- parser @end@ succeeds. Returns the list of values returned by @p@.
263 -- This parser can be used to scan comments:
265 -- > simpleComment = do{ string "<!--"
266 -- > ; manyTill anyChar (try (string "-->"))
269 -- Note the overlapping parsers @anyChar@ and @string \"<!--\"@, and
270 -- therefore the use of the 'try' combinator.
272 manyTill :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
273 manyTill p end = scan
275 scan = do{ end; return [] }
277 do{ x <- p; xs <- scan; return (x:xs) }
279 -- | @lookAhead p@ parses @p@ without consuming any input.
281 lookAhead :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m a
282 lookAhead p = do{ state <- getParserState
284 ; setParserState state