From: Ramakrishnan Muthukrishnan Date: Wed, 4 Feb 2015 01:47:32 +0000 (+0530) Subject: WIP: just experimenting X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/module-simplejson-index.html?a=commitdiff_plain;h=123f573ab444d11478125a8a122b446e2388a6fe;p=functorrent.git WIP: just experimenting --- diff --git a/src/Bencode.hs b/src/Bencode.hs index 951f4f1..d6b5740 100644 --- a/src/Bencode.hs +++ b/src/Bencode.hs @@ -5,6 +5,7 @@ import qualified Data.ByteString.Char8 as BC 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 @@ -25,14 +26,25 @@ data BVal = -- 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"