Browse Source

Working emitter and beginnings of parser

Getty Ritter 8 years ago
commit
a24b8da11b
4 changed files with 105 additions and 0 deletions
  1. 49 0
      Data/Eben.hs
  2. 30 0
      LICENSE
  3. 2 0
      Setup.hs
  4. 24 0
      eben.cabal

+ 49 - 0
Data/Eben.hs

@@ -0,0 +1,49 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Data.Eben where
+
+import qualified Data.ByteString as B
+import           Data.ByteString.Lazy (ByteString)
+import qualified Data.ByteString.Lazy as BS
+import           Data.ByteString.Builder (Builder)
+import qualified Data.ByteString.Builder as BL
+import           Data.Int (Int64)
+import           Data.List (sortOn)
+import           Data.Map.Strict (Map)
+import qualified Data.Map as M
+import           Data.Monoid ((<>))
+
+data Value
+  = List [Value]
+  | Dict (Map B.ByteString Value)
+  | String B.ByteString
+  | Integer Int64
+  | Float Float
+    deriving (Eq, Show, Read)
+
+decode :: ByteString -> Either String Value
+decode bs = case BS.uncons bs of
+  ('l', rs) -> ()
+  ('d', rs) -> ()
+  ('i', rs) -> ()
+  ('f', rs) -> ()
+  (i  , rs)
+    | isDigit i -> 
+
+encode :: Value -> ByteString
+encode = BL.toLazyByteString . go
+  where go (List vs) =
+          BL.char7 'l' <> foldMap go vs <> BL.char7 'e'
+        go (Dict vs) =
+          BL.char7 'd'
+          <> mconcat [ str k <> go v | (k, v) <- sortOn fst (M.toList vs) ]
+          <> BL.char7 'e'
+        go (Integer i) =
+          BL.char7 'i' <> BL.string8 (show i) <> BL.char7 'e'
+        go (Float f) =
+          BL.char7 'f' <> BL.floatLE f <> BL.char7 'e'
+        go (String bs) = str bs
+        str bs =
+          BL.intDec (B.length bs)
+          <> BL.char7 ':'
+          <> BL.byteString bs

+ 30 - 0
LICENSE

@@ -0,0 +1,30 @@
+Copyright (c) 2016, Getty Ritter
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+    * Neither the name of Getty Ritter nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 2 - 0
Setup.hs

@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain

+ 24 - 0
eben.cabal

@@ -0,0 +1,24 @@
+-- Initial eben.cabal generated by cabal init.  For further documentation, 
+-- see http://haskell.org/cabal/users-guide/
+
+name:                eben
+version:             0.1.0.0
+-- synopsis:            
+-- description:         
+license:             BSD3
+license-file:        LICENSE
+author:              Getty Ritter
+maintainer:          gdritter@galois.com
+-- copyright:           
+category:            Data
+build-type:          Simple
+-- extra-source-files:  
+cabal-version:       >=1.10
+
+library
+  exposed-modules:     Data.Eben
+  -- other-modules:
+  -- other-extensions:
+  build-depends:       base >=4.8 && <4.9, bytestring, containers
+  -- hs-source-dirs:
+  default-language:    Haskell2010