Debian Codegraph @

Data/SCargot/Repr/WellFormed.hs

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