Fix bug in parenthesized expression

Reviewers: buda

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D249
This commit is contained in:
Mislav Bradac 2017-04-07 18:32:40 +02:00
parent db740fb9fc
commit 7a58ec5afb
3 changed files with 34 additions and 12 deletions

View File

@ -360,7 +360,7 @@ antlrcpp::Any CypherMainVisitor::visitRangeLiteral(
antlrcpp::Any CypherMainVisitor::visitExpression( antlrcpp::Any CypherMainVisitor::visitExpression(
CypherParser::ExpressionContext *ctx) { CypherParser::ExpressionContext *ctx) {
return visitChildren(ctx); return static_cast<Expression *>(ctx->expression12()->accept(this));
} }
// OR. // OR.
@ -499,7 +499,7 @@ antlrcpp::Any CypherMainVisitor::visitExpression3(
if (ctx->children.size() > 1u) { if (ctx->children.size() > 1u) {
throw NotYetImplemented(); throw NotYetImplemented();
} }
return visitChildren(ctx); return static_cast<Expression *>(visitChildren(ctx));
} }
antlrcpp::Any CypherMainVisitor::visitExpression2( antlrcpp::Any CypherMainVisitor::visitExpression2(
@ -525,11 +525,12 @@ antlrcpp::Any CypherMainVisitor::visitAtom(CypherParser::AtomContext *ctx) {
// TODO: implement other clauses. // TODO: implement other clauses.
throw NotYetImplemented(); throw NotYetImplemented();
} else if (ctx->parenthesizedExpression()) { } else if (ctx->parenthesizedExpression()) {
return ctx->parenthesizedExpression()->accept(this); return static_cast<Expression *>(
ctx->parenthesizedExpression()->accept(this));
} else if (ctx->variable()) { } else if (ctx->variable()) {
std::string variable = ctx->variable()->accept(this); std::string variable = ctx->variable()->accept(this);
users_identifiers.insert(variable); users_identifiers.insert(variable);
return (Expression *)storage_.Create<Identifier>(variable); return static_cast<Expression *>(storage_.Create<Identifier>(variable));
} }
// TODO: Implement this. We don't support comprehensions, functions, // TODO: Implement this. We don't support comprehensions, functions,
// filtering... at the moment. // filtering... at the moment.
@ -556,6 +557,11 @@ antlrcpp::Any CypherMainVisitor::visitLiteral(
return visitChildren(ctx); return visitChildren(ctx);
} }
antlrcpp::Any CypherMainVisitor::visitParenthesizedExpression(
CypherParser::ParenthesizedExpressionContext *ctx) {
return static_cast<Expression *>(ctx->expression()->accept(this));
}
antlrcpp::Any CypherMainVisitor::visitNumberLiteral( antlrcpp::Any CypherMainVisitor::visitNumberLiteral(
CypherParser::NumberLiteralContext *ctx) { CypherParser::NumberLiteralContext *ctx) {
if (ctx->integerLiteral()) { if (ctx->integerLiteral()) {

View File

@ -350,20 +350,26 @@ class CypherMainVisitor : public antlropencypher::CypherBaseVisitor {
CypherParser::Expression3Context *ctx) override; CypherParser::Expression3Context *ctx) override;
/** /**
* Property lookup, test for node labels existence... * Property lookup, test for node labels existence...
* *
* @return Expression* * @return Expression*
*/ */
antlrcpp::Any visitExpression2( antlrcpp::Any visitExpression2(
CypherParser::Expression2Context *ctx) override; CypherParser::Expression2Context *ctx) override;
/** /**
* Literals, params, list comprehension... * Literals, params, list comprehension...
* *
* @return Expression* * @return Expression*
*/ */
antlrcpp::Any visitAtom(CypherParser::AtomContext *ctx) override; antlrcpp::Any visitAtom(CypherParser::AtomContext *ctx) override;
/**
* @return Expression*
*/
antlrcpp::Any visitParenthesizedExpression(
CypherParser::ParenthesizedExpressionContext *ctx) override;
/** /**
* @return Literal* * @return Literal*
*/ */

View File

@ -121,6 +121,16 @@ TEST(CypherMainVisitorTest, NullLiteral) {
ASSERT_EQ(literal->value_.type(), TypedValue::Type::Null); ASSERT_EQ(literal->value_.type(), TypedValue::Type::Null);
} }
TEST(CypherMainVisitorTest, ParenthesizedExpression) {
AstGenerator ast_generator("RETURN (2)");
auto *query = ast_generator.query_;
auto *return_clause = dynamic_cast<Return *>(query->clauses_[0]);
auto *literal = dynamic_cast<Literal *>(
return_clause->named_expressions_[0]->expression_);
ASSERT_TRUE(literal);
ASSERT_EQ(literal->value_.Value<int64_t>(), 2);
}
TEST(CypherMainVisitorTest, OrOperator) { TEST(CypherMainVisitorTest, OrOperator) {
AstGenerator ast_generator("RETURN true Or false oR n"); AstGenerator ast_generator("RETURN true Or false oR n");
auto *query = ast_generator.query_; auto *query = ast_generator.query_;