E4182#
Compiler diagnostic name: regex_constant_ref_in_lex_is_not_supported.
Regex constants are not supported in this lexmatch context.
Legacy lexmatch can use regex constants only in limited contexts: a
StringView target with the default first-match strategy. Other lexmatch
forms, such as with longest, require literal regex patterns.
Warning
Longest-match lexmatch is deprecated for new code. Use lexscan ... with longest, or prefer regex match expressions such as value =~ SOME_REGEX when
you do not need lexer-style behavior.
Erroneous example#
The following example uses a regex constant in a longest-match lexmatch:
///|
const WORD = re"[A-Za-z]+"
///|
pub fn match_word(input : String) -> Unit {
lexmatch input with longest {
(WORD, _) => ()
_ => ()
}
}
MoonBit will report an error.
Suggestion#
Use a literal regex pattern with lexscan, or rewrite the code with a regex
match expression:
///|
pub fn match_word(input : String) -> Unit {
lexscan input with longest {
(re"^[A-Za-z]+", after=_) => ()
_ => ()
}
}