* Documentation

Smart Error Recovery

In-depth architectural overview of Turf’s advanced diagnostic engine and error recovery mechanisms.

Turf is designed with a “Diagnostics-First” philosophy. Unlike traditional compilers that halt at the first sign of trouble, Turf’s architecture is built to gracefully handle errors, recover, and continue analysis to provide the most comprehensive feedback possible.

Distributed Diagnostic Engine

The core of Turf’s error handling is the DiagnosticEngine, a centralized registry for all compiler issues. This engine is decoupled from the parser and code generator, allowing for:

Phase 1: Heuristic-Based Recovery

The current implementation (Phase 1) uses advanced string-matching heuristics to suggest fixes for common developer mistakes.

1. Damerau-Levenshtein Distance

Turf implements the Damerau-Levenshtein distance algorithm in Algorithms.cpp to provide suggestions for:

2. Guarded Recursive Descent

The parser uses a setjmp/longjmp mechanism (as seen in main.cpp) to create “recovery points.” When an error is encountered:

Phase 2: Speculative Re-parsing (Conceptual)

Future versions of Turf will introduce Speculative Re-parsing, where the compiler internally “tries out” various fixes for an error.

Architectural Flow:

  1. Error Trigger: An invalid token is encountered.
  2. Context Snapshot: The compiler creates a snapshot of the current AST and Symbol Table.
  3. Speculation: The compiler generates multiple “fix candidates” (using heuristics or LLMs).
  4. Validation: Each candidate is speculatively parsed.
  5. Ranking: Candidates that resolve the error and lead to a valid AST are ranked and presented to the user.

Semantic Analysis & Linting

Beyond syntax, Turf’s Lint.cpp performs deep semantic checks to prevent logical errors:

By combining these advanced diagnostic techniques, Turf provides a development experience that is both helpful and educational for the programmer.