@@ -115,20 +115,29 @@ instance Functor m => Functor (Parser m a) where
116 fmap3 g = fmap (fmap (fmap g))
116 fmap3 g = fmap (fmap (fmap g))
119+wrap :: Monad m => b -> Parser m a b
120+wrap b = Parser (\_ _ -> pure $ Stop 0 ()) -- step
121+ (pure ()) -- initial
122+ (\_ -> pure $ Right b) -- extract
118{-# ANN type SeqParseState Fuse #-}
124{-# ANN type SeqParseState Fuse #-}
119data SeqParseState sl f sr =
125data SeqParseState sl f sr =
120 SeqParseL sl | SeqParseR f sr | SeqParseLErr String
126 SeqParseL sl | SeqParseR f sr | SeqParseLErr String
128+-- XXX implement interleaved variant, parse using the first parser and then
129+-- using the second parser alternately.
122-- | Apply two parsing folds sequentially to an input. The input is provided to
131-- | Apply two parsing folds sequentially to an input. The input is provided to
123-- the first parser, if the parser fails then whole composition fails. If the
132-- the first parser, if the parser fails then whole composition fails. If the
124-- parser succeeds, the remaining input is supplied to the second parser. If
133-- parser succeeds, the remaining input is supplied to the second parser. If
125-- the second parser succeeds the composed parser succeeds otherwise it fails.
134-- the second parser succeeds the composed parser succeeds otherwise it fails.
136+-- This is the opposite of the "append" operation on streams.
127instance Monad m => Applicative (Parser m a) where
138instance Monad m => Applicative (Parser m a) where
128 {-# INLINE pure #-}
139 {-# INLINE pure #-}
129− pure b = Parser (\_ _ -> return $ Stop 0 ())
140+ pure = wrap
131− (\_ -> return $ Right b)
133 {-# INLINE (<*>) #-}
142 {-# INLINE (<*>) #-}
134 (Parser stepL initialL extractL) <*> (Parser stepR initialR extractR) =
143 (Parser stepL initialL extractL) <*> (Parser stepR initialR extractR) =