Skip to content

Commit 76b9c19

Browse files
committed
Add ParserK pattern synonyms for compatibility
1 parent 1addabb commit 76b9c19

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 (..)
@@ -126,12 +128,40 @@ type StepParser a m r = Input a -> m (Step a m r)
126128
-- /Pre-release/
127129
--
128130
data Step a m r =
129-
-- The Int is the current stream position index wrt to the start of the
130-
-- array.
131-
Done !Int r
132-
| Partial !Int (StepParser a m r)
133-
| Continue !Int (StepParser a m r)
134-
| Error !Int String
131+
SDone !Int r
132+
| SPartial !Int (StepParser a m r)
133+
| SContinue !Int (StepParser a m r)
134+
| SError !Int String
135+
136+
--------------------------------------------------------------------------------
137+
-- Custom Patterns
138+
--------------------------------------------------------------------------------
139+
140+
incrIndex :: Step a m r -> Step a m r
141+
incrIndex (SPartial i s) = SPartial (i + 1) s
142+
incrIndex (SContinue i s) = SContinue (i + 1) s
143+
incrIndex (SDone i b) = SDone (i + 1) b
144+
incrIndex (SError i s) = SError (i + 1) s
145+
146+
pattern Partial :: Int -> StepParser a m r -> Step a m r
147+
pattern Partial i s <- (incrIndex -> SPartial i s)
148+
where Partial i s = SPartial (i - 1) s
149+
150+
pattern Continue :: Int -> StepParser a m r -> Step a m r
151+
pattern Continue i s <- (incrIndex -> SContinue i s)
152+
where Continue i s = SContinue (i - 1) s
153+
154+
pattern Done :: Int -> r -> Step a m r
155+
pattern Done i b <- (incrIndex -> SDone i b)
156+
where Done i b = SDone (i - 1) b
157+
158+
pattern Error :: Int -> String -> Step a m r
159+
pattern Error i b <- (incrIndex -> SError i b)
160+
where Error i b = SError (i - 1) b
161+
162+
--------------------------------------------------------------------------------
163+
-- Code
164+
--------------------------------------------------------------------------------
135165

136166
instance Functor m => Functor (Step a m) where
137167
fmap f (Done n r) = Done n (f r)

0 commit comments

Comments
 (0)