realm-js/parser/parser.cpp

345 lines
11 KiB
C++
Raw Normal View History

2015-11-09 19:29:57 +00:00
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#include "parser.hpp"
2015-11-06 00:21:19 +00:00
#include <iostream>
#include <pegtl.hh>
2015-11-06 16:23:23 +00:00
#include <pegtl/analyze.hh>
#include <pegtl/trace.hh>
2015-11-06 00:21:19 +00:00
using namespace pegtl;
2015-11-09 19:29:57 +00:00
namespace realm {
namespace parser {
// strings
struct unicode : list< seq< one< 'u' >, rep< 4, must< xdigit > > >, one< '\\' > > {};
2015-11-12 22:24:37 +00:00
struct escaped_char : one< '"', '\'', '\\', '/', 'b', 'f', 'n', 'r', 't', '0' > {};
2015-11-09 19:29:57 +00:00
struct escaped : sor< escaped_char, unicode > {};
struct unescaped : utf8::range< 0x20, 0x10FFFF > {};
2015-11-12 22:24:37 +00:00
struct chars : if_then_else< one< '\\' >, must< escaped >, unescaped > {};
struct dq_string_content : until< at< one< '"' > >, must< chars > > {};
struct dq_string : seq< one< '"' >, must< dq_string_content >, any > {};
2015-11-12 22:24:37 +00:00
struct sq_string_content : until< at< one< '\'' > >, must< chars > > {};
struct sq_string : seq< one< '\'' >, must< sq_string_content >, any > {};
2015-11-09 19:29:57 +00:00
// numbers
struct minus : opt< one< '-' > > {};
struct dot : one< '.' > {};
struct float_num : sor<
seq< plus< digit >, dot, star< digit > >,
seq< star< digit >, dot, plus< digit > >
> {};
struct hex_num : seq< one< '0' >, one< 'x', 'X' >, plus< xdigit > > {};
struct int_num : plus< digit > {};
struct number : seq< minus, sor< float_num, hex_num, int_num > > {};
struct true_value : pegtl_istring_t("true") {};
struct false_value : pegtl_istring_t("false") {};
2015-11-09 19:29:57 +00:00
// key paths
struct key_path : list< seq< sor< alpha, one< '_' > >, star< sor< alnum, one< '_', '-' > > > >, one< '.' > > {};
// argument
2015-11-12 22:34:47 +00:00
struct argument_index : plus< digit > {};
struct argument : seq< one< '$' >, must< argument_index > > {};
2015-11-09 19:29:57 +00:00
// expressions and operators
struct expr : sor< dq_string, sq_string, number, argument, true_value, false_value, key_path > {};
2015-11-09 19:29:57 +00:00
struct eq : sor< two< '=' >, one< '=' > > {};
struct noteq : pegtl::string< '!', '=' > {};
struct lteq : pegtl::string< '<', '=' > {};
struct lt : one< '<' > {};
struct gteq : pegtl::string< '>', '=' > {};
struct gt : one< '>' > {};
struct contains : pegtl_istring_t("contains") {};
struct begins : pegtl_istring_t("beginswith") {};
struct ends : pegtl_istring_t("endswith") {};
template<typename A, typename B>
struct pad_plus : seq< plus< B >, A, plus< B > > {};
struct padded_oper : pad_plus< sor< contains, begins, ends >, blank > {};
struct symbolic_oper : pad< sor< eq, noteq, lteq, lt, gteq, gt >, blank > {};
2015-11-09 19:29:57 +00:00
// predicates
struct comparison_pred : seq< expr, sor< padded_oper, symbolic_oper >, expr > {};
2015-11-09 19:29:57 +00:00
struct pred;
struct group_pred : if_must< one< '(' >, pad< pred, blank >, one< ')' > > {};
struct true_pred : pegtl_istring_t("truepredicate") {};
struct false_pred : pegtl_istring_t("falsepredicate") {};
2015-11-09 19:29:57 +00:00
2015-11-16 22:48:12 +00:00
struct not_pre : seq< sor< one< '!' >, pegtl_istring_t("not") > > {};
struct atom_pred : seq< opt< not_pre >, pad< sor< group_pred, true_pred, false_pred, comparison_pred >, blank > > {};
2015-11-09 19:29:57 +00:00
2015-11-12 23:40:54 +00:00
struct and_op : pad< sor< two< '&' >, pegtl_istring_t("and") >, blank > {};
struct or_op : pad< sor< two< '|' >, pegtl_istring_t("or") >, blank > {};
2015-11-09 19:29:57 +00:00
struct or_ext : if_must< or_op, pred > {};
struct and_ext : if_must< and_op, pred > {};
2015-11-09 19:29:57 +00:00
struct and_pred : seq< atom_pred, star< and_ext > > {};
struct pred : seq< and_pred, star< or_ext > > {};
// state
struct ParserState
{
std::vector<Predicate *> predicate_stack;
2015-11-24 19:18:03 +00:00
Predicate &current()
{
2015-11-10 17:09:20 +00:00
return *predicate_stack.back();
}
2015-11-24 19:18:03 +00:00
bool current_is_compound()
{
return current().type == Predicate::Type::And || current().type == Predicate::Type::Or;
}
bool negate_next = false;
2015-11-24 19:18:03 +00:00
void addExpression(Expression && exp)
2015-11-06 16:23:23 +00:00
{
2015-11-24 19:18:03 +00:00
Predicate &cur = current();
if (cur.type == Predicate::Type::Comparison) {
cur.cmpr.expr[1] = std::move(exp);
2015-11-09 19:29:57 +00:00
predicate_stack.pop_back();
}
else {
2015-11-24 19:18:03 +00:00
assert(current_is_compound());
2015-11-10 17:09:20 +00:00
Predicate p(Predicate::Type::Comparison);
p.cmpr.expr[0] = std::move(exp);
2015-11-09 20:20:59 +00:00
if (negate_next) {
p.negate = true;
negate_next = false;
}
2015-11-24 19:18:03 +00:00
cur.cpnd.sub_predicates.emplace_back(std::move(p));
predicate_stack.push_back(&cur.cpnd.sub_predicates.back());
2015-11-06 16:23:23 +00:00
}
2015-11-09 19:29:57 +00:00
}
};
// rules
template< typename Rule >
struct action : nothing< Rule > {};
2015-11-06 16:23:23 +00:00
#ifdef REALM_PARSER_PRINT_TOKENS
#define DEBUG_PRINT_TOKEN(string) std::cout << string << std::endl
#else
#define DEBUG_PRINT_TOKEN(string)
#endif
2015-11-09 19:29:57 +00:00
template<> struct action< and_ext >
{
static void apply( const input & in, ParserState & state )
2015-11-06 16:23:23 +00:00
{
DEBUG_PRINT_TOKEN("<and>");
2015-11-24 19:18:03 +00:00
assert(state.current_is_compound());
2015-11-09 20:13:01 +00:00
// if we were put into an OR group we need to rearrange
2015-11-10 17:09:20 +00:00
auto &current = state.current();
if (current.type == Predicate::Type::Or) {
2015-11-23 16:47:09 +00:00
auto &sub_preds = current.cpnd.sub_predicates;
2015-11-10 17:09:20 +00:00
auto &second_last = sub_preds[sub_preds.size()-2];
2015-11-09 20:13:01 +00:00
if (second_last.type == Predicate::Type::And) {
// if we are in an OR group and second to last predicate group is
// an AND group then move the last predicate inside
2015-11-10 17:09:20 +00:00
second_last.cpnd.sub_predicates.push_back(std::move(sub_preds.back()));
2015-11-09 20:13:01 +00:00
sub_preds.pop_back();
}
else {
// otherwise combine last two into a new AND group
2015-11-10 17:09:20 +00:00
Predicate pred(Predicate::Type::And);
pred.cpnd.sub_predicates.emplace_back(std::move(second_last));
pred.cpnd.sub_predicates.emplace_back(std::move(sub_preds.back()));
2015-11-09 20:13:01 +00:00
sub_preds.pop_back();
sub_preds.pop_back();
2015-11-10 17:09:20 +00:00
sub_preds.push_back(std::move(pred));
2015-11-09 20:13:01 +00:00
}
}
2015-11-09 19:29:57 +00:00
}
};
2015-11-06 16:23:23 +00:00
2015-11-09 19:29:57 +00:00
template<> struct action< or_ext >
{
static void apply( const input & in, ParserState & state )
2015-11-06 00:21:19 +00:00
{
DEBUG_PRINT_TOKEN("<or>");
2015-11-24 19:18:03 +00:00
assert(state.current_is_compound());
2015-11-09 20:13:01 +00:00
// if already an OR group do nothing
2015-11-10 17:09:20 +00:00
auto &current = state.current();
if (current.type == Predicate::Type::Or) {
2015-11-09 20:13:01 +00:00
return;
}
// if only two predicates in the group, then convert to OR
2015-11-10 17:09:20 +00:00
auto &sub_preds = state.current().cpnd.sub_predicates;
2015-11-24 19:18:03 +00:00
assert(sub_preds.size() > 1);
if (sub_preds.size() == 2) {
2015-11-10 17:09:20 +00:00
current.type = Predicate::Type::Or;
2015-11-09 20:13:01 +00:00
return;
}
// split the current group into to groups which are ORed together
2015-11-10 17:09:20 +00:00
Predicate pred1(Predicate::Type::And), pred2(Predicate::Type::And);
2015-11-24 19:18:03 +00:00
std::vector<Predicate>::iterator second_last = sub_preds.end() - 2;
pred1.cpnd.sub_predicates.insert(pred1.cpnd.sub_predicates.begin(), sub_preds.begin(), second_last);
pred2.cpnd.sub_predicates.insert(pred2.cpnd.sub_predicates.begin(), second_last, sub_preds.end());
2015-11-09 20:13:01 +00:00
2015-11-10 17:09:20 +00:00
current.type = Predicate::Type::Or;
sub_preds.clear();
sub_preds.emplace_back(std::move(pred1));
sub_preds.emplace_back(std::move(pred2));
2015-11-09 19:29:57 +00:00
}
};
2015-11-06 16:23:23 +00:00
2015-11-09 16:27:27 +00:00
#define EXPRESSION_ACTION(rule, type) \
template<> struct action< rule > { \
static void apply( const input & in, ParserState & state ) { \
DEBUG_PRINT_TOKEN(in.string()); \
state.addExpression(Expression(type, in.string())); }};
2015-11-09 19:29:57 +00:00
EXPRESSION_ACTION(dq_string_content, Expression::Type::String)
EXPRESSION_ACTION(sq_string_content, Expression::Type::String)
EXPRESSION_ACTION(key_path, Expression::Type::KeyPath)
EXPRESSION_ACTION(number, Expression::Type::Number)
EXPRESSION_ACTION(true_value, Expression::Type::True)
EXPRESSION_ACTION(false_value, Expression::Type::False)
EXPRESSION_ACTION(argument_index, Expression::Type::Argument)
2015-11-09 19:29:57 +00:00
template<> struct action< true_pred >
{
static void apply( const input & in, ParserState & state )
{
DEBUG_PRINT_TOKEN(in.string());
state.current().cpnd.sub_predicates.emplace_back(Predicate::Type::True);
}
};
template<> struct action< false_pred >
{
static void apply( const input & in, ParserState & state )
{
DEBUG_PRINT_TOKEN(in.string());
state.current().cpnd.sub_predicates.emplace_back(Predicate::Type::False);
}
};
2015-11-09 19:29:57 +00:00
#define OPERATOR_ACTION(rule, oper) \
template<> struct action< rule > { \
static void apply( const input & in, ParserState & state ) { \
DEBUG_PRINT_TOKEN(in.string()); \
2015-11-10 17:09:20 +00:00
state.current().cmpr.op = oper; }};
2015-11-09 19:29:57 +00:00
OPERATOR_ACTION(eq, Predicate::Operator::Equal)
OPERATOR_ACTION(noteq, Predicate::Operator::NotEqual)
OPERATOR_ACTION(gteq, Predicate::Operator::GreaterThanOrEqual)
OPERATOR_ACTION(gt, Predicate::Operator::GreaterThan)
OPERATOR_ACTION(lteq, Predicate::Operator::LessThanOrEqual)
OPERATOR_ACTION(lt, Predicate::Operator::LessThan)
OPERATOR_ACTION(begins, Predicate::Operator::BeginsWith)
OPERATOR_ACTION(ends, Predicate::Operator::EndsWith)
OPERATOR_ACTION(contains, Predicate::Operator::Contains)
template<> struct action< one< '(' > >
{
static void apply( const input & in, ParserState & state )
{
DEBUG_PRINT_TOKEN("<begin_group>");
2015-11-06 00:21:19 +00:00
2015-11-10 17:09:20 +00:00
Predicate group(Predicate::Type::And);
2015-11-09 20:20:59 +00:00
if (state.negate_next) {
group.negate = true;
state.negate_next = false;
}
2015-11-10 17:09:20 +00:00
state.current().cpnd.sub_predicates.emplace_back(std::move(group));
state.predicate_stack.push_back(&state.current().cpnd.sub_predicates.back());
2015-11-09 19:29:57 +00:00
}
};
template<> struct action< group_pred >
{
static void apply( const input & in, ParserState & state )
{
DEBUG_PRINT_TOKEN("<end_group>");
2015-11-09 19:29:57 +00:00
state.predicate_stack.pop_back();
}
};
template<> struct action< not_pre >
2015-11-06 00:21:19 +00:00
{
2015-11-09 19:29:57 +00:00
static void apply( const input & in, ParserState & state )
{
DEBUG_PRINT_TOKEN("<not>");
2015-11-09 20:20:59 +00:00
state.negate_next = true;
2015-11-06 00:21:19 +00:00
}
2015-11-09 19:29:57 +00:00
};
template< typename Rule >
struct error_message_control : pegtl::normal< Rule >
{
static const std::string error_message;
template< typename Input, typename ... States >
static void raise( const Input & in, States && ... )
{
throw pegtl::parse_error( error_message, in );
}
};
template<>
const std::string error_message_control< chars >::error_message = "Invalid characters in string constant.";
template< typename Rule>
const std::string error_message_control< Rule >::error_message = "Invalid predicate.";
2015-11-09 19:29:57 +00:00
Predicate parse(const std::string &query)
{
2015-11-10 17:09:20 +00:00
Predicate out_predicate(Predicate::Type::And);
2015-11-09 19:29:57 +00:00
ParserState state;
state.predicate_stack.push_back(&out_predicate);
pegtl::parse< must< pred, eof >, action, error_message_control >(query, query, state);
2015-11-10 17:09:20 +00:00
if (out_predicate.type == Predicate::Type::And && out_predicate.cpnd.sub_predicates.size() == 1) {
return std::move(out_predicate.cpnd.sub_predicates.back());
2015-11-10 05:12:18 +00:00
}
2015-11-10 17:09:20 +00:00
return std::move(out_predicate);
2015-11-06 00:21:19 +00:00
}
2015-11-23 16:47:09 +00:00
void analyzeGrammar()
2015-11-20 23:16:35 +00:00
{
analyze<pred>();
}
2015-11-09 19:29:57 +00:00
}}