git.iko.soy/typeable/attoparsec
on master help sign in
Change the API around a little.
commit34d6aca762f22de0684a15e35610dfde7f7ef88f authorBryan O'Sullivan <bos@serpentine.com> dateFri 25 Jan 2008, 13:11 (19 years ago) parent6400b21
1 file changed+42−12
src/Text/ParserCombinators/ByteStringParser.hs+42−12
src/Text/ParserCombinators/ByteStringParser.hs+42−12
@@ -38,6 +38,7 @@ module Text.ParserCombinators.ByteStringParser
3838 , skipMany1
3939 , count
4040 , lookAhead
41+ , peek
4142 , sepBy
4243 , sepBy1
4344
@@ -48,16 +49,22 @@ module Text.ParserCombinators.ByteStringParser
4849 , anyChar
4950 , space
5051 , char
52+ , notChar
5153 , string
5254 , stringCI
5355 , byteString
5456 , byteStringCI
5557
58+ -- * Parser converters.
59+ , maybeP
60+ , eitherP
61+
5662 -- * Miscellaneous functions.
5763 , getInput
5864 , getConsumed
5965 , takeWhile
6066 , takeWhile1
67+ , takeTill
6168 , takeAll
6269 , skipWhile
6370 , skipSpace
@@ -67,7 +74,7 @@ module Text.ParserCombinators.ByteStringParser
6774 , notInClass
6875 ) where
6976
70−import Control.Applicative (Applicative(..))
77+import Control.Applicative (Applicative(..), (<$>), (<*))
7178import Control.Monad (MonadPlus(..), ap, liftM2)
7279import qualified Data.ByteString.Lazy.Char8 as C
7380import Data.Char (isDigit, isLetter, isSpace, toLower)
@@ -75,7 +82,7 @@ import Data.Int (Int64)
7582import qualified Data.Set as S
7683import Prelude hiding (takeWhile)
7784
78−type ParseError = (C.ByteString, String)
85+type ParseError = String
7986
8087-- * Parser Monad
8188
@@ -176,6 +183,11 @@ char :: Char -> Parser Char
176183char c = satisfy (== c) <?> [c]
177184{-# INLINE char #-}
178185
186+-- | Satisfy a specific character.
187+notChar :: Char -> Parser Char
188+notChar c = satisfy (/= c) <?> "not " ++ [c]
189+{-# INLINE notChar #-}
190+
179191charClass :: String -> S.Set Char
180192charClass s = S.fromList (go s)
181193 where go (a:'-':b:xs) = [a..b] ++ go xs
@@ -252,6 +264,10 @@ takeWhile f = Parser $ \(S bs n) ->
252264 in Right (h, S bs' (n + C.length h))
253265{-# INLINE takeWhile #-}
254266
267+takeTill :: (Char -> Bool) -> Parser C.ByteString
268+takeTill p = takeWhile (not . p) <* satisfy p
269+{-# INLINE takeTill #-}
270+
255271takeWhile1 :: (Char -> Bool) -> Parser C.ByteString
256272takeWhile1 f = Parser $ \(S bs n) ->
257273 let (h, bs') = C.span f bs
@@ -313,29 +329,43 @@ match p = do bs <- getInput
313329 end <- getConsumed
314330 return (C.take (end - start) bs)
315331
316−lookAhead :: Parser a -> Parser (Maybe a)
332+maybeP :: Parser a -> Parser (Maybe a)
333+maybeP p = (Just <$> p) <|> pure Nothing
334+{-# INLINE maybeP #-}
335+
336+eitherP :: Parser a -> Parser b -> Parser (Either a b)
337+eitherP a b = (Left <$> a) <|> (Right <$> b)
338+{-# INLINE eitherP #-}
339+
340+peek :: Parser a -> Parser (Maybe a)
341+peek p = Parser $ \s ->
342+ case unParser p s of
343+ Right (m, _) -> Right (Just m, s)
344+ _ -> Right (Nothing, s)
345+
346+lookAhead :: Parser a -> Parser a
317347lookAhead p = Parser $ \s ->
318− case unParser p s of
319− Right (m, _) -> Right (Just m, s)
320− _ -> Right (Nothing, s)
348+ case unParser p s of
349+ Right (m, _) -> Right (m, s)
350+ Left (e, bs) -> Left (e, bs)
321351
322352parseAt :: Parser a -> C.ByteString -> Int64
323− -> Either ParseError (a, C.ByteString)
353+ -> (C.ByteString, Either ParseError a)
324354parseAt p bs n =
325355 case unParser p (S bs n) of
326− Left (bs', msg) -> Left (bs', showError msg)
327− Right (a, S bs' _) -> Right (a, bs')
356+ Left (bs', msg) -> (bs', Left $ showError msg)
357+ Right (a, S bs' _) -> (bs', Right a)
328358 where
329359 showError [msg] = "Parser error, expected:\n" ++ msg ++ "\n"
330360 showError msgs = "Parser error, expected one of:\n" ++ unlines msgs
331361
332362-- | Run a parser.
333363parse :: Parser a -> C.ByteString
334− -> Either ParseError (a, C.ByteString)
364+ -> (C.ByteString, Either ParseError a)
335365parse p bs = parseAt p bs 0
336366
337367parseTest :: (Show a) => Parser a -> C.ByteString -> IO ()
338368parseTest p s =
339369 case parse p s of
340− Left (st, msg) -> putStrLn $ msg ++ "\nGot:\n" ++ show st
341− Right (r,_) -> print r
370+ (st, Left msg) -> putStrLn $ msg ++ "\nGot:\n" ++ show st
371+ (_, Right r) -> print r