Browse Source

Parser implementation, more or less

Getty Ritter 8 years ago
commit
c16bc4c042
4 changed files with 116 additions and 0 deletions
  1. 56 0
      Data/ADTN.hs
  2. 30 0
      LICENSE
  3. 2 0
      Setup.hs
  4. 28 0
      adtn.cabal

+ 56 - 0
Data/ADTN.hs

@@ -0,0 +1,56 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Data.ADTN where
+
+import           Control.Applicative((<|>))
+import           Data.Attoparsec.ByteString
+import           Data.Attoparsec.ByteString.Char8
+import           Data.ByteString (ByteString)
+import qualified Data.ByteString as BS
+import           Data.Map.Strict (Map)
+import qualified Data.Map as M
+import           Data.Text (Text)
+import qualified Data.Text as T
+import           Data.Vector (Vector)
+import qualified Data.Vector as V
+
+
+data Value
+  = Sum Text Array
+  | Product Object
+  | List Array
+  | Integer Integer
+  | Double Double
+  | Symbol Text
+  | String Text
+    deriving (Eq, Show)
+
+type Array = Vector Value
+type Object = Map Text Value
+
+decodeValue :: ByteString -> Either String Value
+decodeValue = parseOnly pVal
+  where pVal :: Parser Value
+        pVal = skipSpace *> (pSum <|> pProd <|> pList <|> pLit)
+        pSum = Sum <$> (char '(' *> skipSpace *> pIdent)
+                   <*> (pValueList <* char ')')
+        pProd =  Product . M.fromList
+             <$> (char '{' *> pProdBody <* skipSpace <* char '}')
+        pProdBody = many' pPair
+        pPair = (,) <$> (skipSpace *> pIdent) <*> pVal
+        pList = List <$> (char '[' *> pValueList <* skipSpace <* char ']')
+        pLit  =  Symbol  <$> pIdent
+             <|> String  <$> pString
+             <|> Integer <$> decimal
+        pValueList = V.fromList <$> many' pVal
+        pIdent = T.pack <$> many1' letter_ascii
+        pString = T.pack <$> (char '"' *> manyTill pStrChar (char '"'))
+        pStrChar =  '\n' <$ string "\\n"
+                <|> '\t' <$ string "\\t"
+                <|> '\r' <$ string "\\r"
+                <|> '\b' <$ string "\\b"
+                <|> '\f' <$ string "\\f"
+                <|> '\'' <$ string "\\'"
+                <|> '\"' <$ string "\\\""
+                <|> '\\' <$ string "\\\\"
+                <|> anyChar

+ 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

+ 28 - 0
adtn.cabal

@@ -0,0 +1,28 @@
+-- Initial adtn.cabal generated by cabal init.  For further documentation, 
+-- see http://haskell.org/cabal/users-guide/
+
+name:                adtn
+version:             0.1.0.0
+-- synopsis:            
+-- description:         
+license:             BSD3
+license-file:        LICENSE
+author:              Getty Ritter
+maintainer:          gettyritter@gmail.com
+-- copyright:           
+category:            Data
+build-type:          Simple
+-- extra-source-files:  
+cabal-version:       >=1.10
+
+library
+  exposed-modules:     Data.ADTN
+--  other-modules:       
+  build-depends:       base >=4.8 && <4.9,
+                       attoparsec,
+                       bytestring,
+                       aeson,
+                       containers,
+                       text,
+                       vector
+  default-language:    Haskell2010