[Haskell-cafe] Parsec lookAhead with choice (<|>) doesn't work as expected?

Baojun Wang wangbj at gmail.com
Fri May 2 21:32:06 UTC 2014


Hi List,

There is my first email on haskell-cafe, I must admit haskell is a amazing
language even though I found it hard to program.

I'm trying to use Parsec to parse a text file something like:

format 1)
HEADER
BODY

format 2)
BODY

BODY = many DATA_FIELDS

I must distinguish the format is either 1) or 2), because I need process
them differently. So I'm trying to use lookAhead & <|>, something like:

format1Header = do
  HEADER_parser1
  HEADER_parser2
  return ..

format2Header = do
  -- whatever
  return ..

headerVersion = do
  version <- (lookAhead format1Header) <|> (lookAhead format2Header)
  return version

Howerver if format1Header parser failed headerVersion just return failure
without trying format2Header parser.

Full source code is attached, you can load the source then just run ``main''

I searched seems similar issue had been reported:
https://groups.google.com/forum/#!topic/fa.haskell/NGjQqR05-TA

Is there anything I'm fundamentally wrong with lookAhead and <|> in Parsec?
How can I get headerVersion works as expected?

Thank you
Baojun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140502/75f8d5a7/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: la.hs
Type: text/x-haskell
Size: 1710 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140502/75f8d5a7/attachment.hs>


More information about the Haskell-Cafe mailing list