make the parser reentrant

This commit is contained in:
Florian Stinglmayr 2018-02-14 18:31:52 +00:00
parent baeea3eee6
commit 25227d5c6c
4 changed files with 43 additions and 13 deletions

View File

@ -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

View File

@ -8,11 +8,17 @@
#include <bsd/stdlib.h>
#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;
}

View File

@ -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;
}
%%

View File

@ -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)
{
}