E4184#
Compiler diagnostic name: start_rest_binder_is_not_supported_in_longest_match_strategy.
最长匹配策略的 lexmatch 不支持开头的剩余绑定器。
在 lexmatch ... with longest 中,引擎会选择最长的匹配分支。允许在正则前出现剩余绑定器会让这个选择变得含糊,因此开头的剩余绑定器会被拒绝。
错误示例#
///|
pub fn match_text(text : String) -> Unit {
lexmatch text with longest {
(_, "abc", _) => ()
_ => ()
}
}
第一个 _ 试图在 "abc" 正则模式之前绑定或跳过输入。
修改建议#
使用 lexscan 时,请将正则锚定在输入开头,并仅在需要时通过 after= 绑定后缀。
///|
pub fn match_text(text : String) -> Unit {
lexscan text with longest {
(re"^abc", after=_) => ()
_ => ()
}
}