Skip to content

Tokenize yield keyword as T_YIELD on PHP 5.4, and yield from as T_YIELD_FROM on PHP < 7.0 #1524

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

Merged
merged 2 commits into from
Jul 20, 2017
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function getErrorList()
548 => 1,
641 => 1,
669 => 1,
688 => 1,
744 => 1,
748 => 1,
767 => 1,
Expand All @@ -109,15 +110,6 @@ public function getErrorList()
864 => 1,
);

// The yield tests will only work in PHP versions where yield exists and
// will throw errors in earlier versions.
if (PHP_VERSION_ID < 50500) {
$errors[676] = 1;
$errors[874] = 1;
} else {
$errors[688] = 1;
}

// Scalar type hints only work from PHP 7 onwards.
if (PHP_VERSION_ID >= 70000) {
$errors[17] = 3;
Expand Down
66 changes: 66 additions & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,72 @@ protected function tokenize($string)
continue;
}//end if

/*
Before PHP 7.0, the "yield from" was tokenized as
T_YIELD, T_WHITESPACE and T_STRING. So look for
and change this token in earlier versions.
*/

if (PHP_VERSION_ID < 70000
&& PHP_VERSION_ID >= 50500
&& $tokenIsArray === true
&& $token[0] === T_YIELD
&& isset($tokens[($stackPtr + 1)]) === true
&& isset($tokens[($stackPtr + 2)]) === true
&& $tokens[($stackPtr + 1)][0] === T_WHITESPACE
&& $tokens[($stackPtr + 2)][0] === T_STRING
&& strtolower($tokens[($stackPtr + 2)][1]) === 'from'
) {
$newToken = array();
$newToken['code'] = T_YIELD_FROM;
$newToken['type'] = 'T_YIELD_FROM';
$newToken['content'] = $token[1].$tokens[($stackPtr + 1)][1].$tokens[($stackPtr + 2)][1];
$finalTokens[$newStackPtr] = $newToken;

$newStackPtr++;
$stackPtr += 2;
continue;
}

/*
Before PHP 5.5, the yield keyword was tokenized as
T_STRING. So look for and change this token in
earlier versions.
Checks also if it is just "yield" or "yield from".
*/

if (PHP_VERSION_ID < 50500
&& $tokenIsArray === true
&& $token[0] === T_STRING
&& strtolower($token[1]) === 'yield'
) {
if (isset($tokens[($stackPtr + 1)]) === true
&& isset($tokens[($stackPtr + 2)]) === true
&& $tokens[($stackPtr + 1)][0] === T_WHITESPACE
&& $tokens[($stackPtr + 2)][0] === T_STRING
&& strtolower($tokens[($stackPtr + 2)][1]) === 'from'
) {
$newToken = array();
$newToken['code'] = T_YIELD_FROM;
$newToken['type'] = 'T_YIELD_FROM';
$newToken['content'] = $token[1].$tokens[($stackPtr + 1)][1].$tokens[($stackPtr + 2)][1];
$finalTokens[$newStackPtr] = $newToken;

$newStackPtr++;
$stackPtr += 2;
continue;
}

$newToken = array();
$newToken['code'] = T_YIELD;
$newToken['type'] = 'T_YIELD';
$newToken['content'] = $token[1];
$finalTokens[$newStackPtr] = $newToken;

$newStackPtr++;
continue;
}//end if

/*
Before PHP 5.6, the ... operator was tokenized as three
T_STRING_CONCAT tokens in a row. So look for and combine
Expand Down