From b515b4b6d9d0cbb3d290ff26273595d8b480b0eb Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Thu, 5 Nov 2015 16:21:19 -0800 Subject: [PATCH] beginnings of a parser --- parser/parser.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++ parser/query.abnf | 23 +++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 parser/parser.cpp create mode 100644 parser/query.abnf diff --git a/parser/parser.cpp b/parser/parser.cpp new file mode 100644 index 00000000..5d89918e --- /dev/null +++ b/parser/parser.cpp @@ -0,0 +1,65 @@ +#include +#include + +#include + +using namespace pegtl; + +namespace query +{ + // strings + struct unicode : list< seq< one< 'u' >, rep< 4, must< xdigit > > >, one< '\\' > > {}; + struct escaped_char : one< '"', '\\', '/', 'b', 'f', 'n', 'r', 't' > {}; + struct escaped : sor< escaped_char, unicode > {}; + struct unescaped : utf8::range< 0x20, 0x10FFFF > {}; + struct char_ : if_then_else< one< '\\' >, must< escaped >, unescaped > {}; + + struct string_content : until< at< one< '"' > >, must< char_ > > {}; + struct string : seq< one< '"' >, must< string_content >, any > + { + using content = string_content; + }; + + // 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 > > {}; + + // key paths + struct key_path : list< must< 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< '>', '=' > > {}; + + // predicates + struct pred : seq< expr, plus< blank >, oper, plus< blank >, expr > {}; + + // rules + template< typename Rule > + struct action : nothing< Rule > {}; + template<> struct action< expr > + { + static void apply( const input & in, std::string & string_value ) + { + std::cout << in.string() << std::endl; + } + }; +} + +int main( int argc, char ** argv ) +{ + if ( argc > 1 ) { + std::string intstring; + parse< must< query::expr, eof>, query::action >( 1, argv, intstring); + } +} + diff --git a/parser/query.abnf b/parser/query.abnf new file mode 100644 index 00000000..d70416d3 --- /dev/null +++ b/parser/query.abnf @@ -0,0 +1,23 @@ + + +pred = expr 1*WSP oper 1*WSP expr +;pred =/ "(" *WSP pred *WSP ")" +;pred =/ ("NOT" / "!") 1*WSP pred +;pred =/ pred 1*WSP ("OR" / "||") !*WSP pred +;pred =/ pred 1*WSP ("AND" / "&&") 1*WSP pred + +oper = "=" / "==" / "!=" / "<" / "<=" / ">" / ">=" + +expr = string / num / key-path + +key-path = key *("." key) +key = 1*(ALPHA / "_") + +string = dq-string / sq-string +sq-string = "'" *(%x20-ffffffff) "'" +dq-string = DQUOTE *("\\" / %x20-21 / %x23-ffffffff) DQUOTE + +num = float-num / int-num / hex-num +float-num = ["-"] (*DIGIT "." 1*DIGIT / "." 1*DIGIT) +int-num = ["-"] 1*DIGIT +hex-num = ["-"] ["0"] "x" 1*HEXDIG