[英]C# Roslyn Compiler Syntaxkind Value Keyword
i am currently using the Syntax API from Roslyn to generate some C# code and i am wondering about a small thing when defining a set accessor like this:我目前正在使用来自 Roslyn 的语法 API 来生成一些 C# 代码,并且在定义这样的集合访问器时我想知道一件小事:
var setAccessor = AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
.AddBodyStatements(ExpressionStatement(InvocationExpression(IdentifierName("SetField"))
.AddArgumentListArguments(Argument(IdentifierName("value")))));
var propertyDeclaration = PropertyDeclaration(ParseTypeName("FieldType"), "Field")
.AddModifiers(Token(SyntaxKind.PublicKeyword))
.AddAccessorListAccessors(setAccessor);
which works perfectly fine.效果很好。 I am just a bit confused about the
value
keyword in the setAccessor, because normally i would expect to be able to declare it like this:我对 setAccessor 中的
value
关键字有点困惑,因为通常我希望能够像这样声明它:
IdentifierName(Token(SyntaxKind.ValueKeyWord))
but as far as I can see there is no SyntaxKind.ValueKeyword
, am I missing something or do I get something wrong here?但据我所见,没有
SyntaxKind.ValueKeyword
,我是遗漏了什么还是这里有什么问题?
value
is a special contextual keyword. value
是一个特殊的上下文关键字。
The contextual keyword
value
is used in theset
accessor in property and indexer declarations.在属性和索引器声明中的
set
访问器中使用上下文关键字value
。 It is similar to an input parameter of a method.它类似于方法的输入参数。
The C# standard spec says: C# 标准规范说:
The following identifiers have special meaning in the syntactic grammar, but they are not keywords: add (§17.7), alias (§16.3), get (§17.6.2), global (§16.7), partial (§17.1.4), remove (§17.7), set (§17.6.2), value (§17.6.2, §17.7.2), where (§25.7), and yield (§15.14).
以下标识符在句法语法中具有特殊含义,但它们不是关键字:add(§17.7)、alias(§16.3)、get(§17.6.2)、global(§16.7)、partial(§17.1.4) ,删除(§17.7),设置(§17.6.2),值(§17.6.2,§17.7.2),其中(§25.7)和产量(§15.14)。 For convenience and clarity, these identifiers appear as terminals in the syntactic grammar;
为了方便和清楚起见,这些标识符在句法语法中显示为终结符; however, they are identifiers.
但是,它们是标识符。 [Note: As a result, unlike keywords, these identifiers can be written with a @ prefix and can contain unicode-escape-sequences.
[注意:因此,与关键字不同,这些标识符可以使用@前缀编写,并且可以包含 unicode-escape-sequences。 end note]
尾注]
In other words, the syntax model treats all contextual keywords as identifiers, rather than as regular keywords.换句话说,语法 model 将所有上下文关键字视为标识符,而不是常规关键字。 Some contextual keywords (like
yield
) do appear in SyntaxKind , but others (like var
and value
do not).一些上下文关键字(如
yield
)确实出现在SyntaxKind中,但其他关键字(如var
和value
没有)。
This github issue includes some relevant discussion, including this:此 github 问题包括一些相关讨论,包括:
it is a contextual keyword , but not a keyword .
它是上下文关键字,但不是关键字。 Roslyn's syntactic model only contains explicit support for contextual keywords for which the context is syntactic.
Roslyn 的语法 model 仅包含对上下文是语法的上下文关键字的显式支持。 That is why the syntactic model doesn't tell you about the contextual keywords
var
andvalue
.这就是为什么语法 model 没有告诉你上下文关键字
var
和value
的原因。 In any case all of the contextual keywords are identifiers.在任何情况下,所有上下文关键字都是标识符。
This SO question is related.这个 SO question是相关的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.