使用 Pest.rs,我如何指定要锚定整行的评论?

Using Pest.rs, how can I specify that comments are to be anchored and whole line?

Exim uses a really awkward comment syntax,

Blank lines in the file, and lines starting with a # character (ignoring leading white space) are treated as comments and are ignored. Note: A # character other than at the beginning of a line is not treated specially, and does not introduce a comment.

这意味着,

# This is is a comment
This has no comments # at all

有没有办法用 Pest.rs 镜像这个?我试过了,

COMMENT    = { "#" ~ (!NEWLINE ~ ANY)* ~ NEWLINE }
WHITESPACE = _{ " " }
main       = { SOI ~ ASCII_ALPHA* ~ EOI }

但是,这将匹配

MyText # Exim test this is not a comment 

如何将评论锚定在左侧?

默认的 COMMENT 扩展无法做到这一点,因为它已扩展到 all instances of rule-concatenation with ~ except for the atomics.。下面两行是一样的,

a = { b ~ c }
a = { b ~ WHITESPACE* ~ (COMMENT ~ WHITESPACE*)* ~ c }

这实际上意味着,如果您要使用 ~COMMENT,则必须将规则限制为 atomic rules with @ and $

而不是这个,对于基于行的语法,我最终改进了这个而不是使用 COMMENT 宏。而是定义我自己的宏,_COMMENT 以避免正常扩展为非原子标记,

WHITESPACE = _{ " " }
_COMMENT   = { "#" ~ (!NEWLINE ~ ANY)* ~ NEWLINE }
expr       = { ASCII_ALPHA+ }
stmt       = { expr ~ NEWLINE }
conf       = { SOI ~ (stmt | _COMMENT | NEWLINE)+ ~ EOI }

注意这里 stmt_COMMENT 都是 NEWLINE 终止的,并且 conf 包括其中的 1 个或多个。