E4182#
Compiler diagnostic name: regex_constant_ref_in_lex_is_not_supported.
此 lexmatch 上下文不支持正则常量。
旧的 lexmatch 只能在有限上下文中使用正则常量:目标为 StringView,并且使用默认的首次匹配策略。其他 lexmatch 形式(例如 with longest)需要使用正则字面量模式。
警告
新代码不应使用最长匹配 lexmatch。请使用 lexscan ... with longest;如果不需要词法分析器风格的行为,则优先使用 value =~ SOME_REGEX 这类正则匹配表达式。
错误示例#
下面的示例在最长匹配的 lexmatch 中使用了正则常量:
///|
const WORD = re"[A-Za-z]+"
///|
pub fn match_word(input : String) -> Unit {
lexmatch input with longest {
(WORD, _) => ()
_ => ()
}
}
MoonBit 会报告一个错误。
建议#
在 lexscan 中使用正则字面量模式,或者用正则匹配表达式重写代码:
///|
pub fn match_word(input : String) -> Unit {
lexscan input with longest {
(re"^[A-Za-z]+", after=_) => ()
_ => ()
}
}