且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Grako-如何进行错误处理?

更新时间:2023-12-01 21:10:34

对于初学者,第一个规则确实与空字符串匹配.也许您想尝试类似的方法:

For starters, the first rule does match the empty string. Perhaps you'd want to try something like:

pattern   = { tag | function }+ $ ;

是的,如果无法解析输入字符串,则生成的解析器将引发异常.注意上述规则中的$:它告诉解析器它应该在该位置看到输入的结尾.没有它,解析器很乐于仅解析部分输入而成功.

Yes, the generated parser will raise an exception if it can't parse the input string; note the $in the above rule: it tells the parser it should see the end of the input in that position. Without it, the parser is happy to succeed having parsed only part of the input.

然后,我认为命名元素中的命名元素不会产生预期的结果.

Then, I don't think that named elements within named elements will produce the desired results.

这是语法版本,可能会产生您想要的内容:

This is a version of the grammar that may produce what you want:

pattern   = { tag | function }+ $ ;
tag       = ( "%" tag:id "%" );
function  = ("$" function:id "()" );
id        = ?/([^\\%$,()=])+/? ;