store negated predicates in parse tree

This commit is contained in:
Ari Lazier 2015-11-09 12:20:59 -08:00
parent 4b3417736f
commit 11b27dbdbf
2 changed files with 14 additions and 0 deletions

View File

@ -95,6 +95,7 @@ struct ParserState
{ {
std::vector<Predicate *> predicate_stack; std::vector<Predicate *> predicate_stack;
Predicate *current() { return predicate_stack.back(); } Predicate *current() { return predicate_stack.back(); }
bool negate_next;
void addExpression(Expression exp) void addExpression(Expression exp)
{ {
@ -106,6 +107,10 @@ struct ParserState
Predicate p; Predicate p;
p.type = Predicate::Type::Comparison; p.type = Predicate::Type::Comparison;
p.sub_expressions.emplace_back(std::move(exp)); p.sub_expressions.emplace_back(std::move(exp));
if (negate_next) {
p.negate = true;
negate_next = false;
}
current()->sub_predicates.emplace_back(std::move(p)); current()->sub_predicates.emplace_back(std::move(p));
predicate_stack.push_back(&current()->sub_predicates.back()); predicate_stack.push_back(&current()->sub_predicates.back());
} }
@ -244,6 +249,11 @@ template<> struct action< one< '(' > >
Predicate group; Predicate group;
group.type = Predicate::Type::And; group.type = Predicate::Type::And;
if (state.negate_next) {
group.negate = true;
state.negate_next = false;
}
state.current()->sub_predicates.emplace_back(std::move(group)); state.current()->sub_predicates.emplace_back(std::move(group));
state.predicate_stack.push_back(&state.current()->sub_predicates.back()); state.predicate_stack.push_back(&state.current()->sub_predicates.back());
} }
@ -264,6 +274,8 @@ template<> struct action< not_pre >
static void apply( const input & in, ParserState & state ) static void apply( const input & in, ParserState & state )
{ {
std::cout << "<not>" << std::endl; std::cout << "<not>" << std::endl;
state.negate_next = true;
} }
}; };

View File

@ -69,6 +69,8 @@ namespace realm {
// for compounds // for compounds
std::vector<Predicate> sub_predicates; std::vector<Predicate> sub_predicates;
bool negate;
}; };
Predicate parse(const std::string &query); Predicate parse(const std::string &query);