Roslyn C# rebuild incorrect expression for to for statement
up vote
4
down vote
favorite
I have problem with rebuild for statement. I want to rebuild fragment code:
FOR(j, variable or integer, integer, > or < or <= or >=, - or +);
to
for(var j = variable or integer; j > or < or >= or <= 15; j-- or j++){}
For example FOR(j, k, >, -);
-> for(var j = k; j > 15; j--){}
. Besides I don't know how can separate elements on list <ArgumentListSyntax>
to IdentifierNameSyntax
or LiteralExpressionSyntax
,when there will be two IdentifierNameSyntax
or LiteralExpressionSyntax
on list. So I don't know if my attempt to solve is correct.
public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
{
// FOR(j, k, 10, >, -);
if (node.Kind() == SyntaxKind.InvocationExpression)
{
InvocationExpressionSyntax invocationExpression = node;
if (invocationExpression.GetFirstToken().ToString() == "FOR")
{
//List<ArgumentSyntax> argumentList = new List<ArgumentSyntax>();
//List<IdentifierNameSyntax> test = new List<IdentifierNameSyntax>();
var tmp = node.ChildNodes().OfType<ArgumentListSyntax>().FirstOrDefault();
var tmp1 = tmp.ChildNodes().OfType<ArgumentSyntax>().FirstOrDefault();
var tmp2 = tmp1.ChildNodes().OfType<IdentifierNameSyntax>().FirstOrDefault();
var tmp3 = tmp.Arguments.ElementAt(1);
var tmp4 = tmp3.ChildNodes().OfType<IdentifierNameSyntax>().FirstOrDefault();
Console.WriteLine(tmp.Arguments.ElementAt(0));
Console.WriteLine(tmp.Arguments);
Console.WriteLine(tmp2.GetFirstToken());
Console.WriteLine(tmp4);
node = node.ReplaceNode(node, SyntaxFactory.ForStatement(SyntaxKind.ForKeyword, SyntaxKind.OpenParenToken,
SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName("var"), )));
}
}
return base.VisitInvocationExpression(node);
}
c# for-loop roslyn
add a comment |
up vote
4
down vote
favorite
I have problem with rebuild for statement. I want to rebuild fragment code:
FOR(j, variable or integer, integer, > or < or <= or >=, - or +);
to
for(var j = variable or integer; j > or < or >= or <= 15; j-- or j++){}
For example FOR(j, k, >, -);
-> for(var j = k; j > 15; j--){}
. Besides I don't know how can separate elements on list <ArgumentListSyntax>
to IdentifierNameSyntax
or LiteralExpressionSyntax
,when there will be two IdentifierNameSyntax
or LiteralExpressionSyntax
on list. So I don't know if my attempt to solve is correct.
public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
{
// FOR(j, k, 10, >, -);
if (node.Kind() == SyntaxKind.InvocationExpression)
{
InvocationExpressionSyntax invocationExpression = node;
if (invocationExpression.GetFirstToken().ToString() == "FOR")
{
//List<ArgumentSyntax> argumentList = new List<ArgumentSyntax>();
//List<IdentifierNameSyntax> test = new List<IdentifierNameSyntax>();
var tmp = node.ChildNodes().OfType<ArgumentListSyntax>().FirstOrDefault();
var tmp1 = tmp.ChildNodes().OfType<ArgumentSyntax>().FirstOrDefault();
var tmp2 = tmp1.ChildNodes().OfType<IdentifierNameSyntax>().FirstOrDefault();
var tmp3 = tmp.Arguments.ElementAt(1);
var tmp4 = tmp3.ChildNodes().OfType<IdentifierNameSyntax>().FirstOrDefault();
Console.WriteLine(tmp.Arguments.ElementAt(0));
Console.WriteLine(tmp.Arguments);
Console.WriteLine(tmp2.GetFirstToken());
Console.WriteLine(tmp4);
node = node.ReplaceNode(node, SyntaxFactory.ForStatement(SyntaxKind.ForKeyword, SyntaxKind.OpenParenToken,
SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName("var"), )));
}
}
return base.VisitInvocationExpression(node);
}
c# for-loop roslyn
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have problem with rebuild for statement. I want to rebuild fragment code:
FOR(j, variable or integer, integer, > or < or <= or >=, - or +);
to
for(var j = variable or integer; j > or < or >= or <= 15; j-- or j++){}
For example FOR(j, k, >, -);
-> for(var j = k; j > 15; j--){}
. Besides I don't know how can separate elements on list <ArgumentListSyntax>
to IdentifierNameSyntax
or LiteralExpressionSyntax
,when there will be two IdentifierNameSyntax
or LiteralExpressionSyntax
on list. So I don't know if my attempt to solve is correct.
public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
{
// FOR(j, k, 10, >, -);
if (node.Kind() == SyntaxKind.InvocationExpression)
{
InvocationExpressionSyntax invocationExpression = node;
if (invocationExpression.GetFirstToken().ToString() == "FOR")
{
//List<ArgumentSyntax> argumentList = new List<ArgumentSyntax>();
//List<IdentifierNameSyntax> test = new List<IdentifierNameSyntax>();
var tmp = node.ChildNodes().OfType<ArgumentListSyntax>().FirstOrDefault();
var tmp1 = tmp.ChildNodes().OfType<ArgumentSyntax>().FirstOrDefault();
var tmp2 = tmp1.ChildNodes().OfType<IdentifierNameSyntax>().FirstOrDefault();
var tmp3 = tmp.Arguments.ElementAt(1);
var tmp4 = tmp3.ChildNodes().OfType<IdentifierNameSyntax>().FirstOrDefault();
Console.WriteLine(tmp.Arguments.ElementAt(0));
Console.WriteLine(tmp.Arguments);
Console.WriteLine(tmp2.GetFirstToken());
Console.WriteLine(tmp4);
node = node.ReplaceNode(node, SyntaxFactory.ForStatement(SyntaxKind.ForKeyword, SyntaxKind.OpenParenToken,
SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName("var"), )));
}
}
return base.VisitInvocationExpression(node);
}
c# for-loop roslyn
I have problem with rebuild for statement. I want to rebuild fragment code:
FOR(j, variable or integer, integer, > or < or <= or >=, - or +);
to
for(var j = variable or integer; j > or < or >= or <= 15; j-- or j++){}
For example FOR(j, k, >, -);
-> for(var j = k; j > 15; j--){}
. Besides I don't know how can separate elements on list <ArgumentListSyntax>
to IdentifierNameSyntax
or LiteralExpressionSyntax
,when there will be two IdentifierNameSyntax
or LiteralExpressionSyntax
on list. So I don't know if my attempt to solve is correct.
public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
{
// FOR(j, k, 10, >, -);
if (node.Kind() == SyntaxKind.InvocationExpression)
{
InvocationExpressionSyntax invocationExpression = node;
if (invocationExpression.GetFirstToken().ToString() == "FOR")
{
//List<ArgumentSyntax> argumentList = new List<ArgumentSyntax>();
//List<IdentifierNameSyntax> test = new List<IdentifierNameSyntax>();
var tmp = node.ChildNodes().OfType<ArgumentListSyntax>().FirstOrDefault();
var tmp1 = tmp.ChildNodes().OfType<ArgumentSyntax>().FirstOrDefault();
var tmp2 = tmp1.ChildNodes().OfType<IdentifierNameSyntax>().FirstOrDefault();
var tmp3 = tmp.Arguments.ElementAt(1);
var tmp4 = tmp3.ChildNodes().OfType<IdentifierNameSyntax>().FirstOrDefault();
Console.WriteLine(tmp.Arguments.ElementAt(0));
Console.WriteLine(tmp.Arguments);
Console.WriteLine(tmp2.GetFirstToken());
Console.WriteLine(tmp4);
node = node.ReplaceNode(node, SyntaxFactory.ForStatement(SyntaxKind.ForKeyword, SyntaxKind.OpenParenToken,
SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName("var"), )));
}
}
return base.VisitInvocationExpression(node);
}
c# for-loop roslyn
c# for-loop roslyn
edited Nov 19 at 18:53
Xiaoy312
10.9k12033
10.9k12033
asked Nov 19 at 18:49
A.Kowalski
212
212
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You don't need to separate elements in the ArgumentListSyntax
to the literals or identifiers, because you actually have a formal structure of supporting invocation: FOR(j, variable|integer, variable|integer, >|<|<=|>=, -|+);
so you assume that the second and third arguments may be literal or identifier, fourth is comparison element and so on. Therefore, you need to check that the input arguments satisfy these conditions and if are satisfiable do something useful:
...
// FOR(j, variable | integer, variable | integer, > | < | <= | >=, - | +);
if (node.Expression is IdentifierNameSyntax identifier && identifier.Identifier.ValueText.Equals("FOR"))
{
var arguments = node.ArgumentList.Arguments;
if (arguments.Count != 5) return node;
var second = arguments[1].Expression;
switch (second)
{
case IdentifierNameSyntax variable:
// and some sepcific logic for identifier
break;
case LiteralExpressionSyntax literal when literal.Kind() == SyntaxKind.NumericLiteralExpression:
// and some sepcific logic for literals and check,
// that the input literal is integer and is not rational value
break;
default:
// current argument isn't literal or identifier you can not do anything
return node;
}
// do the similar check for the other arguments
// and replace node as you wish
...
}
If you still assume that your invocation can contains a couple of another nodes as arguments, for example for(j, "foo", "foo", method(), initValue, method(), 15, >, >, >, -, "foo")
you will need to take arguments by different logic, for example take the first literal or identifier or something else:
...
// the same code from the example above
// here you can use an another logic to retrieve expression that you want
var second = arguments.First(x => x.IsKind(SyntaxKind.NumericLiteralExpression) || x.IsKind(SyntaxKind.IdentifierName)).Expression;
switch (second)
{
// the same code from the example above
}
// the same code from the example above
...
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You don't need to separate elements in the ArgumentListSyntax
to the literals or identifiers, because you actually have a formal structure of supporting invocation: FOR(j, variable|integer, variable|integer, >|<|<=|>=, -|+);
so you assume that the second and third arguments may be literal or identifier, fourth is comparison element and so on. Therefore, you need to check that the input arguments satisfy these conditions and if are satisfiable do something useful:
...
// FOR(j, variable | integer, variable | integer, > | < | <= | >=, - | +);
if (node.Expression is IdentifierNameSyntax identifier && identifier.Identifier.ValueText.Equals("FOR"))
{
var arguments = node.ArgumentList.Arguments;
if (arguments.Count != 5) return node;
var second = arguments[1].Expression;
switch (second)
{
case IdentifierNameSyntax variable:
// and some sepcific logic for identifier
break;
case LiteralExpressionSyntax literal when literal.Kind() == SyntaxKind.NumericLiteralExpression:
// and some sepcific logic for literals and check,
// that the input literal is integer and is not rational value
break;
default:
// current argument isn't literal or identifier you can not do anything
return node;
}
// do the similar check for the other arguments
// and replace node as you wish
...
}
If you still assume that your invocation can contains a couple of another nodes as arguments, for example for(j, "foo", "foo", method(), initValue, method(), 15, >, >, >, -, "foo")
you will need to take arguments by different logic, for example take the first literal or identifier or something else:
...
// the same code from the example above
// here you can use an another logic to retrieve expression that you want
var second = arguments.First(x => x.IsKind(SyntaxKind.NumericLiteralExpression) || x.IsKind(SyntaxKind.IdentifierName)).Expression;
switch (second)
{
// the same code from the example above
}
// the same code from the example above
...
}
add a comment |
up vote
1
down vote
You don't need to separate elements in the ArgumentListSyntax
to the literals or identifiers, because you actually have a formal structure of supporting invocation: FOR(j, variable|integer, variable|integer, >|<|<=|>=, -|+);
so you assume that the second and third arguments may be literal or identifier, fourth is comparison element and so on. Therefore, you need to check that the input arguments satisfy these conditions and if are satisfiable do something useful:
...
// FOR(j, variable | integer, variable | integer, > | < | <= | >=, - | +);
if (node.Expression is IdentifierNameSyntax identifier && identifier.Identifier.ValueText.Equals("FOR"))
{
var arguments = node.ArgumentList.Arguments;
if (arguments.Count != 5) return node;
var second = arguments[1].Expression;
switch (second)
{
case IdentifierNameSyntax variable:
// and some sepcific logic for identifier
break;
case LiteralExpressionSyntax literal when literal.Kind() == SyntaxKind.NumericLiteralExpression:
// and some sepcific logic for literals and check,
// that the input literal is integer and is not rational value
break;
default:
// current argument isn't literal or identifier you can not do anything
return node;
}
// do the similar check for the other arguments
// and replace node as you wish
...
}
If you still assume that your invocation can contains a couple of another nodes as arguments, for example for(j, "foo", "foo", method(), initValue, method(), 15, >, >, >, -, "foo")
you will need to take arguments by different logic, for example take the first literal or identifier or something else:
...
// the same code from the example above
// here you can use an another logic to retrieve expression that you want
var second = arguments.First(x => x.IsKind(SyntaxKind.NumericLiteralExpression) || x.IsKind(SyntaxKind.IdentifierName)).Expression;
switch (second)
{
// the same code from the example above
}
// the same code from the example above
...
}
add a comment |
up vote
1
down vote
up vote
1
down vote
You don't need to separate elements in the ArgumentListSyntax
to the literals or identifiers, because you actually have a formal structure of supporting invocation: FOR(j, variable|integer, variable|integer, >|<|<=|>=, -|+);
so you assume that the second and third arguments may be literal or identifier, fourth is comparison element and so on. Therefore, you need to check that the input arguments satisfy these conditions and if are satisfiable do something useful:
...
// FOR(j, variable | integer, variable | integer, > | < | <= | >=, - | +);
if (node.Expression is IdentifierNameSyntax identifier && identifier.Identifier.ValueText.Equals("FOR"))
{
var arguments = node.ArgumentList.Arguments;
if (arguments.Count != 5) return node;
var second = arguments[1].Expression;
switch (second)
{
case IdentifierNameSyntax variable:
// and some sepcific logic for identifier
break;
case LiteralExpressionSyntax literal when literal.Kind() == SyntaxKind.NumericLiteralExpression:
// and some sepcific logic for literals and check,
// that the input literal is integer and is not rational value
break;
default:
// current argument isn't literal or identifier you can not do anything
return node;
}
// do the similar check for the other arguments
// and replace node as you wish
...
}
If you still assume that your invocation can contains a couple of another nodes as arguments, for example for(j, "foo", "foo", method(), initValue, method(), 15, >, >, >, -, "foo")
you will need to take arguments by different logic, for example take the first literal or identifier or something else:
...
// the same code from the example above
// here you can use an another logic to retrieve expression that you want
var second = arguments.First(x => x.IsKind(SyntaxKind.NumericLiteralExpression) || x.IsKind(SyntaxKind.IdentifierName)).Expression;
switch (second)
{
// the same code from the example above
}
// the same code from the example above
...
}
You don't need to separate elements in the ArgumentListSyntax
to the literals or identifiers, because you actually have a formal structure of supporting invocation: FOR(j, variable|integer, variable|integer, >|<|<=|>=, -|+);
so you assume that the second and third arguments may be literal or identifier, fourth is comparison element and so on. Therefore, you need to check that the input arguments satisfy these conditions and if are satisfiable do something useful:
...
// FOR(j, variable | integer, variable | integer, > | < | <= | >=, - | +);
if (node.Expression is IdentifierNameSyntax identifier && identifier.Identifier.ValueText.Equals("FOR"))
{
var arguments = node.ArgumentList.Arguments;
if (arguments.Count != 5) return node;
var second = arguments[1].Expression;
switch (second)
{
case IdentifierNameSyntax variable:
// and some sepcific logic for identifier
break;
case LiteralExpressionSyntax literal when literal.Kind() == SyntaxKind.NumericLiteralExpression:
// and some sepcific logic for literals and check,
// that the input literal is integer and is not rational value
break;
default:
// current argument isn't literal or identifier you can not do anything
return node;
}
// do the similar check for the other arguments
// and replace node as you wish
...
}
If you still assume that your invocation can contains a couple of another nodes as arguments, for example for(j, "foo", "foo", method(), initValue, method(), 15, >, >, >, -, "foo")
you will need to take arguments by different logic, for example take the first literal or identifier or something else:
...
// the same code from the example above
// here you can use an another logic to retrieve expression that you want
var second = arguments.First(x => x.IsKind(SyntaxKind.NumericLiteralExpression) || x.IsKind(SyntaxKind.IdentifierName)).Expression;
switch (second)
{
// the same code from the example above
}
// the same code from the example above
...
}
answered Nov 23 at 17:38
George Alexandria
2,02421121
2,02421121
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380917%2froslyn-c-sharp-rebuild-incorrect-expression-for-to-for-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown