Tesearis.HlslParser 1.0.0

dotnet add package Tesearis.HlslParser --version 1.0.0
                    
NuGet\Install-Package Tesearis.HlslParser -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tesearis.HlslParser" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tesearis.HlslParser" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Tesearis.HlslParser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Tesearis.HlslParser --version 1.0.0
                    
#r "nuget: Tesearis.HlslParser, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Tesearis.HlslParser@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Tesearis.HlslParser&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Tesearis.HlslParser&version=1.0.0
                    
Install as a Cake Tool

Tesearis.HlslParser

Standalone HLSL/Cg lexer, preprocessor and syntax tree for shader source.

Tesearis.HlslParser parses HLSL/Cg source, either a whole standalone file (.hlsl, .cginc,.compute) or a region embedded inside a document, into an immutable, strongly-typed AST for static analysis and diagnostics tooling.

Features

  • Zero dependencies: no runtime dependencies at all.
  • Standalone and embeddable: parses a whole .hlsl/.cginc/.compute file directly, or a substring embedded inside a document.
  • Full HLSL/Cg grammar: structs, buffers, resources, functions and full statement/expression parsing inside function bodies, including brace-initializer lists (float3 v = {1, 2, 3};, with nesting for matrix/array/struct initializers).
  • Real preprocessor: genuine #define/#undef macro expansion and #if/#ifdef/#ifndef/#elif/#else/#endif conditional evaluation, since both are self-contained within a single file's text.
  • Best-effort recovery: never throws on malformed source; it returns a (possibly partial) tree plus a full diagnostics list. That makes it suitable for live analysis over source that's routinely mid-edit, in addition to normal batch/CI parsing.
  • Targeted: netstandard2.0 (for use in the Unity Editor) and net8.0.

What this library does not do

  • It never reads files from disk.
  • It never resolves #include targets.
  • It doesn't recognize casts to user-defined type names (only built-in ones).

Usage

Parsing a standalone file

using Tesearis.HlslParser.Parsing;
using Tesearis.HlslParser.Syntax;

HlslParseResult result = Hlsl.Parse(sourceText, "shader.hlsl");

if (result.HasErrors)
{
    foreach (var diagnostic in result.Diagnostics)
    {
        Console.WriteLine(diagnostic);
    }
}

var compilationUnit = (CompilationUnitNode)result.Root;
foreach (var declaration in compilationUnit.Declarations)
{
    Console.WriteLine(declaration.Kind);
}

Hlsl.Parse never throws on malformed input — it always returns a (possibly partial) tree in result.Root alongside result.Diagnostics.

Parsing an embedded region

Use Hlsl.ParseEmbedded when the HLSL/Cg source is a substring of a larger document (e.g. a Unity ShaderLab HLSLPROGRAM block). Spans and diagnostics are reported in terms of the outer document, using the offset/line where the embedded block starts:

HlslParseResult result = Hlsl.ParseEmbedded(
    body: hlslBlockText,
    baseOffset: hlslBlockStartOffset,
    fileName: "MyShader.shader",
    baseLine: hlslBlockStartLine);

Walking the syntax tree

For quick queries, use LINQ over DescendantsAndSelf():

using System.Linq;

var functions = result.Root.DescendantsAndSelf().OfType<FunctionDeclarationNode>();

For more structured traversal, subclass HlslVisitor and override the node kinds you care about:

class FunctionCollector : HlslVisitor
{
    public List<FunctionDeclarationNode> Functions { get; } = new();

    public override void VisitFunctionDeclaration(FunctionDeclarationNode node)
    {
        Functions.Add(node);
        base.DefaultVisit(node);
    }
}

var collector = new FunctionCollector();
collector.Visit(result.Root);

To inspect the whole tree at a glance, HlslTreeDumper.Dump renders it as indented text:

string dump = HlslTreeDumper.Dump(result.Root, result.Source);

Diagnostics

using Tesearis.HlslParser.Diagnostics;

foreach (Diagnostic diagnostic in result.Diagnostics)
{
    // diagnostic.Severity, diagnostic.Id, diagnostic.Message, diagnostic.Span
    Console.WriteLine(diagnostic); // "shader.hlsl(3,10): error HL0203: ..."
}

Building & testing

dotnet build Tesearis.HlslParser.slnx
dotnet test Tesearis.HlslParser.slnx

License

MIT — see LICENSE.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 104 8/21/2026