我已经从事我自己的ECMAScript实现了很长时间了.我基本上已经手工完成了所有事情,以帮助您深入了解该过程.反复尝试分析和理解语法的这一部分都失败了,所以我一直在研究项目的其他部分,但现在我正要研究对象字面量,因此我确实需要完善语法分析器.谁能用一个语言解析器新手可以理解的术语来表达?
我最大的困惑是:
new MemberExpression Arguments
这应该是一个成员表达式,但这似乎与以下内容冲突:
NewExpression : MemberExpression new NewExpression
新表达式是成员表达式还是左侧表达式?老实说,我在为具体语法安排适当的C#类时遇到了麻烦.
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
这是我一直在使用的班级设计,但是当我继续研究规范时,我的疑虑不会消失.
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)
{
}
}
基于安迪的回答,我想到了一个有意义的新设计.
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;
}
}
解决方法:
这是一种不同的可视化方式.这是我在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
看起来MemberExpressions是NewExpressions,它们依次是LeftHandSideExpressions.我希望这有帮助.