gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:55:35 30 4
gpt4 key购买 nike

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 反射(reflect)这一点?这个我试过了
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_COMMENTNEWLINE终止,并且 conf 包含 1 个或多个。

关于parsing - 使用 Pest.rs,我如何指定要锚定和整行的评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66726411/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com