Boost.Spirit.Qi - 规则开头的错误
如何在规则开始时检测错误?例如,请考虑 Mini XML示例包含在文档中。如果我给解析器提供像:
How would I detect an error at the start of a rule? For example, consider the Mini XML example included in the docs. If I feed the parser something like:
<element>this is an error<element>
然后我得到:
错误!期望在这里:
Error! Expecting here: ""
错误!期望在这里:
解析失败。
然后考虑送入:
element>this is an error</element>
我得到非常通用,不太有用:
And I get the very generic and not so useful:
解析失败。
Parsing failed.
如何修改规则以报告错误
How could I modify the rule to report the error in an informative way?
您需要在文档根级别
其他邮件是由失败的期望点产生的。您需要在开始时添加额外的期望点。我会这样做:
The other messages are generated by failed expectation points. You'll want an extra expectation point at the start. I'd do this:
- 将旧
xml
重命名为element
-
创建一个新的
xml
规则, :
- rename old
xml
rule toelement
create a new
xml
rule that has the element at an expectation point:
xml = qi::eps > element;
[不要更改任何内容]
[Don't change anything else]
利润!
profit!
>输出变为:
The output becomes:
Error! Expecting <element> here: "element>this is a test</element>
"
-------------------------
Parsing failed
-------------------------
请参阅此处