WellFormed.hs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 x ::: xs <- (uncons -> Just (x, xs))
  58. -- | A shorter alias for `WFSList`
  59. --
  60. -- >>> L [A "pachy", A "derm"]
  61. -- WFSList [WFSAtom "pachy",WFSAtom "derm"]
  62. pattern L xs = R.WFSList xs
  63. -- | A shorter alias for `WFSAtom`
  64. --
  65. -- >>> A "elephant"
  66. -- WFSAtom "elephant"
  67. pattern A a = R.WFSAtom a
  68. -- | A shorter alias for `WFSList` @[]@
  69. --
  70. -- >>> Nil
  71. -- WFSList []
  72. pattern Nil = R.WFSList []
  73. getShape :: WellFormedSExpr a -> String
  74. getShape WFSAtom {} = "atom"
  75. getShape (WFSList []) = "empty list"
  76. getShape (WFSList sx) = "list of length " ++ show (length sx)
  77. -- | Utility function for parsing a pair of things.
  78. --
  79. -- >>> fromPair (isAtom "pachy") (asAtom return) (L [A "pachy", A "derm"])
  80. -- Right ((), "derm")
  81. -- >>> fromPair (isAtom "pachy") fromAtom (L [A "pachy"])
  82. -- Left "Expected two-element list"
  83. fromPair :: (WellFormedSExpr t -> Either String a)
  84. -> (WellFormedSExpr t -> Either String b)
  85. -> WellFormedSExpr t -> Either String (a, b)
  86. fromPair pl pr (L [l, r]) = (,) <$> pl l <*> pr r
  87. fromPair _ _ sx = Left ("fromPair: expected two-element list; found " ++ getShape sx)
  88. -- | Utility function for parsing a list of things.
  89. --
  90. -- >>> fromList fromAtom (L [A "this", A "that", A "the-other"])
  91. -- Right ["this","that","the-other"]
  92. -- >>> fromList fromAtom (A "pachyderm")
  93. -- Left "asList: expected proper list; found dotted list"
  94. fromList :: (WellFormedSExpr t -> Either String a)
  95. -> WellFormedSExpr t -> Either String [a]
  96. fromList p (L ss) = mapM p ss
  97. fromList _ sx = Left ("fromList: expected list; found " ++ getShape sx)
  98. -- | Utility function for parsing a single atom
  99. --
  100. -- >>> fromAtom (A "elephant")
  101. -- Right "elephant"
  102. -- >>> fromAtom (L [A "elephant"])
  103. -- Left "fromAtom: expected atom; found list"
  104. fromAtom :: WellFormedSExpr t -> Either String t
  105. fromAtom (A a) = return a
  106. fromAtom sx = Left ("fromAtom: expected atom; found " ++ getShape sx)
  107. -- | Parses a two-element list using the provided function.
  108. --
  109. -- >>> let go (A l) (A r) = return (l ++ r); go _ _ = Left "expected atoms"
  110. -- >>> asPair go (L [A "pachy", A "derm"])
  111. -- Right "pachyderm"
  112. -- >>> asPair go (L [A "elephant"])
  113. -- Left "asPair: expected two-element list; found list of length 1"
  114. asPair :: ((WellFormedSExpr t, WellFormedSExpr t) -> Either String a)
  115. -> WellFormedSExpr t -> Either String a
  116. asPair f (L [l, r]) = f (l, r)
  117. asPair _ sx = Left ("asPair: expected two-element list; found " ++ getShape sx)
  118. -- | Parse an arbitrary-length list using the provided function.
  119. --
  120. -- >>> let go xs = concat <$> mapM fromAtom xs
  121. -- >>> asList go (L [A "el", A "eph", A "ant"])
  122. -- Right "elephant"
  123. -- >>> asList go (A "pachyderm")
  124. -- Left "asList: expected list; found atom"
  125. asList :: ([WellFormedSExpr t] -> Either String a)
  126. -> WellFormedSExpr t -> Either String a
  127. asList f (L ls) = f ls
  128. asList _ sx = Left ("asList: expected list; found " ++ getShape sx)
  129. -- | Match a given literal atom, failing otherwise.
  130. --
  131. -- >>> isAtom "elephant" (A "elephant")
  132. -- Right ()
  133. -- >>> isAtom "elephant" (L [A "elephant"])
  134. -- Left "isAtom: expected atom; found list"
  135. isAtom :: Eq t => t -> WellFormedSExpr t -> Either String ()
  136. isAtom s (A s')
  137. | s == s' = return ()
  138. | otherwise = Left "isAtom: failed to match atom"
  139. isAtom _ sx = Left ("isAtom: expected atom; found " ++ getShape sx)
  140. -- | Match an empty list, failing otherwise.
  141. --
  142. -- >>> isNil (L [])
  143. -- Right ()
  144. -- >>> isNil (A "elephant")
  145. -- Left "isNil: expected nil; found atom"
  146. isNil :: WellFormedSExpr t -> Either String ()
  147. isNil Nil = return ()
  148. isNil sx = Left ("isNil: expected nil; found " ++ getShape sx)
  149. -- | Parse an atom using the provided function.
  150. --
  151. -- >>> import Data.Char (toUpper)
  152. -- >>> asAtom (return . map toUpper) (A "elephant")
  153. -- Right "ELEPHANT"
  154. -- >>> asAtom (return . map toUpper) (L [])
  155. -- Left "asAtom: expected atom; found list"
  156. asAtom :: (t -> Either String a) -> WellFormedSExpr t -> Either String a
  157. asAtom f (A s) = f s
  158. asAtom _ sx = Left ("asAtom: expected atom; found " ++ getShape sx)
  159. -- | Parse an assoc-list using the provided function.
  160. --
  161. -- >>> let def (x, y) = do { a <- fromAtom x; b <- fromAtom y; return (a ++ ": " ++ b) }
  162. -- >>> let defList xs = do { defs <- mapM def xs; return (unlines defs) }
  163. -- >>> asAssoc defList (L [ L [A "legs", A "four"], L [ A "trunk", A "one"] ])
  164. -- Right "legs: four\ntrunk: one\n"
  165. -- >>> asAssoc defList (L [ L [A "legs", A "four"], L [ A "elephant"] ])
  166. -- Left "asAssoc: expected pair; found list of length 1"
  167. asAssoc :: ([(WellFormedSExpr t, WellFormedSExpr t)] -> Either String a)
  168. -> WellFormedSExpr t -> Either String a
  169. asAssoc f (L ss) = gatherPairs ss >>= f
  170. where gatherPairs (L [a, b] : ts) = (:) <$> pure (a, b) <*> gatherPairs ts
  171. gatherPairs [] = pure []
  172. gatherPairs (sx:_) = Left ("asAssoc: expected pair; found " ++ getShape sx)
  173. asAssoc _ sx = Left ("asAssoc: expected list; found " ++ getShape sx)
  174. -- | Run the parser on the first element of a Haskell list of "WellFormedSExpr" values,
  175. -- failing if the list is empty. This is useful in conjunction with the `asList`
  176. -- function.
  177. car :: (WellFormedSExpr t -> Either String t')
  178. -> [WellFormedSExpr t] -> Either String t'
  179. car f (x:_) = f x
  180. car _ [] = Left "car: Taking car of zero-element list"
  181. -- | Run the parser on all but the first element of a Haskell list of "WellFormedSExpr" values,
  182. -- failing if the list is empty. This is useful in conjunction with the `asList`
  183. -- function.
  184. cdr :: ([WellFormedSExpr t] -> Either String t')
  185. -> [WellFormedSExpr t] -> Either String t'
  186. cdr f (_:xs) = f xs
  187. cdr _ [] = Left "cdr: Taking cdr of zero-element list"