# 10.3 Recovery and order

> After a syntax error, the engine looks for a safe point to keep reading: that way it reports every independent error at once, each one only once.

Web page: https://pseudolearn.app/en/spec/diagnosticos/recuperacion

> **In one sentence**
>
> After a syntax error, the engine looks for a safe point to keep reading: that way it reports every independent error at once, each one only once.

## 10.3.1 How it works

When a statement is badly written, analysis does not stop. It discards what it does not understand up to a **synchronization** point —the end of the line, a `;` or the edge of a block— and continues from there. Every broken statement produces **at most one** main diagnostic, and errors that fall on discarded text are not reported.

### Program: Two independent errors

```pseudolearn
Algorithm Dos
  Define a As Integer <- 1
  Write a;
  Write (a + ;
  Write "fin";
EndAlgorithm
```

**Diagnostics**
- `missingStatementTerminator` (error, 2:27): Expected a semicolon at the end of the statement
- `expectedExpression` (error, 4:14): Expected an expression

*The program finishes with an error.*

**Figure 10.3-1: Synchronizing after an error**

Four lines. The first and third have an error and a mark that jumps to the next safe point; the second and fourth are recognised as usual.

Each error discards up to the next safe point and analysis continues. Correct lines are recognised as usual.

A block left unclosed produces **a single** diagnostic, with a related span pointing to where it opens, even if other outer blocks are pending:

### Program: Two unclosed blocks

```pseudolearn
Algorithm Abierto
  While Verdadero Do
    If Verdadero Then
      Write 1;
EndAlgorithm
```

**Diagnostics**
- `unclosedIfStatement` (error, 5:1): Unclosed if statement

*The program finishes with an error.*

## 10.3.2 Order

Diagnostics are published in a fixed order that does not depend on the language or on when they were found: first by phase (lexical, syntactic, semantic), then by position, length and code.

> **Limit**
>
> - At most one hundred main syntax diagnostics are reported. When the cap is reached, `maxDiagnosticsExceeded` is added as a summary.
> - Even an empty or completely invalid text produces a tree: the editor always has something to work with.

**In short**
- After an error, analysis synchronizes and continues.
- A broken statement gives at most one main diagnostic.
- An unclosed block gives a single diagnostic.
- The order is fixed: phase, position, length and code.

**See also**
- [10.2 Severity and classes](https://pseudolearn.app/en/spec/diagnosticos/severidad.md)
- [2.9 Statement end](https://pseudolearn.app/en/spec/lexico/fin-de-sentencia.md)
