Skip to content

Commit

Permalink
Add support for new PHP 8.1 modifiers (#796)
Browse files Browse the repository at this point in the history
Implement support for readonly properties (https://wiki.php.net/rfc/readonly_properties_v2) and
final class contstants (https://wiki.php.net/rfc/final_class_const).
  • Loading branch information
kocsismate committed Jul 21, 2021
1 parent c4304c7 commit 55c4269
Show file tree
Hide file tree
Showing 26 changed files with 1,457 additions and 1,211 deletions.
4 changes: 3 additions & 1 deletion grammar/php7.y
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ reserved_non_modifiers:

semi_reserved:
reserved_non_modifiers
| T_STATIC | T_ABSTRACT | T_FINAL | T_PRIVATE | T_PROTECTED | T_PUBLIC
| T_STATIC | T_ABSTRACT | T_FINAL | T_PRIVATE | T_PROTECTED | T_PUBLIC | T_READONLY
;

identifier_ex:
Expand Down Expand Up @@ -535,6 +535,7 @@ optional_visibility_modifier:
| T_PUBLIC { $$ = Stmt\Class_::MODIFIER_PUBLIC; }
| T_PROTECTED { $$ = Stmt\Class_::MODIFIER_PROTECTED; }
| T_PRIVATE { $$ = Stmt\Class_::MODIFIER_PRIVATE; }
| T_READONLY { $$ = Stmt\Class_::MODIFIER_READONLY; }
;

parameter:
Expand Down Expand Up @@ -725,6 +726,7 @@ member_modifier:
| T_STATIC { $$ = Stmt\Class_::MODIFIER_STATIC; }
| T_ABSTRACT { $$ = Stmt\Class_::MODIFIER_ABSTRACT; }
| T_FINAL { $$ = Stmt\Class_::MODIFIER_FINAL; }
| T_READONLY { $$ = Stmt\Class_::MODIFIER_READONLY; }
;

property_declaration_list:
Expand Down
2 changes: 1 addition & 1 deletion grammar/tokens.y
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
%token T_USE
%token T_INSTEADOF
%token T_GLOBAL
%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC
%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC T_READONLY
%token T_VAR
%token T_UNSET
%token T_ISSET
Expand Down
11 changes: 11 additions & 0 deletions lib/PhpParser/Builder/ClassConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ public function makePrivate() {
return $this;
}

/**
* Makes the constant final.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeFinal() {
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);

return $this;
}

/**
* Sets doc comment for the constant.
*
Expand Down
11 changes: 11 additions & 0 deletions lib/PhpParser/Builder/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ public function makeStatic() {
return $this;
}

/**
* Makes the property readonly.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeReadonly() {
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_READONLY);

return $this;
}

/**
* Sets default value for the property.
*
Expand Down
4 changes: 3 additions & 1 deletion lib/PhpParser/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ private function defineCompatibilityTokens() {
'T_ENUM',
'T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG',
'T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG',
'T_READONLY',
];

// PHP-Parser might be used together with another library that also emulates some or all
Expand Down Expand Up @@ -536,6 +537,7 @@ protected function createTokenMap() : array {
$tokenMap[\T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG] = Tokens::T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG;
$tokenMap[\T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG] = Tokens::T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG;
$tokenMap[\T_ENUM] = Tokens::T_ENUM;
$tokenMap[\T_READONLY] = Tokens::T_READONLY;

return $tokenMap;
}
Expand All @@ -544,7 +546,7 @@ private function createIdentifierTokenMap(): array {
// Based on semi_reserved production.
return array_fill_keys([
\T_STRING,
\T_STATIC, \T_ABSTRACT, \T_FINAL, \T_PRIVATE, \T_PROTECTED, \T_PUBLIC,
\T_STATIC, \T_ABSTRACT, \T_FINAL, \T_PRIVATE, \T_PROTECTED, \T_PUBLIC, \T_READONLY,
\T_INCLUDE, \T_INCLUDE_ONCE, \T_EVAL, \T_REQUIRE, \T_REQUIRE_ONCE, \T_LOGICAL_OR, \T_LOGICAL_XOR, \T_LOGICAL_AND,
\T_INSTANCEOF, \T_NEW, \T_CLONE, \T_EXIT, \T_IF, \T_ELSEIF, \T_ELSE, \T_ENDIF, \T_ECHO, \T_DO, \T_WHILE,
\T_ENDWHILE, \T_FOR, \T_ENDFOR, \T_FOREACH, \T_ENDFOREACH, \T_DECLARE, \T_ENDDECLARE, \T_AS, \T_TRY, \T_CATCH,
Expand Down
2 changes: 2 additions & 0 deletions lib/PhpParser/Lexer/Emulative.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Lexer\TokenEmulator\MatchTokenEmulator;
use PhpParser\Lexer\TokenEmulator\NullsafeTokenEmulator;
use PhpParser\Lexer\TokenEmulator\NumericLiteralSeparatorEmulator;
use PhpParser\Lexer\TokenEmulator\ReadonlyTokenEmulator;
use PhpParser\Lexer\TokenEmulator\ReverseEmulator;
use PhpParser\Lexer\TokenEmulator\TokenEmulator;

Expand Down Expand Up @@ -53,6 +54,7 @@ public function __construct(array $options = [])
new NullsafeTokenEmulator(),
new AttributeEmulator(),
new EnumTokenEmulator(),
new ReadonlyTokenEmulator(),
];

// Collect emulators that are relevant for the PHP version we're running
Expand Down
23 changes: 23 additions & 0 deletions lib/PhpParser/Lexer/TokenEmulator/ReadonlyTokenEmulator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace PhpParser\Lexer\TokenEmulator;

use PhpParser\Lexer\Emulative;

final class ReadonlyTokenEmulator extends KeywordEmulator
{
public function getPhpVersion(): string
{
return Emulative::PHP_8_1;
}

public function getKeywordString(): string
{
return 'readonly';
}

public function getKeywordToken(): int
{
return \T_READONLY;
}
}
9 changes: 9 additions & 0 deletions lib/PhpParser/Node/Stmt/ClassConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public function isPrivate() : bool {
return (bool) ($this->flags & Class_::MODIFIER_PRIVATE);
}

/**
* Whether constant is final.
*
* @return bool
*/
public function isFinal() : bool {
return (bool) ($this->flags & Class_::MODIFIER_FINAL);
}

public function getType() : string {
return 'Stmt_ClassConst';
}
Expand Down
5 changes: 5 additions & 0 deletions lib/PhpParser/Node/Stmt/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Class_ extends ClassLike
const MODIFIER_STATIC = 8;
const MODIFIER_ABSTRACT = 16;
const MODIFIER_FINAL = 32;
const MODIFIER_READONLY = 64;

const VISIBILITY_MODIFIER_MASK = 7; // 1 | 2 | 4

Expand Down Expand Up @@ -96,6 +97,10 @@ public static function verifyModifier($a, $b) {
throw new Error('Multiple final modifiers are not allowed');
}

if ($a & self::MODIFIER_READONLY && $b & self::MODIFIER_READONLY) {
throw new Error('Multiple readonly modifiers are not allowed');
}

if ($a & 48 && $b & 48) {
throw new Error('Cannot use the final modifier on an abstract class member');
}
Expand Down
9 changes: 9 additions & 0 deletions lib/PhpParser/Node/Stmt/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ public function isStatic() : bool {
return (bool) ($this->flags & Class_::MODIFIER_STATIC);
}

/**
* Whether the property is readonly.
*
* @return bool
*/
public function isReadonly() : bool {
return (bool) ($this->flags & Class_::MODIFIER_READONLY);
}

public function getType() : string {
return 'Stmt_Property';
}
Expand Down
3 changes: 3 additions & 0 deletions lib/PhpParser/NodeDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ protected function dumpFlags($flags) {
if ($flags & Class_::MODIFIER_FINAL) {
$strs[] = 'MODIFIER_FINAL';
}
if ($flags & Class_::MODIFIER_READONLY) {
$strs[] = 'MODIFIER_READONLY';
}

if ($strs) {
return implode(' | ', $strs) . ' (' . $flags . ')';
Expand Down
67 changes: 34 additions & 33 deletions lib/PhpParser/Parser/Php5.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
*/
class Php5 extends \PhpParser\ParserAbstract
{
protected $tokenToSymbolMapSize = 395;
protected $tokenToSymbolMapSize = 396;
protected $actionTableSize = 1093;
protected $gotoTableSize = 643;

protected $invalidSymbol = 167;
protected $invalidSymbol = 168;
protected $errorSymbol = 1;
protected $defaultAction = -32766;
protected $unexpectedTokenRule = 32767;
Expand Down Expand Up @@ -194,38 +194,39 @@ class Php5 extends \PhpParser\ParserAbstract
"'`'",
"']'",
"'\"'",
"T_READONLY",
"T_ENUM",
"T_NULLSAFE_OBJECT_OPERATOR",
"T_ATTRIBUTE"
);

protected $tokenToSymbol = array(
0, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 56, 163, 167, 160, 55, 167, 167,
158, 159, 53, 50, 8, 51, 52, 54, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 31, 155,
44, 16, 46, 30, 68, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 70, 167, 162, 36, 167, 161, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 156, 35, 157, 58, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 167, 167, 167, 167,
167, 167, 167, 167, 167, 167, 1, 2, 3, 4,
0, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 56, 163, 168, 160, 55, 168, 168,
158, 159, 53, 50, 8, 51, 52, 54, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 31, 155,
44, 16, 46, 30, 68, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 70, 168, 162, 36, 168, 161, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 156, 35, 157, 58, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
168, 168, 168, 168, 168, 168, 1, 2, 3, 4,
5, 6, 7, 9, 10, 11, 12, 13, 14, 15,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 32, 33, 34, 37, 38, 39, 40,
Expand All @@ -235,11 +236,11 @@ class Php5 extends \PhpParser\ParserAbstract
83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
123, 124, 125, 126, 127, 128, 129, 164, 130, 131,
132, 165, 133, 134, 135, 136, 137, 138, 139, 140,
141, 142, 143, 144, 145, 146, 147, 148, 149, 150,
151, 152, 153, 154, 166
113, 114, 115, 116, 117, 118, 119, 120, 121, 164,
122, 123, 124, 125, 126, 127, 128, 129, 165, 130,
131, 132, 166, 133, 134, 135, 136, 137, 138, 139,
140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
150, 151, 152, 153, 154, 167
);

protected $action = array(
Expand Down

0 comments on commit 55c4269

Please sign in to comment.