Quellcode durchsuchen

Working on Common Lisp repr

Getty Ritter vor 10 Jahren
Ursprung
Commit
f5c0fe8951
2 geänderte Dateien mit 58 neuen und 17 gelöschten Zeilen
  1. 22 6
      Data/SCargot/CommonLisp.hs
  2. 36 11
      Data/SCargot/General.hs

+ 22 - 6
Data/SCargot/CommonLisp.hs

@@ -6,11 +6,27 @@
 
 module Data.SCargot.CommonLisp where
 
-data Atom
-  = Symbol Text
-  | String Text
-  | Integer Int
-  | True
+data CLAtom
+  = CLSymbol Text
+  | CLString Text
+  | CLInteger Integer
+  | CLRatio Integer Integer
+  | CLFloat Double
     deriving (Eq, Show, Read)
 
-parseSexpr :: Text -> Either SExprError
+type CommonLispSpec carrier = SExprSpec CLAtom carrier
+
+withComments :: CommonLispSpec c -> CommonLispSpec c
+withComments = addCommentType (const () <$> (char ';' *> restOfLine))
+
+withQuote :: CommonLispSpec (SCons CLAtom) -> CommonLispSpec (SCons CLAtom)
+withQuote = addReader '\'' (go <$> parse)
+  where go v = SCons q (SCons v SNil)
+
+-- | Adds support for the '#(...)' sugar for vectors. (This will be
+--   parsed as '(vector ...)', and
+withVectors :: CommonLispSpec c -> CommonLispSpec c
+withVectors = addReader '#' (go <$> parse)
+
+parse :: CommonLispSpec c -> Text -> Either String c
+serialize :: CommonLispSpec c -> c -> Text

+ 36 - 11
Data/SCargot/General.hs

@@ -1,4 +1,19 @@
-module Data.SCargot.General where
+module Data.SCargot.General
+  ( -- * SExprSpec
+    SExprSpec
+  , mkSpec
+  , convertSpec
+  , addReader
+  , addCommentType
+  , asRich
+  , asWellFormed
+    -- * A Few Standard Reader Macros
+  , quote
+  , vector
+    -- * Using a SExprSpec
+  , parseSExpr
+  , serializeSExpr
+  ) where
 
 import           Control.Applicative
 import           Data.Attoparsec.Text
@@ -8,21 +23,23 @@ import qualified Data.Map.String as M
 import           Data.SCargot.Repr
 
 type ReaderMacroMap atom = Map Char (Reader atom)
+type CommentMap = Map Char (Parser ())
 type Reader atom = (Parser (SExpr atom) -> Parser (SExpr atom))
 type Serializer atom = atom -> Text
 
 -- | A 'SExprSpec' describes a parser and emitter for a particular
 --   variant of S-Expressions. The @atom@ type corresponds to a
 --   Haskell type used to represent the atoms, and the @carrier@
+--   type corresponds to the parsed S-Expression structure. The
+--   'SExprSpec' type is deliberately opaque so that it must be
+--   constructed and modified with other helper functions.
 data SExprSpec atom carrier = SExprSpec
-  { sesPAtom  :: Parser atom
-  , sesSAtom  :: Serializer atom
-  , rmMap     :: ReaderMacroMap atom
-  , postparse :: SExpr atom -> Either String carrier
-  , preserial :: carrier -> SExpr atom
+  { sesPAtom   :: Parser atom
+  , sesSAtom   :: Serializer atom
+  , readerMap  :: ReaderMacroMap atom
+  , commentMap :: CommentMap
+  , postparse  :: SExpr atom -> Either String carrier
+  , preserial  :: carrier -> SExpr atom
   }
 
 -- | This creates a basic 'SExprSpec' when given a parser and serializer
@@ -53,17 +70,22 @@ convertSpec f g spec = spec
 addReader :: Char -> Reader a -> SExprSpec a c -> SExprSpec a c
 addReader c reader spec = spec { rmMap = insert c reader (rmMap spec) }
 
+addCommentType :: Char -> Comment -> SExprSpec a c -> SExprSpec a c
+addCommentType c comment spec = spec { }
+
 quote :: atom -> Reader atom
 quote q parse = go <$> parse
   where go v = SCons q (SCons v SNil)
 
-toRich :: SExprSpec a (SExpr b) -> SExprSpec a (RichSExpr b)
-toRich = convertSpec (return . toRich) fromRich
+asRich :: SExprSpec a (SExpr b) -> SExprSpec a (RichSExpr b)
+asRich = convertSpec (return . toRich) fromRich
 
-toWellFormed :: SExprSpec a (SExpr b) -> SExprSpec a (WellFormedSExpr b)
-toWellFormed = convertSpec toWellFormed fromWellFormed
+asWellFormed :: SExprSpec a (SExpr b) -> SExprSpec a (WellFormedSExpr b)
+asWellFormed = convertSpec toWellFormed fromWellFormed
 
-parseGenericSExpr :: Parser atom  -> ReaderMacroMap atom -> Parser (SExpr atom)
+parseGenericSExpr :: Parser atom  -> ReaderMacroMap atom -> CommentMap -> Parser (SExpr atom)
+parseGenericSExpr atom reader comment =
+  char '(' *> 
 
 -- |
 parseSExpr :: SExprSpec atom carrier -> Text -> Either String carrier