Atom.hs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. module Data.SCargot.Atom
  2. ( -- $intro
  3. atom
  4. , mkAtomParser
  5. ) where
  6. import Data.SCargot.Parse (SExprParser, mkParser)
  7. import Data.SCargot.Repr (SExpr)
  8. import Text.Parsec (choice)
  9. import Text.Parsec.Text (Parser)
  10. -- | A convenience function for defining an atom parser from a wrapper
  11. -- function and a parser. This is identical to 'fmap' specialized to
  12. -- operate over 'Parser' values, and is provided as sugar.
  13. atom :: (t -> atom) -> Parser t -> Parser atom
  14. atom = fmap
  15. -- | A convenience function for defining a 'SExprSpec' from a list of
  16. -- possible atom parsers, which will be tried in sequence before failing.
  17. mkAtomParser :: [Parser atom] -> SExprParser atom (SExpr atom)
  18. mkAtomParser = mkParser . choice
  19. {- $intro
  20. This module defines small convenience functions for building an atom
  21. type from several individual parsers. This is easy to do without these
  22. functions, but these functions communicate intent more directly:
  23. > data Atom
  24. > = Ident Text
  25. > | Num Integer
  26. >
  27. > myParser :: SExprParser Atom (SExpr Atom)
  28. > myParser = mkAtomParser
  29. > [ atom Ident parseR7RSIdent
  30. > , atom Num signedDecNumber
  31. > ]
  32. -}