diff src/dict.c @ 23088:285cde4b8d0e v8.2.2090

patch 8.2.2090: Vim9: dict does not accept a key in quotes Commit: https://github.com/vim/vim/commit/c5e6a7179d7dee4315b412b56e172bb1ff092d3e Author: Bram Moolenaar <Bram@vim.org> Date: Fri Dec 4 19:12:14 2020 +0100 patch 8.2.2090: Vim9: dict does not accept a key in quotes Problem: Vim9: dict does not accept a key in quotes. Solution: Recognize a key in single or double quotes.
author Bram Moolenaar <Bram@vim.org>
date Fri, 04 Dec 2020 19:15:03 +0100
parents 4b398a229b0b
children 545ff3b4543c
line wrap: on
line diff
--- a/src/dict.c
+++ b/src/dict.c
@@ -801,7 +801,7 @@ skip_literal_key(char_u *key)
  * Return FAIL when there is no valid key.
  */
     static int
-get_literal_key(char_u **arg, typval_T *tv)
+get_literal_key_tv(char_u **arg, typval_T *tv)
 {
     char_u *p = skip_literal_key(*arg);
 
@@ -815,6 +815,47 @@ get_literal_key(char_u **arg, typval_T *
 }
 
 /*
+ * Get a literal key for a Vim9 dict:
+ * {"name": value},
+ * {'name': value},
+ * {name: value} use "name" as a literal key
+ * Return the key in allocated memory or NULL in the case of an error.
+ * "arg" is advanced to just after the key.
+ */
+    char_u *
+get_literal_key(char_u **arg)
+{
+    char_u	*key;
+    char_u	*end;
+    typval_T	rettv;
+
+    if (**arg == '\'')
+    {
+	if (eval_lit_string(arg, &rettv, TRUE) == FAIL)
+	    return NULL;
+	key = rettv.vval.v_string;
+    }
+    else if (**arg == '"')
+    {
+	if (eval_string(arg, &rettv, TRUE) == FAIL)
+	    return NULL;
+	key = rettv.vval.v_string;
+    }
+    else
+    {
+	end = skip_literal_key(*arg);
+	if (end == *arg)
+	{
+	    semsg(_(e_invalid_key_str), *arg);
+	    return NULL;
+	}
+	key = vim_strnsave(*arg, end - *arg);
+	*arg = end;
+    }
+    return key;
+}
+
+/*
  * Allocate a variable for a Dictionary and fill it from "*arg".
  * "*arg" points to the "{".
  * "literal" is TRUE for #{key: val}
@@ -864,11 +905,18 @@ eval_dict(char_u **arg, typval_T *rettv,
     {
 	int	has_bracket = vim9script && **arg == '[';
 
-	if (literal || (vim9script && !has_bracket))
+	if (literal)
 	{
-	    if (get_literal_key(arg, &tvkey) == FAIL)
+	    if (get_literal_key_tv(arg, &tvkey) == FAIL)
 		goto failret;
 	}
+	else if (vim9script && !has_bracket)
+	{
+	    tvkey.vval.v_string = get_literal_key(arg);
+	    if (tvkey.vval.v_string == NULL)
+		goto failret;
+	    tvkey.v_type = VAR_STRING;
+	}
 	else
 	{
 	    if (has_bracket)