E4185#
Compiler diagnostic name: invalid_lex_pattern.
无效的 lexmatch 模式形状。
lexmatch 模式允许一个正则模式,并可选带有一个起始剩余绑定和一个结束剩余绑定。其他顶层模式形状都是无效的。
警告
新代码中已不推荐使用 lexmatch。除非你确实需要旧式词法分析器风格的行为,否则请优先使用 value =~ (re"...", before~, after~) 这类正则匹配表达式。
错误示例#
下面的模式包含了过多的顶层部分:
///|
fn classify(input : String) -> String {
lexmatch input with longest {
("a", before, "b", after) => before.to_owned() + after.to_owned()
_ => "none"
}
}
///|
test {
ignore(classify)
}
MoonBit 会报告一个错误。
建议#
使用一个正则模式,并按需添加起始和结束剩余绑定。在新代码中,正则匹配表达式通常更清晰:
///|
fn classify(input : String) -> String {
if input =~ (re"a" + re"b", before~, after~) {
before.to_owned() + after.to_owned()
} else {
"none"
}
}
///|
test {
inspect(classify("xabz"), content="xz")
}