gdritter repos adnot / c16bc4c
Parser implementation, more or less Getty Ritter 8 years ago
4 changed file(s) with 116 addition(s) and 0 deletion(s). Collapse all Expand all
1 {-# LANGUAGE OverloadedStrings #-}
2
3 module Data.ADTN where
4
5 import Control.Applicative((<|>))
6 import Data.Attoparsec.ByteString
7 import Data.Attoparsec.ByteString.Char8
8 import Data.ByteString (ByteString)
9 import qualified Data.ByteString as BS
10 import Data.Map.Strict (Map)
11 import qualified Data.Map as M
12 import Data.Text (Text)
13 import qualified Data.Text as T
14 import Data.Vector (Vector)
15 import qualified Data.Vector as V
16
17
18 data Value
19 = Sum Text Array
20 | Product Object
21 | List Array
22 | Integer Integer
23 | Double Double
24 | Symbol Text
25 | String Text
26 deriving (Eq, Show)
27
28 type Array = Vector Value
29 type Object = Map Text Value
30
31 decodeValue :: ByteString -> Either String Value
32 decodeValue = parseOnly pVal
33 where pVal :: Parser Value
34 pVal = skipSpace *> (pSum <|> pProd <|> pList <|> pLit)
35 pSum = Sum <$> (char '(' *> skipSpace *> pIdent)
36 <*> (pValueList <* char ')')
37 pProd = Product . M.fromList
38 <$> (char '{' *> pProdBody <* skipSpace <* char '}')
39 pProdBody = many' pPair
40 pPair = (,) <$> (skipSpace *> pIdent) <*> pVal
41 pList = List <$> (char '[' *> pValueList <* skipSpace <* char ']')
42 pLit = Symbol <$> pIdent
43 <|> String <$> pString
44 <|> Integer <$> decimal
45 pValueList = V.fromList <$> many' pVal
46 pIdent = T.pack <$> many1' letter_ascii
47 pString = T.pack <$> (char '"' *> manyTill pStrChar (char '"'))
48 pStrChar = '\n' <$ string "\\n"
49 <|> '\t' <$ string "\\t"
50 <|> '\r' <$ string "\\r"
51 <|> '\b' <$ string "\\b"
52 <|> '\f' <$ string "\\f"
53 <|> '\'' <$ string "\\'"
54 <|> '\"' <$ string "\\\""
55 <|> '\\' <$ string "\\\\"
56 <|> anyChar
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 adtn.cabal generated by cabal init. For further documentation,
2 -- see http://haskell.org/cabal/users-guide/
3
4 name: adtn
5 version: 0.1.0.0
6 -- synopsis:
7 -- description:
8 license: BSD3
9 license-file: LICENSE
10 author: Getty Ritter
11 maintainer: gettyritter@gmail.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.ADTN
20 -- other-modules:
21 build-depends: base >=4.8 && <4.9,
22 attoparsec,
23 bytestring,
24 aeson,
25 containers,
26 text,
27 vector
28 default-language: Haskell2010