语法解析BUG: yield from捕获

<?php
function from123(){}
function bug() {
    yield from123();
}
bug();
/*
# 引入yield from语法之前版本 && >= 7.1.4版本无此BUG
Output for 5.6.0 - 5.6.30, hhvm-3.12.14 - 3.19.0, 7.0.18, 7.1.4

# 语法错误
Output for 7.0.0 - 7.0.17, 7.1.0 - 7.1.3
Parse error: syntax error, unexpected '(' in /in/PqKpi on line 4

Process exited with code 255.
*/
<?php
class from123 {
    public static function xxx() { }
}
function g() {
    yield from123::xxx();
}
Output for 7.0.0 - 7.0.17, 7.1.0 - 7.1.3
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) in /in/sFgBN on line 6

Process exited with code 255.

说明

yield from 匹配过早

影响

匹配该正则的生成器函数/from\d[a-zA-Z0-9_\x80-\xff]/i, 不能进行yield.

暂时规避方案

<?php
function from123(){}
function bug() {
    yield (from123());
}
bug();

修复方案

官方7.1.4FIX方案, 修改词法文件

-<ST_IN_SCRIPTING>"yield"{WHITESPACE}"from" {
+<ST_IN_SCRIPTING>"yield"{WHITESPACE}"from"[^a-zA-Z0-9_\x80-\xff] {
 +    yyless(--yyleng);

链接

  1. https://github.com/php/php-src/commit/0fb640c71763ddb1b8017c87cec10fc76764feff
  2. https://bugs.php.net/bug.php?id=74302

results matching ""

    No results matching ""