Skip to content

Commit

Permalink
Cleanup and migration of verbatim code files to resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
GGG-KILLER committed Feb 1, 2024
1 parent a74a21d commit f276e0d
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
/// 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
[global::System.AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class GreenNodeAttribute(params object[] kinds) : global::System.Attribute
{
/// <summary>
/// This node's Kind.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/// <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
[global::System.AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class GreenTreeRootAttribute(Type redBase, string suffix, Type kindEnum) : global::System.Attribute
{
/// <summary>
/// The suffix for nodes in this tree.
Expand All @@ -14,12 +14,12 @@ internal sealed class GreenTreeRootAttribute(Type redBase, string suffix, Type k
/// <summary>
/// The base node type for all nodes in the red tree.
/// </summary>
public Type RedBase { get; } = redBase;
public global::System.Type RedBase { get; } = redBase;

/// <summary>
/// The enum type that contains the definitions for the node kinds.
/// </summary>
public Type KindEnum { get; } = kindEnum;
public global::System.Type KindEnum { get; } = kindEnum;

/// <summary>
/// Whether to create base visitor implementations for this tree.
Expand All @@ -36,6 +36,12 @@ internal sealed class GreenTreeRootAttribute(Type redBase, string suffix, Type k
/// </summary>
public bool CreateRewriter { get; set; }

/// <summary>
/// Whether to generate list types and infrastructure for this tree.
/// This implies in having roslyn MIT-licensed code being added to your project.
/// </summary>
public bool CreateLists { get; set; }

/// <summary>
/// Whether to generate a file with only comments dumping the entire tree structure.
/// </summary>
Expand Down
97 changes: 0 additions & 97 deletions Tsu.Trees.RedGreen/src/Code.cs

This file was deleted.

11 changes: 5 additions & 6 deletions Tsu.Trees.RedGreen/src/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@ public sealed class Generator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterResourcesToCopy([
"GreenTreeRootAttribute.cs",
"GreenNodeAttribute.cs",
]);

var roots = context.GetTreeInfos();
var nodes = context.GetNodeInfos();

var trees = TreeCreator.BuildTree(roots, nodes);

context.RegisterPostInitializationOutput(ctx =>
{
ctx.AddSource("GreenNodeAttribute.g.cs", Code.GreenNodeAttributeCode);
ctx.AddSource("GreenTreeRootAttribute.g.cs", Code.GreenTreeRootAttributeCode);
});

context.RegisterSourceOutput(trees, (ctx, tree) =>
{
if (!tree.DebugDump)
Expand Down
30 changes: 30 additions & 0 deletions Tsu.Trees.RedGreen/src/ResourceCopier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Reflection;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace Tsu.Trees.RedGreen.SourceGenerator;

internal static class ResourceCopier
{
private static readonly Assembly _assembly = typeof(TemplateGenerator).Assembly;

public static void RegisterResourcesToCopy(this IncrementalGeneratorInitializationContext context, IEnumerable<string> paths)
{
context.RegisterPostInitializationOutput(ctx =>
{
foreach (var path in paths)
{
SourceText sourceText;

using (var stream = _assembly.GetManifestResourceStream(path))
using (var reader = new StreamReader(stream))
{
sourceText = SourceText.From(reader, (int) stream.Length, Encoding.UTF8);
}

ctx.AddSource(path, sourceText);
}
});
}
}
17 changes: 17 additions & 0 deletions Tsu.Trees.RedGreen/src/Templates/GreenListAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Tsu.Trees.RedGreen;

/// <summary>
/// Marks this struct as a green list base.
/// </summary>
/// <param name="kinds">
/// The kind that all lists will have.
/// This should be the value on the enum (e.g.: SyntaxKind.List)
/// </param>
[global::System.AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
internal sealed class GreenListAttribute(object kind) : global::System.Attribute
{
/// <summary>
/// The List Kind.
/// </summary>
public object Kind { get; } = kind;
}
17 changes: 17 additions & 0 deletions Tsu.Trees.RedGreen/src/Templates/GreenNodeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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>
[global::System.AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class GreenNodeAttribute(params object[] kinds) : global::System.Attribute
{
/// <summary>
/// This node's Kind.
/// </summary>
public object[] Kinds { get; } = kinds;
}
49 changes: 49 additions & 0 deletions Tsu.Trees.RedGreen/src/Templates/GreenTreeRootAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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>
[global::System.AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class GreenTreeRootAttribute(Type redBase, string suffix, Type kindEnum) : global::System.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 global::System.Type RedBase { get; } = redBase;

/// <summary>
/// The enum type that contains the definitions for the node kinds.
/// </summary>
public global::System.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 list types and infrastructure for this tree.
/// This implies in having roslyn MIT-licensed code being added to your project.
/// </summary>
public bool CreateLists { get; set; }

/// <summary>
/// Whether to generate a file with only comments dumping the entire tree structure.
/// </summary>
public bool DebugDump { get; set; }
}
5 changes: 5 additions & 0 deletions Tsu.Trees.RedGreen/src/Tsu.Trees.RedGreen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
</PropertyGroup>

<ItemGroup>
<Compile Remove="Templates/**/*.cs" />
<EmbeddedResource Include="Templates/GreenTreeRootAttribute.cs" Visible="true" LogicalName="GreenTreeRootAttribute.cs" />
<EmbeddedResource Include="Templates/GreenNodeAttribute.cs" Visible="true" LogicalName="GreenNodeAttribute.cs" />
<EmbeddedResource Include="Templates/GreenListAttribute.cs" Visible="true" LogicalName="GreenListAttribute.cs" />

<EmbeddedResource Include="Templates/Internal/GreenRoot.sbn-cs" Visible="true" LogicalName="Internal/GreenRoot.sbn-cs" />
<EmbeddedResource Include="Templates/Internal/GreenNodes.sbn-cs" Visible="true" LogicalName="Internal/GreenNodes.sbn-cs" />
<EmbeddedResource Include="Templates/Internal/GreenWalker.sbn-cs" Visible="true" LogicalName="Internal/GreenWalker.sbn-cs" />
Expand Down
68 changes: 0 additions & 68 deletions Tsu.Trees.RedGreen/src/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,74 +78,6 @@ public static string ToCSharpString(this INamespaceSymbol symbol, bool noGlobal
return noGlobal ? str.Substring("global::".Length) : str;
}

public static void WriteLines(this IndentedTextWriter writer, string text)
{
var initialIndent = writer.Indent;
var lines = text.Split('\n');
foreach (var l in lines)
{
var line = l.TrimEnd();
if (line.Length == 0)
{
writer.WriteLineNoTabs("");
}
else
{
var indents = countIndents(line, 4);
if (writer.Indent != initialIndent + indents)
writer.Indent = initialIndent + indents;
writer.WriteLine(line.TrimStart());
}
}

static int countIndents(string str, int spaces)
{
var n = 0;
for (var idx = 0; idx < str.Length; idx++)
{
if (str[idx] == ' ')
n++;
else
break;
}
return n / spaces;
}
}

public static void WriteLines(this IndentedTextWriter writer, string text, params object[] args)
{
var initialIndent = writer.Indent;
var lines = text.Split('\n');
foreach (var l in lines)
{
var line = l.TrimEnd();
if (line.Length == 0)
{
writer.WriteLineNoTabs("");
}
else
{
var indents = countIndents(line, 4);
if (writer.Indent != initialIndent + indents)
writer.Indent = initialIndent + indents;
writer.WriteLine(line.TrimStart(), args);
}
}

static int countIndents(string str, int spaces)
{
var n = 0;
for (var idx = 0; idx < str.Length; idx++)
{
if (str[idx] == ' ')
n++;
else
break;
}
return n / spaces;
}
}

public static SourceText ToSourceText(this StringBuilder builder) =>
SourceText.From(new StringBuilderReader(builder), builder.Length, Encoding.UTF8);
}

0 comments on commit f276e0d

Please sign in to comment.