From 8a1acbe4e1fc887ba11bd0b9fa985b8d65421970 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 5 Sep 2021 16:46:01 +0200 Subject: [PATCH] Add await support --- .github/workflows/build.yml | 48 ------------------- lib/PhpParser/Lexer/Emulative.php | 6 ++- .../TokenEmulator/AwaitTokenEmulator.php | 23 +++++++++ .../Lexer/TokenEmulator/KeywordEmulator.php | 3 ++ .../prettyPrinter/expr/arrow_function.test | 4 +- 5 files changed, 33 insertions(+), 51 deletions(-) delete mode 100644 .github/workflows/build.yml create mode 100644 lib/PhpParser/Lexer/TokenEmulator/AwaitTokenEmulator.php diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 702dadb..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -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 diff --git a/lib/PhpParser/Lexer/Emulative.php b/lib/PhpParser/Lexer/Emulative.php index 5c56e02..f3a4d5c 100644 --- a/lib/PhpParser/Lexer/Emulative.php +++ b/lib/PhpParser/Lexer/Emulative.php @@ -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, '>='); } diff --git a/lib/PhpParser/Lexer/TokenEmulator/AwaitTokenEmulator.php b/lib/PhpParser/Lexer/TokenEmulator/AwaitTokenEmulator.php new file mode 100644 index 0000000..18ff0ee --- /dev/null +++ b/lib/PhpParser/Lexer/TokenEmulator/AwaitTokenEmulator.php @@ -0,0 +1,23 @@ +isKeywordContext($tokens, $i)) { $tokens[$i][0] = $this->getKeywordToken(); + if ($keywordString === 'await') { + $tokens[$i][1] = 'yield'; + } } } diff --git a/test/code/prettyPrinter/expr/arrow_function.test b/test/code/prettyPrinter/expr/arrow_function.test index 78a684c..fdb2d0e 100644 --- a/test/code/prettyPrinter/expr/arrow_function.test +++ b/test/code/prettyPrinter/expr/arrow_function.test @@ -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; \ No newline at end of file +fn($a, $b) => ($a and $b); +fn($a, $b) => ($a && $b); \ No newline at end of file