aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.me.uk>2020-01-12 18:15:27 +0100
committerThomas White <taw@bitwiz.me.uk>2020-01-12 18:26:37 +0100
commit07fb4674904bac673ddbb5d6ce4afbe959c4dcd3 (patch)
tree4f8742bf77387d66b198044ea94ec6c209d729eb
parentd4071cb956c80143d188813d8064333946c38ecf (diff)
Make the lexer and parser re-entrant
-rw-r--r--libstorycode/storycode.c13
-rw-r--r--libstorycode/storycode.l24
-rw-r--r--libstorycode/storycode.y16
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; }
<cond_align,cond_sqb>(?i:center) { return SC_CENTER; }
<cond_align,cond_sqb>(?i:right) { return SC_RIGHT; }
-<cond_font>.*\n { sclval.str = strdup(yytext);
- sclval.str[yyleng-1] = '\0';
+<cond_font>.*\n { yylval->str = strdup(yytext);
+ yylval->str[yyleng-1] = '\0';
BEGIN(0);
lineno++;
return SC_FONTNAME; }
<INITIAL>IMAGE { BEGIN(cond_image); return SC_IMAGEFRAME; }
<cond_image>:[ ] { BEGIN(cond_image_filename); return SC_TEXT_START; }
-<cond_image_filename>[^\n]* { sclval.str = strdup(yytext);
+<cond_image_filename>[^\n]* { yylval->str = strdup(yytext);
lineno++;
return SC_FILENAME; }
<INITIAL>: { BEGIN(cond_prerun); return SC_TEXT_START; }
<cond_prerun>[ ] { BEGIN(cond_runtext); }
<cond_runtext>[\\] { BEGIN(cond_stringesc); }
-<cond_stringesc>. { sclval.str = strdup(yytext); BEGIN(cond_runtext); return SC_RUN_TEXT; }
+<cond_stringesc>. { yylval->str = strdup(yytext); BEGIN(cond_runtext); return SC_RUN_TEXT; }
<cond_runtext>[\*] { return '*'; }
<cond_runtext>[/] { return '/'; }
<cond_runtext>[_] { return '_'; }
-<cond_runtext>[^\\\*/_\n]* { sclval.str = strdup(yytext);
- sclval.str[yyleng] = '\0';
+<cond_runtext>[^\\\*/_\n]* { yylval->str = strdup(yytext);
+ yylval->str[yyleng] = '\0';
return SC_RUN_TEXT; }
<cond_runtext>\n { BEGIN(0); lineno++; }
<cond_prerun>\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 ','; }
-<cond_col>#[[:xdigit:]]{6} { sclval.str = strdup(yytext); return SC_HEXCOL; }
+<cond_col>#[[: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 <stdlib.h>
#include <string.h>
- 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 <val> VALUE
%type <grad> 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);
}