Skip to content

Commit aad05f8

Browse files
adithyaovharendra-kumar
authored andcommitted
Add ParserK pattern synonyms for compatibility
1 parent 5b9de04 commit aad05f8

1 file changed

Lines changed: 37 additions & 7 deletions

File tree

  • core/src/Streamly/Internal/Data/ParserK

core/src/Streamly/Internal/Data/ParserK/Type.hs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE PatternSynonyms #-}
2+
{-# LANGUAGE ViewPatterns #-}
13
-- |
24
-- Module : Streamly.Internal.Data.Parser.ParserK.Type
35
-- Copyright : (c) 2020 Composewell Technologies
@@ -23,7 +25,7 @@
2325

2426
module Streamly.Internal.Data.ParserK.Type
2527
(
26-
Step (..)
28+
Step(Partial, Continue, Done, Error, SPartial, SContinue, SDone, SError)
2729
, Input (..)
2830
, ParseResult (..)
2931
, ParserK (..)
@@ -121,12 +123,40 @@ type StepParser a m r = Input a -> m (Step a m r)
121123
-- /Pre-release/
122124
--
123125
data Step a m r =
124-
-- The Int is the current stream position index wrt to the start of the
125-
-- array.
126-
Done !Int r
127-
| Partial !Int (StepParser a m r)
128-
| Continue !Int (StepParser a m r)
129-
| Error !Int String
126+
SDone !Int r
127+
| SPartial !Int (StepParser a m r)
128+
| SContinue !Int (StepParser a m r)
129+
| SError !Int String
130+
131+
--------------------------------------------------------------------------------
132+
-- Custom Patterns
133+
--------------------------------------------------------------------------------
134+
135+
incrIndex :: Step a m r -> Step a m r
136+
incrIndex (SPartial i s) = SPartial (i + 1) s
137+
incrIndex (SContinue i s) = SContinue (i + 1) s
138+
incrIndex (SDone i b) = SDone (i + 1) b
139+
incrIndex (SError i s) = SError (i + 1) s
140+
141+
pattern Partial :: Int -> StepParser a m r -> Step a m r
142+
pattern Partial i s <- (incrIndex -> SPartial i s)
143+
where Partial i s = SPartial (i - 1) s
144+
145+
pattern Continue :: Int -> StepParser a m r -> Step a m r
146+
pattern Continue i s <- (incrIndex -> SContinue i s)
147+
where Continue i s = SContinue (i - 1) s
148+
149+
pattern Done :: Int -> r -> Step a m r
150+
pattern Done i b <- (incrIndex -> SDone i b)
151+
where Done i b = SDone (i - 1) b
152+
153+
pattern Error :: Int -> String -> Step a m r
154+
pattern Error i b <- (incrIndex -> SError i b)
155+
where Error i b = SError (i - 1) b
156+
157+
--------------------------------------------------------------------------------
158+
-- Code
159+
--------------------------------------------------------------------------------
130160

131161
instance Functor m => Functor (Step a m) where
132162
fmap f (Done n r) = Done n (f r)

0 commit comments

Comments
 (0)