on master help sign in
Simplify the API of wordQuotedBy
commitad4566b080b2de7e9e00249216175cd6fa6ed2a3 authorHarendra Kumar <harendra@composewell.com> dateSun 19 Feb 2023, 17:42 (4 years ago) parent33d584c
1 file changed+35−31
core/src/Streamly/Internal/Data/Parser/ParserD.hs+35−31
core/src/Streamly/Internal/Data/Parser/ParserD.hs+35−31
@@ -1583,9 +1583,9 @@ wordFramedBy isEsc isBegin isEnd isSep
15831583data WordQuotedState s b a =
15841584 WordQuotedSkipPre !s
15851585 | WordUnquotedWord !s
1586− | WordQuotedWord !s !Int a
1586+ | WordQuotedWord !s !Int !a !a
15871587 | WordUnquotedEsc !s
1588− | WordQuotedEsc !s !Int a
1588+ | WordQuotedEsc !s !Int !a !a
15891589 | WordQuotedSkipPost !b
15901590
15911591-- | Like 'wordFramedBy' but the closing quote is determined by the opening
@@ -1611,14 +1611,13 @@ data WordQuotedState s b a =
16111611{-# INLINE wordQuotedBy #-}
16121612wordQuotedBy :: (Monad m, Eq a) =>
16131613 Bool -- ^ keep the quotes in the output
1614− -> (a -> Bool) -- ^ Escape
1615− -> (a -> Bool) -- ^ left quote
1616− -> (a -> Bool) -- ^ right quote
1617− -> (a -> a) -- ^ get right quote from the left quote
1618− -> (a -> Bool) -- ^ word seperator
1614+ -> (a -> Bool) -- ^ Matches an escape elem?
1615+ -> (a -> Maybe a) -- ^ If left quote, return right quote, else nothing.
1616+ -> (a -> Bool) -- ^ Matches a right quote?
1617+ -> (a -> Bool) -- ^ Matches a word seperator?
16191618 -> Fold m a b
16201619 -> Parser a m b
1621−wordQuotedBy keepQuotes isEsc isBegin isEnd toRight isSep
1620+wordQuotedBy keepQuotes isEsc toRight isEnd isSep
16221621 (Fold fstep finitial fextract) =
16231622 Parser step initial extract
16241623
@@ -1633,11 +1632,11 @@ wordQuotedBy keepQuotes isEsc isBegin isEnd toRight isSep
16331632 error "wordQuotedBy: fold done without input"
16341633
16351634 {-# INLINE process #-}
1636− process s a n q = do
1635+ process s a n ql qr = do
16371636 res <- fstep s a
16381637 return
16391638 $ case res of
1640− FL.Partial s1 -> Continue 0 (WordQuotedWord s1 n q)
1639+ FL.Partial s1 -> Continue 0 (WordQuotedWord s1 n ql qr)
16411640 FL.Done b -> Done 0 b
16421641
16431642 {-# INLINE processUnquoted #-}
@@ -1651,45 +1650,50 @@ wordQuotedBy keepQuotes isEsc isBegin isEnd toRight isSep
16511650 step (WordQuotedSkipPre s) a
16521651 | isEsc a = return $ Continue 0 $ WordUnquotedEsc s
16531652 | isSep a = return $ Partial 0 $ WordQuotedSkipPre s
1654− | isBegin a =
1655− if keepQuotes
1656− then process s a 1 a
1657− else return $ Continue 0 $ WordQuotedWord s 1 a
1658− | isEnd a =
1659− return $ Error "wordQuotedBy: missing frame start"
1660− | otherwise = processUnquoted s a
1653+ | otherwise =
1654+ case toRight a of
1655+ Just qr ->
1656+ if keepQuotes
1657+ then process s a 1 a qr
1658+ else return $ Continue 0 $ WordQuotedWord s 1 a qr
1659+ Nothing
1660+ | isEnd a ->
1661+ return $ Error "wordQuotedBy: missing frame start"
1662+ | otherwise -> processUnquoted s a
16611663 step (WordUnquotedWord s) a
16621664 | isEsc a = return $ Continue 0 $ WordUnquotedEsc s
16631665 | isSep a = do
16641666 b <- fextract s
16651667 return $ Partial 0 $ WordQuotedSkipPost b
16661668 | otherwise = do
1667− if isBegin a
1668− then if keepQuotes
1669− then process s a 1 a
1670− else return $ Continue 0 $ WordQuotedWord s 1 a
1671− else if isEnd a
1669+ case toRight a of
1670+ Just qr ->
1671+ if keepQuotes
1672+ then process s a 1 a qr
1673+ else return $ Continue 0 $ WordQuotedWord s 1 a qr
1674+ Nothing ->
1675+ if isEnd a
16721676 then return $ Error "wordQuotedBy: missing frame start"
16731677 else processUnquoted s a
1674− step (WordQuotedWord s n q) a
1675− | isEsc a = return $ Continue 0 $ WordQuotedEsc s n q
1678+ step (WordQuotedWord s n ql qr) a
1679+ | isEsc a = return $ Continue 0 $ WordQuotedEsc s n ql qr
16761680 -- XXX Will this ever occur? Will n ever be 0?
16771681 | n == 0 && isSep a = do
16781682 b <- fextract s
16791683 return $ Partial 0 $ WordQuotedSkipPost b
16801684 | otherwise = do
1681− if a == toRight q
1685+ if a == qr
16821686 then
16831687 if n == 1
16841688 then if keepQuotes
16851689 then processUnquoted s a
16861690 else return $ Continue 0 $ WordUnquotedWord s
1687− else process s a (n - 1) q
1688− else if a == q
1689− then process s a (n + 1) q
1690− else process s a n q
1691+ else process s a (n - 1) ql qr
1692+ else if a == ql
1693+ then process s a (n + 1) ql qr
1694+ else process s a n ql qr
16911695 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
16931697 step (WordQuotedSkipPost b) a =
16941698 return
16951699 $ if not (isSep a)
@@ -1700,7 +1704,7 @@ wordQuotedBy keepQuotes isEsc isBegin isEnd toRight isSep
17001704
17011705 extract (WordQuotedSkipPre s) = fmap (Done 0) $ fextract s
17021706 extract (WordUnquotedWord s) = fmap (Done 0) $ fextract s
1703− extract (WordQuotedWord s n _) =
1707+ extract (WordQuotedWord s n _ _) =
17041708 if n == 0
17051709 then fmap (Done 0) $ fextract s
17061710 else err "wordQuotedBy: missing frame end"