Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve value from optional parser in Parsec?

Sorry if it's a novice question - I want to parse something defined by

Exp ::= Mandatory_Part Optional_Part0 Optional_Part1

I thought I could do this:

proc::Parser String

proc = do {

    ;str<-parserMandatoryPart

    ;str0<-optional(parserOptionalPart0)  --(1)

    ;str1<-optional(parserOptionalPart1)  --(2)

    ;return str++str0++str1

}

I want to get str0/str1 if optional parts are present, otherwise, str0/str1 would be "". But (1) and (2) won't work since optional() doesn't allow extracting result from its parameters, in this case, parserOptionalPart0/parserOptionalPart1.

Now What would be the proper way to do it?

Many thanks!

Billy R

like image 936
Bill Rong Avatar asked Oct 13 '10 07:10

Bill Rong


1 Answers

The function you're looking for is optionMaybe. It returns Nothing if the parser failed, and returns the content in Just if it consumed input.

like image 125
Paul Avatar answered Oct 16 '22 16:10

Paul