From 677a95a77c76b4b5761440a7de0f3435cdd5e875 Mon Sep 17 00:00:00 2001 From: GGG Date: Mon, 29 Jan 2024 18:08:28 -0300 Subject: [PATCH] Fix handling of nodes without any components. --- .../Sample.Internal.g.cs | 2 +- .../Sample.Main.g.cs | 2 +- .../Sample.Red.g.cs | 3 -- .../sourcegen/RedTreeGenerator.cs | 15 ++++--- .../sourcegen/VisitorGenerator.cs | 43 +++++++++++-------- 5 files changed, 36 insertions(+), 29 deletions(-) diff --git a/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Internal.g.cs b/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Internal.g.cs index 6c0f058..142d011 100644 --- a/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Internal.g.cs +++ b/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Internal.g.cs @@ -597,7 +597,7 @@ protected override void DefaultVisit(global::Tsu.Trees.RedGreen.Sample.Internal. public partial class SampleRewriter : Tsu.Trees.RedGreen.Sample.SampleVisitor { public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitSemicolonToken(global::Tsu.Trees.RedGreen.Sample.SemicolonTokenSample node) => - node.Update(); + node; public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitAssignmentStatement(global::Tsu.Trees.RedGreen.Sample.AssignmentStatement node) => node.Update((global::Tsu.Trees.RedGreen.Sample.SemicolonTokenSample?)Visit(node.Semicolon) ?? throw new global::System.InvalidOperationException("Semicolon cannot be null."), (global::Tsu.Trees.RedGreen.Sample.IdentifierExpressionSample?)Visit(node.Identifier) ?? throw new global::System.InvalidOperationException("Identifier cannot be null."), (global::Tsu.Trees.RedGreen.Sample.ExpressionSample?)Visit(node.Value) ?? throw new global::System.InvalidOperationException("Value cannot be null.")); public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitExpressionStatement(global::Tsu.Trees.RedGreen.Sample.ExpressionStatementSample node) => diff --git a/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Main.g.cs b/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Main.g.cs index 3d17c3c..ee40b99 100644 --- a/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Main.g.cs +++ b/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Main.g.cs @@ -117,7 +117,7 @@ protected override void DefaultVisit(global::Tsu.Trees.RedGreen.Sample.SampleNod public partial class SampleRewriter : Tsu.Trees.RedGreen.Sample.SampleVisitor { public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitSemicolonToken(global::Tsu.Trees.RedGreen.Sample.SemicolonTokenSample node) => - node.Update(); + node; public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitAssignmentStatement(global::Tsu.Trees.RedGreen.Sample.AssignmentStatement node) => node.Update((global::Tsu.Trees.RedGreen.Sample.SemicolonTokenSample?)Visit(node.Semicolon) ?? throw new global::System.InvalidOperationException("Semicolon cannot be null."), (global::Tsu.Trees.RedGreen.Sample.IdentifierExpressionSample?)Visit(node.Identifier) ?? throw new global::System.InvalidOperationException("Identifier cannot be null."), (global::Tsu.Trees.RedGreen.Sample.ExpressionSample?)Visit(node.Value) ?? throw new global::System.InvalidOperationException("Value cannot be null.")); public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitExpressionStatement(global::Tsu.Trees.RedGreen.Sample.ExpressionStatementSample node) => diff --git a/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Red.g.cs b/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Red.g.cs index fc68813..79e1315 100644 --- a/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Red.g.cs +++ b/Tsu.Trees.RedGreen/sample/Generated/Tsu.Trees.RedGreen.SourceGenerator/Tsu.Trees.RedGreen.SourceGenerator.Generator/Sample.Red.g.cs @@ -151,7 +151,6 @@ public bool Contains(global::Tsu.Trees.RedGreen.Sample.SampleNode other) public abstract partial class ExpressionSample : global::Tsu.Trees.RedGreen.Sample.SampleNode { - internal ExpressionSample(global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode green, global::Tsu.Trees.RedGreen.Sample.SampleNode? parent) : base(green, parent) { @@ -339,8 +338,6 @@ internal IdentifierExpressionSample(global::Tsu.Trees.RedGreen.Sample.Internal.G public abstract partial class StatementSample : global::Tsu.Trees.RedGreen.Sample.SampleNode { - private global::Tsu.Trees.RedGreen.Sample.SemicolonTokenSample? _semicolon; - internal StatementSample(global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode green, global::Tsu.Trees.RedGreen.Sample.SampleNode? parent) : base(green, parent) { diff --git a/Tsu.Trees.RedGreen/sourcegen/RedTreeGenerator.cs b/Tsu.Trees.RedGreen/sourcegen/RedTreeGenerator.cs index bb8e16c..7275bf5 100644 --- a/Tsu.Trees.RedGreen/sourcegen/RedTreeGenerator.cs +++ b/Tsu.Trees.RedGreen/sourcegen/RedTreeGenerator.cs @@ -333,14 +333,17 @@ private static void WriteRedNode(this IndentedTextWriter writer, Tree tree, Node writer.WriteLine('{'); writer.Indent++; { - foreach (var child in node.Children) + if (node.Descendants.Length == 0) { - writer.WriteLine("private {0}.{1}? {2};", - tree.RedBase.ContainingNamespace.ToCSharpString(false), - child.Type.Name, - child.FieldName); + foreach (var child in node.Children) + { + writer.WriteLine("private {0}.{1}? {2};", + tree.RedBase.ContainingNamespace.ToCSharpString(false), + child.Type.Name, + child.FieldName); + } + writer.WriteLineNoTabs(""); } - writer.WriteLineNoTabs(""); writer.WriteLines($$""" internal {{node.TypeSymbol.Name}}({{tree.GreenBase.ToCSharpString()}} green, {{tree.RedBase.ToCSharpString()}}? parent) diff --git a/Tsu.Trees.RedGreen/sourcegen/VisitorGenerator.cs b/Tsu.Trees.RedGreen/sourcegen/VisitorGenerator.cs index c343af1..4170048 100644 --- a/Tsu.Trees.RedGreen/sourcegen/VisitorGenerator.cs +++ b/Tsu.Trees.RedGreen/sourcegen/VisitorGenerator.cs @@ -254,30 +254,37 @@ public static void WriteRewriter(this IndentedTextWriter writer, Tree tree, INam node.TypeSymbol.Name); writer.Indent++; { - writer.Write("node.Update("); - var first = true; - foreach (var component in node.RequiredComponents) + if (!node.RequiredComponents.Any()) { - if (!first) writer.Write(", "); - first = false; - - if (component.Type.DerivesFrom(tree.GreenBase)) + writer.WriteLine("node;"); + } + else + { + writer.Write("node.Update("); + var first = true; + foreach (var component in node.RequiredComponents) { - writer.Write("({0}.{1}?)Visit(node.{2})", - baseType.ContainingNamespace.ToCSharpString(false), - component.Type.Name, - component.PropertyName); - if (!component.IsOptional) + if (!first) writer.Write(", "); + first = false; + + if (component.Type.DerivesFrom(tree.GreenBase)) { - writer.Write(" ?? throw new global::System.InvalidOperationException(\"{0} cannot be null.\")", component.PropertyName); + writer.Write("({0}.{1}?)Visit(node.{2})", + baseType.ContainingNamespace.ToCSharpString(false), + component.Type.Name, + component.PropertyName); + if (!component.IsOptional) + { + writer.Write(" ?? throw new global::System.InvalidOperationException(\"{0} cannot be null.\")", component.PropertyName); + } + } + else + { + writer.Write("node.{0}", component.PropertyName); } } - else - { - writer.Write("node.{0}", component.PropertyName); - } + writer.WriteLine(");"); } - writer.WriteLine(");"); } writer.Indent--; }