Skip to content

Commit

Permalink
Remove the library and move attributes into generator.
Browse files Browse the repository at this point in the history
And a few other changes:
- Make dumping the tree optional as it's not necessary;
- Remove dependency on external dependencies for the generator.
  • Loading branch information
GGG-KILLER committed Jan 29, 2024
1 parent 677a95a commit cf98c70
Show file tree
Hide file tree
Showing 30 changed files with 565 additions and 482 deletions.
8 changes: 1 addition & 7 deletions Tsu.Trees.RedGreen/Tsu.Trees.RedGreen.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tsu.Trees.RedGreen.Sample", "sample\Tsu.Trees.RedGreen.Sample.csproj", "{D00F090D-145E-462E-BE75-F2805ADB572A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tsu.Trees.RedGreen.SourceGenerator", "sourcegen\Tsu.Trees.RedGreen.SourceGenerator.csproj", "{A0A6DBE6-9899-42C6-A3EC-90153F844109}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tsu.Trees.RedGreen", "src\Tsu.Trees.RedGreen.csproj", "{ABE13963-6F78-47DF-A652-303BD6565A93}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tsu.Trees.RedGreen", "src\Tsu.Trees.RedGreen.csproj", "{A0A6DBE6-9899-42C6-A3EC-90153F844109}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -26,9 +24,5 @@ Global
{A0A6DBE6-9899-42C6-A3EC-90153F844109}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0A6DBE6-9899-42C6-A3EC-90153F844109}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0A6DBE6-9899-42C6-A3EC-90153F844109}.Release|Any CPU.Build.0 = Release|Any CPU
{ABE13963-6F78-47DF-A652-303BD6565A93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ABE13963-6F78-47DF-A652-303BD6565A93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABE13963-6F78-47DF-A652-303BD6565A93}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABE13963-6F78-47DF-A652-303BD6565A93}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Tsu.Trees.RedGreen;
namespace Tsu.Trees.RedGreen;

