Skip to content

Commit

Permalink
Add tuple support
Browse files Browse the repository at this point in the history
  • Loading branch information
macsux committed Nov 4, 2024
1 parent 87ef2f6 commit b951a83
Show file tree
Hide file tree
Showing 20 changed files with 685 additions and 54 deletions.
21 changes: 21 additions & 0 deletions Rewrite/src/Rewrite.CSharp/CSharpPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ public CSharpPrinter()
return tupleExpression;
}

public override J? VisitTupleType(Cs.TupleType node, PrintOutputCapture<TState> p)
{
BeforeSyntax(node, CsSpace.Location.TUPLE_TYPE_PREFIX, p);
VisitContainer("(", node.Padding.Elements, CsContainer.Location.TUPLE_TYPE_ELEMENTS, ",", ")", p);
AfterSyntax(node, p);
return node;
}

public override J? VisitTupleElement(Cs.TupleElement node, PrintOutputCapture<TState> p)
{
BeforeSyntax(node, CsSpace.Location.TUPLE_ELEMENT_PREFIX, p);
Visit(node.Type, p);
if (node.Name != null)
{
Visit(node.Name, p);
}
AfterSyntax(node, p);
return node;
}

public override J? VisitParenthesizedVariableDesignation(Cs.ParenthesizedVariableDesignation node, PrintOutputCapture<TState> p)
{
BeforeSyntax(node, CsSpace.Location.NAMED_ARGUMENT_PREFIX, p);
Expand Down Expand Up @@ -677,6 +697,7 @@ private class CSharpJavaPrinter(CSharpPrinter<TState> _parent) : JavaPrinter<TSt
return base.Visit(tree, p);
}


public override J VisitNewArray(J.NewArray newArray, PrintOutputCapture<TState> p)
{
BeforeSyntax(newArray, Space.Location.NEW_ARRAY_PREFIX, p);
Expand Down
23 changes: 23 additions & 0 deletions Rewrite/src/Rewrite.CSharp/CSharpVisitor.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,29 @@ public override bool IsAcceptable(SourceFile sourceFile, P p)
return constructorInitializer;
}

public virtual J? VisitTupleType(Cs.TupleType tupleType, P p)
{
tupleType = tupleType.WithPrefix(VisitSpace(tupleType.Prefix, CsSpace.Location.TUPLE_TYPE_PREFIX, p)!);
var tempExpression = (Expression) VisitExpression(tupleType, p);
if (tempExpression is not Cs.TupleType)
{
return tempExpression;
}
tupleType = (Cs.TupleType) tempExpression;
tupleType = tupleType.WithMarkers(VisitMarkers(tupleType.Markers, p));
tupleType = tupleType.Padding.WithElements(VisitContainer(tupleType.Padding.Elements, CsContainer.Location.TUPLE_TYPE_ELEMENTS, p)!);
return tupleType;
}

public virtual J? VisitTupleElement(Cs.TupleElement tupleElement, P p)
{
tupleElement = tupleElement.WithPrefix(VisitSpace(tupleElement.Prefix, CsSpace.Location.TUPLE_ELEMENT_PREFIX, p)!);
tupleElement = tupleElement.WithMarkers(VisitMarkers(tupleElement.Markers, p));
tupleElement = tupleElement.WithType(VisitAndCast<TypeTree>(tupleElement.Type, p)!);
tupleElement = tupleElement.WithName(VisitAndCast<J.Identifier>(tupleElement.Name, p));
return tupleElement;
}

