public virtual Expression visit(PropertyExpression expression) { expression.leftExpression.visitExpression(this); write("["); expression.rightExpression.visitExpression(this); write("]"); return(expression); }
/** * The grammar for the 'new' keyword is a little complicated. The keyword * 'new' can occur in either a NewExpression (where its not followed by * an argument list) or in MemberExpresison (where it is followed by an * argument list). The intention seems to be that an argument list should * bind to any unmatched 'new' keyword to the left in the same expression * if possible, otherwise an argument list is taken as a call expression. * * Since the rest of the productions for NewExpressions and CallExpressions * are similar we roll these two into one routine with a parameter to * indicate whether we're currently parsing a 'new' expression or not. * * @param newFlag if we're currently parsing a 'new' expression * @return an expression */ private Expression parseMemberExpression(bool newFlag) { Expression expression; // new expressions are right associative if (nextToken == Token.KEYWORD_NEW) { Expression name; readToken(Token.KEYWORD_NEW); name = parseMemberExpression(true); if (nextToken == Token.OPERATOR_OPENPAREN) { Expression[] arguments = parseArgumentList(); expression = new NewExpression(name, arguments); } else { expression = new NewExpression(name, null); } } else if (nextToken == Token.KEYWORD_FUNCTION) { expression = parseFunctionLiteral(false); } else { expression = parsePrimaryExpression(); } // call expressions are left associative while (true) { if (!newFlag && nextToken == Token.OPERATOR_OPENPAREN) { Expression[] arguments = parseArgumentList(); expression = new CallExpression(expression, arguments); } else if (nextToken == Token.OPERATOR_OPENSQUARE) { readToken(Token.OPERATOR_OPENSQUARE); Expression property = parseExpression(true); readToken(Token.OPERATOR_CLOSESQUARE); expression = new PropertyExpression(expression, property); } else if (nextToken == Token.OPERATOR_DOT) { // transform x.bar to x["bar"] readToken(Token.OPERATOR_DOT); Identifier identifier = parseIdentifier(); expression = new PropertyExpression(expression, new StringLiteral(identifier.str)); } else { return expression; } } }
public virtual Expression visit(PropertyExpression expression) { write("property"); return(expression); }