Basic.hs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Data.SCargot.Basic
  3. ( basicSpec
  4. , asRich
  5. , asWellFormed
  6. , addReader
  7. , setComment
  8. , withSemicolonComments
  9. , withQuote
  10. ) where
  11. import Data.Char (isAlphaNum)
  12. import Data.Attoparsec.Text (Parser, takeWhile1)
  13. import Data.Text (Text)
  14. import Data.SCargot.Repr.Basic
  15. import Data.SCargot.General hiding (withQuote)
  16. isAtomChar :: Char -> Bool
  17. isAtomChar c = isAlphaNum c
  18. || c == '-'
  19. || c == '*'
  20. || c == '/'
  21. || c == '+'
  22. || c == '<'
  23. || c == '>'
  24. || c == '='
  25. || c == '!'
  26. || c == '?'
  27. -- | A 'SExprSpec' that understands atoms to be sequences of
  28. -- alphanumeric characters as well as the punctuation
  29. -- characters @-*/+<>=!?@, and does no processing of them.
  30. -- This is not quite representative of actual lisps, which
  31. -- would (for example) accept various kinds of string
  32. -- literals. This should be sufficient for most ad-hoc
  33. -- storage or configuration formats.
  34. basicSpec :: SExprSpec Text (SExpr Text)
  35. basicSpec = mkSpec (takeWhile1 isAtomChar) id
  36. -- | Add the ability to understand a quoted S-Expression.
  37. -- This means that @'sexpr@ becomes sugar for
  38. -- @(quote sexpr)@. This is a variation on the identically-named
  39. -- function in Data.SCargot.General that has been specialized
  40. -- for the Basic atom type.
  41. withQuote :: SExprSpec Text a -> SExprSpec Text a
  42. withQuote = addReader '\'' (fmap go)
  43. where go s = SCons (SAtom "quote") (SCons s SNil)