SPB Git

Repository Public

diagnostics.md 5.1 KB

# Diagnostics

A diagnostic is what the compiler tells you about your code: an error that prevents compilation, or a warning about something that compiles but probably is not what you meant.

# Reading one

text
piece.sonata:4:23: error[S2001]: unknown instrument 'GrandPiana'
4 |     instrument piano: GrandPiana {
  |                       ^^^^^^^^^^
  help: did you mean 'GrandPiano'?
  • piece.sonata:4:23 — the file, the line and the column.
  • error or warning, and a code that identifies the rule.
  • The message, the line of source with the exact range underlined and, when the compiler has one, a suggestion.

In the app, problems are underlined in the editor, marked in the gutter and listed in the console. Click one to go to it. When the fix is mechanical and safe — a misspelt name with one close match, for instance — the console offers a button that applies it.

The compiler does not stop at the first problem: it recovers and reports what it can in one pass, up to 200 diagnostics.

# The families of codes

Prefix Stage Typical cause
L0… reading the text an unterminated string, an unknown character, a file that is too large
P1… syntax a missing brace, a statement in the wrong place, a reserved word used as a name
S2… names an unknown instrument, effect, preset, parameter or variable; a name defined twice
T3… types and units seconds added to beats, a value out of range, a missing unit
M4… music a note out of an instrument's range, a signature change inside a bar, a dangling tie
G5… signal graphs a feedback loop without a delay, buses routed in a circle
Q6… limits too many notes, endless recursion
I7… modules a module that cannot be found, an import cycle

Every code is documented in Documentation/spec/diagnostics.md, and a test checks that the compiler emits no code that is missing from that list.

# The ones you will meet

S2001 — unknown instrument. The name after the colon is not an instrument of the standard library or of your project.

sonata,error=S2001
composition "S2001" {
    instrument a: Pianoforte {
        play C4
    }
}

P1010 — a reserved word used as a name. out, play, tempo, chord and the other keywords cannot name variables. Note names cannot either: A4 and Bb are always notes.

T3001 — an operator applied to the wrong types. The message names both types.

sonata,error=T3001
composition "T3001" {
    instrument a: GrandPiano {
        play C4 + 440Hz
    }
}

T3003 — a decimal where an exact duration is needed. Write 3/8, not 0.375.

T3005 — a value outside its range. The message gives the range.

T3006 — a missing or wrong unit. A time parameter needs ms, s or a musical duration; a frequency needs Hz.

sonata,error=T3006
composition "T3006" {
    instrument a: GrandPiano {
        play C4
        effects {
            eq(low_cut: 80)
        }
    }
}

S2022 and S2023 — unknown preset, unknown parameter. Usually a typing mistake; the suggestion names the closest match. The valid names are in the reference and in sonata docs NAME.

M4001 — a note outside the instrument's range (warning). The note is rendered anyway.

M4002 — a signature change inside a bar. Finish the bar first, or declare the change at composition level with at.

sonata,error=M4002
composition "M4002" {
    signature 4/4
    instrument a: GrandPiano {
        play C4
        signature 3/4
    }
}

M4005 — a tie with nothing to tie to (warning). The next note of the same voice has another pitch, or does not start where this one ends.

G5002 — a loop without a delay in a patch. A node reads a node defined later. Route the feedback through delay(in: …).

Q6001 and Q6003 — too many notes, calls nested too deep. Usually a loop or a recursion that does not end where you expected.

I7001 — module not found. import a.b looks for a/b.sonata beside the file being compiled (in a project: under src/).

# Warnings are worth reading

A warning does not stop the piece from playing. It marks a place where what you wrote and what you get may differ. A clean compilation — no error and no warning — is the normal state of a finished piece; every example shipped with Sonata compiles without a single diagnostic.

# Studio edits that no longer apply

When the code changes under an edit made in the Studio, the edit is skipped rather than applied to the wrong note. The console lists each one as Studio edit skipped with the reason. See Studio.

# From the command line

sonata check file.sonata prints diagnostics in the form above on standard error and exits with 1 if there is an error. --json prints them as data — code, severity, message, file, line, column, byte range, suggestion and fix — for editors and scripts. See Command line.

Files main
Menu

Theme