jaf: process escapes in string constants

This commit is contained in:
Nunuhara Cabbage
2020-07-05 19:35:17 -07:00
parent 0970e0c7da
commit 8fe1001fc3
5 changed files with 32 additions and 5 deletions
+1
View File
@@ -289,6 +289,7 @@ struct jaf_expression *jaf_integer(int i);
struct jaf_expression *jaf_parse_integer(struct string *text);
struct jaf_expression *jaf_float(float f);
struct jaf_expression *jaf_parse_float(struct string *text);
struct string *jaf_process_string(struct string *text);
struct jaf_expression *jaf_string(struct string *text);
struct jaf_expression *jaf_identifier(struct string *name);
struct jaf_expression *jaf_unary_expr(enum jaf_operator op, struct jaf_expression *expr);
+27
View File
@@ -69,6 +69,33 @@ struct jaf_expression *jaf_parse_float(struct string *text)
return jaf_float(f);
}
struct string *jaf_process_string(struct string *text)
{
char *src, *dst;
for (src = dst = text->text; *src;) {
if (*src == '\\') {
src++;
switch (*src++) {
case 'n': *dst++ = '\n'; break;
case 'r': *dst++ = '\r'; break;
case 'b': *dst++ = '\b'; break;
case '"': *dst++ = '"'; break;
case '\\': *dst++ = '\\'; break;
default: ERROR("Unhandled escape sequence in string");
}
} else if (*src == '"') {
src++;
while (*src++ != '"');
} else {
*dst++ = *src++;
}
}
*dst = '\0';
text->size = dst - text->text;
return text;
}
struct jaf_expression *jaf_string(struct string *text)
{
struct jaf_expression *e = jaf_expr(JAF_EXP_STRING, 0);
+2 -4
View File
@@ -34,8 +34,6 @@ E ([Ee][+-]?{D}+)
P ([Pp][+-]?{D}+)
FS (f|F|l|L)
IS (((u|U)(l|L|ll|LL)?)|((l|L|ll|LL)(u|U)?))
CP (u|U|L)
SP (u8|u|U|L)
ES (\\(['"\?\\abfnrtv]|[0-7]{1,3}|x[a-fA-F0-9]+))
WS [ \t\v\f]
@@ -99,7 +97,7 @@ unsigned long jaf_line = 1;
{HP}{H}+{IS}? { RETURN_STRING(I_CONSTANT); }
{NZ}{D}*{IS}? { RETURN_STRING(I_CONSTANT); }
"0"{O}*{IS}? { RETURN_STRING(I_CONSTANT); }
{CP}?"'"([^'\\\n]|{ES})+"'" { RETURN_STRING(I_CONSTANT); }
"'"([^'\\\n]|{ES})+"'" { RETURN_STRING(I_CONSTANT); }
{D}+{E}{FS}? { RETURN_STRING(F_CONSTANT); }
{D}*"."{D}+{E}?{FS}? { RETURN_STRING(F_CONSTANT); }
@@ -108,7 +106,7 @@ unsigned long jaf_line = 1;
{HP}{H}*"."{H}+{P}{FS}? { RETURN_STRING(F_CONSTANT); }
{HP}{H}+"."{P}{FS}? { RETURN_STRING(F_CONSTANT); }
({SP}?\"([^"\\\n]|{ES})*\"{WS}*)+ { yylval.string = make_string(yytext+1, yyleng-2); return STRING_LITERAL; }
(\"([^"\\\n]|{ES})*\"{WS}*)+ { yylval.string = make_string(yytext+1, yyleng-2); return STRING_LITERAL; }
"..." { return ELLIPSIS; }
">>=" { return RIGHT_ASSIGN; }
+1 -1
View File
@@ -223,7 +223,7 @@ enumeration_constant /* before it has been defined as such */
;
string
: STRING_LITERAL { $$ = $1; }
: STRING_LITERAL { $$ = jaf_process_string($1); }
| FILE_MACRO { $$ = cstr_to_string(jaf_file); }
| LINE_MACRO { $$ = integer_to_string(jaf_line); }
| FUNC_MACRO { ERROR("__FUNC__ not supported"); }
+1
View File
@@ -59,6 +59,7 @@ cd $(dirname "$0")
./test-runner.sh s-gte.jaf
./test-runner.sh s-eq.jaf
./test-runner.sh s-neq.jaf
./test-runner.sh string-constant.jaf
#./test-runner.sh s-length.jaf
#./test-runner.sh s-lengthbyte.jaf
#./test-runner.sh s-empty.jaf