Code

frontend: Free dynamically allocated memory in the parser.
[sysdb.git] / src / frontend / grammar.y
index 8b2a8bf27c066d5a3cc0d12a481d01f5847d4d64..4f7a8913630c547b19c5edc86b4d320804ecd2ec 100644 (file)
 
 %{
 
-#include "frontend/connection.h"
+#include "frontend/connection-private.h"
 #include "frontend/parser.h"
 #include "frontend/grammar.h"
 
+#include "core/store.h"
+
 #include "utils/error.h"
 #include "utils/llist.h"
 
 #include <stdio.h>
+#include <string.h>
 
 int
 sdb_fe_yylex(YYSTYPE *yylval, YYLTYPE *yylloc, sdb_fe_yyscan_t yyscanner);
@@ -48,6 +51,9 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 /* quick access to the current parse tree */
 #define pt sdb_fe_yyget_extra(scanner)->parsetree
 
+/* quick access to the parser mode */
+#define parser_mode sdb_fe_yyget_extra(scanner)->mode
+
 %}
 
 %pure-parser
@@ -59,6 +65,8 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 %name-prefix="sdb_fe_yy"
 
 %union {
+       char *str;
+
        sdb_llist_t     *list;
        sdb_conn_node_t *node;
 }
@@ -67,18 +75,28 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 
 %token SCANNER_ERROR
 
-%token IDENTIFIER
-%token <node> LIST
+%token <str> IDENTIFIER
+%token <node> FETCH LIST
 
 %type <list> statements
 %type <node> statement
+       fetch_statement
        list_statement
+       expression
 
 %%
 
 statements:
        statements ';' statement
                {
+                       /* only accept this in default parse mode */
+                       if (parser_mode != SDB_PARSE_DEFAULT) {
+                               sdb_fe_yyerror(&yylloc, scanner,
+                                               YY_("syntax error, unexpected statement, "
+                                                       "expecting expression"));
+                               YYABORT;
+                       }
+
                        if ($3) {
                                sdb_llist_append(pt, SDB_OBJ($3));
                                sdb_object_deref(SDB_OBJ($3));
@@ -87,6 +105,30 @@ statements:
        |
        statement
                {
+                       /* only accept this in default parse mode */
+                       if (parser_mode != SDB_PARSE_DEFAULT) {
+                               sdb_fe_yyerror(&yylloc, scanner,
+                                               YY_("syntax error, unexpected statement, "
+                                                       "expecting expression"));
+                               YYABORT;
+                       }
+
+                       if ($1) {
+                               sdb_llist_append(pt, SDB_OBJ($1));
+                               sdb_object_deref(SDB_OBJ($1));
+                       }
+               }
+       |
+       expression
+               {
+                       /* only accept this in expression parse mode */
+                       if (! (parser_mode & SDB_PARSE_EXPR)) {
+                               sdb_fe_yyerror(&yylloc, scanner,
+                                               YY_("syntax error, unexpected expression, "
+                                                       "expecting statement"));
+                               YYABORT;
+                       }
+
                        if ($1) {
                                sdb_llist_append(pt, SDB_OBJ($1));
                                sdb_object_deref(SDB_OBJ($1));
@@ -95,6 +137,8 @@ statements:
        ;
 
 statement:
+       fetch_statement
+       |
        list_statement
        |
        /* empty */
@@ -103,11 +147,39 @@ statement:
                }
        ;
 
+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;
+               }
+       ;
+
 list_statement:
        LIST
                {
-                       $$ = sdb_object_create_T(/* name = */ NULL, sdb_conn_node_t);
-                       ((sdb_conn_node_t *)$$)->cmd = CONNECTION_LIST;
+                       $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
+                                               sdb_conn_node_t));
+                       $$->cmd = CONNECTION_LIST;
+               }
+       ;
+
+expression:
+       IDENTIFIER
+               {
+                       $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
+                                               conn_node_matcher_t));
+                       $$->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;
                }
        ;