reentrant parser (but not for scanner)

This commit is contained in:
Yuichi Nishiwaki 2013-10-23 03:13:10 +09:00
parent dd3adf30e6
commit bb0bb0e186
2 changed files with 11 additions and 7 deletions

View File

@ -11,15 +11,16 @@ struct parser_control {
pic_state *pic;
pic_value value;
bool incomp;
int yynerrs;
};
void init_scanner(const char *);
void final_scanner(void);
void yyerror(struct parser_control *, const char *);
int yylex(struct parser_control *);
%}
%pure_parser
%parse-param {struct parser_control *p}
%lex-param {struct parser_control *p}
@ -118,6 +119,7 @@ void
yyerror(struct parser_control *p, const char *msg)
{
puts(msg);
p->yynerrs++;
}
bool
@ -127,12 +129,13 @@ pic_parse(pic_state *pic, const char *str, pic_value *v)
p.pic = pic;
p.incomp = false;
p.yynerrs = 0;
init_scanner(str);
yyparse(&p);
final_scanner();
if (yynerrs > 0) {
if (p.yynerrs > 0) {
p.value = pic_undef_value();
}

View File

@ -9,11 +9,12 @@ struct parser_control {
pic_state *pic;
pic_value value;
bool incomp;
int yynerrs;
};
static pic_value new_escaped_string(pic_state *, const char *);
#define YY_DECL int yylex (struct parser_control *p)
#define YY_DECL int yylex (YYSTYPE *yylvalp, struct parser_control *p)
%}
%option noinput
@ -43,11 +44,11 @@ string_elem [^\"\\]|"\\\""|"\\\\"
"(" return tLPAREN;
")" return tRPAREN;
"'" return tQUOTE;
{boolean} { yylval.datum = pic_bool_value(strcmp(yytext, "#t") == 0 || strcmp(yytext, "#true") == 0); return tBOOLEAN; }
{real} { yylval.datum = pic_float_value(atof(yytext)); return tNUMBER; }
{identifier} { yylval.datum = pic_intern_cstr(p->pic, yytext); return tSYMBOL; }
{boolean} { yylvalp->datum = pic_bool_value(strcmp(yytext, "#t") == 0 || strcmp(yytext, "#true") == 0); return tBOOLEAN; }
{real} { yylvalp->datum = pic_float_value(atof(yytext)); return tNUMBER; }
{identifier} { yylvalp->datum = pic_intern_cstr(p->pic, yytext); return tSYMBOL; }
{string} {
yylval.datum = new_escaped_string(p->pic, yytext);
yylvalp->datum = new_escaped_string(p->pic, yytext);
return tSTRING;
}