Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Attoparsec support saving and modifying user state?

I'm using Attoparsec, and I would like to track a user state value throughout a parsing task.

I'm familiar with the monadic functions getState, putState, and modifyState of Parsec, but I can't seem to find an analogue within Attoparsec. Is there a trivial way to do this with something internal to Attoparsec or by using a State monad?

like image 921
Robert Bermani Avatar asked May 28 '15 11:05

Robert Bermani


1 Answers

You can use StateT s Parser, just be mindful that backtracking in the parser also rolls back the state, so you only get those stateful actions that were invoked on the code path with the successful parse.

{-# LANGUAGE OverloadedStrings #-}

import Data.Attoparsec.ByteString.Char8
import Control.Monad.State
import Control.Applicative

test :: StateT Int Parser ()
test = do
  many $ choice [
    (modify (+1) *> lift (string "car")),
    (modify (+1) *> lift (string "cat"))]
  pure ()

parseOnly (runStateT test 0) "catcatcat"
-- Right ((),3)

Also, we can use most of the Attoparsec combinators out of the box, because they have generic types with Alternative, MonadPlus, Applicative or Monad constraints, and StateT defines lift-through instances for these. We can use lift for the basic Parser-s.

like image 135
András Kovács Avatar answered Nov 17 '22 00:11

András Kovács