/
Combinators.hs
479 lines (428 loc) · 16.2 KB
/
Combinators.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
{-# LANGUAGE CPP #-}
{-# LANGUAGE UndecidableInstances #-}
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 704
#define USE_DEFAULT_SIGNATURES
#endif
#ifdef USE_DEFAULT_SIGNATURES
{-# LANGUAGE DefaultSignatures, TypeFamilies #-}
#endif
#if !MIN_VERSION_base(4,6,0)
#define ORPHAN_ALTERNATIVE_READP
#endif
#ifdef ORPHAN_ALTERNATIVE_READP
{-# OPTIONS_GHC -fno-warn-orphans #-}
#endif
-----------------------------------------------------------------------------
-- |
-- Module : Text.Parser.Combinators
-- Copyright : (c) Edward Kmett 2011-2012
-- License : BSD3
--
-- Maintainer : ekmett@gmail.com
-- Stability : experimental
-- Portability : non-portable
--
-- Alternative parser combinators
--
-----------------------------------------------------------------------------
module Text.Parser.Combinators
(
-- * Parsing Combinators
choice
, option
, optional -- from Control.Applicative, parsec optionMaybe
, skipOptional -- parsec optional
, between
, surroundedBy
, some -- from Control.Applicative, parsec many1
, many -- from Control.Applicative
, sepBy
, sepBy1
, sepByNonEmpty
, sepEndBy1
, sepEndByNonEmpty
, sepEndBy
, endBy1
, endByNonEmpty
, endBy
, count
, chainl
, chainr
, chainl1
, chainr1
, manyTill
-- * Parsing Class
, Parsing(..)
) where
import Control.Applicative
import Control.Monad (MonadPlus(..))
import Control.Monad.Trans.Class
import Control.Monad.Trans.State.Lazy as Lazy
import Control.Monad.Trans.State.Strict as Strict
import Control.Monad.Trans.Writer.Lazy as Lazy
import Control.Monad.Trans.Writer.Strict as Strict
import Control.Monad.Trans.RWS.Lazy as Lazy
import Control.Monad.Trans.RWS.Strict as Strict
import Control.Monad.Trans.Reader
import Control.Monad.Trans.Identity
import Data.Foldable (asum)
import Data.List.NonEmpty
#if __GLASGOW_HASKELL__ < 710
import Data.Monoid
#ifdef ORPHAN_ALTERNATIVE_READP
import Data.Orphans ()
#endif
import Data.Traversable (sequenceA)
#endif
#ifdef MIN_VERSION_parsec
import qualified Text.Parsec as Parsec
#endif
#ifdef MIN_VERSION_attoparsec
import qualified Data.Attoparsec.Types as Att
import qualified Data.Attoparsec.Combinator as Att
#endif
import qualified Text.ParserCombinators.ReadP as ReadP
#ifdef MIN_VERSION_binary
import Control.Monad (when, unless)
import qualified Data.Binary.Get as B
#endif
#if MIN_VERSION_base(4,9,0)
import Control.Monad (replicateM)
#endif
-- | @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.
choice :: Alternative m => [m a] -> m a
choice = asum
{-# INLINE choice #-}
-- | @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)
option :: Alternative m => a -> m a -> m a
option x p = p <|> pure x
{-# INLINE option #-}
-- | @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)
skipOptional :: Alternative m => m a -> m ()
skipOptional p = (() <$ p) <|> pure ()
{-# INLINE skipOptional #-}
-- | @between open close p@ parses @open@, followed by @p@ and @close@.
-- Returns the value returned by @p@.
--
-- > braces = between (symbol "{") (symbol "}")
between :: Applicative m => m bra -> m ket -> m a -> m a
between bra ket p = bra *> p <* ket
{-# INLINE between #-}
-- | @p \`surroundedBy\` f@ is @p@ surrounded by @f@. Shortcut for @between f f p@.
-- As in @between@, returns the value returned by @p@.
surroundedBy :: Applicative m => m a -> m sur -> m a
surroundedBy p bound = between bound bound p
{-# INLINE surroundedBy #-}
-- | @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 ",")
sepBy :: Alternative m => m a -> m sep -> m [a]
sepBy p sep = sepBy1 p sep <|> pure []
{-# INLINE sepBy #-}
-- | @sepBy1 p sep@ parses /one/ or more occurrences of @p@, separated
-- by @sep@. Returns a list of values returned by @p@.
sepBy1 :: Alternative m => m a -> m sep -> m [a]
sepBy1 p sep = toList <$> sepByNonEmpty p sep
{-# INLINE sepBy1 #-}
-- | @sepByNonEmpty p sep@ parses /one/ or more occurrences of @p@, separated
-- by @sep@. Returns a non-empty list of values returned by @p@.
sepByNonEmpty :: Alternative m => m a -> m sep -> m (NonEmpty a)
sepByNonEmpty p sep = (:|) <$> p <*> many (sep *> p)
{-# INLINE sepByNonEmpty #-}
-- | @sepEndBy1 p sep@ parses /one/ or more occurrences of @p@,
-- separated and optionally ended by @sep@. Returns a list of values
-- returned by @p@.
sepEndBy1 :: Alternative m => m a -> m sep -> m [a]
sepEndBy1 p sep = toList <$> sepEndByNonEmpty p sep
-- | @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@.
sepEndByNonEmpty :: Alternative m => m a -> m sep -> m (NonEmpty a)
sepEndByNonEmpty p sep = (:|) <$> p <*> ((sep *> sepEndBy p sep) <|> pure [])
-- | @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
sepEndBy :: Alternative m => m a -> m sep -> m [a]
sepEndBy p sep = sepEndBy1 p sep <|> pure []
{-# INLINE sepEndBy #-}
-- | @endBy1 p sep@ parses /one/ or more occurrences of @p@, separated
-- and ended by @sep@. Returns a list of values returned by @p@.
endBy1 :: Alternative m => m a -> m sep -> m [a]
endBy1 p sep = some (p <* sep)
{-# INLINE endBy1 #-}
-- | @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@.
endByNonEmpty :: Alternative m => m a -> m sep -> m (NonEmpty a)
endByNonEmpty p sep = some1 (p <* sep)
{-# INLINE endByNonEmpty #-}
-- | @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
endBy :: Alternative m => m a -> m sep -> m [a]
endBy p sep = many (p <* sep)
{-# INLINE endBy #-}
-- | @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@.
count :: Applicative m => Int -> m a -> m [a]
#if MIN_VERSION_base(4,9,0)
count = replicateM
#else
count n p | n <= 0 = pure []
| otherwise = sequenceA (replicate n p)
#endif
{-# INLINE count #-}
-- | @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.
chainr :: Alternative m => m a -> m (a -> a -> a) -> a -> m a
chainr p op x = chainr1 p op <|> pure x
{-# INLINE chainr #-}
-- | @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.
chainl :: Alternative m => m a -> m (a -> a -> a) -> a -> m a
chainl p op x = chainl1 p op <|> pure x
{-# INLINE chainl #-}
-- | @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 "-"
chainl1 :: Alternative m => m a -> m (a -> a -> a) -> m a
chainl1 p op = scan where
scan = p <**> rst
rst = (\f y g x -> g (f x y)) <$> op <*> p <*> rst <|> pure id
{-# INLINE chainl1 #-}
-- | @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@.
chainr1 :: Alternative m => m a -> m (a -> a -> a) -> m a
chainr1 p op = scan where
scan = p <**> rst
rst = (flip <$> op <*> scan) <|> pure id
{-# INLINE chainr1 #-}
-- | @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.
manyTill :: Alternative m => m a -> m end -> m [a]
manyTill p end = go where go = ([] <$ end) <|> ((:) <$> p <*> go)
{-# INLINE manyTill #-}
infixr 0 <?>
-- | Additional functionality needed to describe parsers independent of input type.
class Alternative m => Parsing m where
-- | 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.
try :: m a -> m a
-- | Give a parser a name
(<?>) :: m a -> String -> m a
-- | A version of many that discards its input. Specialized because it
-- can often be implemented more cheaply.
skipMany :: m a -> m ()
skipMany p = () <$ many p
{-# INLINE skipMany #-}
-- | @skipSome p@ applies the parser @p@ /one/ or more times, skipping
-- its result. (aka skipMany1 in parsec)
skipSome :: m a -> m ()
skipSome p = p *> skipMany p
{-# INLINE skipSome #-}
-- | Used to emit an error on an unexpected token
unexpected :: String -> m a
#ifdef USE_DEFAULT_SIGNATURES
default unexpected :: (MonadTrans t, Monad n, Parsing n, m ~ t n) =>
String -> m a
unexpected = lift . unexpected
{-# INLINE unexpected #-}
#endif
-- | 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"
eof :: m ()
#ifdef USE_DEFAULT_SIGNATURES
default eof :: (MonadTrans t, Monad n, Parsing n, m ~ t n) => m ()
eof = lift eof
{-# INLINE eof #-}
#endif
-- | @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
notFollowedBy :: Show a => m a -> m ()
instance (Parsing m, MonadPlus m) => Parsing (Lazy.StateT s m) where
try (Lazy.StateT m) = Lazy.StateT $ try . m
{-# INLINE try #-}
Lazy.StateT m <?> l = Lazy.StateT $ \s -> m s <?> l
{-# INLINE (<?>) #-}
unexpected = lift . unexpected
{-# INLINE unexpected #-}
eof = lift eof
{-# INLINE eof #-}
notFollowedBy (Lazy.StateT m) = Lazy.StateT
$ \s -> notFollowedBy (fst <$> m s) >> return ((),s)
{-# INLINE notFollowedBy #-}
instance (Parsing m, MonadPlus m) => Parsing (Strict.StateT s m) where
try (Strict.StateT m) = Strict.StateT $ try . m
{-# INLINE try #-}
Strict.StateT m <?> l = Strict.StateT $ \s -> m s <?> l
{-# INLINE (<?>) #-}
unexpected = lift . unexpected
{-# INLINE unexpected #-}
eof = lift eof
{-# INLINE eof #-}
notFollowedBy (Strict.StateT m) = Strict.StateT
$ \s -> notFollowedBy (fst <$> m s) >> return ((),s)
{-# INLINE notFollowedBy #-}
instance (Parsing m, MonadPlus m) => Parsing (ReaderT e m) where
try (ReaderT m) = ReaderT $ try . m
{-# INLINE try #-}
ReaderT m <?> l = ReaderT $ \e -> m e <?> l
{-# INLINE (<?>) #-}
skipMany (ReaderT m) = ReaderT $ skipMany . m
{-# INLINE skipMany #-}
unexpected = lift . unexpected
{-# INLINE unexpected #-}
eof = lift eof
{-# INLINE eof #-}
notFollowedBy (ReaderT m) = ReaderT $ notFollowedBy . m
{-# INLINE notFollowedBy #-}
instance (Parsing m, MonadPlus m, Monoid w) => Parsing (Strict.WriterT w m) where
try (Strict.WriterT m) = Strict.WriterT $ try m
{-# INLINE try #-}
Strict.WriterT m <?> l = Strict.WriterT (m <?> l)
{-# INLINE (<?>) #-}
unexpected = lift . unexpected
{-# INLINE unexpected #-}
eof = lift eof
{-# INLINE eof #-}
notFollowedBy (Strict.WriterT m) = Strict.WriterT
$ notFollowedBy (fst <$> m) >>= \x -> return (x, mempty)
{-# INLINE notFollowedBy #-}
instance (Parsing m, MonadPlus m, Monoid w) => Parsing (Lazy.WriterT w m) where
try (Lazy.WriterT m) = Lazy.WriterT $ try m
{-# INLINE try #-}
Lazy.WriterT m <?> l = Lazy.WriterT (m <?> l)
{-# INLINE (<?>) #-}
unexpected = lift . unexpected
{-# INLINE unexpected #-}
eof = lift eof
{-# INLINE eof #-}
notFollowedBy (Lazy.WriterT m) = Lazy.WriterT
$ notFollowedBy (fst <$> m) >>= \x -> return (x, mempty)
{-# INLINE notFollowedBy #-}
instance (Parsing m, MonadPlus m, Monoid w) => Parsing (Lazy.RWST r w s m) where
try (Lazy.RWST m) = Lazy.RWST $ \r s -> try (m r s)
{-# INLINE try #-}
Lazy.RWST m <?> l = Lazy.RWST $ \r s -> m r s <?> l
{-# INLINE (<?>) #-}
unexpected = lift . unexpected
{-# INLINE unexpected #-}
eof = lift eof
{-# INLINE eof #-}
notFollowedBy (Lazy.RWST m) = Lazy.RWST
$ \r s -> notFollowedBy ((\(a,_,_) -> a) <$> m r s) >>= \x -> return (x, s, mempty)
{-# INLINE notFollowedBy #-}
instance (Parsing m, MonadPlus m, Monoid w) => Parsing (Strict.RWST r w s m) where
try (Strict.RWST m) = Strict.RWST $ \r s -> try (m r s)
{-# INLINE try #-}
Strict.RWST m <?> l = Strict.RWST $ \r s -> m r s <?> l
{-# INLINE (<?>) #-}
unexpected = lift . unexpected
{-# INLINE unexpected #-}
eof = lift eof
{-# INLINE eof #-}
notFollowedBy (Strict.RWST m) = Strict.RWST
$ \r s -> notFollowedBy ((\(a,_,_) -> a) <$> m r s) >>= \x -> return (x, s, mempty)
{-# INLINE notFollowedBy #-}
instance (Parsing m, Monad m) => Parsing (IdentityT m) where
try = IdentityT . try . runIdentityT
{-# INLINE try #-}
IdentityT m <?> l = IdentityT (m <?> l)
{-# INLINE (<?>) #-}
skipMany = IdentityT . skipMany . runIdentityT
{-# INLINE skipMany #-}
unexpected = lift . unexpected
{-# INLINE unexpected #-}
eof = lift eof
{-# INLINE eof #-}
notFollowedBy (IdentityT m) = IdentityT $ notFollowedBy m
{-# INLINE notFollowedBy #-}
#ifdef MIN_VERSION_parsec
instance (Parsec.Stream s m t, Show t) => Parsing (Parsec.ParsecT s u m) where
try = Parsec.try
(<?>) = (Parsec.<?>)
skipMany = Parsec.skipMany
skipSome = Parsec.skipMany1
unexpected = Parsec.unexpected
eof = Parsec.eof
notFollowedBy = Parsec.notFollowedBy
#endif
#ifdef MIN_VERSION_attoparsec
instance Att.Chunk t => Parsing (Att.Parser t) where
try = Att.try
(<?>) = (Att.<?>)
skipMany = Att.skipMany
skipSome = Att.skipMany1
unexpected = fail
eof = Att.endOfInput
notFollowedBy p = optional p >>= maybe (pure ()) (unexpected . show)
#endif
#ifdef MIN_VERSION_binary
instance Parsing B.Get where
try = id
(<?>) = flip B.label
skipMany p = do skipped <- True <$ p <|> pure False
when skipped $ skipMany p
unexpected = fail
eof = do isEof <- B.isEmpty
unless isEof $ fail "Parsing.eof"
notFollowedBy p = optional p >>= maybe (pure ()) (unexpected . show)
#endif
instance Parsing ReadP.ReadP where
try = id
(<?>) = const
skipMany = ReadP.skipMany
skipSome = ReadP.skipMany1
unexpected = const ReadP.pfail
eof = ReadP.eof
notFollowedBy p = ((Just <$> p) ReadP.<++ pure Nothing)
>>= maybe (pure ()) (unexpected . show)