From 07fb4674904bac673ddbb5d6ce4afbe959c4dcd3 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 12 Jan 2020 18:15:27 +0100 Subject: Make the lexer and parser re-entrant --- libstorycode/storycode.c | 13 +++++++------ libstorycode/storycode.l | 24 +++++++++++++----------- libstorycode/storycode.y | 16 +++++++++------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/libstorycode/storycode.c b/libstorycode/storycode.c index 983ae02..89c0440 100644 --- a/libstorycode/storycode.c +++ b/libstorycode/storycode.c @@ -46,14 +46,15 @@ Narrative *storycode_parse_presentation(const char *sc) { YY_BUFFER_STATE b; Narrative *n; + yyscan_t scanner; - //BEGIN(0); - b = sc_scan_string(sc); - scdebug = 1; + yylex_init(&scanner); + b = yy_scan_string(sc, scanner); + yydebug = 0; n = narrative_new(); - scparse(n); - sc_delete_buffer(b); - //narrative_debug(n); + yyparse(n, scanner); + yy_delete_buffer(b, scanner); + yylex_destroy(scanner); return n; } diff --git a/libstorycode/storycode.l b/libstorycode/storycode.l index e7c4d15..05a5a06 100644 --- a/libstorycode/storycode.l +++ b/libstorycode/storycode.l @@ -33,8 +33,10 @@ int sqb_caller = 0; %} -%option prefix="sc" -%option noyywrap nounput noinput +%option noyywrap nounput noinput never-interactive nounistd +%option reentrant +%option bison-bridge bison-locations + %s cond_geom %s cond_font %s cond_filename @@ -75,27 +77,27 @@ BGCOL { BEGIN(cond_col); return SC_BGCOL; } (?i:center) { return SC_CENTER; } (?i:right) { return SC_RIGHT; } -.*\n { sclval.str = strdup(yytext); - sclval.str[yyleng-1] = '\0'; +.*\n { yylval->str = strdup(yytext); + yylval->str[yyleng-1] = '\0'; BEGIN(0); lineno++; return SC_FONTNAME; } IMAGE { BEGIN(cond_image); return SC_IMAGEFRAME; } :[ ] { BEGIN(cond_image_filename); return SC_TEXT_START; } -[^\n]* { sclval.str = strdup(yytext); +[^\n]* { yylval->str = strdup(yytext); lineno++; return SC_FILENAME; } : { BEGIN(cond_prerun); return SC_TEXT_START; } [ ] { BEGIN(cond_runtext); } [\\] { BEGIN(cond_stringesc); } -. { sclval.str = strdup(yytext); BEGIN(cond_runtext); return SC_RUN_TEXT; } +. { yylval->str = strdup(yytext); BEGIN(cond_runtext); return SC_RUN_TEXT; } [\*] { return '*'; } [/] { return '/'; } [_] { return '_'; } -[^\\\*/_\n]* { sclval.str = strdup(yytext); - sclval.str[yyleng] = '\0'; +[^\\\*/_\n]* { yylval->str = strdup(yytext); + yylval->str[yyleng] = '\0'; return SC_RUN_TEXT; } \n { BEGIN(0); lineno++; } \n { BEGIN(0); lineno++; } @@ -107,12 +109,12 @@ BGCOL { BEGIN(cond_col); return SC_BGCOL; } [{] { return '{'; } [}] { return '}'; } [. ] {} -[0-9\.]+ { sclval.val = atof(yytext); return SC_VALUE; } -[uf] { sclval.character = yytext[0]; return SC_UNIT; } +[0-9\.]+ { yylval->val = atof(yytext); return SC_VALUE; } +[uf] { yylval->character = yytext[0]; return SC_UNIT; } [+] { return '+'; } [x] { return 'x'; } [,] { return ','; } -#[[:xdigit:]]{6} { sclval.str = strdup(yytext); return SC_HEXCOL; } +#[[:xdigit:]]{6} { yylval->str = strdup(yytext); return SC_HEXCOL; } %% diff --git a/libstorycode/storycode.y b/libstorycode/storycode.y index f3c2653..0e00e36 100644 --- a/libstorycode/storycode.y +++ b/libstorycode/storycode.y @@ -21,8 +21,11 @@ */ %define api.token.prefix {SC_} -%define api.prefix {sc} +%define api.pure full %locations +%lex-param {yscan_t scanner} +%parse-param {Narrative *n}; +%parse-param {yyscan_t scanner}; %code requires { @@ -71,6 +74,8 @@ int max_paras; }; + typedef void *yyscan_t; + } %union { @@ -103,9 +108,8 @@ #include #include - extern int sclex(); - extern int scparse(); - void scerror(Narrative *n, const char *s); + extern int yylex(); + void yyerror(YYLTYPE *locp, Narrative *n, yyscan_t scanner, const char *s); extern int lineno; %} @@ -173,8 +177,6 @@ %type VALUE %type gradtype -%parse-param { Narrative *n }; - %{ static void merge_style(struct parse_style *combined, struct parse_style inp) @@ -620,6 +622,6 @@ styledef: %% -void scerror(Narrative *n, const char *s) { +void yyerror(YYLTYPE *locp, Narrative *n, yyscan_t scanner, const char *s) { printf("Storycode parse error at line %i\n", lineno); } -- cgit v1.2.3