Debian Codegraph @

src/Replace/Megaparsec/Internal/ByteString.hs

raw
1 -- |
2 -- Module : Replace.Megaparsec.Internal.ByteString
3 -- Copyright : ©2019 James Brock
4 -- License : BSD2
5 -- Maintainer: James Brock <[email protected]>
6 --
7 -- This internal module is for 'Data.ByteString.ByteString' 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.ByteString
20 (
21 -- * Parser combinator
22 sepCapByteString
23 , anyTillByteString
24 )
25 where
26
27 import Control.Monad
28 import qualified Data.ByteString as B
29 import Text.Megaparsec
30
31 {-# INLINE [1] sepCapByteString #-}
32 sepCapByteString
33 :: forall e s m a. (MonadParsec e s m, s ~ B.ByteString)
34 => m a -- ^ The pattern matching parser @sep@
35 -> m [Either (Tokens s) a]
36 sepCapByteString sep = getInput >>= go
37 where
38 -- the go function will search for the first pattern match,
39 -- and then capture the pattern match along with the preceding
40 -- unmatched string, and then recurse.
41 -- restBegin is the rest of the buffer after the last pattern
42 -- match.
43 go restBegin = do
44 (<|>)
45 ( do
46 restThis <- getInput
47 -- About 'thisiter':
48 -- It looks stupid and introduces a completely unnecessary
49 -- Maybe, but when I refactor to eliminate 'thisiter' and
50 -- the Maybe then the benchmarks get dramatically worse.
51 thisiter <- (<|>)
52 ( do
53 x <- try sep
54 restAfter <- getInput
55 -- Don't allow a match of a zero-width pattern
56 when (B.length restAfter >= B.length restThis) empty
57 pure $ Just (x, restAfter)
58 )
59 (anySingle >> pure Nothing)
60 case thisiter of
61 (Just (x, restAfter)) | B.length restThis < B.length restBegin -> do
62 -- we've got a match with some preceding unmatched string
63 let unmatched = B.take (B.length restBegin - B.length restThis) restBegin
64 (Left unmatched:) <$> (Right x:) <$> go restAfter
65 (Just (x, restAfter)) -> do
66 -- we're got a match with no preceding unmatched string
67 (Right x:) <$> go restAfter
68 Nothing -> go restBegin -- no match, try again
69 )
70 ( do
71 -- We're at the end of the input, so return
72 -- whatever unmatched string we've got since offsetBegin
73 if B.length restBegin > 0 then
74 pure [Left restBegin]
75 else pure []
76 )
77
78 {-# INLINE [1] anyTillByteString #-}
79 anyTillByteString
80 :: forall e s m a. (MonadParsec e s m, s ~ B.ByteString)
81 => m a -- ^ The pattern matching parser @sep@
82 -> m (Tokens s, a)
83 anyTillByteString sep = do
84 begin <- getInput
85 (end, x) <- go
86 pure (B.take (B.length begin - B.length end) begin, x)
87 where
88 go = do
89 end <- getInput
90 r <- optional $ try sep
91 case r of
92 Nothing -> anySingle >> go
93 Just x -> pure (end, x)