E4218

E4218#

Compiler diagnostic name: lexscan_pattern_not_anchored.

A lexical regex pattern is not anchored where anchoring is required.

Patterns used by lexscan, including scans of an in-memory @lexbuf.StringScanner, or by lexmatch with the longest strategy, must start at the current input position. Anchor each such regex with ^.

Erroneous example#

///|
pub fn classify(input : String) -> String {
  lexmatch input with longest {
    re"[a-z]+" => "word"
    _ => "other"
  }
}

Suggestion#

Add ^ to the start of the regex. Add $ as well only when the case must consume the whole input.

///|
pub fn classify(input : String) -> String {
  lexmatch input with longest {
    (re"^[a-z]+", after=_) => "word"
    _ => "other"
  }
}