E4220#
Compiler diagnostic name: regex_match_binding_not_supported.
词法分支使用了当前匹配模式不支持的绑定。
对于 String 和 StringView 输入,首次匹配的 lexmatch 同时支持 before= 和 after=。最长匹配模式从输入开头开始,因此支持 after=,但不支持 before=。lexscan 不支持这两种绑定:流式目标会消费输入缓冲区,而内存中的 @lexbuf.StringScanner 会在其 cursor 字段中存储下一次扫描的位置。
错误示例#
///|
pub fn first_word(input : String) -> StringView {
lexmatch input with longest {
(re"^[a-z]+" as word, before=_) => word
_ => ""
}
}
建议#
对于 lexscan,请移除 before= 或 after=,并用 ^ 锚定正则开头。对于 lexmatch,请移除 before=,用 ^ 锚定正则开头;需要未匹配的后缀时使用 after=。
///|
pub fn first_word(input : String) -> StringView {
lexmatch input with longest {
(re"^[a-z]+" as word, after=_) => word
_ => ""
}
}