An open API service indexing awesome lists of open source software.

https://github.com/dcronqvist/inspectree

🔎 A .NET library for inspecting entire syntax trees of methods in C#
https://github.com/dcronqvist/inspectree

csharp roslyn source-generator

Last synced: over 1 year ago
JSON representation

🔎 A .NET library for inspecting entire syntax trees of methods in C#

Awesome Lists containing this project

README

          

# 🔎 InspecTree

InspecTree is a library for inspecting entire syntax trees of lambda functions in C#. [`Expression`](https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression-1) is commonly used for this purpose, but it is limited to single expressions unless you manually construct a [`BlockExpression`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.expressions.blockexpression). InspecTree allows you to inspect entire syntax trees of lambda functions, including entire statement bodies.

## Quick example

This library was initially created with the intention of transpiling C# code to GLSL. Here's a small example of how to access the syntax tree of a lambda function to transpile it to GLSL.

```csharp
// Partial required due to source generator
// creating an overload in the same class
public partial class Program
{
public static void Main(string[] args)
{
var glslCode = TranspileToGLSL(() =>
{
var x = 2 + 5f;
var color = new Vector4(1.0f);

if (x > 4)
{
color = new Vector4(0.2f, 0.3f, x, 1.0f);
}

return color;
});
}

// Overload of this method is generated by source generator that
// accepts a Func, and therefore accepts a lambda function
public static string TranspileToGLSL(InspecTree> shader)
{
var transpiler = new GLSLTranspiler();
return transpiler.Transpile(shader.SyntaxTree);
}
}
```

If you want a more complete example of where this is done, check out the [InspecTree.Example](src/InspecTree.Example/Program.cs) project.

## How it works

InspecTree uses two source generators to make it possible to access both the syntax tree and semantic model of a lambda.

- For each method that accepts an `InspecTree` parameter, it generates an overload that accepts a `Func` parameter instead. This allows you to pass a lambda function to your method since there is no way to implicitly convert a lambda to a user-defined type.

- For each invocation to one of the generated overloads, it generates an interceptor method that will be invoked instead. This interceptor method will create an `InspecTree>` object from the `Func` parameter and store the lambda function's syntax tree and semantic model.