Skip to content

Commit 784c564

Browse files
committed
Tokenize "yield from" as T_YIELD_FROM token on PHP < 7.0
1 parent 7301cb8 commit 784c564

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/Tokenizers/PHP.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,17 +721,62 @@ protected function tokenize($string)
721721
continue;
722722
}//end if
723723

724+
/*
725+
Before PHP 7.0, the "yield from" was tokenized as
726+
T_YIELD, T_WHITESPACE and T_STRING. So look for
727+
and change this token in earlier versions.
728+
*/
729+
730+
if (PHP_VERSION_ID < 70000
731+
&& PHP_VERSION_ID >= 50500
732+
&& $tokenIsArray === true
733+
&& $token[0] === T_YIELD
734+
&& isset($tokens[($stackPtr + 1)]) === true
735+
&& isset($tokens[($stackPtr + 2)]) === true
736+
&& $tokens[($stackPtr + 1)][0] === T_WHITESPACE
737+
&& $tokens[($stackPtr + 2)][0] === T_STRING
738+
&& strtolower($tokens[($stackPtr + 2)][1]) === 'from'
739+
) {
740+
$newToken = array();
741+
$newToken['code'] = T_YIELD_FROM;
742+
$newToken['type'] = 'T_YIELD_FROM';
743+
$newToken['content'] = $token[1].$tokens[($stackPtr + 1)][1].$tokens[($stackPtr + 2)][1];
744+
$finalTokens[$newStackPtr] = $newToken;
745+
746+
$newStackPtr++;
747+
$stackPtr += 2;
748+
continue;
749+
}
750+
724751
/*
725752
Before PHP 5.5, the yield keyword was tokenized as
726753
T_STRING. So look for and change this token in
727754
earlier versions.
755+
Checks also if it is just "yield" or "yield from".
728756
*/
729757

730758
if (PHP_VERSION_ID < 50500
731759
&& $tokenIsArray === true
732760
&& $token[0] === T_STRING
733761
&& strtolower($token[1]) === 'yield'
734762
) {
763+
if (isset($tokens[($stackPtr + 1)]) === true
764+
&& isset($tokens[($stackPtr + 2)]) === true
765+
&& $tokens[($stackPtr + 1)][0] === T_WHITESPACE
766+
&& $tokens[($stackPtr + 2)][0] === T_STRING
767+
&& strtolower($tokens[($stackPtr + 2)][1]) === 'from'
768+
) {
769+
$newToken = array();
770+
$newToken['code'] = T_YIELD_FROM;
771+
$newToken['type'] = 'T_YIELD_FROM';
772+
$newToken['content'] = $token[1].$tokens[($stackPtr + 1)][1].$tokens[($stackPtr + 2)][1];
773+
$finalTokens[$newStackPtr] = $newToken;
774+
775+
$newStackPtr++;
776+
$stackPtr += 2;
777+
continue;
778+
}
779+
735780
$newToken = array();
736781
$newToken['code'] = T_YIELD;
737782
$newToken['type'] = 'T_YIELD';

0 commit comments

Comments
 (0)