Skip to content

Implement arbitrary string interpolation #8256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Zend/tests/arbitrary_string_interpolation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Arbitrary string interpolation with "{$:expr}"
--FILE--
<?php

const foo = 'bar';

function foo() {
return 'bar';
}

echo "{$:foo()}", PHP_EOL;
echo "{$:foo}", PHP_EOL;
echo "{$:'f' . 'o' . 'o'}", PHP_EOL;
echo "{$:"f" . "o" . "o"}", PHP_EOL;
echo "{$:"{$:"foo"}"}", PHP_EOL;
echo "{$:1 + 2 + 3}", PHP_EOL;

?>
--EXPECT--
bar
bar
foo
foo
foo
6
2 changes: 1 addition & 1 deletion Zend/tests/flexible-heredoc-error7.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Note: the closing ?> has been deliberately elided.
echo <<<END

--EXPECTF--
Parse error: syntax error, unexpected end of file, expecting variable or heredoc end or "${" or "{$" in %s on line %d
Parse error: syntax error, unexpected end of file in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/flexible-nowdoc-error7.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Note: the closing ?> has been deliberately elided.
echo <<<'END'

--EXPECTF--
Parse error: syntax error, unexpected end of file, expecting variable or heredoc end or "${" or "{$" in %s on line %d
Parse error: syntax error, unexpected end of file in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/flexible-nowdoc-error8.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ eval('<<<\'end\'

?>
--EXPECTF--
Parse error: syntax error, unexpected end of file, expecting variable or heredoc end or "${" or "{$" in %s on line %d
Parse error: syntax error, unexpected end of file in %s on line %d
1 change: 0 additions & 1 deletion Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -9550,7 +9550,6 @@ static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
ZVAL_EMPTY_STRING(&result->u.constant);
/* empty string */
}
CG(active_op_array)->last = reserved_op_number - 1;
return;
} else if (last_const_node.op_type == IS_CONST) {
opline = &CG(active_op_array)->opcodes[reserved_op_number];
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
%token T_END_HEREDOC "heredoc end"
%token T_DOLLAR_OPEN_CURLY_BRACES "'${'"
%token T_CURLY_OPEN "'{$'"
%token T_CURLY_DOLLAR_COLON "'{$:'"
%token T_PAAMAYIM_NEKUDOTAYIM "'::'"
%token T_NS_SEPARATOR "'\\'"
%token T_ELLIPSIS "'...'"
Expand Down Expand Up @@ -1497,6 +1498,7 @@ encaps_var:
{ $$ = zend_ast_create(ZEND_AST_DIM,
zend_ast_create(ZEND_AST_VAR, $2), $4); }
| T_CURLY_OPEN variable '}' { $$ = $2; }
| T_CURLY_DOLLAR_COLON expr '}' { $$ = $2; }
;

encaps_var_offset:
Expand Down
7 changes: 7 additions & 0 deletions Zend/zend_language_scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -2761,6 +2761,13 @@ skip_escape_conversion:
}


<ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"{$:" {
yy_push_state(ST_IN_SCRIPTING);
enter_nesting('{');
RETURN_TOKEN(T_CURLY_DOLLAR_COLON);
}


<ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"{$" {
yy_push_state(ST_IN_SCRIPTING);
yyless(1);
Expand Down
8 changes: 4 additions & 4 deletions ext/tokenizer/tests/token_get_all_heredoc_nowdoc.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,11 @@ Line 2: T_END_HEREDOC (' INNER_END')

Test case 5

Parse error: syntax error, unexpected end of file, expecting variable or heredoc end or "${" or "{$" on line 2
Parse error: syntax error, unexpected end of file on line 2

Test case 6

Parse error: syntax error, unexpected end of file, expecting variable or heredoc end or "${" or "{$" on line 2
Parse error: syntax error, unexpected end of file on line 2

Test case 7

Expand Down Expand Up @@ -409,8 +409,8 @@ Line 3: T_END_HEREDOC (' INNER_END')

Test case 17

Parse error: syntax error, unexpected end of file, expecting variable or heredoc end or "${" or "{$" on line 4
Parse error: syntax error, unexpected end of file on line 4

Test case 18

Parse error: syntax error, unexpected end of file, expecting variable or heredoc end or "${" or "{$" on line 4
Parse error: syntax error, unexpected end of file on line 4
2 changes: 2 additions & 0 deletions ext/tokenizer/tokenizer_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ void tokenizer_register_constants(INIT_FUNC_ARGS) {
REGISTER_LONG_CONSTANT("T_END_HEREDOC", T_END_HEREDOC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("T_DOLLAR_OPEN_CURLY_BRACES", T_DOLLAR_OPEN_CURLY_BRACES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("T_CURLY_OPEN", T_CURLY_OPEN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("T_CURLY_DOLLAR_COLON", T_CURLY_DOLLAR_COLON, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("T_PAAMAYIM_NEKUDOTAYIM", T_PAAMAYIM_NEKUDOTAYIM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("T_NS_SEPARATOR", T_NS_SEPARATOR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("T_ELLIPSIS", T_ELLIPSIS, CONST_CS | CONST_PERSISTENT);
Expand Down Expand Up @@ -315,6 +316,7 @@ char *get_token_type_name(int token_type)
case T_END_HEREDOC: return "T_END_HEREDOC";
case T_DOLLAR_OPEN_CURLY_BRACES: return "T_DOLLAR_OPEN_CURLY_BRACES";
case T_CURLY_OPEN: return "T_CURLY_OPEN";
case T_CURLY_DOLLAR_COLON: return "T_CURLY_DOLLAR_COLON";
case T_PAAMAYIM_NEKUDOTAYIM: return "T_DOUBLE_COLON";
case T_NS_SEPARATOR: return "T_NS_SEPARATOR";
case T_ELLIPSIS: return "T_ELLIPSIS";
Expand Down