diff --git a/makefile b/makefile index 02be748..0c94d89 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,7 @@ YACC = src/grammar/$(GRAMMAR).y BUILDPATH = ./build LOCPATH = ./bin -SRC = src/bot/handlers.c src/bot/context.c src/main.c src/grammar/types.h $(BUILDPATH)/$(GRAMMAR)/expenses.yy.c $(BUILDPATH)/$(GRAMMAR)/expenses.tab.c +SRC = src/grammar/types.c $(BUILDPATH)/$(GRAMMAR)/expenses.yy.c $(BUILDPATH)/$(GRAMMAR)/expenses.tab.c src/bot/handlers.c src/bot/context.c src/main.c INSTALLPATH = /usr/local CFLAGS = `pkg-config --cflags glib-2.0` LFLAGS = -lstrophe `pkg-config --libs glib-2.0` diff --git a/src/bot/handlers.c b/src/bot/handlers.c index 4ef20a6..47a2f22 100644 --- a/src/bot/handlers.c +++ b/src/bot/handlers.c @@ -60,7 +60,14 @@ intext = g_string_new(xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"))); printf("Incoming message from %s: %s\n", xmpp_stanza_get_attribute(stanza, "from"), intext->str); - + + /* parse with yyparse() */ + if(grammar_parse_string(intext) == 0) { + printf("===== Parse OK!\n"); + } else { + fprintf(stderr, "===== Parse error\n"); + } + reply = xmpp_stanza_new(ctx); xmpp_stanza_set_name(reply, "message"); xmpp_stanza_set_type(reply, xmpp_stanza_get_type(stanza)?xmpp_stanza_get_type(stanza):"chat"); @@ -80,7 +87,9 @@ xmpp_send(conn, reply); xmpp_stanza_release(reply); - free(replytext); + g_string_free(replytext, true); + g_string_free(intext, true); + return 1; } diff --git a/src/bot/handlers.h b/src/bot/handlers.h index 07a3618..8675f8a 100644 --- a/src/bot/handlers.h +++ b/src/bot/handlers.h @@ -7,6 +7,7 @@ #include #include #include +#include "../../build/expenses/expenses.tab.h" /* respond to version queries */ int version_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); diff --git a/src/grammar/expenses.l b/src/grammar/expenses.l index c3aa604..ec4727c 100644 --- a/src/grammar/expenses.l +++ b/src/grammar/expenses.l @@ -1,10 +1,10 @@ /* expenses.l: Expenses lexical analysis file + * TODO free memory allocated with g_string_new AFTER the parser is done (see grammar.y) */ %{ #include #include -#include "../../src/grammar/types.h" #include "expenses.tab.h" %} diff --git a/src/grammar/expenses.y b/src/grammar/expenses.y index b1e0769..0cb32d3 100644 --- a/src/grammar/expenses.y +++ b/src/grammar/expenses.y @@ -15,10 +15,14 @@ int yywrap() { return 1; -} +} %} +%code requires { +#include "../../src/grammar/types.h" +} + %define parse.lac full %define parse.error verbose @@ -48,7 +52,7 @@ line: '\n' | msg - | error { yyerrok; } + | error '\n' { yyerrok; } ; msg: update value TOK_USER TOK_STR { printf("update: %d %f %s %s\n", $1, $2, $3->str, $4->str); $$ = 0;} diff --git a/src/grammar/types.c b/src/grammar/types.c new file mode 100644 index 0000000..c4db6b3 --- /dev/null +++ b/src/grammar/types.c @@ -0,0 +1,11 @@ +/* function definition to parse a string instead of a file + */ +#include "types.h" + +int grammar_parse_string(const GString *buf) { + int res; + yy_scan_string(buf->str); + res = yyparse(); + yylex_destroy(); + return res; +} diff --git a/src/grammar/types.h b/src/grammar/types.h index 716dd38..12d9e15 100644 --- a/src/grammar/types.h +++ b/src/grammar/types.h @@ -1,6 +1,8 @@ -/* additional types for expenses +/* additional types for expenses grammar */ #pragma once +#include +#include typedef enum BotCommand { BOT_CMD_IN, @@ -15,3 +17,11 @@ BOT_TS_MONTHLY, BOT_TS_YEARLY } BotReportTimespan; + +int yylex(); +int yyparse(); +int yylex_destroy(); +typedef struct yy_buffer_state * YY_BUFFER_STATE; +YY_BUFFER_STATE yy_scan_string(const char * str); + +int grammar_parse_string(const GString *buf);