🐛 string escape quote.

main
bog 2023-12-10 22:43:48 +01:00
parent f4eb33c76c
commit 9c74cce2a4
2 changed files with 41 additions and 5 deletions

View File

@ -184,8 +184,8 @@ node_t* lexer_try_new_str(lexer_t* lexer)
{ {
assert(lexer); assert(lexer);
size_t cursor = lexer->cursor; ssize_t cursor = lexer->cursor;
size_t len = strlen(lexer->source); ssize_t len = strlen(lexer->source);
str_t res_str; str_t res_str;
str_init(&res_str); str_init(&res_str);
@ -200,10 +200,43 @@ node_t* lexer_try_new_str(lexer_t* lexer)
while (cursor < len while (cursor < len
&& lexer->source[cursor] != '"') && lexer->source[cursor] != '"')
{
if (lexer->source[cursor] == '\\')
{
if (cursor + 1 < len)
{
switch (lexer->source[cursor + 1])
{
case '"': {
str_push(&res_str, '"');
cursor += 2;
} break;
case 'n': {
str_push(&res_str, '\n');
cursor += 2;
} break;
case 't': {
str_push(&res_str, '\t');
cursor += 2;
} break;
case 'r': {
str_push(&res_str, '\r');
cursor += 2;
} break;
default: cursor++; break;
}
}
}
else
{ {
str_push(&res_str, lexer->source[cursor]); str_push(&res_str, lexer->source[cursor]);
cursor++; cursor++;
} }
}
if (cursor >= len || lexer->source[cursor] != '"') if (cursor >= len || lexer->source[cursor] != '"')
{ {

View File

@ -55,7 +55,7 @@ static void test_lexer(char const* source, size_t n, ...)
node_str(tok, tok_str, SZ); node_str(tok, tok_str, SZ);
char* oracle = va_arg(lst, char*); char* oracle = va_arg(lst, char*);
cr_assert_str_eq(oracle, tok_str); cr_assert_str_eq(tok_str, oracle);
node_free(tok); node_free(tok);
} }
@ -101,4 +101,7 @@ Test(lexer, str) {
test_lexer(" \"bonjour\" \" monde \"", 2, test_lexer(" \"bonjour\" \" monde \"", 2,
"STR[bonjour]", "STR[bonjour]",
"STR[ monde ]"); "STR[ monde ]");
test_lexer("\"the \\\"cat\\\"\"", 1, "STR[the \"cat\"]");
test_lexer("\"hello\\n\\tworld\"", 1, "STR[hello\n\tworld]");
} }