E4220

E4220#

Compiler diagnostic name: regex_match_binding_not_supported.

词法分支使用了当前匹配模式不支持的绑定。

对于文本和字节输入,first-match lexmatch 同时支持 before= 和 after=。longest-match 模式从输入开头匹配,因此仅支持 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
    _ => ""
  }
}