如何从Parsec中的可选分析器中检索值? [英] How to retrieve value from optional parser in Parsec?

查看:111
本文介绍了如何从Parsec中的可选分析器中检索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这是一个新手问题 - 我想解析一些由

定义的内容

Exp :: = Mandatory_Part Optional_Part0 Optional_Part1


$ b $我可以这样做:

  proc :: Parser String 

proc = do {

; str< -parserMandatoryPart

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

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

; return str ++ str0 ++ str1

}

如果存在可选部分,我想获得str0 / str1,否则,str0 / str1将为。
但是(1)和(2)不起作用,因为optional()不允许从其参数中提取结果,在这种情况下,parserOptionalPart0 / parserOptionalPart1。

现在, 适合做的方法是什么?

非常感谢!

比利R

解决方案

您正在寻找的函数是 optionMaybe 。如果解析器失败,它将返回 Nothing ,并返回 Just 中的内容,如果它消费了输入。 b $ b

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

解决方案

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.

这篇关于如何从Parsec中的可选分析器中检索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