added: support for basic escape sequences

This commit is contained in:
Sven Vogel 2024-06-23 15:14:42 +02:00
parent f142f6dc30
commit a1b6757635
3 changed files with 44 additions and 1 deletions

View File

@ -98,7 +98,10 @@
yytext = yytext +1; yytext = yytext +1;
yytext[yyleng - 2] = 0; yytext[yyleng - 2] = 0;
DEBUG("\"%s\" tokenized with \'ValStr\'", yytext); yylval.string = mem_strdup(MemoryNamespaceLex, yytext); return(ValStr);}; DEBUG("\"%s\" tokenized with \'ValStr\'", yytext);
yylval.string = collapse_escape_sequences(yytext);
return(ValStr);
};
\"\"\"[^\"]*\"\"\" { \"\"\"[^\"]*\"\"\" {
yytext = yytext +3; yytext = yytext +3;
yytext[yyleng - 6] = 0; yytext[yyleng - 6] = 0;

View File

@ -84,3 +84,41 @@ int getNextLine(void) {
return 0; return 0;
} }
struct ConstEscSeq {
char* esc;
char* rep;
};
static struct ConstEscSeq sequences[] = {
{
"\\n",
"\n"
},
{
"\\\\",
"\\"
},
{
"\\t",
"\t"
},
{
"\\r",
"\r"
}
};
char* collapse_escape_sequences(char* string) {
GString* unesc = g_string_new(string);
for (int i = 0; i < 4; i++) {
g_string_replace(unesc, sequences[i].esc, sequences[i].rep, 0);
}
char* str = mem_strdup(MemoryNamespaceLex, unesc->str);
g_string_free(unesc, TRUE);
return str;
}

View File

@ -36,4 +36,6 @@ int nextChar(char *dst);
*/ */
int getNextLine(void); int getNextLine(void);
char* collapse_escape_sequences(char* string);
#endif // LEX_UTIL_H_ #endif // LEX_UTIL_H_