| 1 |
-- | |
| 2 |
-- Module : Replace.Megaparsec.Internal.Text |
| 3 |
-- Copyright : ©2019 James Brock |
| 4 |
-- License : BSD2 |
| 5 |
-- Maintainer: James Brock <[email protected]> |
| 6 |
-- |
| 7 |
-- This internal module is for 'Data.Text.Text' specializations. |
| 8 |
-- |
| 9 |
-- The functions in this module are intended to be chosen automatically |
| 10 |
-- by rewrite rules in the "Replace.Megaparsec" module, so you should never |
| 11 |
-- need to import this module. |
| 12 |
-- |
| 13 |
-- Names in this module may change without a major version increment. |
| 14 |
|
| 15 |
{-# LANGUAGE RankNTypes #-} |
| 16 |
{-# LANGUAGE TypeFamilies #-} |
| 17 |
{-# LANGUAGE TypeOperators #-} |
| 18 |
|
| 19 |
module Replace.Megaparsec.Internal.Text |
| 20 |
( |
| 21 |
-- * Parser combinator |
| 22 |
sepCapText |
| 23 |
, anyTillText |
| 24 |
) |
| 25 |
where |
| 26 |
|
| 27 |
import Control.Monad |
| 28 |
import qualified Data.Text as T |
| 29 |
import Data.Text.Internal (Text(..)) |
| 30 |
import Text.Megaparsec |
| 31 |
|
| 32 |
{-# INLINE [1] sepCapText #-} |
| 33 |
sepCapText |
| 34 |
:: forall e s m a. (MonadParsec e s m, s ~ T.Text) |
| 35 |
=> m a -- ^ The pattern matching parser @sep@ |
| 36 |
-> m [Either (Tokens s) a] |
| 37 |
sepCapText sep = getInput >>= go |
| 38 |
where |
| 39 |
-- the go function will search for the first pattern match, |
| 40 |
-- and then capture the pattern match along with the preceding |
| 41 |
-- unmatched string, and then recurse. |
| 42 |
-- restBegin is the rest of the buffer after the last pattern |
| 43 |
-- match. |
| 44 |
go restBegin@(Text tarray beginIndx beginLen) = do |
| 45 |
(<|>) |
| 46 |
( do |
| 47 |
(Text _ _ thisLen) <- getInput |
| 48 |
-- About 'thisiter': |
| 49 |
-- It looks stupid and introduces a completely unnecessary |
| 50 |
-- Maybe, but when I refactor to eliminate 'thisiter' and |
| 51 |
-- the Maybe then the benchmarks get dramatically worse. |
| 52 |
thisiter <- (<|>) |
| 53 |
( do |
| 54 |
x <- try sep |
| 55 |
restAfter@(Text _ _ afterLen) <- getInput |
| 56 |
-- Don't allow a match of a zero-width pattern |
| 57 |
when (afterLen >= thisLen) empty |
| 58 |
pure $ Just (x, restAfter) |
| 59 |
) |
| 60 |
(anySingle >> pure Nothing) |
| 61 |
case thisiter of |
| 62 |
(Just (x, restAfter)) | thisLen < beginLen -> do |
| 63 |
-- we've got a match with some preceding unmatched string |
| 64 |
let unmatched = Text tarray beginIndx (beginLen - thisLen) |
| 65 |
(Left unmatched:) <$> (Right x:) <$> go restAfter |
| 66 |
(Just (x, restAfter)) -> do |
| 67 |
-- we're got a match with no preceding unmatched string |
| 68 |
(Right x:) <$> go restAfter |
| 69 |
Nothing -> go restBegin -- no match, try again |
| 70 |
) |
| 71 |
( do |
| 72 |
-- We're at the end of the input, so return |
| 73 |
-- whatever unmatched string we've got since offsetBegin |
| 74 |
if beginLen > 0 then |
| 75 |
pure [Left restBegin] |
| 76 |
else pure [] |
| 77 |
) |
| 78 |
|
| 79 |
{-# INLINE [1] anyTillText #-} |
| 80 |
anyTillText |
| 81 |
:: forall e s m a. (MonadParsec e s m, s ~ T.Text) |
| 82 |
=> m a -- ^ The pattern matching parser @sep@ |
| 83 |
-> m (Tokens s, a) |
| 84 |
anyTillText sep = do |
| 85 |
(Text tarray beginIndx beginLen) <- getInput |
| 86 |
(thisLen, x) <- go |
| 87 |
pure (Text tarray beginIndx (beginLen - thisLen), x) |
| 88 |
where |
| 89 |
go = do |
| 90 |
(Text _ _ thisLen) <- getInput |
| 91 |
r <- optional $ try sep |
| 92 |
case r of |
| 93 |
Nothing -> anySingle >> go |
| 94 |
Just x -> pure (thisLen, x) |
| 95 |
|