What is the reason for passing SyntaxKind methods in a syntax class factory?

Roslyn CTP typically uses the following method signature:

LiteralExpressionSyntax LiteralExpression(SyntaxKind kind, SyntaxToken token) 

What is the reason for passing SyntaxKind? token has the Kind property, why is Roslyn not using it?

To be more precise, a custom Kind is required, claims to be limited to a narrow set of values, and then checks that token Kind also belongs to that narrow set, provided Kind provided.

Why doesn't Roslyn use token.Kind and allow the user to pass an explicit view?

UPD: Actually, the main question is: what are the situations when the Kind submitted by the user will differ from token kind?

+4
source share
1 answer

If you look closely, you will see that the checked types are not actually the same in each case. There are separate SyntaxKinds for and expressions - so for the token there will be SyntaxKind.TrueKeyword and SyntaxKind.TrueLiteralExpression for the resulting LiteralExpressionSyntax . To answer your updated question, the result of expression.Kind will never match token.Kind .

However, it seems that in this case there is a one-to-one correspondence between the types of tokens and types of expressions, which means that we could get the type of expression from the type of token. I will do it internally.

+7
source

Source: https://habr.com/ru/post/1487739/


All Articles