use start state to parse strings

This commit is contained in:
Yuichi Nishiwaki 2013-10-23 13:44:45 +09:00
parent dd7958c3d6
commit cc025626d5
1 changed files with 13 additions and 32 deletions

View File

@ -13,7 +13,7 @@ struct parser_control {
int yynerrs; int yynerrs;
}; };
static pic_value new_escaped_string(pic_state *, const char *); void yyerror(struct parser_control *p, const char *msg);
#define YY_DECL int yylex_ (YYSTYPE *yylvalp, yyscan_t yyscanner, struct parser_control *p) #define YY_DECL int yylex_ (YYSTYPE *yylvalp, yyscan_t yyscanner, struct parser_control *p)
%} %}
@ -36,8 +36,7 @@ sign [+-]?
infnan "+inf.0"|"-inf.0"|"+nan.0"|"-nan.0" infnan "+inf.0"|"-inf.0"|"+nan.0"|"-nan.0"
/* string */ /* string */
string \"{string_elem}*\" %x STRING
string_elem [^\"\\]|"\\\""|"\\\\"
%% %%
@ -52,10 +51,17 @@ string_elem [^\"\\]|"\\\""|"\\\\"
{boolean} { yylvalp->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} { yylvalp->datum = pic_float_value(atof(yytext)); return tNUMBER; } {real} { yylvalp->datum = pic_float_value(atof(yytext)); return tNUMBER; }
{identifier} { yylvalp->datum = pic_intern_cstr(p->pic, yytext); return tSYMBOL; } {identifier} { yylvalp->datum = pic_intern_cstr(p->pic, yytext); return tSYMBOL; }
{string} { "\"" BEGIN(STRING);
yylvalp->datum = new_escaped_string(p->pic, yytext); <STRING>{
return tSTRING; [^\\"]* yymore();
} <<EOF>> { yyerror(p, "eof in string"); BEGIN(INITIAL); }
"\"" {
yytext[yyleng-1] = '\0';
yylvalp->datum = pic_str_new_cstr(p->pic, yytext);
BEGIN(INITIAL);
return tSTRING;
}
}
%% %%
@ -64,28 +70,3 @@ yywrap(yyscan_t yyscanner)
{ {
return 1; return 1;
} }
static pic_value
new_escaped_string(pic_state *pic, const char *str)
{
size_t len;
char *new_str;
int i,j;
pic_value v;
len = strlen(str);
new_str = (char *)pic_alloc(pic, len + 1);
for (j = 0, i = 1; i < len - 1; ++i, ++j) {
if (str[i] == '\\') {
++i;
new_str[j] = str[i];
continue;
}
new_str[j] = str[i];
}
new_str[j] = '\0';
v = pic_str_new_cstr(pic, new_str);
pic_free(pic, new_str);
return v;
}