This commit is contained in:
Yuichi Nishiwaki 2014-03-04 23:58:07 +09:00
parent 818fb41f77
commit f0367d95a0
3 changed files with 31 additions and 38 deletions

View File

@ -24,15 +24,11 @@ enum {
typedef union YYSTYPE {
int i;
double f;
struct {
char *buf;
size_t len;
} str;
char c;
struct {
char *dat;
size_t len, capa;
} blob;
size_t len;
} buf;
char c;
} YYSTYPE;
struct parser_control {

View File

@ -89,7 +89,7 @@ read_datum(int tok, yyscan_t scanner)
switch (tok) {
case tSYMBOL:
return pic_symbol_value(pic_intern(pic, yylval.str.buf, yylval.str.len));
return pic_symbol_value(pic_intern(pic, yylval.buf.dat, yylval.buf.len));
case tINT:
return pic_int_value(yylval.i);
@ -104,13 +104,13 @@ read_datum(int tok, yyscan_t scanner)
return pic_char_value(yylval.c);
case tSTRING:
val = pic_obj_value(pic_str_new(pic, yylval.str.buf, yylval.str.len));
pic_free(pic, yylval.str.buf);
val = pic_obj_value(pic_str_new(pic, yylval.buf.dat, yylval.buf.len));
pic_free(pic, yylval.buf.dat);
return val;
case tBYTEVECTOR:
val = pic_obj_value(pic_blob_new(pic, yylval.blob.dat, yylval.blob.len));
pic_free(pic, yylval.blob.dat);
val = pic_obj_value(pic_blob_new(pic, yylval.buf.dat, yylval.buf.len));
pic_free(pic, yylval.buf.dat);
return val;
case tLPAREN:

View File

@ -111,34 +111,34 @@ infnan "+inf.0"|"-inf.0"|"+nan.0"|"-nan.0"
}
{identifier} {
yylval.str.buf = yytext;
yylval.str.len = yyleng;
yylval.buf.dat = yytext;
yylval.buf.len = yyleng;
return tSYMBOL;
}
"\"" {
BEGIN(STRING);
yylval.str.len = 0;
yylval.str.buf = yyalloc(yylval.str.len + 1, yyscanner);
strcpy(yylval.str.buf, "");
yylval.buf.len = 0;
yylval.buf.dat = yyalloc(yylval.buf.len + 1, yyscanner);
strcpy(yylval.buf.dat, "");
}
<STRING>[^\\"]+ {
yylval.str.len += yyleng;
yylval.str.buf = yyrealloc(yylval.str.buf, yylval.str.len + 1, yyscanner);
strcpy(yylval.str.buf + yylval.str.len - yyleng, yytext);
yylval.buf.len += yyleng;
yylval.buf.dat = yyrealloc(yylval.buf.dat, yylval.buf.len + 1, yyscanner);
strcpy(yylval.buf.dat + yylval.buf.len - yyleng, yytext);
}
<STRING>\\. {
yylval.str.len += 1;
yylval.str.buf = yyrealloc(yylval.str.buf, yylval.str.len + 1, yyscanner);
yylval.str.buf[yylval.str.len] = '\0';
yylval.buf.len += 1;
yylval.buf.dat = yyrealloc(yylval.buf.dat, yylval.buf.len + 1, yyscanner);
yylval.buf.dat[yylval.buf.len] = '\0';
switch (yytext[yyleng - 1]) {
case 'a': yylval.str.buf[yylval.str.len - 1] = '\a'; break;
case 'b': yylval.str.buf[yylval.str.len - 1] = '\b'; break;
case 't': yylval.str.buf[yylval.str.len - 1] = '\t'; break;
case 'n': yylval.str.buf[yylval.str.len - 1] = '\n'; break;
case 'r': yylval.str.buf[yylval.str.len - 1] = '\r'; break;
default: yylval.str.buf[yylval.str.len - 1] = yytext[yyleng - 1]; break;
case 'a': yylval.buf.dat[yylval.buf.len - 1] = '\a'; break;
case 'b': yylval.buf.dat[yylval.buf.len - 1] = '\b'; break;
case 't': yylval.buf.dat[yylval.buf.len - 1] = '\t'; break;
case 'n': yylval.buf.dat[yylval.buf.len - 1] = '\n'; break;
case 'r': yylval.buf.dat[yylval.buf.len - 1] = '\r'; break;
default: yylval.buf.dat[yylval.buf.len - 1] = yytext[yyleng - 1]; break;
}
}
<STRING>\\[:blank:]*\n[:blank:]* {
@ -165,9 +165,8 @@ infnan "+inf.0"|"-inf.0"|"+nan.0"|"-nan.0"
"#u8(" {
BEGIN(BYTEVECTOR);
yylval.blob.len = 0;
yylval.blob.capa = 10;
yylval.blob.dat = yyalloc(10, yyscanner);
yylval.buf.len = 0;
yylval.buf.dat = NULL;
}
<BYTEVECTOR>[ \r\n\t] {
/* skip whitespace */
@ -175,14 +174,12 @@ infnan "+inf.0"|"-inf.0"|"+nan.0"|"-nan.0"
<BYTEVECTOR>{uinteger} {
int i = atoi(yytext);
if (0 > i || i > 255) {
yyfree(yylval.blob.dat, yyscanner);
yyfree(yylval.buf.dat, yyscanner);
REJECT;
}
yylval.blob.dat[yylval.blob.len++] = (char)i;
if (yylval.blob.len > yylval.blob.capa) {
yylval.blob.capa *= 2;
yylval.blob.dat = yyrealloc(yylval.blob.dat, yylval.blob.capa, yyscanner);
}
yylval.buf.len += 1;
yylval.buf.dat = yyrealloc(yylval.buf.dat, yylval.buf.len, yyscanner);
yylval.buf.dat[yylval.buf.len - 1] = (char)i;
}
<BYTEVECTOR>")" {
BEGIN(INITIAL);