import qualified Data.Map as M
import qualified Text.Parsec.ByteString as ParsecBS
import Text.ParserCombinators.Parsec
+import Text.ParserCombinators.Parsec.Char
import Control.Applicative ((<*))
import Data.Functor
-- Right ""
-- >>> parse bencStr "Bstr" (BC.pack "0:hello")
-- Right ""
+--
bencStr :: ParsecBS.Parser BC.ByteString
bencStr = do _ <- spaces
ds <- many1 digit <* char ':'
s <- count (read ds) anyChar
return (BC.pack s)
+bencInt :: ParsecBS.Parser Integer
+bencInt = do _ <- spaces
+ ds <- between (char 'i') (char 'e') numbers1
+ return (read ds)
+ where numbers = do sign <- option ' ' (try (char '-'))
+ ds' <- many1 digit
+ return (sign : ds')
+ numbers1 = many1 anyChar
+
bencParser :: ParsecBS.Parser BVal
-bencParser = Bstr <$> bencStr
+bencParser = Bstr <$> bencStr <|>
+ Bint <$> bencInt
decode :: BC.ByteString -> Either ParseError BVal
decode = parse bencParser "BVal"