Baojun Wang | 2 May 23:32 2014
Picon

Parsec lookAhead with choice (<|>) doesn't work as expected?

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
Attachment (la.hs): text/x-haskell, 2309 bytes
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Roman Cheplyaka | 2 May 23:45 2014

Re: Parsec lookAhead with choice (<|>) doesn't work as expected?

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

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Baojun Wang | 2 May 23:53 2014
Picon

Re: Parsec lookAhead with choice (<|>) doesn't work as expected?

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


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Baojun Wang | 3 May 01:37 2014
Picon

Re: Parsec lookAhead with choice (<|>) doesn't work as expected?

Thanks, problem fixed by adding try, the error turns out to be something else :)


On Fri, May 2, 2014 at 2:53 PM, Baojun Wang <wangbj <at> gmail.com> wrote:
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



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Mateusz Kowalczyk | 3 May 10:21 2014
Picon

Re: Parsec lookAhead with choice (<|>) doesn't work as expected?

On 05/03/2014 01:37 AM, Baojun Wang wrote:
> Thanks, problem fixed by adding try, the error turns out to be something
> else :)
> 

I'd like to note that Attoparsec backtracks by default, might be worth
looking into.

> 
> On Fri, May 2, 2014 at 2:53 PM, Baojun Wang <wangbj <at> gmail.com> wrote:
> 
>> 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
>>>
>>>
>>
> 
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 

--

-- 
Mateusz K.

Gmane