views:

82

answers:

1

I've been working on my own implementation of ECMAScript for quite some time now. I have basically done everything by hand to help gain a deep understanding of the process. Repeated attempts to analyze and understand this portion of the grammar have failed so I've been working on other portions of the project but now I am at a point were I will be working on object literals so I really need to polish my syntactic analyzer. Can anyone put this in terms a language parser novice could understand?

My biggest source of confusion is the following:

new MemberExpression Arguments

This is supposed to be a member expression, but this seemingly conflicts with the following:

NewExpression :
    MemberExpression 
    new NewExpression

Is a new expression a member expression or a left hand side expression? To be honest I am having trouble laying out the proper C# classes for the concrete grammar.

MemberExpression : 
    PrimaryExpression 
    FunctionExpression 
    MemberExpression [ Expression ] 
    MemberExpression . IdentifierName 
    new MemberExpression Arguments 

NewExpression :
    MemberExpression 
    new NewExpression 

CallExpression :
    MemberExpression Arguments 
    CallExpression Arguments 
    CallExpression [ Expression ] 
    CallExpression . IdentifierName 

LeftHandSideExpression :
    NewExpression 
    CallExpression 

This is the class design I've been working with but as I continue to study the spec my doubts just won't go away.

public abstract class LeftHandSideExpression : ConcreteExpression
{

}

public sealed class NewExpression : LeftHandSideExpression
{
    public NewExpression(MemberExpression memberExpression, Arguments arguments)
    {

    }

    public NewExpression(NewExpression newExpression, Arguments arguments)
    {

    }        
}

public sealed class CallExpression : LeftHandSideExpression
{
    public CallExpression(MemberExpression memberExpression, Arguments arguments)
    {

    }

    public CallExpression(CallExpression callExpression, Arguments arguments)
    {

    }
}

public sealed class MemberExpression : ConcreteExpression
{
    public MemberExpression(PrimaryExpression primaryExpression)
    {

    }

    public MemberExpression(PrimaryExpression primaryExpression, string identifierName)
    {

    }

    public MemberExpression(PrimaryExpression primaryExpression, ConcreteExpression indexerExpression)
    {

    }

    public MemberExpression(FunctionExpression functionExpression)
    {

    }

    public MemberExpression(FunctionExpression functionExpression, string identifierName)
    {

    }

    public MemberExpression(FunctionExpression functionExpression, ConcreteExpression indexerExpression)
    {

    }
}

Based off of Andy's answer I came up with a new design that makes sense.

public abstract class LeftHandSideExpression : ConcreteExpression
{
    public ConcreteExpression Expression { get; private set; }

    protected LeftHandSideExpression(ConcreteExpression expression)
    {
        Expression = expression;
    }
}

public class NewExpression : LeftHandSideExpression
{
    public Arguments Arguments { get; private set; }

    protected NewExpression(PrimaryExpression primaryExpression)
        : base(primaryExpression)
    {

    }

    protected NewExpression(FunctionExpression functionExpression)
        : base(functionExpression)
    {

    }

    protected NewExpression(MemberExpression memberExpression)
        : base(memberExpression)
    {

    }

    protected NewExpression(CallExpression callExpression)
        : base(callExpression)
    {

    }

    public NewExpression(MemberExpression memberExpression, Arguments arguments)
        : base(memberExpression)
    {
        Arguments = arguments;
    }

    public NewExpression(NewExpression newExpression, Arguments arguments)
        : base(newExpression)
    {
        Arguments = arguments;
    }      
}

public sealed class CallExpression : LeftHandSideExpression
{
    public Arguments Arguments { get; private set; }

    public CallExpression(MemberExpression memberExpression, Arguments arguments)
        : base(memberExpression)
    {
        Arguments = arguments;
    }

    public CallExpression(CallExpression callExpression, Arguments arguments)
        : base(callExpression)
    {
        Arguments = arguments;
    }
}

public class MemberExpression : NewExpression
{
    public MemberExpression(PrimaryExpression primaryExpression)
        : base(primaryExpression)
    {

    }

    public MemberExpression(FunctionExpression functionExpression)
        : base(functionExpression)
    {

    }

    public MemberExpression(MemberExpression memberExpression)
        : base(memberExpression)
    {

    }

    protected MemberExpression(CallExpression callExpression)
        : base(callExpression)
    {

    }
}

public sealed class DotPropertyAccessExpression : MemberExpression
{
    public string IdentifierName { get; private set; }

    public DotPropertyAccessExpression(PrimaryExpression primaryExpression, string identifierName)
        : base(primaryExpression)
    {
        IdentifierName = identifierName;
    }

    public DotPropertyAccessExpression(FunctionExpression functionExpression, string identifierName)
        : base(functionExpression)
    {
        IdentifierName = identifierName;
    }

    public DotPropertyAccessExpression(MemberExpression memberExpression, string identifierName)
        : base(memberExpression)
    {
        IdentifierName = identifierName;
    }

    public DotPropertyAccessExpression(CallExpression callExpression, string identifierName)
        : base(callExpression)
    {
        IdentifierName = identifierName;
    }
}

public sealed class BracketPropertyAccessExpression : MemberExpression
{
    public ConcreteExpression IndexerExpression { get; private set; }

    public BracketPropertyAccessExpression(PrimaryExpression primaryExpression, ConcreteExpression indexerExpression)
        : base(primaryExpression)
    {
        IndexerExpression = indexerExpression;
    }

    public BracketPropertyAccessExpression(FunctionExpression functionExpression, ConcreteExpression indexerExpression)
        : base(functionExpression)
    {
        IndexerExpression = indexerExpression;
    }

    public BracketPropertyAccessExpression(MemberExpression memberExpression, ConcreteExpression indexerExpression)
        : base(memberExpression)
    {
        IndexerExpression = indexerExpression;
    }

    public BracketPropertyAccessExpression(CallExpression callExpression, ConcreteExpression indexerExpression)
        : base(callExpression)
    {
        IndexerExpression = indexerExpression;
    }
}
+3  A: 

Here's a different way of visualizing it. This is the way I see the general hierarchy of productions in section 11.2:

LeftHandSideExpression :
    NewExpression :
        MemberExpression :
            PrimaryExpression
            FunctionExpression
            MemberExpression [ Expression ]
            MemberExpression . IdentifierName
            new MemberExpression Arguments
        new NewExpression
    CallExpression :
        MemberExpression Arguments
        CallExpression Arguments
        CallExpression [ Expression ]
        CallExpression . IdentifierName

It looks like MemberExpressions are NewExpressions, which are, in turn, LeftHandSideExpressions. I hope this helps.

Andy West
It is really amazing what a different perspective can bring. I'll be buying you a beer and then drinking it in your honor. Thanks.
ChaosPandion
Awesome - so glad I could help. I'll drink a beer in my honor as well. ;)
Andy West