Rewrite leftmostLongest and leftmostLongestParse so that they don't rely
on 'head' which is a partial function
This commit is contained in:
@@ -68,15 +68,21 @@ instance Monoid MdToken where
|
||||
|
||||
-- ---------------
|
||||
-- Helpers
|
||||
leftmostLongest :: (Foldable t) => [(a, t b)] -> (a, t b)
|
||||
leftmostLongest :: (Foldable t) => [(a, t b)] -> Maybe (a, t b)
|
||||
leftmostLongest xs =
|
||||
let lastElem = last 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
|
||||
leftmostLongestParse :: ReadP a -> String -> (a, String)
|
||||
leftmostLongestParse parser input = leftmostLongest $ readP_to_S parser input
|
||||
leftmostLongestParse :: (Monoid a) => ReadP a -> String -> (a, String)
|
||||
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
|
||||
lookaheadParse :: (String -> Bool) -> ReadP Char
|
||||
|
Reference in New Issue
Block a user