gdritter repos eben / a24b8da
Working emitter and beginnings of parser Getty Ritter 5 years ago
4 changed file(s) with 105 addition(s) and 0 deletion(s). Collapse all Expand all
1 {-# LANGUAGE OverloadedStrings #-}
2
3 module Data.Eben where
4
5 import qualified Data.ByteString as B
6 import Data.ByteString.Lazy (ByteString)
7 import qualified Data.ByteString.Lazy as BS
8 import Data.ByteString.Builder (Builder)
9 import qualified Data.ByteString.Builder as BL
10 import Data.Int (Int64)
11 import Data.List (sortOn)
12 import Data.Map.Strict (Map)
13 import qualified Data.Map as M
14 import Data.Monoid ((<>))
15
16 data Value
17 = List [Value]
18 | Dict (Map B.ByteString Value)
19 | String B.ByteString
20 | Integer Int64
21 | Float Float
22 deriving (Eq, Show, Read)
23
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 ->
32
33 encode :: Value -> ByteString
34 encode = BL.toLazyByteString . go
35 where go (List vs) =
36 BL.char7 'l' <> foldMap go vs <> BL.char7 'e'
37 go (Dict vs) =
38 BL.char7 'd'
39 <> mconcat [ str k <> go v | (k, v) <- sortOn fst (M.toList vs) ]
40 <> BL.char7 'e'
41 go (Integer i) =
42 BL.char7 'i' <> BL.string8 (show i) <> BL.char7 'e'
43 go (Float f) =
44 BL.char7 'f' <> BL.floatLE f <> BL.char7 'e'
45 go (String bs) = str bs
46 str bs =
47 BL.intDec (B.length bs)
48 <> BL.char7 ':'
49 <> BL.byteString bs
1 Copyright (c) 2016, Getty Ritter
2
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15
16 * Neither the name of Getty Ritter nor the names of other
17 contributors may be used to endorse or promote products derived
18 from this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1 import Distribution.Simple
2 main = defaultMain
1 -- Initial eben.cabal generated by cabal init. For further documentation,
2 -- see http://haskell.org/cabal/users-guide/
3
4 name: eben
5 version: 0.1.0.0
6 -- synopsis:
7 -- description:
8 license: BSD3
9 license-file: LICENSE
10 author: Getty Ritter
11 maintainer: gdritter@galois.com
12 -- copyright:
13 category: Data
14 build-type: Simple
15 -- extra-source-files:
16 cabal-version: >=1.10
17
18 library
19 exposed-modules: Data.Eben
20 -- other-modules:
21 -- other-extensions:
22 build-depends: base >=4.8 && <4.9, bytestring, containers
23 -- hs-source-dirs:
24 default-language: Haskell2010