Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the base access defined correctly in the C# Language Specification 4.0?

While I was looking through the C# Language Specification v4.0 I noticed that there is a group of rules defined as this:

invocationExpression:
    primaryExpression '(' argumentList? ')'

primary-expression: 
    primary-no-array-creation-expression
    array-creation-expression

primary-no-array-creation-expression:
    literal
    simple-name
    parenthesized-expression
    member-access
    invocation-expression
    element-access
    this-access
    base-access
    post-increment-expression
    post-decrement-expression
    object-creation-expression
    delegate-creation-expression
    anonymous-object-creation-expression
    typeof-expression
    checked-expression
    unchecked-expression 
    default-value-expression
    anonymous-method-expression

base-access:
    'base'   '.'   identifier
    'base'   '['   argument-list   ']'

When I tried to match this statement (which is correct statement by the way, I've seen it used in a project)

base.VisitList<T>(list, visitor);

to the given rules I didn't see a way how this can be done. Shouldn't be base-access defined as:

 base-access:
        'base'   '.'   identifier type-parameter-list(opt)
        'base'   '['   argument-list   ']'

or something similar to this in such a way the grammar be able to capture the statement?

EDIT The example I saw was in the C# version of the project db4o and it is something like this (I deleted most of the declarations and left only the important ones)

internal abstract class ExpressionVisitor
{
    protected virtual void VisitList<T>()
    {
    }
}

internal class HashCodeCalculation : ExpressionVisitor
{
    protected override void VisitList<T>()
    {
        base.VisitList<T>();
    }
}
like image 417
sve Avatar asked Sep 26 '13 13:09

sve


2 Answers

It indeed looks like there might be an error in the specification.

The way to parse the expression

this.VisitList<T>(list, visitor)

is as follows:

invocation-expression

primary-expression '(' argumentList? ')'

primary-no-array-creation-expression '(' argumentList? ')'

member-access '(' argumentList? ')'

primary-expression '.' identifier type-argument-listopt '(' argumentList? ')'

this-access '.' identifier type-argument-listopt '(' argumentList? ')'

'this' '.' identifier type-argument-listopt '(' argumentList? ')'

And so on.

In other words, this-access is defined as

this-access:
    'this'

It would make a lot of sense if base-access were defined as

base-access:
    'base'

Then the way to parse the expression

base.VisitList<T>(list, visitor)

would be as follows:

invocation-expression

primary-expression '(' argumentList? ')'

primary-no-array-creation-expression '(' argumentList? ')'

member-access '(' argumentList? ')'

primary-expression '.' identifier type-argument-listopt '(' argumentList? ')'

base-access '.' identifier type-argument-listopt '(' argumentList? ')'

'base' '.' identifier type-argument-listopt '(' argumentList? ')'

And so on.

But it isn't.

like image 125
Kris Vandermotten Avatar answered Sep 24 '22 12:09

Kris Vandermotten


I doubt this is really valid C# code. Maybe you saw it in C++?

The following code fails to compile with a syntax error:

public class VisitList<T> : List<T>
{
    public VisitList(int n)
        : base.VisitList<T>(n)
    {

    }
}

This works:

public class VisitList<T> : List<T>
{
    public VisitList(int n)
        : base(n)
    {

    }
}
like image 20
PMF Avatar answered Sep 25 '22 12:09

PMF