git.iko.soy/typeable/attoparsec
on master help sign in
Miscellaneous changes

Rename eof to endOfInput Make sure that the Combinator module is re-exported

commitbec9844da6695805f16f554dff7c361258c41cba authorBryan O'Sullivan <bos@serpentine.com> dateTue 13 Jan 2009, 23:42 (18 years ago) parent558bb62
6 files changed+69−16
Data/Attoparsec.hs+4−1 Data/Attoparsec/Char8.hs+5−3 Data/Attoparsec/Combinator.hs+37−4 Data/Attoparsec/Incremental.hs+14−1 Data/Attoparsec/Incremental/Char8.hs+4−2 Data/Attoparsec/Internal.hs+5−5
Data/Attoparsec.hs+4−1
@@ -28,7 +28,7 @@ module Data.Attoparsec
2828
2929 -- * Things vaguely like those in @Parsec.Combinator@ (and @Parsec.Prim@)
3030 , try
31− , eof
31+ , endOfInput
3232 , lookAhead
3333 , peek
3434
@@ -53,7 +53,10 @@ module Data.Attoparsec
5353 , skipWhile
5454 , notEmpty
5555 , match
56+
57+ , module Data.Attoparsec.Combinator
5658 ) where
5759
60+import Data.Attoparsec.Combinator
5861import Data.Attoparsec.Internal
5962import Prelude hiding (takeWhile)
Data/Attoparsec/Char8.hs+5−3
@@ -29,7 +29,7 @@ module Data.Attoparsec.Char8
2929
3030 -- * Things vaguely like those in @Parsec.Combinator@ (and @Parsec.Prim@)
3131 , try
32− , eof
32+ , endOfInput
3333 , lookAhead
3434 , peek
3535
@@ -67,9 +67,10 @@ module Data.Attoparsec.Char8
6767 , inClass
6868 , notInClass
6969 , endOfLine
70+
71+ , module Data.Attoparsec.Combinator
7072 ) where
7173
72−import Control.Applicative ((<$>))
7374import qualified Data.ByteString.Char8 as SB
7475import qualified Data.ByteString.Lazy.Char8 as LB
7576import Data.ByteString.Internal (w2c)
@@ -77,8 +78,9 @@ import Data.Char (isDigit, isLetter, isSpace, toLower)
7778import Data.Attoparsec.FastSet
7879 (FastSet, memberChar, set)
7980import qualified Data.Attoparsec.Internal as I
81+import Data.Attoparsec.Combinator
8082import Data.Attoparsec.Internal
81− (Parser, ParseError, (<?>), parse, parseAt, parseTest, try, eof,
83+ (Parser, ParseError, (<?>), parse, parseAt, parseTest, try, endOfInput,
8284 lookAhead, peek, string,
8385 eitherP, getInput, getConsumed, takeAll, takeCount, notEmpty, match,
8486 endOfLine, setInput)
Data/Attoparsec/Combinator.hs+37−4
@@ -2,12 +2,12 @@
22-----------------------------------------------------------------------------
33-- |
44-- Module : Data.Attoparsec.Combinator
5−-- Copyright : Bryan O'Sullivan 2009
5+-- Copyright : Daan Leijen 1999-2001, Bryan O'Sullivan 2009
66-- License : BSD3
77--
88-- Maintainer : bos@serpentine.com
99-- Stability : experimental
10−-- Portability : unknown
10+-- Portability : portable
1111--
1212-- Useful parser combinators, similar to Parsec.
1313--
@@ -16,29 +16,62 @@ module Data.Attoparsec.Combinator
1616 (
1717 choice
1818 , count
19− , many
19+ , option
2020 , many1
2121 , manyTill
2222 , sepBy
2323 , sepBy1
2424 , skipMany
2525 , skipMany1
26+ , module Control.Applicative
2627 ) where
2728
2829import Control.Applicative
2930
31+-- | @choice ps@ tries to apply the parsers in the list @ps@ in order,
32+-- until one of them succeeds. Returns the value of the succeeding
33+-- parser.
3034choice :: Alternative f => [f a] -> f a
3135choice = foldr (<|>) empty
3236
37+-- | @option x p@ tries to apply parser @p@. If @p@ fails without
38+-- consuming input, it returns the value @x@, otherwise the value
39+-- returned by @p@.
40+--
41+-- > priority = option 0 (digitToInt <$> digit)
42+option :: Alternative f => a -> f a -> f a
43+option x p = p <|> pure x
44+
45+-- | @many1 p@ applies the parser @p@ /one/ or more times. Returns a
46+-- list of the returned values of @p@.
47+--
48+-- > word = many1 letter
3349many1 :: Alternative f => f a -> f [a]
3450many1 p = liftA2 (:) p (many p)
3551
52+-- | @sepBy p sep@ parses /zero/ or more occurrences of @p@, separated
53+-- by @sep@. Returns a list of values returned by @p@.
54+--
55+-- > commaSep p = p `sepBy` (symbol ",")
3656sepBy :: Alternative f => f a -> f s -> f [a]
3757sepBy p s = liftA2 (:) p ((s *> sepBy1 p s) <|> pure []) <|> pure []
3858
59+-- | @sepBy1 p sep@ parses /one/ or more occurrences of @p@, separated
60+-- by @sep@. Returns a list of values returned by @p@.
61+--
62+-- > commaSep p = p `sepBy` (symbol ",")
3963sepBy1 :: Alternative f => f a -> f s -> f [a]
40−sepBy1 p s = liftA2 (:) p ((s *> sepBy1 p s) <|> pure [])
64+sepBy1 p s = scan
65+ where scan = liftA2 (:) p ((s *> scan) <|> pure [])
4166
67+-- | @manyTill p end@ applies parser @p@ /zero/ or more times until
68+-- parser @end@ succeeds. Returns the list of values returned by @p@.
69+-- This parser can be used to scan comments:
70+--
71+-- > simpleComment = string "<!--" *> manyTill anyChar (try (string "-->"))
72+--
73+-- Note the overlapping parsers @anyChar@ and @string \"<!--\"@, and
74+-- therefore the use of the 'try' combinator.
4275manyTill :: Alternative f => f a -> f b -> f [a]
4376manyTill p end = scan
4477 where scan = (end *> pure []) <|> liftA2 (:) p scan
Data/Attoparsec/Incremental.hs+14−1
@@ -25,11 +25,13 @@ module Data.Attoparsec.Incremental
2525 , parseTest
2626
2727 , (<?>)
28+ , try
2829 , takeWhile
2930 , takeTill
3031 , takeCount
3132 , string
3233 , satisfy
34+ , endOfInput
3335 , pushBack
3436
3537 , word8
@@ -39,9 +41,11 @@ module Data.Attoparsec.Incremental
3941 , skipWhile
4042
4143 , yield
44+
45+ , module Data.Attoparsec.Combinator
4246 ) where
4347
44−import Control.Applicative
48+import Data.Attoparsec.Combinator
4549import Control.Monad (MonadPlus(..), ap)
4650import Data.Attoparsec.Internal ((+:))
4751import Data.Word (Word8)
@@ -133,6 +137,9 @@ plus p1 p2 =
133137 in
134138 filt $ unParser p1 (S sb lb [] eof (failDepth + 1)) (cutContinuation k)
135139
140+try :: Parser r a -> Parser r a
141+try p = p
142+
136143instance Functor (Parser r) where
137144 fmap f m = Parser $ \s cont -> unParser m s (cont . f)
138145
@@ -234,6 +241,12 @@ pushBack bs =
234241 Parser $ \(S sb lb adds eof failDepth) k ->
235242 k () (mkState (bs `appL` (sb +: lb)) adds eof failDepth)
236243
244+endOfInput :: Parser r ()
245+endOfInput = Parser $ \st@(S sb lb _adds _eof _failDepth) k ->
246+ if not (S.null sb) || not (L.null lb)
247+ then IFailed st "endOfInput: not EOF"
248+ else continue (k ()) endOfInput k st
249+
237250toplevelTranslate :: IResult a -> Result a
238251toplevelTranslate (IFailed _ err) = Failed err
239252toplevelTranslate (IDone (S sb lb _ _ _) value) = Done (sb +: lb) value
Data/Attoparsec/Incremental/Char8.hs+4−2
@@ -2,7 +2,7 @@
22-----------------------------------------------------------------------------
33-- |
44-- Module : Data.Attoparsec.Incremental.Char8
5−-- Copyright : Daan Leijen 1999-2001, Jeremy Shaw 2006, Bryan O'Sullivan 2007-2008
5+-- Copyright : Bryan O'Sullivan 2009
66-- License : BSD3
77--
88-- Maintainer : bos@serpentine.com
@@ -51,9 +51,10 @@ module Data.Attoparsec.Incremental.Char8
5151 , skipSpace
5252 , inClass
5353 , notInClass
54+
55+ , module Data.Attoparsec.Combinator
5456 ) where
5557
56−import Control.Applicative ((<$>))
5758import qualified Data.ByteString.Char8 as SB
5859import qualified Data.ByteString.Lazy.Char8 as LB
5960import Data.ByteString.Internal (w2c)
@@ -66,6 +67,7 @@ import Data.Attoparsec.Incremental
6667 string, takeCount)
6768import Data.ByteString.Lex.Lazy.Double (readDouble)
6869import Prelude hiding (takeWhile)
70+import Data.Attoparsec.Combinator
6971
7072numeric :: String -> (Char -> Bool)
7173 -> (LB.ByteString -> Maybe (a,LB.ByteString)) -> Parser r a
Data/Attoparsec/Internal.hs+5−5
@@ -29,7 +29,7 @@ module Data.Attoparsec.Internal
2929
3030 -- * Things vaguely like those in @Parsec.Combinator@ (and @Parsec.Prim@)
3131 , try
32− , eof
32+ , endOfInput
3333 , lookAhead
3434 , peek
3535
@@ -221,10 +221,10 @@ try p = Parser $ \s@(S sb lb _) ->
221221 ok -> ok
222222
223223-- | Detect 'end of file'.
224−eof :: Parser ()
225−eof = Parser $ \s@(S sb lb _) -> if SB.null sb && LB.null lb
226− then Right ((), s)
227− else Left (sb +: lb, ["EOF"])
224+endOfInput :: Parser ()
225+endOfInput = Parser $ \s@(S sb lb _) -> if SB.null sb && LB.null lb
226+ then Right ((), s)
227+ else Left (sb +: lb, ["EOF"])
228228
229229takeAll :: Parser LB.ByteString
230230takeAll = Parser $ \(S sb lb n) ->