compound predicates

This commit is contained in:
Ari Lazier 2015-11-06 08:23:23 -08:00
parent 6ea80c4ecd
commit ed4e59c8d0
1 changed files with 52 additions and 5 deletions

View File

@ -2,6 +2,8 @@
#include <iostream>
#include <pegtl.hh>
#include <pegtl/analyze.hh>
#include <pegtl/trace.hh>
using namespace pegtl;
@ -34,32 +36,77 @@ namespace query
struct number : seq< minus, sor< float_num, hex_num, int_num > > {};
// key paths
struct key_path : list< must< sor< alpha, one< '_' > >, star< sor< alnum, one< '_', '-' > > > >, one< '.' > > {};
struct key_path : list< seq< sor< alpha, one< '_' > >, star< sor< alnum, one< '_', '-' > > > >, one< '.' > > {};
// expressions and operators
struct expr : sor< string, key_path, number > {};
struct oper : sor< one< '=' >, istring< '=', '=' >, istring< '!', '=' >, one< '<' >, istring< '<', '=' >, one< '>' >, istring< '>', '=' > > {};
struct oper : sor<
two< '=' >,
one< '=' >,
pegtl::string< '!', '=' >,
pegtl::string< '<', '=' >,
one< '<' >,
pegtl::string< '>', '=' >,
one< '>' >
> {};
// predicates
struct pred : seq< expr, plus< blank >, oper, plus< blank >, expr > {};
struct comparison_pred : seq< expr, pad< oper, blank >, expr > {};
struct pred;
struct group_pred : if_must< one< '(' >, pad< pred, blank >, one< ')' > > {};
struct single_pred : pad< sor< group_pred, comparison_pred >, blank > {};
struct not_pre : pegtl::string< 'N', 'O', 'T' > {};
struct atom_pred : seq< opt< not_pre >, single_pred > {};
struct or_ext : if_must< two< '|' >, atom_pred > {};
struct and_ext : if_must< two< '&' >, atom_pred > {};
struct pred : seq< atom_pred, star< sor< or_ext, and_ext > > > {};
// rules
template< typename Rule >
struct action : nothing< Rule > {};
template<> struct action< expr >
template<> struct action< and_ext >
{
static void apply( const input & in, std::string & string_value )
{
std::cout << "<and>" << in.string() << std::endl;
}
};
template<> struct action< or_ext >
{
static void apply( const input & in, std::string & string_value )
{
std::cout << "<or>" << in.string() << std::endl;
}
};
template<> struct action< comparison_pred >
{
static void apply( const input & in, std::string & string_value )
{
std::cout << in.string() << std::endl;
}
};
template<> struct action< group_pred >
{
static void apply( const input & in, std::string & string_value )
{
std::cout << "<group>" << std::endl;
}
};
}
int main( int argc, char ** argv )
{
if ( argc > 1 ) {
std::string intstring;
parse< must< query::expr, eof>, query::action >( 1, argv, intstring);
analyze< query::pred >();
parse< must< seq< query::pred, eof > >, query::action >( 1, argv, intstring);
}
}