Allow functions and calls to functions named await

This commit is contained in:
Daniil Gentili 2021-09-08 18:11:13 +02:00
parent 8a7698dcf3
commit c930958c72
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -24,9 +24,19 @@ abstract class KeywordEmulator extends TokenEmulator
foreach ($tokens as $i => $token) {
if ($token[0] === T_STRING && strtolower($token[1]) === $keywordString
&& $this->isKeywordContext($tokens, $i)) {
$tokens[$i][0] = $this->getKeywordToken();
if ($keywordString === 'await') {
$tokens[$i][1] = 'yield';
// Workaround await not being a reserved token
$prev = $this->getPreviousNonSpaceToken($tokens, $i);
$next = $this->getNextNonSpaceToken($tokens, $i);
if (
// Allow functions and calls to functions named await
$prev[0] !== T_FUNCTION && $next !== '('
) {
$tokens[$i][0] = $this->getKeywordToken();
$tokens[$i][1] = 'yield';
}
} else {
$tokens[$i][0] = $this->getKeywordToken();
}
}
}
@ -51,6 +61,23 @@ abstract class KeywordEmulator extends TokenEmulator
return null;
}
/**
* @param mixed[] $tokens
* @return mixed[]|null
*/
private function getNextNonSpaceToken(array $tokens, int $start)
{
for ($i = $start +1; $i < count($tokens); ++$i) {
if ($tokens[$i][0] === T_WHITESPACE) {
continue;
}
return $tokens[$i];
}
return null;
}
public function reverseEmulate(string $code, array $tokens): array
{
$keywordToken = $this->getKeywordToken();