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

Baojun Wang wangbj at gmail.com
Fri May 2 21:53:42 UTC 2014


Many thanks for your timely help.

Just tried:

nocPeekHeader = do
  st <- (lookAhead noc28hdr') <|>  (lookAhead noc25hdr')

noc28hdr' = try noc28hdr
noc25hdr' = trynoc25hdr

still fail with the same error:

--
(input1.txt)
SNOC ERROR: ERRLOG0 = 0x80030000

(input2.txt)
SNOC ERROR: ERRSTATUS = 0x00000001

Right (28,"SNOC")
Left "input2.txt" (line 1, column 2):
unexpected "S"
expecting "NOC ERROR: ERRLOG"
*Main>


On Fri, May 2, 2014 at 2:45 PM, Roman Cheplyaka <roma at ro-che.info> wrote:

> Try wrapping the first parser in 'try'.
>
> * Baojun Wang <wangbj at gmail.com> [2014-05-02 14:32:06-0700]
> > 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/ab881019/attachment.html>


More information about the Haskell-Cafe mailing list