WellFormed.hs 6.8 KB

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