Mercurial > vim
comparison 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 |
comparison
equal
deleted
inserted
replaced
23087:ca8a14cbafb6 | 23088:285cde4b8d0e |
---|---|
799 /* | 799 /* |
800 * Get the key for #{key: val} into "tv" and advance "arg". | 800 * Get the key for #{key: val} into "tv" and advance "arg". |
801 * Return FAIL when there is no valid key. | 801 * Return FAIL when there is no valid key. |
802 */ | 802 */ |
803 static int | 803 static int |
804 get_literal_key(char_u **arg, typval_T *tv) | 804 get_literal_key_tv(char_u **arg, typval_T *tv) |
805 { | 805 { |
806 char_u *p = skip_literal_key(*arg); | 806 char_u *p = skip_literal_key(*arg); |
807 | 807 |
808 if (p == *arg) | 808 if (p == *arg) |
809 return FAIL; | 809 return FAIL; |
810 tv->v_type = VAR_STRING; | 810 tv->v_type = VAR_STRING; |
811 tv->vval.v_string = vim_strnsave(*arg, p - *arg); | 811 tv->vval.v_string = vim_strnsave(*arg, p - *arg); |
812 | 812 |
813 *arg = p; | 813 *arg = p; |
814 return OK; | 814 return OK; |
815 } | |
816 | |
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; | |
815 } | 856 } |
816 | 857 |
817 /* | 858 /* |
818 * Allocate a variable for a Dictionary and fill it from "*arg". | 859 * Allocate a variable for a Dictionary and fill it from "*arg". |
819 * "*arg" points to the "{". | 860 * "*arg" points to the "{". |
862 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); | 903 *arg = skipwhite_and_linebreak(*arg + 1, evalarg); |
863 while (**arg != '}' && **arg != NUL) | 904 while (**arg != '}' && **arg != NUL) |
864 { | 905 { |
865 int has_bracket = vim9script && **arg == '['; | 906 int has_bracket = vim9script && **arg == '['; |
866 | 907 |
867 if (literal || (vim9script && !has_bracket)) | 908 if (literal) |
868 { | 909 { |
869 if (get_literal_key(arg, &tvkey) == FAIL) | 910 if (get_literal_key_tv(arg, &tvkey) == FAIL) |
870 goto failret; | 911 goto failret; |
912 } | |
913 else if (vim9script && !has_bracket) | |
914 { | |
915 tvkey.vval.v_string = get_literal_key(arg); | |
916 if (tvkey.vval.v_string == NULL) | |
917 goto failret; | |
918 tvkey.v_type = VAR_STRING; | |
871 } | 919 } |
872 else | 920 else |
873 { | 921 { |
874 if (has_bracket) | 922 if (has_bracket) |
875 *arg = skipwhite(*arg + 1); | 923 *arg = skipwhite(*arg + 1); |