Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# grammar "base"

Tags:

c#

grammar

I am looking into the grammar of C# 5.0 and don't quite understand the use of "base". In the reference manual, there is a notion of "base access" defined as:

base-access:
    base   .   identifier
    base   [   expression-list   ]

Where base is a keyword, and it appears that this is the only case. However, I encounter C# inputs such as

base.WithAdditionalDiagnostics<TNode>(node, diagnostics);

Can someone point me out which grammar rule this statement refers to? As 'base' appears to be a normal keyword, not contextual, I assume that there should be a specific grammar rule for this case, and base cannot simply be an identifier.

like image 373
Wickoo Avatar asked Jan 14 '15 13:01

Wickoo


People also ask

What C is used for?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


1 Answers

I believe it should actually be

base-access:
    base   .   identifier type-argument-list_opt
    base   [   expression-list   ]

... which would make it just like member-access:

member-access:
    primary-expression   .   identifier   type-argument-list_opt
    predefined-type   .   identifier   type-argument-list_opt
    qualified-alias-member   .   identifier   type-argument-list_opt

In other words, in an expression

base.WithAdditionalDiagnostics<TNode>(node, diagnostics);

only

base.WithAdditionalDiagnostics<TNode>

is the base-access part - and the rest is parsed as it would be for other calls such as x.WithAdditionalDiagnostics<TNode>(node, diagnostics).

From section 7.6.8 of the C# 5 spec:

At binding-time, base-access expressions of the form base.I and base[E] are evaluated exactly as if they were written ((B)this).I and ((B)this)[E], where B is the base class of the class or struct in which the construct occurs. Thus, base.I and base[E] correspond to this.I and this[E], except this is viewed as an instance of the base class.

Without the additional type-argument-listopt though, I think your existing expression wouldn't parse.

This is actually specified correctly in the 4th edition of the ECMA-334 specification; I shall raise it as a bug with the C# specification (and make sure it doesn't get broken for the 5th edition).

like image 100
Jon Skeet Avatar answered Nov 07 '22 02:11

Jon Skeet