module Data.ParserCombinators.Attoparsec.Char8
(
ParseError
, Parser
, parse
, parseAt
, parseTest
, (<?>)
, try
, manyTill
, eof
, skipMany
, skipMany1
, count
, lookAhead
, peek
, sepBy
, sepBy1
, satisfy
, letter
, digit
, anyChar
, space
, char
, notChar
, string
, stringCI
, eitherP
, getInput
, getConsumed
, takeWhile
, takeWhile1
, takeTill
, takeAll
, skipWhile
, skipSpace
, notEmpty
, match
, inClass
, notInClass
) where
import Control.Applicative ((<$>))
import qualified Data.ByteString.Char8 as SB
import qualified Data.ByteString.Lazy.Char8 as LB
import Data.ByteString.Internal (w2c)
import Data.Char (isDigit, isLetter, isSpace, toLower)
import Data.ParserCombinators.Attoparsec.FastSet
(FastSet, memberChar, set)
import qualified Data.ParserCombinators.Attoparsec.Internal as I
import Data.ParserCombinators.Attoparsec.Internal
(Parser, ParseError, (<?>), parse, parseAt, parseTest, try, manyTill, eof,
skipMany, skipMany1, count, lookAhead, peek, sepBy, sepBy1, string,
eitherP, getInput, getConsumed, takeAll, notEmpty, match)
import Prelude hiding (takeWhile)
satisfy :: (Char -> Bool) -> Parser Char
satisfy p = w2c <$> I.satisfy (p . w2c)
letter :: Parser Char
letter = satisfy isLetter <?> "letter"
digit :: Parser Char
digit = satisfy isDigit <?> "digit"
anyChar :: Parser Char
anyChar = satisfy $ const True
space :: Parser Char
space = satisfy isSpace <?> "space"
char :: Char -> Parser Char
char c = satisfy (== c) <?> [c]
notChar :: Char -> Parser Char
notChar c = satisfy (/= c) <?> "not " ++ [c]
charClass :: String -> FastSet
charClass = set . SB.pack . go
where go (a:'-':b:xs) = [a..b] ++ go xs
go (x:xs) = x : go xs
go _ = ""
inClass :: String -> Char -> Bool
inClass s = (`memberChar` myset)
where myset = charClass s
notInClass :: String -> Char -> Bool
notInClass s = not . inClass s
stringCI :: LB.ByteString -> Parser LB.ByteString
stringCI = I.stringTransform (LB.map toLower)
takeWhile :: (Char -> Bool) -> Parser LB.ByteString
takeWhile p = I.takeWhile (p . w2c)
takeTill :: (Char -> Bool) -> Parser LB.ByteString
takeTill p = I.takeTill (p . w2c)
takeWhile1 :: (Char -> Bool) -> Parser LB.ByteString
takeWhile1 p = I.takeWhile1 (p . w2c)
skipWhile :: (Char -> Bool) -> Parser ()
skipWhile p = I.skipWhile (p . w2c)
skipSpace :: Parser ()
skipSpace = takeWhile isSpace >> return ()