Rewrite leftmostLongest and leftmostLongestParse so that they don't rely

on 'head' which is a partial function
master
Aadhavan Srinivasan 1 week ago
parent 3330185393
commit a60b3754e4

@ -68,15 +68,21 @@ instance Monoid MdToken where
-- --------------- -- ---------------
-- Helpers -- Helpers
leftmostLongest :: (Foldable t) => [(a, t b)] -> (a, t b) leftmostLongest :: (Foldable t) => [(a, t b)] -> Maybe (a, t b)
leftmostLongest xs = leftmostLongest xs =
let lastElem = last xs let lastElem = last xs
filteredLst = filter (\val -> length (snd val) == length (snd lastElem)) xs filteredLst = filter (\val -> length (snd val) == length (snd lastElem)) xs
in head filteredLst in case filteredLst of
[] -> Nothing
(x : xs) -> Just x
-- Get the first parse returned by readP_to_S that consumed the most input -- Get the first parse returned by readP_to_S that consumed the most input
leftmostLongestParse :: ReadP a -> String -> (a, String) leftmostLongestParse :: (Monoid a) => ReadP a -> String -> (a, String)
leftmostLongestParse parser input = leftmostLongest $ readP_to_S parser input leftmostLongestParse parser input =
let res = leftmostLongest $ readP_to_S parser input
in case res of
Nothing -> (mempty, mempty)
Just x -> x
-- Parse if the string that's left matches the string comparator function -- Parse if the string that's left matches the string comparator function
lookaheadParse :: (String -> Bool) -> ReadP Char lookaheadParse :: (String -> Bool) -> ReadP Char

Loading…
Cancel
Save