-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBencode.hs
34 lines (27 loc) · 949 Bytes
/
Bencode.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Bencode where
import qualified Data.Map.Lazy as Map
import Parser
data BencodeVal = BString String | BInt Int | BList [BencodeVal] | BDict [(BencodeVal,BencodeVal)]
deriving (Show)
parseString :: Parser BencodeVal
parseString = do n <- number
char ':'
str <- manyN item n
return $ BString str
parseInt :: Parser BencodeVal
parseInt = do char 'i'
n <- number
char 'e'
return $ BInt n
parseList :: Parser BencodeVal
parseList = do char 'l'
l <- many parseBencode
char 'e'
return $ BList l
parseDict :: Parser BencodeVal
parseDict = do char 'd'
d <- many $ do { k <- parseString; v <- parseBencode; return (k,v)}
char 'e'
return $ BDict d
parseBencode :: Parser BencodeVal
parseBencode = parseString `option` parseInt `option` parseList