/// <summary>
/// Marks this class as a green node.
Expand All @@ -8,7 +8,7 @@ namespace Tsu.Trees.RedGreen;
/// This should be the value on the enum (e.g.: SyntaxKind.ClassDeclarationSyntax)
/// </param>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class GreenNodeAttribute(params object[] kinds) : Attribute
internal sealed class GreenNodeAttribute(params object[] kinds) : Attribute
{
/// <summary>
/// This node's Kind.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace Tsu.Trees.RedGreen;
namespace Tsu.Trees.RedGreen;

/// <summary>
/// An attribute that marks the given class as the base class for all nodes in a green node tree.
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class GreenTreeRootAttribute(Type redBase, string suffix, Type kindEnum) : Attribute
internal sealed class GreenTreeRootAttribute(Type redBase, string suffix, Type kindEnum) : Attribute
{
/// <summary>
/// The suffix for nodes in this tree.
Expand Down Expand Up @@ -36,14 +36,8 @@ public sealed class GreenTreeRootAttribute(Type redBase, string suffix, Type kin
/// </summary>
public bool CreateRewriter { get; set; }

// NOTE: Make cache configurable?
// /// <summary>
// /// Whether to enable the green cache or not.
// /// </summary>
// public bool CacheEnabled { get; set; } = true;

// /// <summary>
// /// The amount of bits from the hash to use for caching.
// /// </summary>
// public int CacheSizeBits { get; set; } = 16;
/// <summary>
/// Whether to generate a file with only comments dumping the entire tree structure.
/// </summary>
public bool DebugDump { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Tsu.Trees.RedGreen.Sample.Internal
{
abstract partial class GreenNode : global::Tsu.Trees.RedGreen.Internal.IGreenNode<global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode, global::Tsu.Trees.RedGreen.Sample.SampleNode, global::Tsu.Trees.RedGreen.Sample.SampleKind>
abstract partial class GreenNode
{
private readonly global::Tsu.Trees.RedGreen.Sample.SampleKind _kind;
private byte _slotCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Tsu.Trees.RedGreen.Sample
{
abstract partial class SampleNode : global::Tsu.Trees.RedGreen.IRedNode<global::Tsu.Trees.RedGreen.Sample.SampleNode, global::Tsu.Trees.RedGreen.Sample.SampleKind>
abstract partial class SampleNode
{
private readonly global::Tsu.Trees.RedGreen.Sample.SampleNode? _parent;

Expand Down
3 changes: 1 addition & 2 deletions Tsu.Trees.RedGreen/sample/Tsu.Trees.RedGreen.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="$(RepositoryRootDirectory)Tsu.Trees.RedGreen/src/Tsu.Trees.RedGreen.csproj" />
<ProjectReference Include="$(RepositoryRootDirectory)Tsu.Trees.RedGreen/sourcegen/Tsu.Trees.RedGreen.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="$(RepositoryRootDirectory)Tsu.Trees.RedGreen/src/Tsu.Trees.RedGreen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
Expand Down

This file was deleted.

74 changes: 74 additions & 0 deletions Tsu.Trees.RedGreen/src/Code.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace Tsu.Trees.RedGreen.SourceGenerator;

internal static class Code
{
public const string GreenNodeAttributeName = "Tsu.Trees.RedGreen.GreenNodeAttribute";

public const string GreenNodeAttributeCode = """
namespace Tsu.Trees.RedGreen;
/// <summary>
/// Marks this class as a green node.
/// </summary>
/// <param name="kinds">
/// The kinds this node could have.
/// This should be the value on the enum (e.g.: SyntaxKind.ClassDeclarationSyntax)
/// </param>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class GreenNodeAttribute(params object[] kinds) : Attribute
{
/// <summary>
/// This node's Kind.
/// </summary>
public object[] Kinds { get; } = kinds;
}
""";

public const string GreenTreeRootAttributeName = "Tsu.Trees.RedGreen.GreenTreeRootAttribute";

public const string GreenTreeRootAttributeCode = """
namespace Tsu.Trees.RedGreen;
/// <summary>
/// An attribute that marks the given class as the base class for all nodes in a green node tree.
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class GreenTreeRootAttribute(Type redBase, string suffix, Type kindEnum) : Attribute
{
/// <summary>
/// The suffix for nodes in this tree.
/// </summary>
public string Suffix { get; } = suffix;
/// <summary>
/// The base node type for all nodes in the red tree.
/// </summary>
public Type RedBase { get; } = redBase;
/// <summary>
/// The enum type that contains the definitions for the node kinds.
/// </summary>
public Type KindEnum { get; } = kindEnum;
/// <summary>
/// Whether to create base visitor implementations for this tree.
/// </summary>
public bool CreateVisitors { get; set; }
/// <summary>
/// Whether to create a base walker implementation for this tree.
/// </summary>
public bool CreateWalker { get; set; }
/// <summary>
/// Whether to generate a base rewriter implementation for this tree.
/// </summary>
public bool CreateRewriter { get; set; }
/// <summary>
/// Whether to generate a file with only comments dumping the entire tree structure.
/// </summary>
public bool DebugDump { get; set; }
}
""";
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

context.RegisterPostInitializationOutput(ctx =>
{
ctx.AddSource("Initialized.g.cs", "// <auto-generated />");
ctx.AddSource("GreenNodeAttribute.g.cs", Code.GreenNodeAttributeCode);
ctx.AddSource("GreenTreeRootAttribute.g.cs", Code.GreenTreeRootAttributeCode);
});

context.RegisterSourceOutput(trees, (ctx, tree) =>
{
if (!tree.DebugDump)
return;

var builder = new StringBuilder();

builder.AppendLine($"// GreenBase = {tree.GreenBase.ToCSharpString()}");
Expand Down Expand Up @@ -70,7 +74,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
queue.Push((node.Item1 + 1, derived));
}

ctx.AddSource($"{tree.GreenBase.Name}.Debug.g.cs", builder.ToSourceText());
ctx.AddSource($"{tree.Suffix}.Debug.g.cs", builder.ToSourceText());
});

context.RegisterGreenOutput(trees);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ public static void RegisterGreenOutput(this IncrementalGeneratorInitializationCo

private static void WriteGreenRoot(this IndentedTextWriter writer, Tree tree, Node root)
{
writer.WriteLine("abstract partial class {0} : global::Tsu.Trees.RedGreen.Internal.IGreenNode<{1}, {2}, {3}>",
root.TypeSymbol.Name,
tree.GreenBase.ToCSharpString(false),
tree.RedBase.ToCSharpString(false),
tree.KindEnum.ToCSharpString(false));
writer.WriteLine("abstract partial class {0}", root.TypeSymbol.Name);
writer.WriteLine('{');
writer.Indent++;
{
Expand Down
Loading

0 comments on commit cf98c70

Please sign in to comment.