comparison src/vim9compile.c @ 22936:00b0275ffe7f v8.2.2015

patch 8.2.2015: Vim9: literal dict #{} is not like any other language Commit: https://github.com/vim/vim/commit/2bede173a177e227e6224a8713f5b88a38d011af Author: Bram Moolenaar <Bram@vim.org> Date: Thu Nov 19 18:53:18 2020 +0100 patch 8.2.2015: Vim9: literal dict #{} is not like any other language Problem: Vim9: literal dict #{} is not like any other language. Solution: Support the JavaScript syntax.
author Bram Moolenaar <Bram@vim.org>
date Thu, 19 Nov 2020 19:00:06 +0100
parents 87b62395a4d1
children d3b26055bfa8
comparison
equal deleted inserted replaced
22935:da7ce666cf1f 22936:00b0275ffe7f
2769 * does not recognize magic braces. 2769 * does not recognize magic braces.
2770 * When "namespace" is TRUE recognize "b:", "s:", etc. 2770 * When "namespace" is TRUE recognize "b:", "s:", etc.
2771 * Return a pointer to just after the name. Equal to "arg" if there is no 2771 * Return a pointer to just after the name. Equal to "arg" if there is no
2772 * valid name. 2772 * valid name.
2773 */ 2773 */
2774 static char_u * 2774 char_u *
2775 to_name_end(char_u *arg, int namespace) 2775 to_name_end(char_u *arg, int namespace)
2776 { 2776 {
2777 char_u *p; 2777 char_u *p;
2778 2778
2779 // Quick check for valid starting character. 2779 // Quick check for valid starting character.
2986 if (d == NULL) 2986 if (d == NULL)
2987 return FAIL; 2987 return FAIL;
2988 *arg = skipwhite(*arg + 1); 2988 *arg = skipwhite(*arg + 1);
2989 for (;;) 2989 for (;;)
2990 { 2990 {
2991 char_u *key = NULL; 2991 char_u *key = NULL;
2992 char_u *end;
2992 2993
2993 if (may_get_next_line(whitep, arg, cctx) == FAIL) 2994 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2994 { 2995 {
2995 *arg = NULL; 2996 *arg = NULL;
2996 goto failret; 2997 goto failret;
2997 } 2998 }
2998 2999
2999 if (**arg == '}') 3000 if (**arg == '}')
3000 break; 3001 break;
3001 3002
3002 if (literal) 3003 // Eventually {name: value} will use "name" as a literal key and
3003 { 3004 // {[expr]: value} for an evaluated key.
3004 char_u *end = to_name_end(*arg, !literal); 3005 // Temporarily: if "name" is indeed a valid key, or "[expr]" is
3005 3006 // used, use the new method, like JavaScript. Otherwise fall back
3007 // to the old method.
3008 end = to_name_end(*arg, FALSE);
3009 if (literal || *end == ':')
3010 {
3006 if (end == *arg) 3011 if (end == *arg)
3007 { 3012 {
3008 semsg(_(e_invalid_key_str), *arg); 3013 semsg(_(e_invalid_key_str), *arg);
3009 return FAIL; 3014 return FAIL;
3010 } 3015 }
3013 return FAIL; 3018 return FAIL;
3014 *arg = end; 3019 *arg = end;
3015 } 3020 }
3016 else 3021 else
3017 { 3022 {
3018 isn_T *isn; 3023 isn_T *isn;
3019 3024 int has_bracket = **arg == '[';
3025
3026 if (has_bracket)
3027 *arg = skipwhite(*arg + 1);
3020 if (compile_expr0(arg, cctx) == FAIL) 3028 if (compile_expr0(arg, cctx) == FAIL)
3021 return FAIL; 3029 return FAIL;
3022 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; 3030 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3023 if (isn->isn_type == ISN_PUSHS) 3031 if (isn->isn_type == ISN_PUSHS)
3024 key = isn->isn_arg.string; 3032 key = isn->isn_arg.string;
3025 else 3033 else
3026 { 3034 {
3027 type_T *keytype = ((type_T **)stack->ga_data) 3035 type_T *keytype = ((type_T **)stack->ga_data)
3028 [stack->ga_len - 1]; 3036 [stack->ga_len - 1];
3029 if (need_type(keytype, &t_string, -1, cctx, 3037 if (need_type(keytype, &t_string, -1, cctx,
3030 FALSE, FALSE) == FAIL) 3038 FALSE, FALSE) == FAIL)
3031 return FAIL; 3039 return FAIL;
3040 }
3041 if (has_bracket)
3042 {
3043 *arg = skipwhite(*arg);
3044 if (**arg != ']')
3045 {
3046 emsg(_(e_missing_matching_bracket_after_dict_key));
3047 return FAIL;
3048 }
3049 ++*arg;
3032 } 3050 }
3033 } 3051 }
3034 3052
3035 // Check for duplicate keys, if using string keys. 3053 // Check for duplicate keys, if using string keys.
3036 if (key != NULL) 3054 if (key != NULL)