From 25227d5c6c460cfd46ee872dcab0774d9984a465 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Wed, 14 Feb 2018 18:31:52 +0000 Subject: [PATCH] make the parser reentrant --- Makefile.am | 6 +++--- lib/dice.c | 27 ++++++++++++++++++++++----- lib/dice_lexer.l | 14 ++++++++++++-- lib/dice_parse.y | 9 ++++++--- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/Makefile.am b/Makefile.am index 8077840..08f40a4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,10 +2,10 @@ AM_YFLAGS = -d BUILT_SOURCES = lib/dice_parse.h lib_LTLIBRARIES = libdice.la -libdice_la_SOURCES = lib/dice.h \ - lib/dice.c \ +libdice_la_SOURCES = lib/dice_lexer.l \ lib/dice_parse.y \ - lib/dice_lexer.l + lib/dice.h \ + lib/dice.c AM_CFLAGS = -Ilib diff --git a/lib/dice.c b/lib/dice.c index 5d98399..595931d 100644 --- a/lib/dice.c +++ b/lib/dice.c @@ -8,11 +8,17 @@ #include #endif -extern void *yy_scan_string(char const *s); -extern void yy_delete_buffer(void *b); +extern int yylex_init(void **state); +extern int yylex_destroy(void *state); +extern void yylex(void *state); +extern void yy_switch_to_buffer(void *buffer, void *scanner); +extern void *yy_scan_string(char const *s, void *scanner); +extern void yy_delete_buffer(void *b, void *scanner); struct dice_ { + int consumed; + uint32_t amount; uint32_t sides; @@ -77,6 +83,7 @@ dice_t dice_simple(uint32_t amount, uint32_t sides) dice_t dice_parse(char const *s) { + void *scanner = NULL; dice_t d = NULL; void *buffer = NULL; int ret = 0; @@ -86,9 +93,14 @@ dice_t dice_parse(char const *s) return NULL; } - buffer = yy_scan_string(s); - ret = yyparse(d); - yy_delete_buffer(buffer); + yylex_init(&scanner); + buffer = yy_scan_string(s, scanner); + yy_switch_to_buffer(buffer, scanner); + + ret = yyparse(scanner, d); + + yy_delete_buffer(buffer, scanner); + yylex_destroy(scanner); if (ret) { dice_free(d); @@ -176,3 +188,8 @@ bool dice_evaluate(dice_t d, dice_result_t **res, size_t *reslen) return true; } + +void dice_update_consumned(dice_t d, int offset) +{ + d->consumed += offset; +} diff --git a/lib/dice_lexer.l b/lib/dice_lexer.l index 847ce53..1ec369b 100644 --- a/lib/dice_lexer.l +++ b/lib/dice_lexer.l @@ -1,9 +1,19 @@ +%option bison-bridge +%option reentrant + %{ #include "dice.h" #include "dice_parse.h" + %} %% -[0-9]+ { yylval.integer = atoi(yytext); return TOK_INTEGER; } -[d] return TOK_DICESEP; +[0-9]+ { + yylval->integer = atoi(yytext); + return TOK_INTEGER; +} + +[d] { + return TOK_DICESEP; +} %% diff --git a/lib/dice_parse.y b/lib/dice_parse.y index 014a05b..dfa9438 100644 --- a/lib/dice_parse.y +++ b/lib/dice_parse.y @@ -1,11 +1,14 @@ -%parse-param {dice_t dice} +%define api.pure full + +%parse-param {void *scanner} {dice_t dice} +%lex-param {void *scanner} %{ #include "dice.h" -extern int yylex(void); +extern int yylex(void *lval, void *scanner); -void yyerror(dice_t dice, char const *err) +void yyerror(void *scanner, dice_t dice, char const *err) { }