Skip to main content
LuaParseOptions configures how the Lua parser processes source code, including which syntax features to accept and how to handle documentation.

Constructor

Creates a new instance of LuaParseOptions.
var options = new LuaParseOptions(LuaSyntaxOptions.Lua54);
syntaxOptions
LuaSyntaxOptions
required
The syntax options defining which Lua features to accept. See LuaSyntaxOptions for available presets.

Properties

SyntaxOptions

public LuaSyntaxOptions SyntaxOptions { get; }
The syntax options that control which language features the parser accepts.
var options = new LuaParseOptions(LuaSyntaxOptions.Luau);
Console.WriteLine(options.SyntaxOptions.AcceptTypedLua); // True for Luau

DocumentationMode

public DocumentationMode DocumentationMode { get; }
This property is currently not functional. Documentation mode is reserved for future use and has no effect on parsing.

Methods

WithSyntaxOptions

Creates a new LuaParseOptions instance with different syntax options.
var lua51Options = new LuaParseOptions(LuaSyntaxOptions.Lua51);
var lua54Options = lua51Options.WithSyntaxOptions(LuaSyntaxOptions.Lua54);
syntaxOptions
LuaSyntaxOptions
required
The new syntax options to use
return
LuaParseOptions
A new options instance with the specified syntax options, or the same instance if the syntax options are identical

WithDocumentationMode

Creates a new LuaParseOptions instance with a different documentation mode.
This method is currently not functional. It’s provided for API compatibility but has no effect.
var options = parseOptions.WithDocumentationMode(DocumentationMode.Parse);
documentationMode
DocumentationMode
The documentation mode (currently has no effect)
return
LuaParseOptions
A new options instance

WithFeatures

Creates a new LuaParseOptions instance with different feature flags.
This method is currently not functional. Feature flags are reserved for future use and have no effect on parsing.
var features = new Dictionary<string, string>
{
    ["feature1"] = "enabled"
};
var options = parseOptions.WithFeatures(features);
features
IEnumerable<KeyValuePair<string, string>>
Feature flags (currently has no effect)
return
LuaParseOptions
A new options instance

Default Options

LuaParseOptions.Default provides sensible defaults:
public static LuaParseOptions Default { get; }
  • SyntaxOptions: LuaSyntaxOptions.All (accepts all Lua syntax features)
  • DocumentationMode: DocumentationMode.Parse
// These are equivalent
var tree1 = LuaSyntaxTree.ParseText(code);
var tree2 = LuaSyntaxTree.ParseText(code, LuaParseOptions.Default);

Example Usage

Parsing Lua 5.1 Code

var lua51Code = @"
local t = {1, 2, 3}
for i, v in ipairs(t) do
    print(i, v)
end
";

var options = new LuaParseOptions(LuaSyntaxOptions.Lua51);
var tree = LuaSyntaxTree.ParseText(lua51Code, options);

Parsing Luau with Type Annotations

var luauCode = @"
type Point = { x: number, y: number }

local function distance(p1: Point, p2: Point): number
    local dx = p2.x - p1.x
    local dy = p2.y - p1.y
    return math.sqrt(dx * dx + dy * dy)
end
";

var options = new LuaParseOptions(LuaSyntaxOptions.Luau);
var tree = LuaSyntaxTree.ParseText(luauCode, options);

Parsing LuaJIT Code

var jitCode = @"
local ffi = require('ffi')
local bit = require('bit')

local x = 0xDEADBEEFULL
local y = bit.bor(0x01, 0x02)
";

var options = new LuaParseOptions(LuaSyntaxOptions.LuaJIT21);
var tree = LuaSyntaxTree.ParseText(jitCode, options);

Custom Syntax Options

// Start with Lua 5.3 and enable additional features
var customSyntax = LuaSyntaxOptions.Lua53.With(
    acceptCCommentSyntax: true,  // Enable C-style comments
    continueType: ContinueType.Keyword  // Enable continue keyword
);

var options = new LuaParseOptions(customSyntax);
var tree = LuaSyntaxTree.ParseText(code, options);

Switching Between Lua Versions

var code = File.ReadAllText("script.lua");

// Try parsing as Lua 5.4 first
var lua54Options = new LuaParseOptions(LuaSyntaxOptions.Lua54);
var tree = LuaSyntaxTree.ParseText(code, lua54Options);

if (tree.GetDiagnostics().Any())
{
    // Fall back to Lua 5.1 if there are errors
    var lua51Options = new LuaParseOptions(LuaSyntaxOptions.Lua51);
    tree = LuaSyntaxTree.ParseText(code, lua51Options);
}

Immutability

LuaParseOptions is immutable. All With* methods return a new instance rather than modifying the existing one:
var options1 = new LuaParseOptions(LuaSyntaxOptions.Lua51);
var options2 = options1.WithSyntaxOptions(LuaSyntaxOptions.Lua54);

Console.WriteLine(ReferenceEquals(options1, options2)); // False
Console.WriteLine(options1.SyntaxOptions == LuaSyntaxOptions.Lua51); // True
Console.WriteLine(options2.SyntaxOptions == LuaSyntaxOptions.Lua54); // True

See Also

Build docs developers (and LLMs) love