Skip to content

Commit

Permalink
Add support for new PHP 8.1 modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Jul 15, 2021
1 parent c758510 commit 3b8b23d
Show file tree
Hide file tree
Showing 22 changed files with 2,423 additions and 2,225 deletions.
3 changes: 2 additions & 1 deletion grammar/php5.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 @@ -542,6 +542,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
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
3 changes: 2 additions & 1 deletion grammar/tokens.y
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
%right T_POW
%right '['
%nonassoc T_NEW T_CLONE
%nonassoc T_NEW T_CLONE
%token T_EXIT
%token T_IF
%left T_ELSEIF
Expand Down Expand Up @@ -74,7 +75,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
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 static.
*
* @return bool
*/
public function isReadonly() : bool {
return (bool) ($this->flags & Class_::MODIFIER_READONLY);
}

public function getType() : string {
return 'Stmt_Property';
}
Expand Down

0 comments on commit 3b8b23d

Please sign in to comment.