Browse Source

Basic repr + some stubs

Getty Ritter 9 years ago
commit
65b9c656d3

+ 16 - 0
Data/SExpression/CommonLisp.hs

@@ -0,0 +1,16 @@
+-- | Contains the type of atoms that Common Lisp understands, as
+--   well as the built-in reader macros that Common Lisp provides.
+--   Given a Common Lisp source file that contains no extra reader
+--   macro definitions, this module should successfully parse and
+--   desugar even quoted lists and vector literals.
+
+module Data.SExpression.CommonLisp where
+
+data Atom
+  = Symbol Text
+  | String Text
+  | Integer Int
+  | True
+    deriving (Eq, Show, Read)
+
+parseSexpr :: Text -> Either SExprError 

+ 1 - 0
Data/SExpression/General.hs

@@ -0,0 +1 @@
+module Data.SExpression.General where

+ 72 - 0
Data/SExpression/Repr.hs

@@ -0,0 +1,72 @@
+module Data.SExpression.Repr
+       ( SExpr(..)
+       , RichSExpr(..)
+       , toRich
+       , fromRich
+       , WellFormedSExpr(..)
+       , toWellFormed
+       , fromWellFormed
+       ) where
+
+-- | All S-Expressions can be understood as a sequence
+--   of @cons@ cells (represented here by @SCons@), the
+--   empty list @nil@ (represented by @SNil@) or an
+--   @atom@.
+data SExpr atom
+  = SCons (SExpr atom) (SExpr atom)
+  | SAtom atom
+  | SNil
+    deriving (Eq, Show, Read)
+
+-- | Sometimes, the cons-based interface is too low
+--   level, and we'd rather have the lists themselves
+--   exposed. In this case, we have @RSList@ to
+--   represent a well-formed cons list, and @RSDotted@
+--   to represent an improper list of the form
+--   @(a b c . d)@.
+data RichSExpr atom
+  = RSList [RichSExpr atom]
+  | RSDotted [RichSExpr atom] atom
+  | RSAtom atom
+    deriving (Eq, Show, Read)
+
+toRich :: SExpr atom -> RichSExpr atom
+toRich (SAtom a) = RSAtom a
+toRich (SCons x xs) = go xs [toRich x]
+  where go (SAtom a) rs    = RSDotted rs a
+        go SNil rs         = RSList rs
+        go (SCons x xs) rs = go xs (toRich x:rs)
+
+fromRich :: RichSExpr atom -> SExpr atom
+fromRich (RSAtom a) = SAtom a
+fromRich (RSList xs) = foldr SCons SNil (map fromRich xs)
+fromRich (RSDotted xs x) = foldr SCons (SAtom x) (map fromRich xs)
+
+-- | A well-formed s-expression is one which does not
+--   contain any dotted lists. This means that not
+--   every value of @SExpr a@ can be converted to a
+--   @WellFormedSExpr a@, although the opposite is
+--   fine.
+data WellFormedSExpr atom
+  = WFSList [WellFormedSExpr atom]
+  | WFSAtom atom
+    deriving (Eq, Show, Read)
+
+-- | This will be @Nothing@ is the argument contains an
+--   improper list. It should hold that
+toWellFormed :: SExpr atom -> Maybe (WellFormedSExpr atom)
+toWellFormed (SAtom a) = Just (WFSAtom a)
+toWellFormed (SCons x xs) = do
+  x' <- toWellFormed x
+  go xs [x']
+  where go (SAtom a) rs = Nothing
+        go SNil rs      = Just (WFSList rs)
+        go (SCons x xs) rs = do
+          x' <- toWellFormed x
+          go xs (x':rs)
+
+-- | Convert a WellFormedSExpr back into a SExpr.
+fromWellFormed :: WellFormedSExpr atom -> SExpr atom
+fromWellFormed (WFSAtom a)  = SAtom a
+fromWellFormed (WFSList xs) =
+  foldr SCons SNil (map fromWellFormed xs)

+ 13 - 0
Data/SExpression/Repr/Rich.hs

@@ -0,0 +1,13 @@
+{-# LANGUAGE PatternSynonyms #-}
+
+module Data.SExpression.Repr.Rich
+       ( pattern List
+       , pattern DotList
+       , pattern Atom
+       ) where
+
+import Data.SExpression.Repr as R
+
+pattern List xs = R.RSList xs
+pattern DotList xs = R.RSDotted xs
+pattern Atom a = R.RSAtom a

+ 11 - 0
Data/SExpression/Repr/WellFormed.hs

@@ -0,0 +1,11 @@
+{-# LANGUAGE PatternSynonyms #-}
+
+module Data.SExpression.Repr.Rich
+       ( pattern List
+       , pattern Atom
+       ) where
+
+import Data.SExpression.Repr as R
+
+pattern List xs = R.WFSList xs
+pattern Atom a  = R.WFSAtom a

+ 30 - 0
LICENSE

@@ -0,0 +1,30 @@
+Copyright (c) 2014, 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
s-expression.cabal

@@ -0,0 +1,24 @@
+-- Initial s-expression.cabal generated by cabal init.  For further 
+-- documentation, see http://haskell.org/cabal/users-guide/
+
+name:                s-expression
+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:     
+  -- other-modules:       
+  -- other-extensions:    
+  build-depends:       base >=4.7 && <4.8
+  -- hs-source-dirs:      
+  default-language:    Haskell2012