protected virtual JContainer<J2>? VisitContainer<J2>(JContainer<J2>? container, CsContainer.Location loc, P p) where J2 : J
{
if (container == null) {
Expand Down
37 changes: 22 additions & 15 deletions Rewrite/src/Rewrite.CSharp/Parser/CSharpParserVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,11 @@ private J.Identifier MapIdentifier(SyntaxToken identifier, JavaType? type)
}

public override J? VisitPredefinedType(PredefinedTypeSyntax node)
{
return MapTypeTree(node);
}

public TypeTree MapTypeTree(SyntaxNode node)
{
var type = MapType(node);
if (type is JavaType.Primitive)
Expand Down Expand Up @@ -1410,14 +1415,25 @@ private J.Identifier MapIdentifier(SyntaxToken identifier, JavaType? type)

public override J? VisitTupleType(TupleTypeSyntax node)
{
// This was added in C# 7.0
return base.VisitTupleType(node);
return new Cs.TupleType(
Core.Tree.RandomId(),
Format(Leading(node)),
Markers.EMPTY,
JContainer.Create(node.Elements.Select(x => JRightPadded.Create(Convert<Cs.TupleElement>(x)!, Format(Trailing(x)))).ToList()),
MapType(node)
);
}

public override J? VisitTupleElement(TupleElementSyntax node)
public override Cs VisitTupleElement(TupleElementSyntax node)
{
// This was added in C# 7.0
return base.VisitTupleElement(node);
var type = MapType(node.Type);
return new Cs.TupleElement(
Core.Tree.RandomId(),
Format(Leading(node)),
Markers.EMPTY,
MapTypeTree(node.Type),
MapIdentifier(node.Identifier, type));

}

public override J? VisitOmittedTypeArgument(OmittedTypeArgumentSyntax node)
Expand Down Expand Up @@ -1563,16 +1579,6 @@ private Enum MapPostfixUnaryOperator(SyntaxToken operatorToken)
return type;
}

// private Cs.Unary.Type MapPostfixUnaryOperatorToJ(SyntaxToken operatorToken)
// {
// J.Unary.Type? type = operatorToken.Kind() switch
// {
// SyntaxKind.PlusPlusToken => J.Unary.Type.PostIncrement,
// SyntaxKind.MinusMinusToken => J.Unary.Type.PostDecrement,
// _ => null
// };
// return type;
// }

public override J? VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
{
Expand Down Expand Up @@ -2523,6 +2529,7 @@ private JRightPadded<Statement> MapAnonymousObjectMember(AnonymousObjectMemberDe

public override J? VisitIsPatternExpression(IsPatternExpressionSyntax node)
{
// todo: handle ConstantPatternSyntax (such as "something is true")
if (node.Pattern is DeclarationPatternSyntax dp)
{
return new J.InstanceOf(
Expand Down
87 changes: 87 additions & 0 deletions Rewrite/src/Rewrite.CSharp/Tree/TupleElement.g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//------------------------------------------------------------------------------
// <auto-generated>
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
#pragma warning disable CS0108
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Rewrite.Core;
using Rewrite.Core.Marker;
using FileAttributes = Rewrite.Core.FileAttributes;
using Rewrite.RewriteJava.Tree;

namespace Rewrite.RewriteCSharp.Tree;

[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")]
[SuppressMessage("ReSharper", "InvertIf")]
[SuppressMessage("ReSharper", "RedundantExtendsListEntry")]
[SuppressMessage("ReSharper", "UnusedMember.Global")]
[SuppressMessage("ReSharper", "RedundantNameQualifier")]
public partial interface Cs : J
{
#if DEBUG_VISITOR
[DebuggerStepThrough]
#endif
public partial class TupleElement(
Guid id,
Space prefix,
Markers markers,
TypeTree type,
J.Identifier? name
) : Cs, MutableTree<TupleElement>
{
public J? AcceptCSharp<P>(CSharpVisitor<P> v, P p)
{
return v.VisitTupleElement(this, p);
}

public Guid Id => id;

public TupleElement WithId(Guid newId)
{
return newId == id ? this : new TupleElement(newId, prefix, markers, type, name);
}
public Space Prefix => prefix;

public TupleElement WithPrefix(Space newPrefix)
{
return newPrefix == prefix ? this : new TupleElement(id, newPrefix, markers, type, name);
}
public Markers Markers => markers;

public TupleElement WithMarkers(Markers newMarkers)
{
return ReferenceEquals(newMarkers, markers) ? this : new TupleElement(id, prefix, newMarkers, type, name);
}
public TypeTree Type => type;

public TupleElement WithType(TypeTree newType)
{
return ReferenceEquals(newType, type) ? this : new TupleElement(id, prefix, markers, newType, name);
}
public J.Identifier? Name => name;

public TupleElement WithName(J.Identifier? newName)
{
return ReferenceEquals(newName, name) ? this : new TupleElement(id, prefix, markers, type, newName);
}
#if DEBUG_VISITOR
[DebuggerStepThrough]
#endif
public bool Equals(Rewrite.Core.Tree? other)
{
return other is TupleElement && other.Id == Id;
}
#if DEBUG_VISITOR
[DebuggerStepThrough]
#endif
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
}
124 changes: 124 additions & 0 deletions Rewrite/src/Rewrite.CSharp/Tree/TupleType.g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//------------------------------------------------------------------------------
// <auto-generated>
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
#pragma warning disable CS0108
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Rewrite.Core;
using Rewrite.Core.Marker;
using FileAttributes = Rewrite.Core.FileAttributes;
using Rewrite.RewriteJava.Tree;

namespace Rewrite.RewriteCSharp.Tree;

[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "PossibleUnintendedReferenceComparison")]
[SuppressMessage("ReSharper", "InvertIf")]
[SuppressMessage("ReSharper", "RedundantExtendsListEntry")]
[SuppressMessage("ReSharper", "UnusedMember.Global")]
[SuppressMessage("ReSharper", "RedundantNameQualifier")]
public partial interface Cs : J
{
#if DEBUG_VISITOR
[DebuggerStepThrough]
#endif
public partial class TupleType(
Guid id,
Space prefix,
Markers markers,
JContainer<TupleElement> elements,
JavaType? type
) : Cs, TypeTree, Expression, MutableTree<TupleType>
{
[NonSerialized] private WeakReference<PaddingHelper>? _padding;

public PaddingHelper Padding
{
get
{
PaddingHelper? p;
if (_padding == null)
{
p = new PaddingHelper(this);
_padding = new WeakReference<PaddingHelper>(p);
}
else
{
_padding.TryGetTarget(out p);
if (p == null || p.T != this)
{
p = new PaddingHelper(this);
_padding.SetTarget(p);
}
}
return p;
}
}

public J? AcceptCSharp<P>(CSharpVisitor<P> v, P p)
{
return v.VisitTupleType(this, p);
}

public Guid Id => id;

public TupleType WithId(Guid newId)
{
return newId == id ? this : new TupleType(newId, prefix, markers, _elements, type);
}
public Space Prefix => prefix;

public TupleType WithPrefix(Space newPrefix)
{
return newPrefix == prefix ? this : new TupleType(id, newPrefix, markers, _elements, type);
}
public Markers Markers => markers;

public TupleType WithMarkers(Markers newMarkers)
{
return ReferenceEquals(newMarkers, markers) ? this : new TupleType(id, prefix, newMarkers, _elements, type);
}
private readonly JContainer<Cs.TupleElement> _elements = elements;
public IList<Cs.TupleElement> Elements => _elements.GetElements();

public TupleType WithElements(IList<Cs.TupleElement> newElements)
{
return Padding.WithElements(JContainer<Cs.TupleElement>.WithElements(_elements, newElements));
}
public JavaType? Type => type;

public TupleType WithType(JavaType? newType)
{
return newType == type ? this : new TupleType(id, prefix, markers, _elements, newType);
}
public sealed record PaddingHelper(Cs.TupleType T)
{
public JContainer<Cs.TupleElement> Elements => T._elements;

public Cs.TupleType WithElements(JContainer<Cs.TupleElement> newElements)
{
return T._elements == newElements ? T : new Cs.TupleType(T.Id, T.Prefix, T.Markers, newElements, T.Type);
}

}

#if DEBUG_VISITOR
[DebuggerStepThrough]
#endif
public bool Equals(Rewrite.Core.Tree? other)
{
return other is TupleType && other.Id == Id;
}
#if DEBUG_VISITOR
[DebuggerStepThrough]
#endif
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
}
Loading

0 comments on commit b951a83

Please sign in to comment.