Basic.hs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Data.SCargot.Language.Basic
  3. ( -- * Spec
  4. -- $descr
  5. basicParser
  6. , basicPrinter
  7. ) where
  8. import Control.Applicative ((<$>))
  9. import Data.Char (isAlphaNum)
  10. import Text.Parsec (many1, satisfy)
  11. import Data.Text (Text, pack)
  12. import Data.SCargot.Repr.Basic (SExpr)
  13. import Data.SCargot ( SExprParser
  14. , SExprPrinter
  15. , mkParser
  16. , flatPrint
  17. )
  18. import Data.SCargot.Comments (withLispComments)
  19. isAtomChar :: Char -> Bool
  20. isAtomChar c = isAlphaNum c
  21. || c == '-' || c == '*' || c == '/'
  22. || c == '+' || c == '<' || c == '>'
  23. || c == '=' || c == '!' || c == '?'
  24. -- $descr
  25. -- The 'basicSpec' describes S-expressions whose atoms are simply
  26. -- text strings that contain alphanumeric characters and a small
  27. -- set of punctuation. It does no parsing of numbers or other data
  28. -- types, and will accept tokens that typical Lisp implementations
  29. -- would find nonsensical (like @77foo@).
  30. --
  31. -- Atoms recognized by the 'basicSpec' are any string matching the
  32. -- regular expression @[A-Za-z0-9+*<>/=!?-]+@.
  33. -- | A 'SExprParser' that understands atoms to be sequences of
  34. -- alphanumeric characters as well as the punctuation
  35. -- characters @[-*/+<>=!?]@, and does no processing of them.
  36. --
  37. -- >>> decode basicParser "(1 elephant)"
  38. -- Right [SCons (SAtom "1") (SCons (SAtom "elephant") SNil)]
  39. basicParser :: SExprParser Text (SExpr Text)
  40. basicParser = mkParser pToken
  41. where pToken = pack <$> many1 (satisfy isAtomChar)
  42. -- | A 'SExprPrinter' that prints textual atoms directly (without quoting
  43. -- or any other processing) onto a single line.
  44. --
  45. -- >>> encode basicPrinter [L [A "1", A "elephant"]]
  46. -- "(1 elephant)"
  47. basicPrinter :: SExprPrinter Text (SExpr Text)
  48. basicPrinter = flatPrint id