gdritter repos s-cargot / a5a5313
Fixed zealous use of `fail` and exported encodeOne in General module Getty Ritter 10 years ago
2 changed file(s) with 13 addition(s) and 12 deletion(s). Collapse all Expand all
1616 , decode
1717 , decodeOne
1818 , encode
19 , encodeOne
1920 -- * Useful Type Aliases
2021 , Reader
2122 , Comment
4545 -- | Utility function for parsing a pair of things.
4646 fromPair :: Parse t a -> Parse t b -> Parse t (a, b)
4747 fromPair pl pr (L [l, r]) = (,) <$> pl l <*> pr r
48 fromPair _ _ sx = fail ("Expected two-element list")
48 fromPair _ _ sx = Left ("Expected two-element list")
4949
5050 -- | Utility function for parsing a list of things.
5151 fromList :: Parse t a -> Parse t [a]
5252 fromList p (L ss) = mapM p ss
53 fromList _ sx = fail ("Expected list")
53 fromList _ sx = Left ("Expected list")
5454
5555 fromAtom :: Parse t t
56 fromAtom (L _) = fail "Expected atom; found list"
56 fromAtom (L _) = Left "Expected atom; found list"
5757 fromAtom (A a) = return a
5858
5959 asPair :: ((S t, S t) -> Either String a) -> S t -> Either String a
6060 asPair f (L [l, r]) = f (l, r)
61 asPair _ sx = fail ("Expected two-element list")
61 asPair _ sx = Left ("Expected two-element list")
6262
6363 asList :: ([S t] -> Either String a) -> S t -> Either String a
6464 asList f (L ls) = f ls
65 asList _ sx = fail ("Expected list")
65 asList _ sx = Left ("Expected list")
6666
6767 isAtom :: Eq t => t -> S t -> Either String ()
6868 isAtom s (A s')
6969 | s == s' = return ()
70 | otherwise = fail ".."
71 isAtom _ _ = fail ".."
70 | otherwise = Left ".."
71 isAtom _ _ = Left ".."
7272
7373 asAtom :: Show t => (t -> Either String a) -> S t -> Either String a
7474 asAtom f (A s) = f s
75 asAtom _ sx = fail ("Expected atom; got" ++ show sx)
75 asAtom _ sx = Left ("Expected atom; got" ++ show sx)
7676
7777 asAssoc :: Show t => ([(S t, S t)] -> Either String a) -> S t -> Either String a
7878 asAssoc f (L ss) = gatherPairs ss >>= f
7979 where gatherPairs (L [a, b] : ss) = (:) <$> pure (a, b) <*> gatherPairs ss
8080 gatherPairs [] = pure []
81 gatherPairs _ = fail "..."
82 asAssoc _ sx = fail ("Expected assoc list; got " ++ show sx)
81 gatherPairs _ = Left "..."
82 asAssoc _ sx = Left ("Expected assoc list; got " ++ show sx)
8383
8484 car :: (S t -> Either String t') -> [S t] -> Either String t'
8585 car f (x:_) = f x
86 car _ [] = fail "car: Taking car of zero-element list"
86 car _ [] = Left "car: Taking car of zero-element list"
8787
8888 cdr :: ([S t] -> Either String t') -> [S t] -> Either String t'
8989 cdr f (_:xs) = f xs
90 cdr _ [] = fail "cdr: Taking cdr of zero-element list"
90 cdr _ [] = Left "cdr: Taking cdr of zero-element list"