mirror of
https://github.com/vim/vim.git
synced 2025-07-26 11:04:33 -04:00
patch 8.2.4883: string interpolation only works in heredoc
Problem: String interpolation only works in heredoc. Solution: Support interpolated strings. Use syntax for heredoc consistent with strings, similar to C#. (closes #10327)
This commit is contained in:
parent
e7d6dbc572
commit
2eaef106e4
@ -1523,6 +1523,25 @@ to be doubled. These two commands are equivalent: >
|
|||||||
if a =~ '\s*'
|
if a =~ '\s*'
|
||||||
|
|
||||||
|
|
||||||
|
interpolated-string *interp-string* *E256*
|
||||||
|
--------------------
|
||||||
|
$"string" interpolated string constant *expr-$quote*
|
||||||
|
$'string' interpolated literal string constant *expr-$'*
|
||||||
|
|
||||||
|
Interpolated strings are an extension of the |string| and |literal-string|,
|
||||||
|
allowing the inclusion of Vim script expressions (see |expr1|). Any
|
||||||
|
expression returning a value can be enclosed between curly braces. The value
|
||||||
|
is converted to a string. All the text and results of the expressions
|
||||||
|
are concatenated to make a new string.
|
||||||
|
|
||||||
|
To include an opening brace '{' or closing brace '}' in the string content
|
||||||
|
double it.
|
||||||
|
|
||||||
|
Examples: >
|
||||||
|
let your_name = input("What's your name? ")
|
||||||
|
echo $"Hello, {your_name}!"
|
||||||
|
echo $"The square root of 9 is {sqrt(9)}"
|
||||||
|
|
||||||
option *expr-option* *E112* *E113*
|
option *expr-option* *E112* *E113*
|
||||||
------
|
------
|
||||||
&option option value, local value if possible
|
&option option value, local value if possible
|
||||||
|
@ -3268,4 +3268,8 @@ EXTERN char e_illegal_map_mode_string_str[]
|
|||||||
EXTERN char e_channel_job_feature_not_available[]
|
EXTERN char e_channel_job_feature_not_available[]
|
||||||
INIT(= N_("E1277: Channel and job feature is not available"));
|
INIT(= N_("E1277: Channel and job feature is not available"));
|
||||||
# endif
|
# endif
|
||||||
|
EXTERN char e_stray_closing_curly_str[]
|
||||||
|
INIT(= N_("E1278: Stray '}' without a matching '{': %s"));
|
||||||
|
EXTERN char e_missing_close_curly_str[]
|
||||||
|
INIT(= N_("E1279: Missing '}': %s"));
|
||||||
#endif
|
#endif
|
||||||
|
@ -3769,8 +3769,12 @@ eval7(
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Environment variable: $VAR.
|
* Environment variable: $VAR.
|
||||||
|
* Interpolated string: $"string" or $'string'.
|
||||||
*/
|
*/
|
||||||
case '$': ret = eval_env_var(arg, rettv, evaluate);
|
case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
|
||||||
|
ret = eval_interp_string(arg, rettv, evaluate);
|
||||||
|
else
|
||||||
|
ret = eval_env_var(arg, rettv, evaluate);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -603,59 +603,88 @@ list_script_vars(int *first)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Evaluate all the Vim expressions (`=expr`) in string "str" and return the
|
* Evaluate all the Vim expressions ({expr}) in string "str" and return the
|
||||||
* resulting string. The caller must free the returned string.
|
* resulting string. The caller must free the returned string.
|
||||||
*/
|
*/
|
||||||
static char_u *
|
char_u *
|
||||||
eval_all_expr_in_str(char_u *str)
|
eval_all_expr_in_str(char_u *str)
|
||||||
{
|
{
|
||||||
garray_T ga;
|
garray_T ga;
|
||||||
char_u *s;
|
|
||||||
char_u *p;
|
char_u *p;
|
||||||
char_u save_c;
|
char_u save_c;
|
||||||
char_u *exprval;
|
char_u *expr_val;
|
||||||
int status;
|
|
||||||
|
|
||||||
ga_init2(&ga, 1, 80);
|
ga_init2(&ga, 1, 80);
|
||||||
p = str;
|
p = str;
|
||||||
|
|
||||||
// Look for `=expr`, evaluate the expression and replace `=expr` with the
|
|
||||||
// result.
|
|
||||||
while (*p != NUL)
|
while (*p != NUL)
|
||||||
{
|
{
|
||||||
s = p;
|
char_u *lit_start;
|
||||||
while (*p != NUL && (*p != '`' || p[1] != '='))
|
char_u *block_start;
|
||||||
p++;
|
char_u *block_end;
|
||||||
ga_concat_len(&ga, s, p - s);
|
int escaped_brace = FALSE;
|
||||||
|
|
||||||
|
// Look for a block start.
|
||||||
|
lit_start = p;
|
||||||
|
while (*p != '{' && *p != '}' && *p != NUL)
|
||||||
|
++p;
|
||||||
|
|
||||||
|
if (*p != NUL && *p == p[1])
|
||||||
|
{
|
||||||
|
// Escaped brace, unescape and continue.
|
||||||
|
// Include the brace in the literal string.
|
||||||
|
++p;
|
||||||
|
escaped_brace = TRUE;
|
||||||
|
}
|
||||||
|
else if (*p == '}')
|
||||||
|
{
|
||||||
|
semsg(_(e_stray_closing_curly_str), str);
|
||||||
|
ga_clear(&ga);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append the literal part.
|
||||||
|
ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
|
||||||
|
|
||||||
if (*p == NUL)
|
if (*p == NUL)
|
||||||
break; // no backtick expression found
|
break;
|
||||||
|
|
||||||
s = p;
|
if (escaped_brace)
|
||||||
p += 2; // skip `=
|
|
||||||
|
|
||||||
status = *p == NUL ? OK : skip_expr(&p, NULL);
|
|
||||||
if (status == FAIL || *p != '`')
|
|
||||||
{
|
{
|
||||||
// invalid expression or missing ending backtick
|
// Skip the second brace.
|
||||||
if (status != FAIL)
|
++p;
|
||||||
emsg(_(e_missing_backtick));
|
continue;
|
||||||
vim_free(ga.ga_data);
|
}
|
||||||
|
|
||||||
|
// Skip the opening {.
|
||||||
|
block_start = ++p;
|
||||||
|
block_end = block_start;
|
||||||
|
if (*block_start != NUL && skip_expr(&block_end, NULL) == FAIL)
|
||||||
|
{
|
||||||
|
ga_clear(&ga);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
s += 2; // skip `=
|
block_end = skipwhite(block_end);
|
||||||
save_c = *p;
|
// The block must be closed by a }.
|
||||||
*p = NUL;
|
if (*block_end != '}')
|
||||||
exprval = eval_to_string(s, TRUE);
|
|
||||||
*p = save_c;
|
|
||||||
p++;
|
|
||||||
if (exprval == NULL)
|
|
||||||
{
|
{
|
||||||
// expression evaluation failed
|
semsg(_(e_missing_close_curly_str), str);
|
||||||
vim_free(ga.ga_data);
|
ga_clear(&ga);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ga_concat(&ga, exprval);
|
save_c = *block_end;
|
||||||
vim_free(exprval);
|
*block_end = NUL;
|
||||||
|
expr_val = eval_to_string(block_start, TRUE);
|
||||||
|
*block_end = save_c;
|
||||||
|
if (expr_val == NULL)
|
||||||
|
{
|
||||||
|
ga_clear(&ga);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
ga_concat(&ga, expr_val);
|
||||||
|
vim_free(expr_val);
|
||||||
|
|
||||||
|
p = block_end + 1;
|
||||||
}
|
}
|
||||||
ga_append(&ga, NUL);
|
ga_append(&ga, NUL);
|
||||||
|
|
||||||
@ -825,7 +854,7 @@ heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
|
|||||||
str = theline + ti;
|
str = theline + ti;
|
||||||
if (vim9compile)
|
if (vim9compile)
|
||||||
{
|
{
|
||||||
if (compile_heredoc_string(str, evalstr, cctx) == FAIL)
|
if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
|
||||||
{
|
{
|
||||||
vim_free(theline);
|
vim_free(theline);
|
||||||
vim_free(text_indent);
|
vim_free(text_indent);
|
||||||
|
@ -105,4 +105,6 @@ void set_callback(callback_T *dest, callback_T *src);
|
|||||||
void copy_callback(callback_T *dest, callback_T *src);
|
void copy_callback(callback_T *dest, callback_T *src);
|
||||||
void expand_autload_callback(callback_T *cb);
|
void expand_autload_callback(callback_T *cb);
|
||||||
void free_callback(callback_T *callback);
|
void free_callback(callback_T *callback);
|
||||||
|
char_u *eval_all_expr_in_str(char_u *str);
|
||||||
|
|
||||||
/* vim: set ft=c : */
|
/* vim: set ft=c : */
|
||||||
|
@ -72,6 +72,7 @@ int eval_string(char_u **arg, typval_T *rettv, int evaluate);
|
|||||||
int eval_lit_string(char_u **arg, typval_T *rettv, int evaluate);
|
int eval_lit_string(char_u **arg, typval_T *rettv, int evaluate);
|
||||||
char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
|
char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
|
||||||
int eval_env_var(char_u **arg, typval_T *rettv, int evaluate);
|
int eval_env_var(char_u **arg, typval_T *rettv, int evaluate);
|
||||||
|
int eval_interp_string(char_u **arg, typval_T *rettv, int evaluate);
|
||||||
linenr_T tv_get_lnum(typval_T *argvars);
|
linenr_T tv_get_lnum(typval_T *argvars);
|
||||||
linenr_T tv_get_lnum_buf(typval_T *argvars, buf_T *buf);
|
linenr_T tv_get_lnum_buf(typval_T *argvars, buf_T *buf);
|
||||||
buf_T *tv_get_buf(typval_T *tv, int curtab_only);
|
buf_T *tv_get_buf(typval_T *tv, int curtab_only);
|
||||||
|
@ -16,7 +16,7 @@ int may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx);
|
|||||||
int may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx);
|
int may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx);
|
||||||
void fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx);
|
void fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx);
|
||||||
int func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type);
|
int func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type);
|
||||||
int compile_heredoc_string(char_u *str, int evalstr, cctx_T *cctx);
|
int compile_all_expr_in_str(char_u *str, int evalstr, cctx_T *cctx);
|
||||||
int assignment_len(char_u *p, int *heredoc);
|
int assignment_len(char_u *p, int *heredoc);
|
||||||
void vim9_declare_error(char_u *name);
|
void vim9_declare_error(char_u *name);
|
||||||
int get_var_dest(char_u *name, assign_dest_T *dest, cmdidx_T cmdidx, int *option_scope, int *vimvaridx, type_T **type, cctx_T *cctx);
|
int get_var_dest(char_u *name, assign_dest_T *dest, cmdidx_T cmdidx, int *option_scope, int *vimvaridx, type_T **type, cctx_T *cctx);
|
||||||
|
@ -377,7 +377,7 @@ func Test_Debugger_breakadd_expr()
|
|||||||
let expected =<< eval trim END
|
let expected =<< eval trim END
|
||||||
Oldval = "10"
|
Oldval = "10"
|
||||||
Newval = "11"
|
Newval = "11"
|
||||||
`=fnamemodify('Xtest.vim', ':p')`
|
{fnamemodify('Xtest.vim', ':p')}
|
||||||
line 1: let g:Xtest_var += 1
|
line 1: let g:Xtest_var += 1
|
||||||
END
|
END
|
||||||
call RunDbgCmd(buf, ':source %', expected)
|
call RunDbgCmd(buf, ':source %', expected)
|
||||||
@ -385,7 +385,7 @@ func Test_Debugger_breakadd_expr()
|
|||||||
let expected =<< eval trim END
|
let expected =<< eval trim END
|
||||||
Oldval = "11"
|
Oldval = "11"
|
||||||
Newval = "12"
|
Newval = "12"
|
||||||
`=fnamemodify('Xtest.vim', ':p')`
|
{fnamemodify('Xtest.vim', ':p')}
|
||||||
line 1: let g:Xtest_var += 1
|
line 1: let g:Xtest_var += 1
|
||||||
END
|
END
|
||||||
call RunDbgCmd(buf, ':source %', expected)
|
call RunDbgCmd(buf, ':source %', expected)
|
||||||
|
@ -890,4 +890,60 @@ func Test_float_compare()
|
|||||||
call v9.CheckLegacyAndVim9Success(lines)
|
call v9.CheckLegacyAndVim9Success(lines)
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_string_interp()
|
||||||
|
let lines =<< trim END
|
||||||
|
call assert_equal('', $"")
|
||||||
|
call assert_equal('foobar', $"foobar")
|
||||||
|
#" Escaping rules.
|
||||||
|
call assert_equal('"foo"{bar}', $"\"foo\"{{bar}}")
|
||||||
|
call assert_equal('"foo"{bar}', $'"foo"{{bar}}')
|
||||||
|
call assert_equal('foobar', $"{\"foo\"}" .. $'{''bar''}')
|
||||||
|
#" Whitespace before/after the expression.
|
||||||
|
call assert_equal('3', $"{ 1 + 2 }")
|
||||||
|
#" String conversion.
|
||||||
|
call assert_equal('hello from ' .. v:version, $"hello from {v:version}")
|
||||||
|
call assert_equal('hello from ' .. v:version, $'hello from {v:version}')
|
||||||
|
#" Paper over a small difference between VimScript behaviour.
|
||||||
|
call assert_equal(string(v:true), $"{v:true}")
|
||||||
|
call assert_equal('(1+1=2)', $"(1+1={1 + 1})")
|
||||||
|
#" Hex-escaped opening brace: char2nr('{') == 0x7b
|
||||||
|
call assert_equal('esc123ape', $"esc\x7b123}ape")
|
||||||
|
call assert_equal('me{}me', $"me{\x7b}\x7dme")
|
||||||
|
VAR var1 = "sun"
|
||||||
|
VAR var2 = "shine"
|
||||||
|
call assert_equal('sunshine', $"{var1}{var2}")
|
||||||
|
call assert_equal('sunsunsun', $"{var1->repeat(3)}")
|
||||||
|
#" Multibyte strings.
|
||||||
|
call assert_equal('say ハロー・ワールド', $"say {'ハロー・ワールド'}")
|
||||||
|
#" Nested.
|
||||||
|
call assert_equal('foobarbaz', $"foo{$\"{'bar'}\"}baz")
|
||||||
|
#" Do not evaluate blocks when the expr is skipped.
|
||||||
|
VAR tmp = 0
|
||||||
|
if v:false
|
||||||
|
echo "${ LET tmp += 1 }"
|
||||||
|
endif
|
||||||
|
call assert_equal(0, tmp)
|
||||||
|
|
||||||
|
#" Stray closing brace.
|
||||||
|
call assert_fails('echo $"moo}"', 'E1278:')
|
||||||
|
#" Undefined variable in expansion.
|
||||||
|
call assert_fails('echo $"{moo}"', 'E121:')
|
||||||
|
#" Empty blocks are rejected.
|
||||||
|
call assert_fails('echo $"{}"', 'E15:')
|
||||||
|
call assert_fails('echo $"{ }"', 'E15:')
|
||||||
|
END
|
||||||
|
call v9.CheckLegacyAndVim9Success(lines)
|
||||||
|
|
||||||
|
let lines =<< trim END
|
||||||
|
call assert_equal('5', $"{({x -> x + 1})(4)}")
|
||||||
|
END
|
||||||
|
call v9.CheckLegacySuccess(lines)
|
||||||
|
|
||||||
|
let lines =<< trim END
|
||||||
|
call assert_equal('5', $"{((x) => x + 1)(4)}")
|
||||||
|
call assert_fails('echo $"{ # foo }"', 'E1279:')
|
||||||
|
END
|
||||||
|
call v9.CheckDefAndScriptSuccess(lines)
|
||||||
|
endfunc
|
||||||
|
|
||||||
" vim: shiftwidth=2 sts=2 expandtab
|
" vim: shiftwidth=2 sts=2 expandtab
|
||||||
|
@ -381,6 +381,17 @@ END
|
|||||||
call assert_equal(['Text', 'with', 'indent'], text)
|
call assert_equal(['Text', 'with', 'indent'], text)
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_let_interpolated()
|
||||||
|
call assert_equal('{text}', $'{{text}}')
|
||||||
|
call assert_equal('{{text}}', $'{{{{text}}}}')
|
||||||
|
let text = 'text'
|
||||||
|
call assert_equal('text{{', $'{text .. "{{"}')
|
||||||
|
call assert_equal('text{{', $"{text .. '{{'}")
|
||||||
|
" FIXME: should not need to escape quotes in the expression
|
||||||
|
call assert_equal('text{{', $'{text .. ''{{''}')
|
||||||
|
call assert_equal('text{{', $"{text .. \"{{\"}")
|
||||||
|
endfunc
|
||||||
|
|
||||||
" Test for the setting a variable using the heredoc syntax.
|
" Test for the setting a variable using the heredoc syntax.
|
||||||
" Keep near the end, this messes up highlighting.
|
" Keep near the end, this messes up highlighting.
|
||||||
func Test_let_heredoc()
|
func Test_let_heredoc()
|
||||||
@ -498,72 +509,72 @@ END
|
|||||||
call assert_equal([' x', ' \y', ' z'], [a, b, c])
|
call assert_equal([' x', ' \y', ' z'], [a, b, c])
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
" Test for evaluating Vim expressions in a heredoc using `=expr`
|
" Test for evaluating Vim expressions in a heredoc using {expr}
|
||||||
" Keep near the end, this messes up highlighting.
|
" Keep near the end, this messes up highlighting.
|
||||||
func Test_let_heredoc_eval()
|
func Test_let_heredoc_eval()
|
||||||
let str = ''
|
let str = ''
|
||||||
let code =<< trim eval END
|
let code =<< trim eval END
|
||||||
let a = `=5 + 10`
|
let a = {5 + 10}
|
||||||
let b = `=min([10, 6])` + `=max([4, 6])`
|
let b = {min([10, 6])} + {max([4, 6])}
|
||||||
`=str`
|
{str}
|
||||||
let c = "abc`=str`d"
|
let c = "abc{str}d"
|
||||||
END
|
END
|
||||||
call assert_equal(['let a = 15', 'let b = 6 + 6', '', 'let c = "abcd"'], code)
|
call assert_equal(['let a = 15', 'let b = 6 + 6', '', 'let c = "abcd"'], code)
|
||||||
|
|
||||||
let $TESTVAR = "Hello"
|
let $TESTVAR = "Hello"
|
||||||
let code =<< eval trim END
|
let code =<< eval trim END
|
||||||
let s = "`=$TESTVAR`"
|
let s = "{$TESTVAR}"
|
||||||
END
|
END
|
||||||
call assert_equal(['let s = "Hello"'], code)
|
call assert_equal(['let s = "Hello"'], code)
|
||||||
|
|
||||||
let code =<< eval END
|
let code =<< eval END
|
||||||
let s = "`=$TESTVAR`"
|
let s = "{$TESTVAR}"
|
||||||
END
|
END
|
||||||
call assert_equal([' let s = "Hello"'], code)
|
call assert_equal([' let s = "Hello"'], code)
|
||||||
|
|
||||||
let a = 10
|
let a = 10
|
||||||
let data =<< eval END
|
let data =<< eval END
|
||||||
`=a`
|
{a}
|
||||||
END
|
END
|
||||||
call assert_equal(['10'], data)
|
call assert_equal(['10'], data)
|
||||||
|
|
||||||
let x = 'X'
|
let x = 'X'
|
||||||
let code =<< eval trim END
|
let code =<< eval trim END
|
||||||
let a = `abc`
|
let a = {{abc}}
|
||||||
let b = `=x`
|
let b = {x}
|
||||||
let c = `
|
let c = {{
|
||||||
END
|
END
|
||||||
call assert_equal(['let a = `abc`', 'let b = X', 'let c = `'], code)
|
call assert_equal(['let a = {abc}', 'let b = X', 'let c = {'], code)
|
||||||
|
|
||||||
let code = 'xxx'
|
let code = 'xxx'
|
||||||
let code =<< eval trim END
|
let code =<< eval trim END
|
||||||
let n = `=5 +
|
let n = {5 +
|
||||||
6`
|
6}
|
||||||
END
|
END
|
||||||
call assert_equal('xxx', code)
|
call assert_equal('xxx', code)
|
||||||
|
|
||||||
let code =<< eval trim END
|
let code =<< eval trim END
|
||||||
let n = `=min([1, 2]` + `=max([3, 4])`
|
let n = {min([1, 2]} + {max([3, 4])}
|
||||||
END
|
END
|
||||||
call assert_equal('xxx', code)
|
call assert_equal('xxx', code)
|
||||||
|
|
||||||
let lines =<< trim LINES
|
let lines =<< trim LINES
|
||||||
let text =<< eval trim END
|
let text =<< eval trim END
|
||||||
let b = `=
|
let b = {
|
||||||
END
|
END
|
||||||
LINES
|
LINES
|
||||||
call v9.CheckScriptFailure(lines, 'E1083:')
|
call v9.CheckScriptFailure(lines, 'E1279:')
|
||||||
|
|
||||||
let lines =<< trim LINES
|
let lines =<< trim LINES
|
||||||
let text =<< eval trim END
|
let text =<< eval trim END
|
||||||
let b = `=abc
|
let b = {abc
|
||||||
END
|
END
|
||||||
LINES
|
LINES
|
||||||
call v9.CheckScriptFailure(lines, 'E1083:')
|
call v9.CheckScriptFailure(lines, 'E1279:')
|
||||||
|
|
||||||
let lines =<< trim LINES
|
let lines =<< trim LINES
|
||||||
let text =<< eval trim END
|
let text =<< eval trim END
|
||||||
let b = `=`
|
let b = {}
|
||||||
END
|
END
|
||||||
LINES
|
LINES
|
||||||
call v9.CheckScriptFailure(lines, 'E15:')
|
call v9.CheckScriptFailure(lines, 'E15:')
|
||||||
@ -571,7 +582,7 @@ END
|
|||||||
" skipped heredoc
|
" skipped heredoc
|
||||||
if 0
|
if 0
|
||||||
let msg =<< trim eval END
|
let msg =<< trim eval END
|
||||||
n is: `=n`
|
n is: {n}
|
||||||
END
|
END
|
||||||
endif
|
endif
|
||||||
|
|
||||||
@ -583,7 +594,7 @@ END
|
|||||||
let lines =<< trim END
|
let lines =<< trim END
|
||||||
let Xvar =<< eval CODE
|
let Xvar =<< eval CODE
|
||||||
let a = 1
|
let a = 1
|
||||||
let b = `=5+`
|
let b = {5+}
|
||||||
let c = 2
|
let c = 2
|
||||||
CODE
|
CODE
|
||||||
let g:Count += 1
|
let g:Count += 1
|
||||||
@ -592,10 +603,10 @@ END
|
|||||||
let g:Count = 0
|
let g:Count = 0
|
||||||
call assert_fails('source', 'E15:')
|
call assert_fails('source', 'E15:')
|
||||||
call assert_equal(1, g:Count)
|
call assert_equal(1, g:Count)
|
||||||
call setline(3, 'let b = `=abc`')
|
call setline(3, 'let b = {abc}')
|
||||||
call assert_fails('source', 'E121:')
|
call assert_fails('source', 'E121:')
|
||||||
call assert_equal(2, g:Count)
|
call assert_equal(2, g:Count)
|
||||||
call setline(3, 'let b = `=abc` + `=min([9, 4])` + 2')
|
call setline(3, 'let b = {abc} + {min([9, 4])} + 2')
|
||||||
call assert_fails('source', 'E121:')
|
call assert_fails('source', 'E121:')
|
||||||
call assert_equal(3, g:Count)
|
call assert_equal(3, g:Count)
|
||||||
call assert_equal('test', g:Xvar)
|
call assert_equal('test', g:Xvar)
|
||||||
|
@ -2670,10 +2670,10 @@ def Test_heredoc_expr()
|
|||||||
var a3 = "3"
|
var a3 = "3"
|
||||||
var a4 = ""
|
var a4 = ""
|
||||||
var code =<< trim eval END
|
var code =<< trim eval END
|
||||||
var a = `=5 + 10`
|
var a = {5 + 10}
|
||||||
var b = `=min([10, 6])` + `=max([4, 6])`
|
var b = {min([10, 6])} + {max([4, 6])}
|
||||||
var c = "`=s`"
|
var c = "{s}"
|
||||||
var d = x`=a1`x`=a2`x`=a3`x`=a4`
|
var d = x{a1}x{a2}x{a3}x{a4}
|
||||||
END
|
END
|
||||||
assert_equal(['var a = 15', 'var b = 6 + 6', 'var c = "local"', 'var d = x1x2x3x'], code)
|
assert_equal(['var a = 15', 'var b = 6 + 6', 'var c = "local"', 'var d = x1x2x3x'], code)
|
||||||
CODE
|
CODE
|
||||||
@ -2681,7 +2681,7 @@ def Test_heredoc_expr()
|
|||||||
|
|
||||||
lines =<< trim CODE
|
lines =<< trim CODE
|
||||||
var code =<< eval trim END
|
var code =<< eval trim END
|
||||||
var s = "`=$SOME_ENV_VAR`"
|
var s = "{$SOME_ENV_VAR}"
|
||||||
END
|
END
|
||||||
assert_equal(['var s = "somemore"'], code)
|
assert_equal(['var s = "somemore"'], code)
|
||||||
CODE
|
CODE
|
||||||
@ -2689,7 +2689,7 @@ def Test_heredoc_expr()
|
|||||||
|
|
||||||
lines =<< trim CODE
|
lines =<< trim CODE
|
||||||
var code =<< eval END
|
var code =<< eval END
|
||||||
var s = "`=$SOME_ENV_VAR`"
|
var s = "{$SOME_ENV_VAR}"
|
||||||
END
|
END
|
||||||
assert_equal([' var s = "somemore"'], code)
|
assert_equal([' var s = "somemore"'], code)
|
||||||
CODE
|
CODE
|
||||||
@ -2697,34 +2697,34 @@ def Test_heredoc_expr()
|
|||||||
|
|
||||||
lines =<< trim CODE
|
lines =<< trim CODE
|
||||||
var code =<< eval trim END
|
var code =<< eval trim END
|
||||||
let a = `abc`
|
let a = {{abc}}
|
||||||
let b = `=g:someVar`
|
let b = {g:someVar}
|
||||||
let c = `
|
let c = {{
|
||||||
END
|
END
|
||||||
assert_equal(['let a = `abc`', 'let b = X', 'let c = `'], code)
|
assert_equal(['let a = {abc}', 'let b = X', 'let c = {'], code)
|
||||||
CODE
|
CODE
|
||||||
v9.CheckDefAndScriptSuccess(lines)
|
v9.CheckDefAndScriptSuccess(lines)
|
||||||
|
|
||||||
lines =<< trim LINES
|
lines =<< trim LINES
|
||||||
var text =<< eval trim END
|
var text =<< eval trim END
|
||||||
let b = `=
|
let b = {
|
||||||
END
|
END
|
||||||
LINES
|
LINES
|
||||||
v9.CheckDefAndScriptFailure(lines, ['E1143: Empty expression: ""', 'E1083: Missing backtick'])
|
v9.CheckDefAndScriptFailure(lines, "E1279: Missing '}'")
|
||||||
|
|
||||||
lines =<< trim LINES
|
lines =<< trim LINES
|
||||||
var text =<< eval trim END
|
var text =<< eval trim END
|
||||||
let b = `=abc
|
let b = {abc
|
||||||
END
|
END
|
||||||
LINES
|
LINES
|
||||||
v9.CheckDefAndScriptFailure(lines, ['E1001: Variable not found: abc', 'E1083: Missing backtick'])
|
v9.CheckDefAndScriptFailure(lines, "E1279: Missing '}'")
|
||||||
|
|
||||||
lines =<< trim LINES
|
lines =<< trim LINES
|
||||||
var text =<< eval trim END
|
var text =<< eval trim END
|
||||||
let b = `=`
|
let b = {}
|
||||||
END
|
END
|
||||||
LINES
|
LINES
|
||||||
v9.CheckDefAndScriptFailure(lines, ['E1015: Name expected: `', 'E15: Invalid expression: "`"'])
|
v9.CheckDefAndScriptFailure(lines, 'E15: Invalid expression: "}"')
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|
||||||
|
@ -2840,6 +2840,25 @@ def Test_disassemble_after_reload()
|
|||||||
delfunc g:ThatFunc
|
delfunc g:ThatFunc
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
def s:MakeString(x: number): string
|
||||||
|
return $"x={x} x^2={x * x}"
|
||||||
|
enddef
|
||||||
|
|
||||||
|
def Test_disassemble_string_interp()
|
||||||
|
var instr = execute('disassemble s:MakeString')
|
||||||
|
assert_match('MakeString\_s*' ..
|
||||||
|
'return $"x={x} x^2={x \* x}"\_s*' ..
|
||||||
|
'0 PUSHS "x="\_s*' ..
|
||||||
|
'1 LOAD arg\[-1\]\_s*' ..
|
||||||
|
'2 2STRING stack\[-1\]\_s*' ..
|
||||||
|
'3 PUSHS " x^2="\_s*' ..
|
||||||
|
'4 LOAD arg\[-1\]\_s*' ..
|
||||||
|
'5 LOAD arg\[-1\]\_s*' ..
|
||||||
|
'6 OPNR \*\_s*' ..
|
||||||
|
'7 2STRING stack\[-1\]\_s*' ..
|
||||||
|
'8 CONCAT size 4\_s*' ..
|
||||||
|
'9 RETURN\_s*',
|
||||||
|
instr)
|
||||||
|
enddef
|
||||||
|
|
||||||
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|
||||||
|
26
src/typval.c
26
src/typval.c
@ -2266,6 +2266,32 @@ eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
|
|||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
|
||||||
|
{
|
||||||
|
typval_T tv;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
// *arg is on the '$' character.
|
||||||
|
(*arg)++;
|
||||||
|
|
||||||
|
rettv->v_type = VAR_STRING;
|
||||||
|
|
||||||
|
if (**arg == '"')
|
||||||
|
ret = eval_string(arg, &tv, evaluate);
|
||||||
|
else
|
||||||
|
ret = eval_lit_string(arg, &tv, evaluate);
|
||||||
|
|
||||||
|
if (ret == FAIL || !evaluate)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
rettv->vval.v_string = eval_all_expr_in_str(tv.vval.v_string);
|
||||||
|
|
||||||
|
clear_tv(&tv);
|
||||||
|
|
||||||
|
return rettv->vval.v_string != NULL ? OK : FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return a string with the string representation of a variable.
|
* Return a string with the string representation of a variable.
|
||||||
* If the memory is allocated "tofree" is set to it, otherwise NULL.
|
* If the memory is allocated "tofree" is set to it, otherwise NULL.
|
||||||
|
@ -746,6 +746,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
4883,
|
||||||
/**/
|
/**/
|
||||||
4882,
|
4882,
|
||||||
/**/
|
/**/
|
||||||
|
@ -969,80 +969,103 @@ theend:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Compile a heredoc string "str" (either containing a literal string or a mix
|
* Compile a string "str" (either containing a literal string or a mix of
|
||||||
* of literal strings and Vim expressions of the form `=<expr>`). This is used
|
* literal strings and Vim expressions of the form `{expr}`). This is used
|
||||||
* when compiling a heredoc assignment to a variable in a Vim9 def function.
|
* when compiling a heredoc assignment to a variable or an interpolated string
|
||||||
* Vim9 instructions are generated to push strings, evaluate expressions,
|
* in a Vim9 def function. Vim9 instructions are generated to push strings,
|
||||||
* concatenate them and create a list of lines. When "evalstr" is TRUE, Vim
|
* evaluate expressions, concatenate them and create a list of lines. When
|
||||||
* expressions in "str" are evaluated.
|
* "evalstr" is TRUE, Vim expressions in "str" are evaluated.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
compile_heredoc_string(char_u *str, int evalstr, cctx_T *cctx)
|
compile_all_expr_in_str(char_u *str, int evalstr, cctx_T *cctx)
|
||||||
{
|
{
|
||||||
char_u *p;
|
char_u *p = str;
|
||||||
char_u *val;
|
char_u *val;
|
||||||
|
char_u save_c;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
if (cctx->ctx_skip == SKIP_YES)
|
if (cctx->ctx_skip == SKIP_YES)
|
||||||
return OK;
|
return OK;
|
||||||
|
|
||||||
if (evalstr && (p = (char_u *)strstr((char *)str, "`=")) != NULL)
|
if (!evalstr || *str == NUL)
|
||||||
{
|
{
|
||||||
char_u *start = str;
|
// Literal string, possibly empty.
|
||||||
int count = 0;
|
val = *str != NUL ? vim_strsave(str) : NULL;
|
||||||
|
return generate_PUSHS(cctx, &val);
|
||||||
|
}
|
||||||
|
|
||||||
// Need to evaluate expressions of the form `=<expr>` in the string.
|
// Push all the string pieces to the stack, followed by a ISN_CONCAT.
|
||||||
// Split the string into literal strings and Vim expressions and
|
while (*p != NUL)
|
||||||
// generate instructions to concatenate the literal strings and the
|
{
|
||||||
// result of evaluating the Vim expressions.
|
char_u *lit_start;
|
||||||
for (;;)
|
char_u *block_start;
|
||||||
|
char_u *block_end;
|
||||||
|
int escaped_brace = FALSE;
|
||||||
|
|
||||||
|
// Look for a block start.
|
||||||
|
lit_start = p;
|
||||||
|
while (*p != '{' && *p != '}' && *p != NUL)
|
||||||
|
++p;
|
||||||
|
|
||||||
|
if (*p != NUL && *p == p[1])
|
||||||
{
|
{
|
||||||
if (p > start)
|
// Escaped brace, unescape and continue.
|
||||||
{
|
// Include the brace in the literal string.
|
||||||
// literal string before the expression
|
++p;
|
||||||
val = vim_strnsave(start, p - start);
|
escaped_brace = TRUE;
|
||||||
generate_PUSHS(cctx, &val);
|
}
|
||||||
count++;
|
else if (*p == '}')
|
||||||
}
|
{
|
||||||
p += 2;
|
semsg(_(e_stray_closing_curly_str), str);
|
||||||
|
return FAIL;
|
||||||
// evaluate the Vim expression and convert the result to string.
|
|
||||||
if (compile_expr0(&p, cctx) == FAIL)
|
|
||||||
return FAIL;
|
|
||||||
may_generate_2STRING(-1, TRUE, cctx);
|
|
||||||
count++;
|
|
||||||
|
|
||||||
p = skipwhite(p);
|
|
||||||
if (*p != '`')
|
|
||||||
{
|
|
||||||
emsg(_(e_missing_backtick));
|
|
||||||
return FAIL;
|
|
||||||
}
|
|
||||||
start = p + 1;
|
|
||||||
|
|
||||||
p = (char_u *)strstr((char *)start, "`=");
|
|
||||||
if (p == NULL)
|
|
||||||
{
|
|
||||||
// no more Vim expressions left to process
|
|
||||||
if (*skipwhite(start) != NUL)
|
|
||||||
{
|
|
||||||
val = vim_strsave(start);
|
|
||||||
generate_PUSHS(cctx, &val);
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count > 1)
|
// Append the literal part.
|
||||||
generate_CONCAT(cctx, count);
|
if (p != lit_start)
|
||||||
}
|
{
|
||||||
else
|
val = vim_strnsave(lit_start, (size_t)(p - lit_start));
|
||||||
{
|
if (generate_PUSHS(cctx, &val) == FAIL)
|
||||||
// literal string
|
return FAIL;
|
||||||
val = vim_strsave(str);
|
++count;
|
||||||
generate_PUSHS(cctx, &val);
|
}
|
||||||
|
|
||||||
|
if (*p == NUL)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (escaped_brace)
|
||||||
|
{
|
||||||
|
// Skip the second brace.
|
||||||
|
++p;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip the opening {.
|
||||||
|
block_start = skipwhite(p + 1);
|
||||||
|
block_end = block_start;
|
||||||
|
if (*block_start != NUL &&skip_expr(&block_end, NULL) == FAIL)
|
||||||
|
return FAIL;
|
||||||
|
block_end = skipwhite(block_end);
|
||||||
|
// The block must be closed by a }.
|
||||||
|
if (*block_end != '}')
|
||||||
|
{
|
||||||
|
semsg(_(e_missing_close_curly_str), str);
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
save_c = *block_end;
|
||||||
|
*block_end = NUL;
|
||||||
|
if (compile_expr0(&block_start, cctx) == FAIL)
|
||||||
|
return FAIL;
|
||||||
|
*block_end = save_c;
|
||||||
|
may_generate_2STRING(-1, TRUE, cctx);
|
||||||
|
++count;
|
||||||
|
|
||||||
|
p = block_end + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Small optimization, if there's only a single piece skip the ISN_CONCAT.
|
||||||
|
if (count != 1)
|
||||||
|
return generate_CONCAT(cctx, count);
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1374,6 +1374,33 @@ compile_get_env(char_u **arg, cctx_T *cctx)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compile "$"string"" or "$'string'".
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
compile_interp_string(char_u **arg, cctx_T *cctx)
|
||||||
|
{
|
||||||
|
typval_T tv;
|
||||||
|
int ret;
|
||||||
|
int evaluate = cctx->ctx_skip != SKIP_YES;
|
||||||
|
|
||||||
|
// *arg is on the '$' character.
|
||||||
|
(*arg)++;
|
||||||
|
|
||||||
|
if (**arg == '"')
|
||||||
|
ret = eval_string(arg, &tv, evaluate);
|
||||||
|
else
|
||||||
|
ret = eval_lit_string(arg, &tv, evaluate);
|
||||||
|
|
||||||
|
if (ret == FAIL || !evaluate)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
ret = compile_all_expr_in_str(tv.vval.v_string, TRUE, cctx);
|
||||||
|
clear_tv(&tv);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Compile "@r".
|
* Compile "@r".
|
||||||
*/
|
*/
|
||||||
@ -2226,10 +2253,14 @@ compile_expr8(
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Environment variable: $VAR.
|
* Environment variable: $VAR.
|
||||||
|
* Interpolated string: $"string" or $'string'.
|
||||||
*/
|
*/
|
||||||
case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
|
case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
|
||||||
return FAIL;
|
return FAIL;
|
||||||
ret = compile_get_env(arg, cctx);
|
if ((*arg)[1] == '"' || (*arg)[1] == '\'')
|
||||||
|
ret = compile_interp_string(arg, cctx);
|
||||||
|
else
|
||||||
|
ret = compile_get_env(arg, cctx);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user