[Haskell-cafe] Parsec lookAhead with choice (<|>) doesn't work as expected?
Roman Cheplyaka
roma at ro-che.info
Fri May 2 21:45:42 UTC 2014
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 --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140503/95960a57/attachment-0001.sig>
More information about the Haskell-Cafe
mailing list