简体   繁体   English

使用 Pest.rs,如果 PUSH 是可选的,如何避免“在空堆栈上调用 peek”?

[英]Using Pest.rs, how can I avoid 'peek was called on empty stack' if the PUSH is optional?

Pest.rs has the ability to push and peek onto a stack. Pest.rs 具有推送和窥视堆栈的能力。 This is useful when a delimiter is given by the user like custom-quoting found in Perl, and PostgreSQL (double dollar syntax).这在用户给出分隔符时很有用,例如 Perl 和 PostgreSQL(双美元语法)中的自定义引用。 How can I do this if the item may not be on the stack.如果项目可能不在堆栈上,我该怎么做。 For example, the Exim config file states,例如,Exim 配置文件指出,

It is also possible to use newline and other control characters (those with code values less than 32, plus DEL) as separators in lists.也可以使用换行符和其他控制字符(代码值小于 32 的字符,加上 DEL)作为列表中的分隔符。 Such separators must be provided literally at the time the list is processed.此类分隔符必须在处理列表时按字面意思提供。 For options that are string-expanded, you can write the separator using a normal escape sequence.对于字符串扩展的选项,您可以使用正常的转义序列编写分隔符。 This will be processed by the expander before the string is interpreted as a list.这将在字符串被解释为列表之前由扩展器处理。 For example, if a newline-separated list of domains is generated by a lookup, you can process it directly by a line such as this:例如,如果通过查找生成以换行符分隔的域列表,您可以通过如下行直接处理它:

 domains = <\n ${lookup mysql{.....}}

You can see here the token <\n which overrides the default separator of : is optional.您可以在这里看到覆盖默认分隔符:的标记<\n是可选的。 You can see here that the syntax is essentially,你可以看到这里的语法本质上是,

list_sep        = ${ "<" ~ (!WHITE_SPACE ~ ANY) }
list            = {
  list_type
  ~ key
  ~ "="
  ~ PUSH(list_sep)?
  ~ !(PEEK ~ ANY)+
  ~ (PEEK ~ !(PEEK ~ ANY))*
}

But when I run that I get,但是当我跑步时,我得到了,

peek was called on empty stack peek 在空堆栈上被调用

Is there anyway to provide for this operation with PEG such that the list is presented to the user as a string, but as an actual list of element tokens?无论如何,是否可以使用 PEG 提供此操作,以便将列表作为字符串呈现给用户,但作为元素标记的实际列表?

One method I've started doing is making sure the PUSH always happens.我开始做的一种方法是确保PUSH总是发生。 It even seems like an opitional PUSH should be a compiler error,甚至似乎可选的PUSH应该是编译器错误,

Rather than而不是

~ PUSH(list_sep)?

Do this,做这个,

~ PUSH(list_sep?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM