diff --git a/src/MdToHTML.hs b/src/MdToHTML.hs index d491290..7889863 100644 --- a/src/MdToHTML.hs +++ b/src/MdToHTML.hs @@ -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