scripting: Added tests for untermianted string
authorLukas Krickl <lukas@krickl.dev>
Sat, 21 Mar 2026 14:26:16 +0000 (15:26 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sat, 21 Mar 2026 14:26:16 +0000 (15:26 +0100)
Added string escape sequences

src/l_lsl.c
src/tests/lsl/strliteral-esc.lsl [new file with mode: 0644]
src/tests/lsl/strliteral-noterm.lsl [new file with mode: 0644]
src/tests/t_lsl.c

index 7bf8286a8157dfeb484ce23a81cf07933da6b95f..fc672494ac26ca8d0c539d7fe3b1ce5355673b03 100644 (file)
@@ -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 (file)
index 0000000..0a0fc05
--- /dev/null
@@ -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 (file)
index 0000000..3f52b40
--- /dev/null
@@ -0,0 +1 @@
+"hello world
index 905682b3bf26a0f9271b9947540f2a0c04973039..ddbb4e1d2591301facf7d4d9f7690154e06386f3 100644 (file)
@@ -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;
 }