Build a String value in the lexer for string constants

This commit is contained in:
Josh Holtrop 2018-04-07 09:26:39 -04:00
parent 961b3297f5
commit aaf47bf934

View File

@ -5,9 +5,12 @@
#include "parser.h" #include "parser.h"
#include "parser.tab.h" #include "parser.tab.h"
#include "String.h"
#define YY_USER_ACTION yylloc->first_column += yyleng; #define YY_USER_ACTION yylloc->first_column += yyleng;
static String * build_string = NULL;
%} %}
%x str %x str
@ -97,21 +100,30 @@ sizeof return TOK_SIZEOF;
0[xX][0-9a-fA-F]+([uU][lL]?[lL]?)? return TOK_INT_CONST; 0[xX][0-9a-fA-F]+([uU][lL]?[lL]?)? return TOK_INT_CONST;
[0-9]*\.[0-9]*[fF]? return TOK_FLOAT_CONST; [0-9]*\.[0-9]*[fF]? return TOK_FLOAT_CONST;
\" BEGIN(str); \" {
if (build_string != NULL)
{
String_free(build_string);
}
build_string = String_new("");
BEGIN(str);
}
<str>{ <str>{
\" return TOK_STR_CONST; \" return TOK_STR_CONST;
\\x[0-9A-Fa-f]{2} { \\x[0-9A-Fa-f]{2} {
/* hexadecimal escape code */ /* hexadecimal escape code */
unsigned int val; unsigned int val;
(void) sscanf(yytext + 2, "%x", &val); (void) sscanf(yytext + 2, "%x", &val);
char v[2] = {(char)val, '\0'};
String_concat(build_string, v);
} }
\\n (void)'\n'; \\n String_concat(build_string, "\n");
\\t (void)'\t'; \\t String_concat(build_string, "\t");
\\r (void)'\r'; \\r String_concat(build_string, "\r");
\\b (void)'\b'; \\b String_concat(build_string, "\b");
\\f (void)'\f'; \\f String_concat(build_string, "\f");
\\. (void)yytext[1]; \\. String_concat(build_string, &yytext[1]);
[^\\\"]+ (void)yytext; [^\\\"]+ String_concat(build_string, yytext);
} }
[a-zA-Z_][a-zA-Z_0-9]* return TOK_IDENTIFIER; [a-zA-Z_][a-zA-Z_0-9]* return TOK_IDENTIFIER;