beginnings of a parser
This commit is contained in:
parent
ee6c6eb30f
commit
b515b4b6d9
|
@ -0,0 +1,65 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <pegtl.hh>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
Loading…
Reference in New Issue