Class Ek9ModuleName

java.lang.Object
org.ek9lang.compiler.support.Ek9ModuleName

public final class Ek9ModuleName extends Object
THE validator for an EK9 module name — use this, do not hand-roll a regex.

A module name is the grammar's dottedName (EK9.g4:753): moduleSegment (DOT moduleSegment)*, where a segment is an Identifier (Letter LetterOrDigit*, so it must START with an ASCII letter — not _, not a digit) or one of ~50 explicitly-listed safe keywords.

Why this parses instead of pattern-matching. A character-class regex is necessary but not sufficient, and the insufficiency is not obvious: regexes operate on characters, not tokens, so any plausible pattern accepts EVERY keyword — including the ones moduleSegment does not list. com.acme.range is legal (RANGE is one of the 105 alternatives); com.acme.mod is not (MOD is absent) — yet both match the same regex. A hand-rolled pattern therefore accepts names that will not parse, and drifts out of sync every time that keyword list changes. Running the real dottedName rule is the only implementation that cannot drift.

The permitted list is wider than intuition suggests. moduleSegment allows if, for, switch and while as segments; the only word-like keywords it excludes are defines, mod, rem, tee, uniq and xor. Do not guess which side a keyword falls on — ask this class. Ek9ModuleNameTest pins both sets.

Validation only: this says whether a name is LEGAL, not whether it is conventional (the grammar permits Com.Acme; any lower-case convention belongs in a compiler warning, not here).

  • Method Details

    • isValid

      public static boolean isValid(String candidate)
      Whether candidate is a legal EK9 module name.
      Parameters:
      candidate - the proposed dotted module name
      Returns:
      true when the grammar's dottedName rule accepts it in full
    • firstProblem

      public static Optional<String> firstProblem(String candidate)
      The first reason candidate is not a legal module name, or empty when it is legal. The message names the offending segment where one can be identified, so a UI field or an MCP error can say WHY rather than just "invalid".
      Parameters:
      candidate - the proposed dotted module name
      Returns:
      a human-readable problem, or empty when valid