Roslyn C#为to语句重建错误的表达式

时间:2018-11-19 18:49:45

标签: c# for-loop roslyn

我在重建语句时遇到问题。我想重建片段代码:

FOR(j, variable or integer, integer, > or < or <= or >=, - or +);

for(var j = variable or integer; j > or < or >= or <= 15; j-- or j++){}

例如FOR(j, k, >, -);-> for(var j = k; j > 15; j--){}。此外,当列表中有两个<ArgumentListSyntax>IdentifierNameSyntax时,我不知道如何将列表LiteralExpressionSyntax分离为IdentifierNameSyntaxLiteralExpressionSyntax的元素。所以我不知道我的解决方法是否正确。

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);
}

1 个答案:

答案 0 :(得分:2)

您不需要将ArgumentListSyntax中的元素与文字或标识符分开,因为您实际上具有支持调用的正式结构:FOR(j, variable|integer, variable|integer, >|<|<=|>=, -|+);,因此您假定第二个和第三个参数可以是文字或标识符,第四个是比较元素,依此类推。因此,您需要检查输入参数是否满足这些条件,如果可以满足要求,请执行一些有用的操作:

...
// 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
    ...
}

如果您仍然假设调用可以包含两个其他节点作为参数,例如for(j, "foo", "foo", method(), initValue, method(), 15, >, >, >, -, "foo"),则需要采用不同的逻辑接受参数,例如,接受第一个文字或标识符或其他内容:< / p>

...
    // 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

    ...
}