LuaSyntaxTree represents a parsed Lua source file. It provides access to the root syntax node, diagnostics, and parsing options.
Static Methods
ParseText
Parses Lua source text into a syntax tree.
From String
From SourceText
SyntaxTree tree = LuaSyntaxTree . ParseText (
"local x = 1" ,
options : new LuaParseOptions ( LuaSyntaxOptions . Lua54 ),
path : "script.lua"
);
text
string | SourceText
required
The Lua source code to parse
options
LuaParseOptions
default: "null"
Parse options specifying the Lua syntax variant and other settings. Defaults to LuaParseOptions.Default (Lua 5.4 with all features).
The file path for diagnostic reporting
(String overload only) The text encoding. Required for debuggability.
cancellationToken
CancellationToken
default: "default"
Cancellation token for parsing
A syntax tree representing the parsed source code
Create
Creates a syntax tree from an existing root node.
var root = SyntaxFactory . CompilationUnit (
SyntaxFactory . StatementList (),
SyntaxFactory . Token ( SyntaxKind . EndOfFileToken )
);
var tree = LuaSyntaxTree . Create (
root ,
options : new LuaParseOptions ( LuaSyntaxOptions . Lua54 ),
path : "generated.lua" ,
encoding : Encoding . UTF8
);
The root node of the syntax tree
options
LuaParseOptions
default: "null"
Parse options to associate with the tree
The file path for the tree
Instance Methods
GetRoot
Gets the root syntax node of the tree.
var tree = LuaSyntaxTree . ParseText ( "local x = 1" );
LuaSyntaxNode root = tree . GetRoot ();
cancellationToken
CancellationToken
default: "default"
Cancellation token
The root node of the syntax tree
TryGetRoot
Attempts to get the root node if it’s already available without blocking.
if ( tree . TryGetRoot ( out var root ))
{
// Process root immediately
}
Receives the root node if available
true if the root was available; otherwise false
GetCompilationUnitRoot
Gets the root node typed as CompilationUnitSyntax.
CompilationUnitSyntax compilationUnit = tree . GetCompilationUnitRoot ();
var statements = compilationUnit . Statements . Statements ;
cancellationToken
CancellationToken
default: "default"
Cancellation token
The root node as a compilation unit
This method throws InvalidCastException if the tree’s root is not a compilation unit. Always ensure HasCompilationUnitRoot is true before calling this method.
GetDiagnostics
Gets all syntax diagnostics (errors and warnings) in the tree.
var tree = LuaSyntaxTree . ParseText ( "local x = " );
var diagnostics = tree . GetDiagnostics ();
foreach ( var diagnostic in diagnostics )
{
Console . WriteLine ( $" { diagnostic . Location } : { diagnostic . GetMessage ()} " );
}
cancellationToken
CancellationToken
default: "default"
Cancellation token
All diagnostics in the syntax tree
This method does not filter diagnostics based on #pragma directives or compiler options.
Properties
Options
public LuaParseOptions Options { get ; }
The parse options used to create this syntax tree.
FilePath
public string FilePath { get ; }
The path of the file this tree represents. May be empty if not specified.
Text
public SourceText Text { get ; }
The source text of the tree. Use TryGetText to check availability without blocking.
Example Usage
using Loretta . CodeAnalysis ;
using Loretta . CodeAnalysis . Lua ;
using Loretta . CodeAnalysis . Lua . Syntax ;
using Loretta . CodeAnalysis . Text ;
var sourceCode = @"
local function greet(name)
print('Hello, ' .. name)
end
greet('World')
" ;
// Parse with Lua 5.4 syntax
var options = new LuaParseOptions ( LuaSyntaxOptions . Lua54 );
var tree = LuaSyntaxTree . ParseText (
sourceCode ,
options ,
path : "example.lua" ,
encoding : Encoding . UTF8
);
// Get the root and check for errors
var root = tree . GetCompilationUnitRoot ();
var diagnostics = tree . GetDiagnostics ();
if ( ! diagnostics . Any ())
{
Console . WriteLine ( "Parse successful!" );
Console . WriteLine ( $"Statements: { root . Statements . Statements . Count } " );
}
else
{
Console . WriteLine ( "Parse errors:" );
foreach ( var diagnostic in diagnostics )
{
Console . WriteLine ( $" { diagnostic } " );
}
}
See Also