From: Lukas Krickl Date: Sat, 21 Mar 2026 14:26:16 +0000 (+0100) Subject: scripting: Added tests for untermianted string X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=4d40e2effa78476162184c3b466cef5cfbc47b62;p=lrts%2F.git scripting: Added tests for untermianted string Added string escape sequences --- diff --git a/src/l_lsl.c b/src/l_lsl.c index 7bf8286..fc67249 100644 --- a/src/l_lsl.c +++ b/src/l_lsl.c @@ -128,6 +128,36 @@ void l_lsl_lex_term_token(struct l_lsl_vm *v, struct l_lsl_lex *lex, t->val[t->len] = '\0'; } +/* escapes a char based on its type + * returns \0 if invalid char is passed */ +char l_lsl_escape_char(char c) { + switch (c) { + case 'a': + return '\a'; + case 'b': + return '\b'; + case 'f': + return '\f'; + case 'n': + return '\n'; + case 'r': + return '\r'; + case 'v': + return 'v'; + case '\\': + return '\\'; + case '\'': + return '\''; + case '\"': + return '\"'; + case '?': + return '\?'; + /* TODO: allow \xhh and \nnn */ + default: + return '\0'; + } +} + char tokbuf[L_LSL_TOK_MAX]; struct l_lsl_token l_lsl_next_token(struct l_lsl_vm *v, struct l_lsl_lex *lex) { char c = 0; @@ -202,7 +232,11 @@ struct l_lsl_token l_lsl_next_token(struct l_lsl_vm *v, struct l_lsl_lex *lex) { c = l_lsl_lex_advance(v, lex); while (c && c != string_term) { - /* TODO: allow escaping */ + /* escape */ + if (c == '\\') { + c = l_lsl_lex_advance(v, lex); + c = l_lsl_escape_char(c); + } l_lsl_lex_write_token(v, lex, &t, c); c = l_lsl_lex_advance(v, lex); } diff --git a/src/tests/lsl/strliteral-esc.lsl b/src/tests/lsl/strliteral-esc.lsl new file mode 100644 index 0000000..0a0fc05 --- /dev/null +++ b/src/tests/lsl/strliteral-esc.lsl @@ -0,0 +1 @@ +'hello\nworld' diff --git a/src/tests/lsl/strliteral-noterm.lsl b/src/tests/lsl/strliteral-noterm.lsl new file mode 100644 index 0000000..3f52b40 --- /dev/null +++ b/src/tests/lsl/strliteral-noterm.lsl @@ -0,0 +1 @@ +"hello world diff --git a/src/tests/t_lsl.c b/src/tests/t_lsl.c index 905682b..ddbb4e1 100644 --- a/src/tests/t_lsl.c +++ b/src/tests/t_lsl.c @@ -15,7 +15,7 @@ int t_lsl_vlue_assert_int(struct l_lsl_value *expected, int t_lsl_vlue_assert_str(struct l_lsl_value *expected, struct l_lsl_value *actual) { T_ASSERT(u_strncmp(expected->data.str_val, actual->data.str_val, 256) == 0, - ("Unexpected int value. Expected %s got %s\n", expected->data.str_val, + ("Unexpected str value. Expected '%s' got '%s'\n", expected->data.str_val, actual->data.str_val)); return 0; @@ -36,8 +36,11 @@ int t_lsl_assert(struct l_lsl_value expect_value, enum l_lsl_error expect_err, l_lsl_vm_free(&v); - T_ASSERT(res != LRTS_NULL, ("Invalid result value\n")) T_ASSERT(err == expect_err, ("Unexpected error code %d\n", err)); + if (expect_err != L_LSL_ERR_OK) { + return 0; + } + T_ASSERT(res != LRTS_NULL, ("Invalid result value\n")) T_ASSERT(res->type == expect_value.type, ("Unexpected return value\n")); if (assert_val(&expect_value, res) != 0) { @@ -81,5 +84,17 @@ int t_test_lsl() { v.data.str_val = "hello world"; T_LSL_ASSERT(v, L_LSL_ERR_OK, "./src/tests/lsl/strliteral.lsl", t_lsl_vlue_assert_str); + + /* unterminated str value */ + v.type = L_LSL_TYPE_STRING; + v.data.str_val = "hello world"; + T_LSL_ASSERT(v, L_LSL_ERR_LEX, "./src/tests/lsl/strliteral-noterm.lsl", + t_lsl_vlue_assert_str); + + /* simple str value with escape sequence */ + v.type = L_LSL_TYPE_STRING; + v.data.str_val = "hello\nworld"; + T_LSL_ASSERT(v, L_LSL_ERR_OK, "./src/tests/lsl/strliteral-esc.lsl", + t_lsl_vlue_assert_str); return 0; }