diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y
index c3ee0af37886d3391d37f0d8fd425a586d0257c1..34a9c6b6474502d9fc059df45a6ed93a79890567 100644 (file)
--- a/src/frontend/grammar.y
+++ b/src/frontend/grammar.y
#include "frontend/grammar.h"
#include "core/store.h"
+#include "core/store-private.h"
#include "utils/error.h"
#include "utils/llist.h"
%locations
%error-verbose
%expect 0
-%name-prefix="sdb_fe_yy"
+%name-prefix "sdb_fe_yy"
%union {
+ const char *sstr; /* static string */
char *str;
+ sdb_data_t data;
+
sdb_llist_t *list;
sdb_conn_node_t *node;
+
+ sdb_store_matcher_t *m;
}
%start statements
%token SCANNER_ERROR
+%token AND OR NOT WHERE
+%token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
+%token CMP_LT CMP_LE CMP_GE CMP_GT
+
+%token FETCH LIST LOOKUP
+
%token <str> IDENTIFIER STRING
-%token <node> FETCH LIST
+
+%token <data> INTEGER FLOAT
+
+/* Precedence (lowest first): */
+%left OR
+%left AND
+%right NOT
+%left CMP_EQUAL CMP_NEQUAL
+%left CMP_LT CMP_LE CMP_GE CMP_GT
+%left CMP_REGEX CMP_NREGEX
+%left '(' ')'
+%left '.'
%type <list> statements
%type <node> statement
fetch_statement
list_statement
+ lookup_statement
expression
+%type <m> matcher
+ compare_matcher
+
+%type <sstr> op
+
+%type <data> data
+
+%destructor { free($$); } <str>
+%destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m>
+
%%
statements:
sdb_fe_yyerror(&yylloc, scanner,
YY_("syntax error, unexpected statement, "
"expecting expression"));
+ sdb_object_deref(SDB_OBJ($3));
YYABORT;
}
sdb_fe_yyerror(&yylloc, scanner,
YY_("syntax error, unexpected statement, "
"expecting expression"));
+ sdb_object_deref(SDB_OBJ($1));
YYABORT;
}
sdb_fe_yyerror(&yylloc, scanner,
YY_("syntax error, unexpected expression, "
"expecting statement"));
+ sdb_object_deref(SDB_OBJ($1));
YYABORT;
}
|
list_statement
|
+ lookup_statement
+ |
/* empty */
{
$$ = NULL;
}
;
+/*
+ * FETCH <hostname>;
+ *
+ * Retrieve detailed information about a single host.
+ */
fetch_statement:
- FETCH IDENTIFIER
- {
- $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
- conn_fetch_t, conn_fetch_destroy));
- CONN_FETCH($$)->name = strdup($2);
- $$->cmd = CONNECTION_FETCH;
- free($2);
- $2 = NULL;
- }
- |
FETCH STRING
{
$$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
conn_fetch_t, conn_fetch_destroy));
CONN_FETCH($$)->name = strdup($2);
$$->cmd = CONNECTION_FETCH;
- free($2);
- $2 = NULL;
+ free($2); $2 = NULL;
}
;
+/*
+ * LIST;
+ *
+ * Returns a list of all hosts in the store.
+ */
list_statement:
LIST
{
}
;
+/*
+ * LOOKUP <type> WHERE <expression>;
+ *
+ * Returns detailed information about <type> matching expression.
+ */
+lookup_statement:
+ LOOKUP IDENTIFIER WHERE expression
+ {
+ /* TODO: support other types as well */
+ if (strcasecmp($2, "hosts")) {
+ char errmsg[strlen($2) + 32];
+ snprintf(errmsg, sizeof(errmsg),
+ YY_("unknown table %s"), $2);
+ sdb_fe_yyerror(&yylloc, scanner, errmsg);
+ free($2); $2 = NULL;
+ sdb_object_deref(SDB_OBJ($4));
+ YYABORT;
+ }
+
+ $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+ conn_lookup_t, conn_lookup_destroy));
+ CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
+ $$->cmd = CONNECTION_LOOKUP;
+ free($2); $2 = NULL;
+ }
+ ;
+
expression:
- IDENTIFIER
+ matcher
{
- $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
- conn_node_matcher_t));
+ if (! $1) {
+ /* TODO: improve error reporting */
+ sdb_fe_yyerror(&yylloc, scanner,
+ YY_("syntax error, invalid expression"));
+ YYABORT;
+ }
+
+ $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+ conn_node_matcher_t, conn_matcher_destroy));
$$->cmd = CONNECTION_EXPR;
- /* XXX: this is just a placeholder for now */
- CONN_MATCHER($$)->matcher = sdb_store_host_matcher($1,
- /* name_re = */ NULL, /* service = */ NULL,
- /* attr = */ NULL);
- free($1);
- $1 = NULL;
+ CONN_MATCHER($$)->matcher = $1;
+ }
+ ;
+
+matcher:
+ '(' matcher ')'
+ {
+ $$ = $2;
+ }
+ |
+ matcher AND matcher
+ {
+ $$ = sdb_store_con_matcher($1, $3);
+ sdb_object_deref(SDB_OBJ($1));
+ sdb_object_deref(SDB_OBJ($3));
+ }
+ |
+ matcher OR matcher
+ {
+ $$ = sdb_store_dis_matcher($1, $3);
+ sdb_object_deref(SDB_OBJ($1));
+ sdb_object_deref(SDB_OBJ($3));
+ }
+ |
+ NOT matcher
+ {
+ $$ = sdb_store_inv_matcher($2);
+ sdb_object_deref(SDB_OBJ($2));
+ }
+ |
+ compare_matcher
+ {
+ $$ = $1;
}
;
+/*
+ * <object_type>.<object_attr> <op> <value>
+ *
+ * Parse matchers comparing object attributes with a value.
+ */
+compare_matcher:
+ IDENTIFIER '.' IDENTIFIER op data
+ {
+ $$ = sdb_store_matcher_parse_cmp($1, $3, $4, &$5);
+ free($1); $1 = NULL;
+ free($3); $3 = NULL;
+ sdb_data_free_datum(&$5);
+ }
+ ;
+
+op:
+ CMP_EQUAL { $$ = "="; }
+ |
+ CMP_NEQUAL { $$ = "!="; }
+ |
+ CMP_REGEX { $$ = "=~"; }
+ |
+ CMP_NREGEX { $$ = "!~"; }
+ |
+ CMP_LT { $$ = "<"; }
+ |
+ CMP_LE { $$ = "<="; }
+ |
+ CMP_GE { $$ = ">="; }
+ |
+ CMP_GT { $$ = ">"; }
+ ;
+
+data:
+ STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
+ |
+ INTEGER { $$ = $1; }
+ |
+ FLOAT { $$ = $1; }
+ ;
+
%%
void