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