gdritter repos s-cargot / 183a85f
Added basic serialization + fixed silly bug in WellFormed serialization Getty Ritter 9 years ago
2 changed file(s) with 14 addition(s) and 3 deletion(s). Collapse all Expand all
3030 import Data.Char (isAlpha, isDigit, isAlphaNum)
3131 import Data.Map.Strict (Map)
3232 import qualified Data.Map.Strict as M
33 import Data.Monoid ((<>))
3334 import Data.Text (Text, pack, unpack)
3435
3536 import Prelude hiding (takeWhile)
238239 parseOnly (many1 parser <* endOfInput) >=> mapM postparse
239240 where parser = parseGenericSExpr sesPAtom readerMap (buildSkip comment)
240241
241 -- | Emit an S-Expression in a machine-readable way. This
242 -- | Encode (without newlines) a single S-expression.
243 encodeSExpr :: SExpr atom -> (atom -> Text) -> Text
244 encodeSExpr SNil _ = "()"
245 encodeSExpr (SAtom s) t = t s
246 encodeSExpr (SCons x xs) t = go xs (encodeSExpr x t)
247 where go (SAtom s) rs = "(" <> rs <> " . " <> t s <> ")"
248 go SNil rs = "(" <> rs <> ")"
249 go (SCons x xs) rs = go xs (rs <> " " <> encodeSExpr x t)
250
251 -- | Emit an S-Expression in a machine-readable way. This does no
252 -- pretty-printing or indentation, and produces no comments.
242253 encode :: SExprSpec atom carrier -> carrier -> Text
243 encode SExprSpec { .. } = undefined
254 encode SExprSpec { .. } c = encodeSExpr (preserial c) sesSAtom
9696 fromWellFormed :: WellFormedSExpr atom -> SExpr atom
9797 fromWellFormed (WFSAtom a) = SAtom a
9898 fromWellFormed (WFSList xs) =
99 foldl SCons SNil (map fromWellFormed xs)
99 foldr SCons SNil (map fromWellFormed xs)