Parsec:消耗所有输入 [英] Parsec: Consume all input

查看:130
本文介绍了Parsec:消耗所有输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



作为一个具体的例子,假设我们使用了Parsec的一个常见问题,那就是它往往会忽略无效输入,如果它发生在正确的地方。具有 integer :: Parser Int ,并且我写了

 表达式= sepBy整数(char'+')

(暂时忽略空白问题。)



正确解析123 + 456 + 789。然而,如果我喂它123 + 456-789,它愉快地忽略了非法的 - 字符和表达的尾部;我实际上想要一个错误消息告诉我有关无效输入,而不仅仅是让它默默地忽略那部分。



我明白这是为什么发生这种情况。我不确定的是如何解决它。什么是设计消费所有提供的输入,并且只有当全部是有效表达式时才成功的解析器的一般方法?

解决方案

其实很简单 - 只需确保它后面跟着 eof

  parse(expression< * eof)< interactive> 123 + 456-789

eof 匹配输入的结尾,即使输入只是一个字符串而不是文件。



显然,这只在解析器的顶层有意义。

One common problem I have with Parsec is that it tends to ignore invalid input if it occurs in the "right" place.

As a concrete example, suppose we have integer :: Parser Int, and I write

expression = sepBy integer (char '+')

(Ignore whitespace issues for a moment.)

This correctly parses something like "123+456+789". However, if I feed it "123+456-789", it merrily ignores the illegal "-" character and the trailing part of the expression; I actually wanted an error message telling me about the invalid input, not just having it silently ignore that part.

I understand why this happens; what I'm not sure about is how to fix it. What is the general method for designing parsers that consume all supplied input and succeed only if all of it is a valid expression?

解决方案

It's actually pretty simple--just ensure it's followed by eof:

parse (expression <* eof) "<interactive>" "123+456-789"

eof matches the end of the input, even if the input is just a string and not a file.

Obviously, this only makes sense at the top level of your parser.

这篇关于Parsec:消耗所有输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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