Skip to content

Commit 4887357

Browse files
tpuntnikic
authored andcommitted
Implement flexible heredoc/nowdoc syntax
RFC: https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes * The ending label no longer has to be followed by a semicolon or newline. Any non-label character is fine. * The ending label may be indented. The indentation will be stripped from all lines in the heredoc/nowdoc string. Lexing of heredoc strings performs a scan-ahead to determine the indentation of the ending label, so that the correct amount of indentation can be removed when calculting the semantic values for use by the parser. This makes the implementation quite a bit more complicated than we would like :/
1 parent dca2d9d commit 4887357

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1888
-623
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ PHP NEWS
1010
. Removed support for BeOS. (Kalle)
1111
. Add PHP_VERSION to phpinfo() <title/>. (github/MattJeevas)
1212
. Add net_get_interfaces(). (Sara, Joe, Anatol)
13+
. Implemented flexible heredoc and nowdoc syntax, per
14+
RFC https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes.
15+
(Thomas Punt)
1316
. Added support for references in list() and array destructuring, per
1417
RFC https://wiki.php.net/rfc/list_reference_assignment.
1518
(David Walker)

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ Standard:
6161
========================================
6262

6363
Core:
64+
. Implemented flexible heredoc and nowdoc syntax: The closing marker for doc
65+
strings is no longer required to be followed by a semicolon or newline.
66+
Additionally the closing marker may be indented, in which case the
67+
indentation will be stripped from all lines in the doc string.
68+
(RFC: https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes)
6469
. Array destructuring now supports reference assignments using the syntax
6570
[&$a, [$b, &$c]] = $d. The same is also supported for list().
6671
(RFC: https://wiki.php.net/rfc/list_reference_assignment)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Flexible heredoc syntax complex test 1: interpolated nested heredocs
3+
with different delimiter names
4+
--FILE--
5+
<?php
6+
7+
$a = 'b';
8+
${"b\nb\n d"} = 'b';
9+
10+
var_dump(<<<DOC1
11+
a
12+
${<<<DOC2
13+
b
14+
${<<<DOC3
15+
a
16+
DOC3}
17+
d
18+
DOC2
19+
}
20+
c
21+
DOC1);
22+
23+
?>
24+
--EXPECT--
25+
string(5) "a
26+
b
27+
c"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Flexible heredoc syntax complex test 2: interpolated nested heredocs
3+
with the same delimiter name
4+
--FILE--
5+
<?php
6+
7+
$a = 'b';
8+
${"b\nb\n d"} = 'b';
9+
10+
var_dump(<<<DOC1
11+
a
12+
${<<<DOC1
13+
b
14+
${<<<DOC1
15+
a
16+
DOC1}
17+
d
18+
DOC1
19+
}
20+
c
21+
DOC1);
22+
23+
?>
24+
--EXPECT--
25+
string(5) "a
26+
b
27+
c"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Flexible heredoc syntax complex test 3: interpolated nested heredocs
3+
with the same delimiter name with different levels of indentation
4+
--FILE--
5+
<?php
6+
7+
${' a'} = ' b';
8+
${' b'} = 'c';
9+
${"b\n b"} = 'b';
10+
11+
var_dump(<<<DOC1
12+
a
13+
${<<<DOC2
14+
b
15+
${<<<DOC3
16+
a
17+
DOC3}
18+
DOC2
19+
}
20+
c
21+
DOC1);
22+
23+
?>
24+
--EXPECT--
25+
string(8) " a
26+
b
27+
c"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Flexible heredoc syntax complex test 4: interpolated variable with
3+
the same delimiter name as the heredoc
4+
--FILE--
5+
<?php
6+
7+
{
8+
$FOO = "FOO";
9+
define("FOO", "FOO");
10+
$b = <<<FOO
11+
Test
12+
${
13+
FOO
14+
}
15+
FOO;
16+
var_dump($b);
17+
}
18+
19+
{
20+
$FOO = "FOO";
21+
$b = <<<FOO
22+
Test
23+
${
24+
FOO
25+
}
26+
FOO;
27+
var_dump($b);
28+
}
29+
30+
?>
31+
--EXPECT--
32+
string(8) "Test
33+
FOO"
34+
string(16) " Test
35+
FOO"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Flexible heredoc syntax 1: different indentation for body (spaces) ending marker (tabs)
3+
--FILE--
4+
<?php
5+
6+
echo <<<END
7+
a
8+
b
9+
c
10+
END;
11+
12+
?>
13+
--EXPECTF--
14+
Parse error: Invalid indentation - tabs and spaces cannot be mixed in %s on line %d
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Flexible heredoc syntax error 10: unindented variable interpolation (as first value)
3+
--FILE--
4+
<?php
5+
6+
$var = 'Bar';
7+
var_dump(<<<TEST
8+
$var
9+
TEST);
10+
11+
?>
12+
--EXPECTF--
13+
Parse error: Invalid body indentation level (expecting an indentation level of at least 1) in %s on line %d
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Flexible heredoc syntax error 11: show erroneous line in error message (variable interpolation)
3+
--FILE--
4+
<?php
5+
6+
echo <<<END
7+
a
8+
$a
9+
END;
10+
11+
?>
12+
--EXPECTF--
13+
Parse error: Invalid body indentation level (expecting an indentation level of at least 1) in %s on line 5
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Flexible heredoc syntax error 12: show erroneous line in error message (mixed indentation)
3+
--FILE--
4+
<?php
5+
6+
echo <<<END
7+
a
8+
b
9+
END;
10+
11+
?>
12+
--EXPECTF--
13+
Parse error: Invalid indentation - tabs and spaces cannot be mixed in %s on line 5

0 commit comments

Comments
 (0)