Skip to content

Commit

Permalink
Fix handling of nodes without any components.
Browse files Browse the repository at this point in the history
  • Loading branch information
GGG-KILLER committed Jan 29, 2024
1 parent 435abbb commit 677a95a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ protected override void DefaultVisit(global::Tsu.Trees.RedGreen.Sample.Internal.
public partial class SampleRewriter : Tsu.Trees.RedGreen.Sample.SampleVisitor<global::Tsu.Trees.RedGreen.Sample.SampleNode>
{
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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected override void DefaultVisit(global::Tsu.Trees.RedGreen.Sample.SampleNod
public partial class SampleRewriter : Tsu.Trees.RedGreen.Sample.SampleVisitor<global::Tsu.Trees.RedGreen.Sample.SampleNode>
{
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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
15 changes: 9 additions & 6 deletions Tsu.Trees.RedGreen/sourcegen/RedTreeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 25 additions & 18 deletions Tsu.Trees.RedGreen/sourcegen/VisitorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
}
Expand Down

0 comments on commit 677a95a

Please sign in to comment.