[cond-expr] parse _ ? _ : _ into conditional AST node

This commit is contained in:
Lu Guanqun 2015-12-22 16:47:55 +00:00
parent 4563d4bd3c
commit 7eefa838a3

View File

@ -932,13 +932,26 @@ ASTPointer<Expression> Parser::parseExpression(
) )
{ {
ASTPointer<Expression> expression = parseBinaryExpression(4, _lookAheadIndexAccessStructure); ASTPointer<Expression> expression = parseBinaryExpression(4, _lookAheadIndexAccessStructure);
if (!Token::isAssignmentOp(m_scanner->currentToken())) if (Token::isAssignmentOp(m_scanner->currentToken()))
return expression; {
Token::Value assignmentOperator = expectAssignmentOperator(); Token::Value assignmentOperator = expectAssignmentOperator();
ASTPointer<Expression> rightHandSide = parseExpression(); ASTPointer<Expression> rightHandSide = parseExpression();
ASTNodeFactory nodeFactory(*this, expression); ASTNodeFactory nodeFactory(*this, expression);
nodeFactory.setEndPositionFromNode(rightHandSide); nodeFactory.setEndPositionFromNode(rightHandSide);
return nodeFactory.createNode<Assignment>(expression, assignmentOperator, rightHandSide); return nodeFactory.createNode<Assignment>(expression, assignmentOperator, rightHandSide);
}
else if (m_scanner->currentToken() == Token::Value::Conditional)
{
m_scanner->next();
ASTPointer<Expression> trueExpression = parseExpression();
expectToken(Token::Colon);
ASTPointer<Expression> falseExpression = parseExpression();
ASTNodeFactory nodeFactory(*this, expression);
nodeFactory.setEndPositionFromNode(falseExpression); // TODO:
return nodeFactory.createNode<Conditional>(expression, trueExpression, falseExpression);
}
else
return expression;
} }
ASTPointer<Expression> Parser::parseBinaryExpression( ASTPointer<Expression> Parser::parseBinaryExpression(