Diagnostic class represents errors, warnings, and informational messages produced during parsing and analysis of Lua code. Each diagnostic includes information about what went wrong, where it occurred, and how severe the issue is.
Key Properties
Id
A unique identifier for the diagnostic. For Loretta diagnostics, this follows the formatLUA#### (e.g., LUA0001, LUA1001).
Location
The primary location where the diagnostic occurred in the source code.Location class provides:
- SourceTree: The syntax tree containing the diagnostic
- SourceSpan: The text span (start/end positions) of the issue
- GetLineSpan(): Line and column information for display
Severity
The effective severity of the diagnostic.DiagnosticSeverity enum:
- Error (3): Something not allowed by the language rules
- Warning (2): Something suspicious but allowed
- Info (1): Informational message, not prescriptive
- Hidden (0): An issue not surfaced through normal means
Descriptor
The diagnostic descriptor that provides metadata about this type of diagnostic.Methods
GetMessage
Returns the human-readable diagnostic message.formatProvider: Optional culture-specific formatting information
Getting Diagnostics from a Syntax Tree
The most common way to access diagnostics is through theGetDiagnostics() method on syntax nodes and trees:
Filtering Diagnostics by Severity
You can filter diagnostics to show only errors, warnings, or other severity levels:Formatting Diagnostics for Display
Here’s an example of formatting diagnostics in a user-friendly way:Common Diagnostic IDs
Loretta uses diagnostic IDs in the formatLUA####. Some common ones include:
Lexer Errors (1-999)
- LUA0001: Invalid string escape sequence
- LUA0003: Unfinished string
- LUA0004: Invalid number format
- LUA0005: Numeric literal too large
- LUA0014: Bad character
- LUA0015: Unexpected token
Parser Errors (1000-1999)
- LUA1000: Identifier expected (keyword used)
- LUA1001: Identifier expected
- LUA1002: Semicolon expected
- LUA1003: Close parenthesis expected
- LUA1004: Left brace expected
- LUA1005: Right brace expected
- LUA1006: Syntax error
- LUA1010: Expression expected
- LUA1012: Invalid statement
The exact diagnostic IDs are defined in the
ErrorCode enum in Loretta’s source code. Version-specific diagnostics are also available for features not supported in certain Lua versions.Additional Properties
AdditionalLocations
Other locations related to the diagnostic, typically referencing related items mentioned in the message.DefaultSeverity
The default severity from the diagnostic’s descriptor (may differ from effective severity).IsWarningAsError
Indicates whether this is a warning being treated as an error.IsSuppressed
Indicates whether the diagnostic has been suppressed.Creating Custom Diagnostics
You can create custom diagnostics using the staticCreate method:
See Also
- DiagnosticDescriptor - Metadata describing a diagnostic type
- Error Handling Guide - Best practices for handling diagnostics
- LuaSyntaxTree - Working with syntax trees