Add await support

This commit is contained in:
Daniil Gentili 2021-09-05 16:46:01 +02:00
parent 2f3765f090
commit 8a1acbe4e1
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
5 changed files with 33 additions and 51 deletions

View File

@ -1,48 +0,0 @@
name: build
on:
push:
branches:
- master
jobs:
run:
runs-on: ubuntu-latest
name: Convert
steps:
- name: Checkout
uses: actions/checkout@v2
with:
ssh-key: ${{ secrets.DEPLOY_KEY }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.0"
coverage: none
- name: Check environment
run: |
php --version
composer --version
- name: Get composer cache directory
id: composercache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ matrix.os }}-composer-${{ matrix.php }}-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ matrix.os }}-composer-${{ matrix.php }}-
- name: Install dependencies
run: |
mv composer.json backup
composer require phabel/phabel --prefer-dist --no-plugins
mv backup composer.json
- name: Convert phabel
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "Github Actions"
php tools/convertPhabel.php all

View File

@ -6,6 +6,7 @@ use PhpParser\Error;
use PhpParser\ErrorHandler;
use PhpParser\Lexer;
use PhpParser\Lexer\TokenEmulator\AttributeEmulator;
use PhpParser\Lexer\TokenEmulator\AwaitTokenEmulator;
use PhpParser\Lexer\TokenEmulator\EnumTokenEmulator;
use PhpParser\Lexer\TokenEmulator\CoaleseEqualTokenEmulator;
use PhpParser\Lexer\TokenEmulator\ExplicitOctalEmulator;
@ -24,12 +25,13 @@ class Emulative extends Lexer
const PHP_7_4 = '7.4dev';
const PHP_8_0 = '8.0dev';
const PHP_8_1 = '8.1dev';
const PHP_UNOBTANIUM = '100.0dev';
/** @var mixed[] Patches used to reverse changes introduced in the code */
private $patches = [];
/** @var TokenEmulator[] */
private $emulators = [];
public $emulators = [];
/** @var string */
private $targetPhpVersion;
@ -47,6 +49,7 @@ class Emulative extends Lexer
parent::__construct($options);
$emulators = [
new AwaitTokenEmulator(),
new FlexibleDocStringEmulator(),
new FnTokenEmulator(),
new MatchTokenEmulator(),
@ -106,6 +109,7 @@ class Emulative extends Lexer
}
private function isForwardEmulationNeeded(string $emulatorPhpVersion): bool {
if ($emulatorPhpVersion === self::PHP_UNOBTANIUM) return true;
return version_compare(\PHP_VERSION, $emulatorPhpVersion, '<')
&& version_compare($this->targetPhpVersion, $emulatorPhpVersion, '>=');
}

View File

@ -0,0 +1,23 @@
<?php declare(strict_types=1);
namespace PhpParser\Lexer\TokenEmulator;
use PhpParser\Lexer\Emulative;
final class AwaitTokenEmulator extends KeywordEmulator
{
public function getPhpVersion(): string
{
return Emulative::PHP_UNOBTANIUM;
}
public function getKeywordString(): string
{
return 'await';
}
public function getKeywordToken(): int
{
return \T_YIELD;
}
}

View File

@ -25,6 +25,9 @@ abstract class KeywordEmulator extends TokenEmulator
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';
}
}
}

View File

@ -18,5 +18,5 @@ fn(&$x) => $x;
fn&($x) => $x;
static fn($x, ...$rest) => $rest;
fn(): int => $x;
fn($a, $b) => $a and $b;
fn($a, $b) => $a && $b;
fn($a, $b) => ($a and $b);
fn($a, $b) => ($a && $b);