diff --git a/parser/parser.cpp b/parser/parser.cpp index 5d89918e..ce211a21 100644 --- a/parser/parser.cpp +++ b/parser/parser.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include 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 << "" << in.string() << std::endl; + } + }; + + template<> struct action< or_ext > + { + static void apply( const input & in, std::string & string_value ) + { + std::cout << "" << 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 << "" << 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); } }