How to retrieve value from optional parser in Parsec?

[亡魂溺海] 提交于 2019-12-10 13:14:23

问题


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


回答1:


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.




回答2:


From the docs:

option x p tries to apply parser p. If p fails without consuming input, it returns the value x, otherwise the value returned by p.

So you could do:

proc :: Parser String
proc = do
  str  <- parserMandatoryPart
  str0 <- option "" parserOptionalPart0
  str1 <- option "" parserOptionalPart1
  return (str++str0++str1)

Watch out for the "without consuming input" part. You may need to wrap either or both optional parsers with try.

I've also adjusted your code style to be more standard, and fixed an error on the last line. return isn't a keyword; it's an ordinary function. So return a ++ b is (return a) ++ b, i.e. almost never what you want.



来源:https://stackoverflow.com/questions/3921559/how-to-retrieve-value-from-optional-parser-in-parsec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!