E4185

E4185#

Compiler diagnostic name: invalid_lex_pattern.

Invalid lexmatch pattern shape.

lexmatch patterns allow one regex pattern, optionally with a start rest binder and an end rest binder. Other top-level pattern shapes are invalid.

Warning

lexmatch is deprecated for new code. Prefer regex match expressions such as value =~ (re"...", before~, after~) unless you specifically need legacy lexer-style behavior.

Erroneous example#

The following pattern has too many top-level pieces:

///|
fn classify(input : String) -> String {
  lexmatch input with longest {
    ("a", before, "b", after) => before.to_owned() + after.to_owned()
    _ => "none"
  }
}

///|
test {
  ignore(classify)
}

MoonBit will report an error.

Suggestion#

Use one regex pattern plus optional start and end rest binders. In new code, a regex match expression is usually clearer:

///|
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")
}