E4180

E4180#

Compiler diagnostic name: unsupported_match_strategy.

不支持的 lexmatch 匹配策略。

lexmatch 只支持默认的首次匹配策略,以及显式的 longest 策略。其他策略名会被拒绝。

警告

新代码中已不推荐使用 lexmatch。除非你确实需要旧式词法分析器风格的行为,否则请优先使用 value =~ re"..." 这类正则匹配表达式。

错误示例#

下面的示例请求了不支持的 shortest 策略:

///|
fn classify(input : String) -> String {
  lexmatch input with shortest {
    "if" => "keyword"
    _ => "identifier"
  }
}

///|
test {
  ignore(classify)
}

MoonBit 会报告一个错误。

建议#

使用受支持的策略之一;如果首次匹配行为已经足够,也可以用正则匹配表达式重写代码:

///|
fn classify(input : String) -> String {
  if input =~ re"^if$" {
    "keyword"
  } else {
    "identifier"
  }
}

///|
test {
  inspect(classify("if"), content="keyword")
  inspect(classify("gift"), content="identifier")
}