WellFormed.hs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. {-# LANGUAGE PatternSynonyms #-}
  2. {-# LANGUAGE ViewPatterns #-}
  3. module Data.SCargot.Repr.WellFormed
  4. ( -- * 'WellFormedSExpr' representation
  5. R.WellFormedSExpr(..)
  6. , R.toWellFormed
  7. , R.fromWellFormed
  8. -- * Constructing and Deconstructing
  9. , cons
  10. , uncons
  11. -- * Useful pattern synonyms
  12. , pattern (:::)
  13. , pattern L
  14. , pattern A
  15. , pattern Nil
  16. -- * Useful processing functions
  17. , fromPair
  18. , fromList
  19. , fromAtom
  20. , asPair
  21. , asList
  22. , isAtom
  23. , isNil
  24. , asAtom
  25. , asAssoc
  26. , car
  27. , cdr
  28. ) where
  29. #if !MIN_VERSION_base(4,8,0)
  30. import Control.Applicative ((<$>), (<*>), pure)
  31. #endif
  32. import Data.SCargot.Repr as R
  33. -- | Produce the head and tail of the s-expression (if possible).
  34. --
  35. -- >>> uncons (L [A "el", A "eph", A "ant"])
  36. -- Just (WFSAtom "el",WFSList [WFSAtom "eph",WFSAtom "ant"])
  37. uncons :: WellFormedSExpr a -> Maybe (WellFormedSExpr a, WellFormedSExpr a)
  38. uncons R.WFSAtom {} = Nothing
  39. uncons (R.WFSList []) = Nothing
  40. uncons (R.WFSList (x:xs)) = Just (x, R.WFSList xs)
  41. -- | Combine the two-expressions into a new one. This will return
  42. -- @Nothing@ if the resulting s-expression is not well-formed.
  43. --
  44. -- >>> cons (A "el") (L [A "eph", A "ant"])
  45. -- Just (WFSList [WFSAtom "el",WFSAtom "eph",WFSAtom "ant"])
  46. -- >>> cons (A "pachy") (A "derm"))
  47. -- Nothing
  48. cons :: WellFormedSExpr a -> WellFormedSExpr a -> Maybe (WellFormedSExpr a)
  49. cons _ (R.WFSAtom {}) = Nothing
  50. cons x (R.WFSList xs) = Just (R.WFSList (x:xs))
  51. -- | A shorter infix alias to grab the head and tail of a `WFSList`. This
  52. -- pattern is unidirectional, because it cannot be guaranteed that it
  53. -- is used to construct well-formed s-expressions; use the function "cons"
  54. -- instead.
  55. --
  56. -- >>> let sum (x ::: xs) = x + sum xs; sum Nil = 0
  57. pattern (:::) :: WellFormedSExpr a -> WellFormedSExpr a -> WellFormedSExpr a
  58. pattern x ::: xs <- (uncons -> Just (x, xs))
  59. -- | A shorter alias for `WFSList`
  60. --
  61. -- >>> L [A "pachy", A "derm"]
  62. -- WFSList [WFSAtom "pachy",WFSAtom "derm"]
  63. pattern L :: [WellFormedSExpr t] -> WellFormedSExpr t
  64. pattern L xs = R.WFSList xs
  65. -- | A shorter alias for `WFSAtom`
  66. --
  67. -- >>> A "elephant"
  68. -- WFSAtom "elephant"
  69. pattern A :: t -> WellFormedSExpr t
  70. pattern A a = R.WFSAtom a
  71. -- | A shorter alias for `WFSList` @[]@
  72. --
  73. -- >>> Nil
  74. -- WFSList []
  75. pattern Nil :: WellFormedSExpr t
  76. pattern Nil = R.WFSList []
  77. getShape :: WellFormedSExpr a -> String
  78. getShape WFSAtom {} = "atom"
  79. getShape (WFSList []) = "empty list"
  80. getShape (WFSList sx) = "list of length " ++ show (length sx)
  81. -- | Utility function for parsing a pair of things.
  82. --
  83. -- >>> fromPair (isAtom "pachy") (asAtom return) (L [A "pachy", A "derm"])
  84. -- Right ((), "derm")
  85. -- >>> fromPair (isAtom "pachy") fromAtom (L [A "pachy"])
  86. -- Left "Expected two-element list"
  87. fromPair :: (WellFormedSExpr t -> Either String a)
  88. -> (WellFormedSExpr t -> Either String b)
  89. -> WellFormedSExpr t -> Either String (a, b)
  90. fromPair pl pr (L [l, r]) = (,) <$> pl l <*> pr r
  91. fromPair _ _ sx = Left ("fromPair: expected two-element list; found " ++ getShape sx)
  92. -- | Utility function for parsing a list of things.
  93. --
  94. -- >>> fromList fromAtom (L [A "this", A "that", A "the-other"])
  95. -- Right ["this","that","the-other"]
  96. -- >>> fromList fromAtom (A "pachyderm")
  97. -- Left "asList: expected proper list; found dotted list"
  98. fromList :: (WellFormedSExpr t -> Either String a)
  99. -> WellFormedSExpr t -> Either String [a]
  100. fromList p (L ss) = mapM p ss
  101. fromList _ sx = Left ("fromList: expected list; found " ++ getShape sx)
  102. -- | Utility function for parsing a single atom
  103. --
  104. -- >>> fromAtom (A "elephant")
  105. -- Right "elephant"
  106. -- >>> fromAtom (L [A "elephant"])
  107. -- Left "fromAtom: expected atom; found list"
  108. fromAtom :: WellFormedSExpr t -> Either String t
  109. fromAtom (A a) = return a
  110. fromAtom sx = Left ("fromAtom: expected atom; found " ++ getShape sx)
  111. -- | Parses a two-element list using the provided function.
  112. --
  113. -- >>> let go (A l) (A r) = return (l ++ r); go _ _ = Left "expected atoms"
  114. -- >>> asPair go (L [A "pachy", A "derm"])
  115. -- Right "pachyderm"
  116. -- >>> asPair go (L [A "elephant"])
  117. -- Left "asPair: expected two-element list; found list of length 1"
  118. asPair :: ((WellFormedSExpr t, WellFormedSExpr t) -> Either String a)
  119. -> WellFormedSExpr t -> Either String a
  120. asPair f (L [l, r]) = f (l, r)
  121. asPair _ sx = Left ("asPair: expected two-element list; found " ++ getShape sx)
  122. -- | Parse an arbitrary-length list using the provided function.
  123. --
  124. -- >>> let go xs = concat <$> mapM fromAtom xs
  125. -- >>> asList go (L [A "el", A "eph", A "ant"])
  126. -- Right "elephant"
  127. -- >>> asList go (A "pachyderm")
  128. -- Left "asList: expected list; found atom"
  129. asList :: ([WellFormedSExpr t] -> Either String a)
  130. -> WellFormedSExpr t -> Either String a
  131. asList f (L ls) = f ls
  132. asList _ sx = Left ("asList: expected list; found " ++ getShape sx)
  133. -- | Match a given literal atom, failing otherwise.
  134. --
  135. -- >>> isAtom "elephant" (A "elephant")
  136. -- Right ()
  137. -- >>> isAtom "elephant" (L [A "elephant"])
  138. -- Left "isAtom: expected atom; found list"
  139. isAtom :: Eq t => t -> WellFormedSExpr t -> Either String ()
  140. isAtom s (A s')
  141. | s == s' = return ()
  142. | otherwise = Left "isAtom: failed to match atom"
  143. isAtom _ sx = Left ("isAtom: expected atom; found " ++ getShape sx)
  144. -- | Match an empty list, failing otherwise.
  145. --
  146. -- >>> isNil (L [])
  147. -- Right ()
  148. -- >>> isNil (A "elephant")
  149. -- Left "isNil: expected nil; found atom"
  150. isNil :: WellFormedSExpr t -> Either String ()
  151. isNil Nil = return ()
  152. isNil sx = Left ("isNil: expected nil; found " ++ getShape sx)
  153. -- | Parse an atom using the provided function.
  154. --
  155. -- >>> import Data.Char (toUpper)
  156. -- >>> asAtom (return . map toUpper) (A "elephant")
  157. -- Right "ELEPHANT"
  158. -- >>> asAtom (return . map toUpper) (L [])
  159. -- Left "asAtom: expected atom; found list"
  160. asAtom :: (t -> Either String a) -> WellFormedSExpr t -> Either String a
  161. asAtom f (A s) = f s
  162. asAtom _ sx = Left ("asAtom: expected atom; found " ++ getShape sx)
  163. -- | Parse an assoc-list using the provided function.
  164. --
  165. -- >>> let def (x, y) = do { a <- fromAtom x; b <- fromAtom y; return (a ++ ": " ++ b) }
  166. -- >>> let defList xs = do { defs <- mapM def xs; return (unlines defs) }
  167. -- >>> asAssoc defList (L [ L [A "legs", A "four"], L [ A "trunk", A "one"] ])
  168. -- Right "legs: four\ntrunk: one\n"
  169. -- >>> asAssoc defList (L [ L [A "legs", A "four"], L [ A "elephant"] ])
  170. -- Left "asAssoc: expected pair; found list of length 1"
  171. asAssoc :: ([(WellFormedSExpr t, WellFormedSExpr t)] -> Either String a)
  172. -> WellFormedSExpr t -> Either String a
  173. asAssoc f (L ss) = gatherPairs ss >>= f
  174. where gatherPairs (L [a, b] : ts) = (:) <$> pure (a, b) <*> gatherPairs ts
  175. gatherPairs [] = pure []
  176. gatherPairs (sx:_) = Left ("asAssoc: expected pair; found " ++ getShape sx)
  177. asAssoc _ sx = Left ("asAssoc: expected list; found " ++ getShape sx)
  178. -- | Run the parser on the first element of a Haskell list of "WellFormedSExpr" values,
  179. -- failing if the list is empty. This is useful in conjunction with the `asList`
  180. -- function.
  181. car :: (WellFormedSExpr t -> Either String t')
  182. -> [WellFormedSExpr t] -> Either String t'
  183. car f (x:_) = f x
  184. car _ [] = Left "car: Taking car of zero-element list"
  185. -- | Run the parser on all but the first element of a Haskell list of "WellFormedSExpr" values,
  186. -- failing if the list is empty. This is useful in conjunction with the `asList`
  187. -- function.
  188. cdr :: ([WellFormedSExpr t] -> Either String t')
  189. -> [WellFormedSExpr t] -> Either String t'
  190. cdr f (_:xs) = f xs
  191. cdr _ [] = Left "cdr: Taking cdr of zero-element list"