CommonLisp.hs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. module Data.SCargot.CommonLisp
  7. ( CLAtom(..)
  8. , CommonLispSpec
  9. , withComments
  10. , withQuote
  11. , withVectors
  12. , decode
  13. , encode
  14. ) where
  15. data CLAtom
  16. = CLSymbol Text
  17. | CLString Text
  18. | CLInteger Integer
  19. | CLRatio Integer Integer
  20. | CLFloat Double
  21. deriving (Eq, Show, Read)
  22. data CommonLispSpec carrier = CommonLispSpec
  23. { sexprSpec :: SExprSpec CLAtom carrier
  24. , octoReaders :: ReaderMacroMap CLAtom
  25. }
  26. withComments :: CommonLispSpec c -> CommonLispSpec c
  27. withComments = addCommentType (const () <$> (char ';' *> restOfLine))
  28. withQuote :: CommonLispSpec (SCons CLAtom) -> CommonLispSpec (SCons CLAtom)
  29. withQuote = addReader '\'' (go <$> parse)
  30. where go v = SCons q (SCons v SNil)
  31. -- | Adds support for the '#(...)' sugar for vectors. (This will be
  32. -- parsed as '(vector ...)', and
  33. withVectors :: CommonLispSpec c -> CommonLispSpec c
  34. withVectors = addReader '#' (go <$> parse)
  35. decode :: CommonLispSpec c -> Text -> Either String c
  36. encode :: CommonLispSpec c -> c -> Text