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

View File

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