What are Diagnostics?
Diagnostics in Loretta represent errors, warnings, and informational messages produced during parsing and analysis. They provide detailed information about issues in Lua code, including:- Syntax errors (missing tokens, unexpected characters)
- Warnings (deprecated features, potential issues)
- Informational messages (suggestions, hints)
The Diagnostic Class
TheDiagnostic class represents a single diagnostic message:
Severity- How severe the diagnostic is (Error, Warning, Info, Hidden)Id- A unique identifier for the diagnostic (e.g., “LUA0001”)GetMessage()- Human-readable description of the issueLocation- Where in the source code the issue occurs
DiagnosticSeverity
Diagnostics have one of four severity levels:Severity Levels Explained
Error - Code that violates Lua syntax rules:Getting Diagnostics from Trees
You can retrieve diagnostics from various parts of the syntax tree:All Diagnostics in a Tree
Diagnostics for a Specific Node
Diagnostics for a Token
Diagnostic Locations
Each diagnostic has aLocation that indicates where the issue occurs:
Location Properties
SourceSpan- TheTextSpancovering the diagnostic locationGetLineSpan()- Returns line and column informationSourceTree- The syntax tree containing the diagnostic
Diagnostic IDs
Each diagnostic has a unique ID that identifies the type of error:DiagnosticDescriptor
ADiagnosticDescriptor provides metadata about a diagnostic type:
Filtering and Analyzing Diagnostics
Filter by Severity
Group by Location
Check if Code is Valid
Complete Diagnostic Example
Here’s a comprehensive example showing diagnostic analysis:Best Practices
Always Check for Errors
Before processing a syntax tree, check if it has errors:Provide User-Friendly Error Messages
Use Diagnostic IDs for Specific Handling
See Also
- Parsing - How diagnostics are generated during parsing
- Syntax Trees - Understanding the structure that produces diagnostics