|
| 1 | +{-# LANGUAGE PatternSynonyms #-} |
| 2 | +{-# LANGUAGE ViewPatterns #-} |
1 | 3 | -- | |
2 | 4 | -- Module : Streamly.Internal.Data.Parser.ParserK.Type |
3 | 5 | -- Copyright : (c) 2020 Composewell Technologies |
|
23 | 25 |
|
24 | 26 | module Streamly.Internal.Data.ParserK.Type |
25 | 27 | ( |
26 | | - Step (..) |
| 28 | + Step(Partial, Continue, Done, Error, SPartial, SContinue, SDone, SError) |
27 | 29 | , Input (..) |
28 | 30 | , ParseResult (..) |
29 | 31 | , ParserK (..) |
@@ -121,12 +123,40 @@ type StepParser a m r = Input a -> m (Step a m r) |
121 | 123 | -- /Pre-release/ |
122 | 124 | -- |
123 | 125 | 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 | +-------------------------------------------------------------------------------- |
130 | 160 |
|
131 | 161 | instance Functor m => Functor (Step a m) where |
132 | 162 | fmap f (Done n r) = Done n (f r) |
|
0 commit comments