git.iko.soy/typeable/attoparsec
on master help sign in
Deduplicated `try` and `<?>` into the Combinator module.
commit85554f7e570212adc92e105178a5306961b014e9 authorMikhail Vorozhtsov <mikhail.vorozhtsov@gmail.com> dateTue 8 Oct 2013, 08:42 (13 years ago) parent7e7dc1f
6 files changed+25−53
Data/Attoparsec/ByteString.hs−2 Data/Attoparsec/ByteString/Char8.hs+1−3 Data/Attoparsec/ByteString/Internal.hs−21 Data/Attoparsec/Combinator.hs+23−3 Data/Attoparsec/Text.hs+1−3 Data/Attoparsec/Text/Internal.hs−21
Data/Attoparsec/ByteString.hs−2
@@ -39,8 +39,6 @@ module Data.Attoparsec.ByteString
3939 , eitherResult
4040
4141 -- * Combinators
42− , (I.<?>)
43− , I.try
4442 , module Data.Attoparsec.Combinator
4543
4644 -- * Parsing individual bytes
Data/Attoparsec/ByteString/Char8.hs+1−3
@@ -37,8 +37,6 @@ module Data.Attoparsec.ByteString.Char8
3737 , A.eitherResult
3838
3939 -- * Combinators
40− , (I.<?>)
41− , I.try
4240 , module Data.Attoparsec.Combinator
4341
4442 -- * Parsing individual characters
@@ -111,7 +109,7 @@ module Data.Attoparsec.ByteString.Char8
111109
112110import Control.Applicative (pure, (*>), (<*), (<$>), (<|>))
113111import Data.Attoparsec.ByteString.FastSet (charClass, memberChar)
114−import Data.Attoparsec.ByteString.Internal (Parser, (<?>))
112+import Data.Attoparsec.ByteString.Internal (Parser)
115113import Data.Attoparsec.Combinator
116114import Data.Attoparsec.Number (Number(..))
117115import Data.Bits (Bits, (.|.), shiftL)
Data/Attoparsec/ByteString/Internal.hs−21
@@ -23,8 +23,6 @@ module Data.Attoparsec.ByteString.Internal
2323 , parseOnly
2424
2525 -- * Combinators
26− , (<?>)
27− , try
2826 , module Data.Attoparsec.Combinator
2927
3028 -- * Parsing individual bytes
@@ -161,15 +159,6 @@ get = T.Parser $ \i0 a0 m0 _kf ks -> ks i0 a0 m0 (unI i0)
161159put :: B.ByteString -> Parser ()
162160put s = T.Parser $ \_i0 a0 m0 _kf ks -> ks (I s) a0 m0 ()
163161
164−-- | Attempt a parse, and if it fails, rewind the input so that no
165−-- input appears to have been consumed.
166−--
167−-- This combinator is provided for compatibility with Parsec.
168−-- Attoparsec parsers always backtrack on failure.
169−try :: Parser a -> Parser a
170−try p = p
171−{-# INLINE try #-}
172−
173162-- | The parser @satisfy p@ succeeds for any byte for which the
174163-- predicate @p@ returns 'True'. Returns the byte that is actually
175164-- parsed.
@@ -481,16 +470,6 @@ atEnd = not <$> wantInput
481470endOfLine :: Parser ()
482471endOfLine = (word8 10 >> return ()) <|> (string "\r\n" >> return ())
483472
484−-- | Name the parser, in case failure occurs.
485−(<?>) :: Parser a
486− -> String -- ^ the name to use if parsing fails
487− -> Parser a
488−p <?> msg0 = T.Parser $ \i0 a0 m0 kf ks ->
489− let kf' i a m strs msg = kf i a m (msg0:strs) msg
490− in T.runParser p i0 a0 m0 kf' ks
491−{-# INLINE (<?>) #-}
492−infix 0 <?>
493−
494473-- | Terminal failure continuation.
495474failK :: Failure a
496475failK i0 _a0 _m0 stack msg = Fail (unI i0) stack msg
Data/Attoparsec/Combinator.hs+23−3
@@ -10,8 +10,9 @@
1010--
1111-- Useful parser combinators, similar to those provided by Parsec.
1212module Data.Attoparsec.Combinator
13− (
14− choice
13+ ( try
14+ , (<?>)
15+ , choice
1516 , count
1617 , option
1718 , many'
@@ -35,13 +36,32 @@ import Control.Monad (MonadPlus(..))
3536import Control.Applicative (many)
3637#endif
3738
39+import Data.Attoparsec.Internal.Types (Parser(..))
3840#if __GLASGOW_HASKELL__ >= 700
39−import Data.Attoparsec.Internal.Types (Parser)
4041import qualified Data.Attoparsec.Zepto as Z
4142import Data.ByteString (ByteString)
4243import Data.Text (Text)
4344#endif
4445
46+-- | Attempt a parse, and if it fails, rewind the input so that no
47+-- input appears to have been consumed.
48+--
49+-- This combinator is provided for compatibility with Parsec.
50+-- Attoparsec parsers always backtrack on failure.
51+try :: Parser t a -> Parser t a
52+try p = p
53+{-# INLINE try #-}
54+
55+-- | Name the parser, in case failure occurs.
56+(<?>) :: Parser t a
57+ -> String -- ^ the name to use if parsing fails
58+ -> Parser t a
59+p <?> msg0 = Parser $ \i0 a0 m0 kf ks ->
60+ let kf' i a m strs msg = kf i a m (msg0:strs) msg
61+ in runParser p i0 a0 m0 kf' ks
62+{-# INLINE (<?>) #-}
63+infix 0 <?>
64+
4565-- | @choice ps@ tries to apply the actions in the list @ps@ in order,
4666-- until one of them succeeds. Returns the value of the succeeding
4767-- action.
Data/Attoparsec/Text.hs+1−3
@@ -41,8 +41,6 @@ module Data.Attoparsec.Text
4141 , eitherResult
4242
4343 -- * Combinators
44− , (I.<?>)
45− , I.try
4644 , module Data.Attoparsec.Combinator
4745
4846 -- * Parsing individual characters
@@ -110,7 +108,7 @@ import Control.Applicative (pure, (<$>), (*>), (<*), (<|>))
110108import Data.Attoparsec.Combinator
111109import Data.Attoparsec.Number (Number(..))
112110import Data.Scientific (Scientific, scientific, coefficient, base10Exponent)
113−import Data.Attoparsec.Text.Internal ((<?>), Parser, Result, parse, takeWhile1)
111+import Data.Attoparsec.Text.Internal (Parser, Result, parse, takeWhile1)
114112import Data.Bits (Bits, (.|.), shiftL)
115113import Data.Char (isAlpha, isDigit, isSpace, ord)
116114import Data.Int (Int8, Int16, Int32, Int64)
Data/Attoparsec/Text/Internal.hs−21
@@ -24,8 +24,6 @@ module Data.Attoparsec.Text.Internal
2424 , parseOnly
2525
2626 -- * Combinators
27− , (<?>)
28− , try
2927 , module Data.Attoparsec.Combinator
3028
3129 -- * Parsing individual characters
@@ -149,15 +147,6 @@ get = T.Parser $ \i0 a0 m0 _kf ks -> ks i0 a0 m0 (unI i0)
149147put :: Text -> Parser ()
150148put s = T.Parser $ \_i0 a0 m0 _kf ks -> ks (I s) a0 m0 ()
151149
152−-- | Attempt a parse, and if it fails, rewind the input so that no
153−-- input appears to have been consumed.
154−--
155−-- This combinator is provided for compatibility with Parsec.
156−-- Attoparsec parsers always backtrack on failure.
157−try :: Parser a -> Parser a
158−try p = p
159−{-# INLINE try #-}
160−
161150unsafeHead :: Text -> Char
162151unsafeHead = T.head
163152
@@ -495,16 +484,6 @@ atEnd = not <$> wantInput
495484endOfLine :: Parser ()
496485endOfLine = (char '\n' >> return ()) <|> (string "\r\n" >> return ())
497486
498−-- | Name the parser, in case failure occurs.
499−(<?>) :: Parser a
500− -> String -- ^ the name to use if parsing fails
501− -> Parser a
502−p <?> msg0 = T.Parser $ \i0 a0 m0 kf ks ->
503− let kf' i a m strs msg = kf i a m (msg0:strs) msg
504− in runParser p i0 a0 m0 kf' ks
505−{-# INLINE (<?>) #-}
506−infix 0 <?>
507−
508487-- | Terminal failure continuation.
509488failK :: Failure a
510489failK i0 _a0 _m0 stack msg = Fail (unI i0) stack msg