public static bool ParseInvocation(StatementSyntax statement, out ContractInvocationInfo invocationInfo) { if (statement != null && statement is ExpressionStatementSyntax exprSt && exprSt.Expression is InvocationExpressionSyntax invocation) { return(ParseInvocation(invocation, out invocationInfo)); } invocationInfo = null; return(false); }
public static bool ParseInvocation(InvocationExpressionSyntax invocation, out ContractInvocationInfo invocationInfo) { if (invocation.ArgumentList.Arguments.Count >= 1 && invocation.ArgumentList.Arguments.Count <= 3 && invocation.Expression is MemberAccessExpressionSyntax memberAccess && memberAccess.Name is SimpleNameSyntax methodNameSyntax) { SimpleNameSyntax className = memberAccess.Expression as SimpleNameSyntax; if (className == null && memberAccess.Expression is MemberAccessExpressionSyntax classMemberAccess) { className = classMemberAccess.Name as SimpleNameSyntax; } if (className != null && ValidContractClasses.Contains(className.Identifier.ValueText)) { SimpleNameSyntax methodName = methodNameSyntax; NameSyntax genericExceptionType = null; if (methodNameSyntax is GenericNameSyntax genericName && genericName.TypeArgumentList.Arguments.Count == 1) { genericExceptionType = genericName.TypeArgumentList.Arguments[0] as NameSyntax; } ExpressionSyntax condition = invocation.ArgumentList.Arguments[0].Expression; ExpressionSyntax message = null; ExpressionSyntax conditionString = null; if (invocation.ArgumentList.Arguments.Count >= 2) { if (invocation.ArgumentList.Arguments[1].NameColon != null) { if (invocation.ArgumentList.Arguments[1].NameColon.Name.Identifier.ValueText == "conditionString") { conditionString = invocation.ArgumentList.Arguments[1].Expression; } else { message = invocation.ArgumentList.Arguments[1].Expression; } } else { message = invocation.ArgumentList.Arguments[1].Expression; } } if (invocation.ArgumentList.Arguments.Count == 3) { if (invocation.ArgumentList.Arguments[2].NameColon != null) { if (invocation.ArgumentList.Arguments[2].NameColon.Name.Identifier.ValueText == "message" || invocation.ArgumentList.Arguments[2].NameColon.Name.Identifier.ValueText == "userMessage") { message = invocation.ArgumentList.Arguments[2].Expression; } else { conditionString = invocation.ArgumentList.Arguments[2].Expression; } } else { conditionString = invocation.ArgumentList.Arguments[2].Expression; } } if (className != null && methodName != null && condition != null) { invocationInfo = new ContractInvocationInfo(className, methodName, genericExceptionType, condition, message, conditionString, invocation.ArgumentList); return(true); } } } invocationInfo = null; return(false); }