Skip to content

Commit c5e6a71

Browse files
committed
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.
1 parent 6cd42db commit c5e6a71

6 files changed

Lines changed: 83 additions & 29 deletions

File tree

runtime/doc/vim9.txt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*vim9.txt* For Vim version 8.2. Last change: 2020 Nov 25
1+
*vim9.txt* For Vim version 8.2. Last change: 2020 Dec 04
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -436,19 +436,25 @@ Dictionary literals ~
436436
Traditionally Vim has supported dictionary literals with a {} syntax: >
437437
let dict = {'key': value}
438438
439-
Later it became clear that using a simple key name is very common, thus
440-
literally dictionaries were introduced in a backwards compatible way: >
439+
Later it became clear that using a simple text key is very common, thus
440+
literal dictionaries were introduced in a backwards compatible way: >
441441
let dict = #{key: value}
442442
443-
However, this #{} syntax is unlike any existing language. As it appears that
444-
using a literal key is much more common than using an expression, and
443+
However, this #{} syntax is unlike any existing language. As it turns out
444+
that using a literal key is much more common than using an expression, and
445445
considering that JavaScript uses this syntax, using the {} form for dictionary
446-
literals was considered a much more useful syntax. In Vim9 script the {} form
446+
literals is considered a much more useful syntax. In Vim9 script the {} form
447447
uses literal keys: >
448448
let dict = {key: value}
449449
450-
In case an expression needs to be used for the key, square brackets can be
451-
used, just like in JavaScript: >
450+
This works for alphanumeric characters, underscore and dash. If you want to
451+
use another character, use a single or double quoted string: >
452+
let dict = {'key with space': value}
453+
let dict = {"key\twith\ttabs": value}
454+
let dict = {'': value} # empty key
455+
456+
In case the key needs to be an expression, square brackets can be used, just
457+
like in JavaScript: >
452458
let dict = {["key" .. nr]: value}
453459
454460

src/dict.c

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ skip_literal_key(char_u *key)
801801
* Return FAIL when there is no valid key.
802802
*/
803803
static int
804-
get_literal_key(char_u **arg, typval_T *tv)
804+
get_literal_key_tv(char_u **arg, typval_T *tv)
805805
{
806806
char_u *p = skip_literal_key(*arg);
807807

@@ -814,6 +814,47 @@ get_literal_key(char_u **arg, typval_T *tv)
814814
return OK;
815815
}
816816

817+
/*
818+
* Get a literal key for a Vim9 dict:
819+
* {"name": value},
820+
* {'name': value},
821+
* {name: value} use "name" as a literal key
822+
* Return the key in allocated memory or NULL in the case of an error.
823+
* "arg" is advanced to just after the key.
824+
*/
825+
char_u *
826+
get_literal_key(char_u **arg)
827+
{
828+
char_u *key;
829+
char_u *end;
830+
typval_T rettv;
831+
832+
if (**arg == '\'')
833+
{
834+
if (eval_lit_string(arg, &rettv, TRUE) == FAIL)
835+
return NULL;
836+
key = rettv.vval.v_string;
837+
}
838+
else if (**arg == '"')
839+
{
840+
if (eval_string(arg, &rettv, TRUE) == FAIL)
841+
return NULL;
842+
key = rettv.vval.v_string;
843+
}
844+
else
845+
{
846+
end = skip_literal_key(*arg);
847+
if (end == *arg)
848+
{
849+
semsg(_(e_invalid_key_str), *arg);
850+
return NULL;
851+
}
852+
key = vim_strnsave(*arg, end - *arg);
853+
*arg = end;
854+
}
855+
return key;
856+
}
857+
817858
/*
818859
* Allocate a variable for a Dictionary and fill it from "*arg".
819860
* "*arg" points to the "{".
@@ -864,10 +905,17 @@ eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
864905
{
865906
int has_bracket = vim9script && **arg == '[';
866907

867-
if (literal || (vim9script && !has_bracket))
908+
if (literal)
909+
{
910+
if (get_literal_key_tv(arg, &tvkey) == FAIL)
911+
goto failret;
912+
}
913+
else if (vim9script && !has_bracket)
868914
{
869-
if (get_literal_key(arg, &tvkey) == FAIL)
915+
tvkey.vval.v_string = get_literal_key(arg);
916+
if (tvkey.vval.v_string == NULL)
870917
goto failret;
918+
tvkey.v_type = VAR_STRING;
871919
}
872920
else
873921
{

src/proto/dict.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ varnumber_T dict_get_number_check(dict_T *d, char_u *key);
3434
varnumber_T dict_get_bool(dict_T *d, char_u *key, int def);
3535
char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
3636
char_u *skip_literal_key(char_u *key);
37+
char_u *get_literal_key(char_u **arg);
3738
int eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal);
3839
void dict_extend(dict_T *d1, dict_T *d2, char_u *action);
3940
dictitem_T *dict_lookup(hashitem_T *hi);

src/testdir/test_vim9_expr.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1930,12 +1930,13 @@ def Test_expr7_dict()
19301930

19311931
assert_equal(g:test_space_dict, {['']: 'empty', [' ']: 'space'})
19321932
assert_equal(g:test_hash_dict, {one: 1, two: 2})
1933+
1934+
assert_equal({['a a']: 1, ['b/c']: 2}, {'a a': 1, "b/c": 2})
19331935
END
19341936
CheckDefAndScriptSuccess(lines)
19351937

19361938
# legacy syntax doesn't work
19371939
CheckDefFailure(["var x = #{key: 8}"], 'E1097:', 2)
1938-
CheckDefFailure(["var x = {'key': 8}"], 'E1014:', 1)
19391940
CheckDefFailure(["var x = 'a' .. #{a: 1}"], 'E1097:', 2)
19401941

19411942
CheckDefFailure(["var x = {a:8}"], 'E1069:', 1)

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2090,
753755
/**/
754756
2089,
755757
/**/

src/vim9compile.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3024,26 +3024,11 @@ compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
30243024
if (**arg == '}')
30253025
break;
30263026

3027-
// {name: value} uses "name" as a literal key and
3028-
// {[expr]: value} uses an evaluated key.
3029-
if (**arg != '[')
3030-
{
3031-
char_u *end = skip_literal_key(*arg);
3032-
3033-
if (end == *arg)
3034-
{
3035-
semsg(_(e_invalid_key_str), *arg);
3036-
return FAIL;
3037-
}
3038-
key = vim_strnsave(*arg, end - *arg);
3039-
if (generate_PUSHS(cctx, key) == FAIL)
3040-
return FAIL;
3041-
*arg = end;
3042-
}
3043-
else
3027+
if (**arg == '[')
30443028
{
30453029
isn_T *isn;
30463030

3031+
// {[expr]: value} uses an evaluated key.
30473032
*arg = skipwhite(*arg + 1);
30483033
if (compile_expr0(arg, cctx) == FAIL)
30493034
return FAIL;
@@ -3066,6 +3051,17 @@ compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
30663051
}
30673052
++*arg;
30683053
}
3054+
else
3055+
{
3056+
// {"name": value},
3057+
// {'name': value},
3058+
// {name: value} use "name" as a literal key
3059+
key = get_literal_key(arg);
3060+
if (key == NULL)
3061+
return FAIL;
3062+
if (generate_PUSHS(cctx, key) == FAIL)
3063+
return FAIL;
3064+
}
30693065

30703066
// Check for duplicate keys, if using string keys.
30713067
if (key != NULL)

0 commit comments

Comments
 (0)