Class Ek9SourceFormatter

java.lang.Object
org.ek9lang.compiler.formatter.Ek9SourceFormatter

public final class Ek9SourceFormatter extends Object
Orchestrates the EK9 source code formatting pipeline. Implements a multi-pass approach:
  • Pass 1: Canonical indentation (2-space, from INDENT/DEDENT tokens)
  • Pass 2: Structural reordering (reference sorting, block ordering, overload colocation)
  • Pass 3: Token spacing (operator spacing, canonical whitespace)
  • Pass 4: Line splitting (long line breaking for params, streams, expressions)
  • Pass 5: Comment formatting (doc comment indentation and wrapping)
Each pass validates its output by re-parsing. If any pass produces unparseable output, the entire operation is aborted and the original file is preserved.

For unparseable files, a Tier 2 pipeline (force mode) attempts error-guided iterative refinement to achieve a parseable state before applying the Tier 1 pipeline.

  • Constructor Details

    • Ek9SourceFormatter

      public Ek9SourceFormatter()
  • Method Details

    • format

      public FormatterResult format(String source)
      Format the given EK9 source code using the Tier 1 pipeline. The input must be parseable. If not, returns a failure result.
      Parameters:
      source - The EK9 source code to format.
      Returns:
      The formatting result containing the formatted output or error information.
    • forceFormat

      public FormatterResult forceFormat(String source)
      Force-format the given EK9 source code. Attempts Tier 2 error-guided refinement for unparseable files, then applies the Tier 1 pipeline if successful.
      Parameters:
      source - The EK9 source code to format (may be unparseable).
      Returns:
      The formatting result containing the formatted output or error information.