gdritter repos s-cargot / 65b9c65
Basic repr + some stubs Getty Ritter 9 years ago
8 changed file(s) with 169 addition(s) and 0 deletion(s). Collapse all Expand all
1 -- | Contains the type of atoms that Common Lisp understands, as
2 -- well as the built-in reader macros that Common Lisp provides.
3 -- Given a Common Lisp source file that contains no extra reader
4 -- macro definitions, this module should successfully parse and
5 -- desugar even quoted lists and vector literals.
6
7 module Data.SExpression.CommonLisp where
8
9 data Atom
10 = Symbol Text
11 | String Text
12 | Integer Int
13 | True
14 deriving (Eq, Show, Read)
15
16 parseSexpr :: Text -> Either SExprError
1 module Data.SExpression.General where
1 {-# LANGUAGE PatternSynonyms #-}
2
3 module Data.SExpression.Repr.Rich
4 ( pattern List
5 , pattern DotList
6 , pattern Atom
7 ) where
8
9 import Data.SExpression.Repr as R
10
11 pattern List xs = R.RSList xs
12 pattern DotList xs = R.RSDotted xs
13 pattern Atom a = R.RSAtom a
1 {-# LANGUAGE PatternSynonyms #-}
2
3 module Data.SExpression.Repr.Rich
4 ( pattern List
5 , pattern Atom
6 ) where
7
8 import Data.SExpression.Repr as R
9
10 pattern List xs = R.WFSList xs
11 pattern Atom a = R.WFSAtom a
1 module Data.SExpression.Repr
2 ( SExpr(..)
3 , RichSExpr(..)
4 , toRich
5 , fromRich
6 , WellFormedSExpr(..)
7 , toWellFormed
8 , fromWellFormed
9 ) where
10
11 -- | All S-Expressions can be understood as a sequence
12 -- of @cons@ cells (represented here by @SCons@), the
13 -- empty list @nil@ (represented by @SNil@) or an
14 -- @atom@.
15 data SExpr atom
16 = SCons (SExpr atom) (SExpr atom)
17 | SAtom atom
18 | SNil
19 deriving (Eq, Show, Read)
20
21 -- | Sometimes, the cons-based interface is too low
22 -- level, and we'd rather have the lists themselves
23 -- exposed. In this case, we have @RSList@ to
24 -- represent a well-formed cons list, and @RSDotted@
25 -- to represent an improper list of the form
26 -- @(a b c . d)@.
27 data RichSExpr atom
28 = RSList [RichSExpr atom]
29 | RSDotted [RichSExpr atom] atom
30 | RSAtom atom
31 deriving (Eq, Show, Read)
32
33 toRich :: SExpr atom -> RichSExpr atom
34 toRich (SAtom a) = RSAtom a
35 toRich (SCons x xs) = go xs [toRich x]
36 where go (SAtom a) rs = RSDotted rs a
37 go SNil rs = RSList rs
38 go (SCons x xs) rs = go xs (toRich x:rs)
39
40 fromRich :: RichSExpr atom -> SExpr atom
41 fromRich (RSAtom a) = SAtom a
42 fromRich (RSList xs) = foldr SCons SNil (map fromRich xs)
43 fromRich (RSDotted xs x) = foldr SCons (SAtom x) (map fromRich xs)
44
45 -- | A well-formed s-expression is one which does not
46 -- contain any dotted lists. This means that not
47 -- every value of @SExpr a@ can be converted to a
48 -- @WellFormedSExpr a@, although the opposite is
49 -- fine.
50 data WellFormedSExpr atom
51 = WFSList [WellFormedSExpr atom]
52 | WFSAtom atom
53 deriving (Eq, Show, Read)
54
55 -- | This will be @Nothing@ is the argument contains an
56 -- improper list. It should hold that
57 toWellFormed :: SExpr atom -> Maybe (WellFormedSExpr atom)
58 toWellFormed (SAtom a) = Just (WFSAtom a)
59 toWellFormed (SCons x xs) = do
60 x' <- toWellFormed x
61 go xs [x']
62 where go (SAtom a) rs = Nothing
63 go SNil rs = Just (WFSList rs)
64 go (SCons x xs) rs = do
65 x' <- toWellFormed x
66 go xs (x':rs)
67
68 -- | Convert a WellFormedSExpr back into a SExpr.
69 fromWellFormed :: WellFormedSExpr atom -> SExpr atom
70 fromWellFormed (WFSAtom a) = SAtom a
71 fromWellFormed (WFSList xs) =
72 foldr SCons SNil (map fromWellFormed xs)
1 Copyright (c) 2014, 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 s-expression.cabal generated by cabal init. For further
2 -- documentation, see http://haskell.org/cabal/users-guide/
3
4 name: s-expression
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:
20 -- other-modules:
21 -- other-extensions:
22 build-depends: base >=4.7 && <4.8
23 -- hs-source-dirs:
24 default-language: Haskell2012