Hi,

So far, I figured it out like this:

updateState ...
mbx <- (p3 >>= return . Just) <|> (return Nothing)
updateState ...
case mbx of
  Just x -> return x
  Nothing -> pzero

but this seems a bit clumsy - is there a more elegant solution?

---------- Forwarded message ----------
From: Dimitry Golubovsky <[EMAIL PROTECTED]>
Date: Sat, May 31, 2008 at 10:12 AM
Subject: Parsec, updateState, and failure
To: haskell <haskell-cafe@haskell.org>


Hi,

If a parser which updated user state fails, will such update be reverted?

Suppose we have two parsers combined with <|>

p = p1 <|> p2

p1 has the following:

p1 = try $ do
 ... -- getting something from the input stream
 updateState (\st -> ...) -- updated state based on what gotten from the input
 x <- p3 -- p3 should see updated state and return something
 updateState (\st -> ...) -- updated state again (if p3 succeeded)
 return x

If p3 fails, p1 fails too (second updateState will not be reached).
But what will p2 (tried next) see in the user state? Will it be state
after the first updateState, or will failure of p1 roll the update
back?

Is there any bracket- or try-catch-finally-like mechanism for Parsec?

Thanks.

--
Dimitry Golubovsky

Anywhere on the Web



-- 
Dimitry Golubovsky

Anywhere on the Web
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to