Better algorithm for oneOfStrings.

This goes character by character, not backtracking.
This commit is contained in:
John MacFarlane
2012-07-24 22:45:22 -07:00
parent 02ef26ae91
commit fbd3d2b450
+9 -2
View File
@@ -173,9 +173,16 @@ notFollowedBy' p = try $ join $ do a <- try p
return (return ())
-- (This version due to Andrew Pimlott on the Haskell mailing list.)
-- | Parses one of a list of strings (tried in order).
-- | Parses one of a list of strings (tried in order).
oneOfStrings :: [String] -> Parsec [Char] st String
oneOfStrings listOfStrings = choice $ map (try . string) listOfStrings
oneOfStrings [] = fail "no strings"
oneOfStrings strs = do
c <- anyChar
let strs' = [xs | (x:xs) <- strs, x == c]
case strs' of
[] -> fail "not found"
z | "" `elem` z -> return [c]
| otherwise -> (c:) `fmap` oneOfStrings strs'
-- | Parses a space or tab.
spaceChar :: Parsec [Char] st Char