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