on master help sign in
Simplify the API of wordQuotedBy
ad4566bHarendra Kumar
core/src/Streamly/Internal/Data/Parser/ParserD.hs+35−31
@@ -1583,9 +1583,9 @@ wordFramedBy isEsc isBegin isEnd isSep
1583data WordQuotedState s b a =
1583data WordQuotedState s b a =
1584 WordQuotedSkipPre !s
1584 WordQuotedSkipPre !s
1585 | WordUnquotedWord !s
1585 | WordUnquotedWord !s
1586− | WordQuotedWord !s !Int a
1586+ | WordQuotedWord !s !Int !a !a
1587 | WordUnquotedEsc !s
1587 | WordUnquotedEsc !s
1588− | WordQuotedEsc !s !Int a
1588+ | WordQuotedEsc !s !Int !a !a
1589 | WordQuotedSkipPost !b
1589 | WordQuotedSkipPost !b
1590
1590
1591-- | Like 'wordFramedBy' but the closing quote is determined by the opening
1591-- | Like 'wordFramedBy' but the closing quote is determined by the opening
@@ -1611,14 +1611,13 @@ data WordQuotedState s b a =
1611{-# INLINE wordQuotedBy #-}
1611{-# INLINE wordQuotedBy #-}
1612wordQuotedBy :: (Monad m, Eq a) =>
1612wordQuotedBy :: (Monad m, Eq a) =>
1613 Bool -- ^ keep the quotes in the output
1613 Bool -- ^ keep the quotes in the output
1614− -> (a -> Bool) -- ^ Escape
1614+ -> (a -> Bool) -- ^ Matches an escape elem?
1615− -> (a -> Bool) -- ^ left quote
1615+ -> (a -> Maybe a) -- ^ If left quote, return right quote, else nothing.
1616− -> (a -> Bool) -- ^ right quote
1616+ -> (a -> Bool) -- ^ Matches a right quote?
1617− -> (a -> a) -- ^ get right quote from the left quote
1618− -> (a -> Bool) -- ^ word seperator
1617+ -> (a -> Bool) -- ^ Matches a word seperator?
1619 -> Fold m a b
1618 -> Fold m a b
1620 -> Parser a m b
1619 -> Parser a m b
1621−wordQuotedBy keepQuotes isEsc isBegin isEnd toRight isSep
1620+wordQuotedBy keepQuotes isEsc toRight isEnd isSep
1622 (Fold fstep finitial fextract) =
1621 (Fold fstep finitial fextract) =
1623 Parser step initial extract
1622 Parser step initial extract
1624
1623
@@ -1633,11 +1632,11 @@ wordQuotedBy keepQuotes isEsc isBegin isEnd toRight isSep
1633 error "wordQuotedBy: fold done without input"
1632 error "wordQuotedBy: fold done without input"
1634
1633
1635 {-# INLINE process #-}
1634 {-# INLINE process #-}
1636− process s a n q = do
1635+ process s a n ql qr = do
1637 res <- fstep s a
1636 res <- fstep s a
1638 return
1637 return
1639 $ case res of
1638 $ case res of
1640− FL.Partial s1 -> Continue 0 (WordQuotedWord s1 n q)
1639+ FL.Partial s1 -> Continue 0 (WordQuotedWord s1 n ql qr)
1641 FL.Done b -> Done 0 b
1640 FL.Done b -> Done 0 b
1642
1641
1643 {-# INLINE processUnquoted #-}
1642 {-# INLINE processUnquoted #-}
@@ -1651,45 +1650,50 @@ wordQuotedBy keepQuotes isEsc isBegin isEnd toRight isSep
1651 step (WordQuotedSkipPre s) a
1650 step (WordQuotedSkipPre s) a
1652 | isEsc a = return $ Continue 0 $ WordUnquotedEsc s
1651 | isEsc a = return $ Continue 0 $ WordUnquotedEsc s
1653 | isSep a = return $ Partial 0 $ WordQuotedSkipPre s
1652 | isSep a = return $ Partial 0 $ WordQuotedSkipPre s
1654− | isBegin a =
1653+ | otherwise =
1654+ case toRight a of
1655+ Just qr ->
1655− if keepQuotes
1656+ if keepQuotes
1656− then process s a 1 a
1657+ then process s a 1 a qr
1657− else return $ Continue 0 $ WordQuotedWord s 1 a
1658+ else return $ Continue 0 $ WordQuotedWord s 1 a qr
1659+ Nothing
1658− | isEnd a =
1660+ | isEnd a ->
1659− return $ Error "wordQuotedBy: missing frame start"
1661+ return $ Error "wordQuotedBy: missing frame start"
1660− | otherwise = processUnquoted s a
1662+ | otherwise -> processUnquoted s a
1661 step (WordUnquotedWord s) a
1663 step (WordUnquotedWord s) a
1662 | isEsc a = return $ Continue 0 $ WordUnquotedEsc s
1664 | isEsc a = return $ Continue 0 $ WordUnquotedEsc s
1663 | isSep a = do
1665 | isSep a = do
1664 b <- fextract s
1666 b <- fextract s
1665 return $ Partial 0 $ WordQuotedSkipPost b
1667 return $ Partial 0 $ WordQuotedSkipPost b
1666 | otherwise = do
1668 | otherwise = do
1667− if isBegin a
1669+ case toRight a of
1670+ Just qr ->
1668− then if keepQuotes
1671+ if keepQuotes
1669− then process s a 1 a
1672+ then process s a 1 a qr
1670− else return $ Continue 0 $ WordQuotedWord s 1 a
1673+ else return $ Continue 0 $ WordQuotedWord s 1 a qr
1674+ Nothing ->
1671− else if isEnd a
1675+ if isEnd a
1672 then return $ Error "wordQuotedBy: missing frame start"
1676 then return $ Error "wordQuotedBy: missing frame start"
1673 else processUnquoted s a
1677 else processUnquoted s a
1674− step (WordQuotedWord s n q) a
1678+ step (WordQuotedWord s n ql qr) a
1675− | isEsc a = return $ Continue 0 $ WordQuotedEsc s n q
1679+ | isEsc a = return $ Continue 0 $ WordQuotedEsc s n ql qr
1676 -- XXX Will this ever occur? Will n ever be 0?
1680 -- XXX Will this ever occur? Will n ever be 0?
1677 | n == 0 && isSep a = do
1681 | n == 0 && isSep a = do
1678 b <- fextract s
1682 b <- fextract s
1679 return $ Partial 0 $ WordQuotedSkipPost b
1683 return $ Partial 0 $ WordQuotedSkipPost b
1680 | otherwise = do
1684 | otherwise = do
1681− if a == toRight q
1685+ if a == qr
1682 then
1686 then
1683 if n == 1
1687 if n == 1
1684 then if keepQuotes
1688 then if keepQuotes
1685 then processUnquoted s a
1689 then processUnquoted s a
1686 else return $ Continue 0 $ WordUnquotedWord s
1690 else return $ Continue 0 $ WordUnquotedWord s
1687− else process s a (n - 1) q
1691+ else process s a (n - 1) ql qr
1688− else if a == q
1692+ else if a == ql
1689− then process s a (n + 1) q
1693+ then process s a (n + 1) ql qr
1690− else process s a n q
1694+ else process s a n ql qr
1691 step (WordUnquotedEsc s) a = processUnquoted s a
1695 step (WordUnquotedEsc s) a = processUnquoted s a
1692− step (WordQuotedEsc s n q) a = process s a n q
1696+ step (WordQuotedEsc s n ql qr) a = process s a n ql qr
1693 step (WordQuotedSkipPost b) a =
1697 step (WordQuotedSkipPost b) a =
1694 return
1698 return
1695 $ if not (isSep a)
1699 $ if not (isSep a)
@@ -1700,7 +1704,7 @@ wordQuotedBy keepQuotes isEsc isBegin isEnd toRight isSep
1700
1704
1701 extract (WordQuotedSkipPre s) = fmap (Done 0) $ fextract s
1705 extract (WordQuotedSkipPre s) = fmap (Done 0) $ fextract s
1702 extract (WordUnquotedWord s) = fmap (Done 0) $ fextract s
1706 extract (WordUnquotedWord s) = fmap (Done 0) $ fextract s
1703− extract (WordQuotedWord s n _) =
1707+ extract (WordQuotedWord s n _ _) =
1704 if n == 0
1708 if n == 0
1705 then fmap (Done 0) $ fextract s
1709 then fmap (Done 0) $ fextract s
1706 else err "wordQuotedBy: missing frame end"
1710 else err "wordQuotedBy: missing frame end"