Browse Source

extended VBNetOutputVisitor to support single and multi line Sub/Function-Lambdas

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/vbnet@5924 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Siegfried Pammer 15 years ago
parent
commit
9a65c044d0
  1. 652
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs
  2. 2
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG
  3. 35
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs
  4. 37
      src/Libraries/NRefactory/Test/Output/VBNet/VBNetOutputTest.cs

652
src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs

File diff suppressed because it is too large Load Diff

2
src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG

@ -2793,8 +2793,6 @@ FormalParameter<out ParameterDeclarationExpression p> @@ -2793,8 +2793,6 @@ FormalParameter<out ParameterDeclarationExpression p>
type.RankSpecifier = (int[])arrayModifiers.ToArray(typeof(int));
}
}
} else {
type = new TypeReference("System.Object", arrayModifiers == null ? null : (int[])arrayModifiers.ToArray(typeof(int)));
}
.)
[ "=" Expr<out expr> ]

35
src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs

@ -2932,22 +2932,51 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -2932,22 +2932,51 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public override object TrackedVisitLambdaExpression(LambdaExpression lambdaExpression, object data)
{
if (!lambdaExpression.ExpressionBody.IsNull) {
bool isSub = !lambdaExpression.ReturnType.IsNull &&
lambdaExpression.ReturnType.Type == "System.Void" && lambdaExpression.ReturnType.IsKeyword;
if (isSub)
outputFormatter.PrintToken(Tokens.Sub);
else
outputFormatter.PrintToken(Tokens.Function);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
AppendCommaSeparatedList(lambdaExpression.Parameters);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
outputFormatter.Space();
if (!lambdaExpression.ExpressionBody.IsNull) {
return lambdaExpression.ExpressionBody.AcceptVisitor(this, data);
} else {
OutputAnonymousMethodWithStatementBody(lambdaExpression.Parameters, lambdaExpression.StatementBody);
if (!isSub && !lambdaExpression.ReturnType.IsNull) {
outputFormatter.PrintToken(Tokens.As);
outputFormatter.Space();
TrackedVisit(lambdaExpression.ReturnType, data);
}
if (lambdaExpression.StatementBody is BlockStatement)
outputFormatter.NewLine();
TrackedVisit(lambdaExpression.StatementBody, data);
if (lambdaExpression.StatementBody is BlockStatement) {
outputFormatter.NewLine();
outputFormatter.PrintToken(Tokens.End);
outputFormatter.Space();
if (isSub)
outputFormatter.PrintToken(Tokens.Sub);
else
outputFormatter.PrintToken(Tokens.Function);
}
return null;
}
}
void OutputAnonymousMethodWithStatementBody(List<ParameterDeclarationExpression> parameters, Statement body)
{
//Error("VB does not support anonymous methods/lambda expressions with a statement body", body.StartLocation);
Error("VB does not support anonymous methods/lambda expressions with a statement body", body.StartLocation);
outputFormatter.PrintToken(Tokens.Function);
outputFormatter.PrintToken(Tokens.OpenParenthesis);

37
src/Libraries/NRefactory/Test/Output/VBNet/VBNetOutputTest.cs

@ -553,5 +553,42 @@ End Using"); @@ -553,5 +553,42 @@ End Using");
{
TestTypeMember("Friend WithEvents Button1 As System.Windows.Forms.Button");
}
[Test]
public void SimpleFunctionLambda()
{
TestExpression("Function(x) x * x");
}
[Test]
public void SimpleFunctionLambdaWithType()
{
TestExpression("Function(x As Integer) x * x");
}
[Test]
public void SimpleSubLambdaWithType()
{
TestExpression("Sub(x As Integer) Console.WriteLine(x)");
}
[Test]
public void BlockSubLambdaWithType()
{
TestExpression("Sub(x As Integer)\n" +
" Console.WriteLine(x)\n" +
"End Sub");
}
[Test]
public void BlockFunctionLambdaWithType()
{
TestExpression("Function(x As Integer) As Integer\n" +
" If x < 2 Then\n" +
" Return x\n" +
" End If\n" +
" Return x * x\n" +
"End Function");
}
}
}

Loading…
Cancel
Save