A bit more parsing, still not done
Getty Ritter
8 years ago
1 | 1 | {-# LANGUAGE OverloadedStrings #-} |
2 | {-# LANGUAGE ViewPatterns #-} | |
2 | 3 | |
3 | 4 | module Data.Eben where |
4 | 5 | |
12 | 13 | import Data.Map.Strict (Map) |
13 | 14 | import qualified Data.Map as M |
14 | 15 | import Data.Monoid ((<>)) |
16 | import Data.Word (Word8) | |
15 | 17 | |
16 | 18 | data Value |
17 | 19 | = List [Value] |
18 | 20 | | Dict (Map B.ByteString Value) |
19 | | String B.ByteString | |
20 | 21 | | Integer Int64 |
21 | 22 | | Float Float |
23 | | String B.ByteString | |
22 | 24 | deriving (Eq, Show, Read) |
23 | 25 | |
24 | decode :: ByteString -> Either String Value | |
25 | decode bs = case BS.uncons bs of | |
26 | ('l', rs) -> () | |
27 | ('d', rs) -> () | |
28 | ('i', rs) -> () | |
29 | ('f', rs) -> () | |
30 | (i , rs) | |
31 | | isDigit i -> | |
26 | decode :: ByteString -> Maybe (Value, ByteString) | |
27 | decode bs = go | |
28 | where go = case BS.uncons bs of | |
29 | Just (108, rs) -> decodeList | |
30 | Just (100, rs) -> decodeDict | |
31 | Just (105, rs) -> decodeInt | |
32 | Just (102, rs) -> decodeFloat | |
33 | Just (i , rs) | |
34 | | isDigit (fromIntegral i) -> | |
35 | let (is, rs') = BS.break (== 58) rs | |
36 | len = toNum (toDigit i) is | |
37 | (str, rs'') = BS.splitAt len (BS.tail rs') | |
38 | in Just (String (BS.toStrict str), rs'') | |
39 | | otherwise -> Nothing | |
40 | ||
41 | isDigit :: Word8 -> Bool | |
42 | isDigit n = n >= 48 && n <= 57 | |
43 | ||
44 | toDigit :: Word8 -> Int64 | |
45 | toDigit n = fromIntegral n - 48 | |
46 | ||
47 | toNum :: Int64 -> ByteString -> Int64 | |
48 | toNum n (BS.uncons->Just(b, bs)) = | |
49 | toNum (n * 10 + fromIntegral b) bs | |
50 | toNum n _ = n | |
51 | ||
32 | 52 | |
33 | 53 | encode :: Value -> ByteString |
34 | 54 | encode = BL.toLazyByteString . go |