From f31ddb36a22e9c99ce4a2c2de14c2265b48d7b27 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 23 Oct 2013 03:45:57 +0900 Subject: [PATCH] reentrant scanner --- src/parse.y | 22 +++++++++++++++++----- src/scan.l | 18 ++++-------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/parse.y b/src/parse.y index c2214071..c5363b3c 100644 --- a/src/parse.y +++ b/src/parse.y @@ -9,15 +9,20 @@ struct parser_control { pic_state *pic; + void *yyscanner; pic_value value; bool incomp; int yynerrs; }; -void init_scanner(const char *); -void final_scanner(void); - void yyerror(struct parser_control *, const char *); + +/* just for supressing warnings. a little bit evil */ +int yylex(); +int yylex_(); +void yylex_init(); +void yy_scan_string(); +void yylex_destroy(); %} %pure_parser @@ -122,6 +127,12 @@ yyerror(struct parser_control *p, const char *msg) p->yynerrs++; } +int +yylex(YYSTYPE *yylvalp, struct parser_control *p) +{ + return yylex_(yylvalp, p->yyscanner, p); +} + bool pic_parse(pic_state *pic, const char *str, pic_value *v) { @@ -131,9 +142,10 @@ pic_parse(pic_state *pic, const char *str, pic_value *v) p.incomp = false; p.yynerrs = 0; - init_scanner(str); + yylex_init(&p.yyscanner); + yy_scan_string(str, p.yyscanner); yyparse(&p); - final_scanner(); + yylex_destroy(p.yyscanner); if (p.yynerrs > 0) { p.value = pic_undef_value(); diff --git a/src/scan.l b/src/scan.l index 3881b3f4..6f8af06b 100644 --- a/src/scan.l +++ b/src/scan.l @@ -7,6 +7,7 @@ struct parser_control { pic_state *pic; + void *yyscanner; pic_value value; bool incomp; int yynerrs; @@ -14,9 +15,10 @@ struct parser_control { static pic_value new_escaped_string(pic_state *, const char *); -#define YY_DECL int yylex (YYSTYPE *yylvalp, struct parser_control *p) +#define YY_DECL int yylex_ (YYSTYPE *yylvalp, yyscan_t yyscanner, struct parser_control *p) %} +%option reentrant %option noinput %option nounput @@ -55,7 +57,7 @@ string_elem [^\"\\]|"\\\""|"\\\\" %% int -yywrap() +yywrap(yyscan_t yyscanner) { return 1; } @@ -84,15 +86,3 @@ new_escaped_string(pic_state *pic, const char *str) pic_free(pic, new_str); return v; } - -void -init_scanner(const char *str) -{ - yy_scan_string(str); -} - -void -final_scanner() -{ - yylex_destroy(); -}