1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-30 04:19:30 +01:00

[5.4] Add support for traits

This commit is contained in:
nikic 2011-10-30 12:03:29 +01:00
parent 613aa84f80
commit 6ecae86981
10 changed files with 1535 additions and 1253 deletions

View File

@ -65,6 +65,7 @@
%token T_CATCH
%token T_THROW
%token T_USE
%token T_INSTEADOF
%token T_GLOBAL
%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC
%token T_VAR
@ -232,6 +233,8 @@ class_declaration_statement:
{ $$ = Stmt_Class[$2, [type: $1, extends: $3, implements: $4, stmts: $6]]; }
| T_INTERFACE T_STRING interface_extends_list '{' class_statement_list '}'
{ $$ = Stmt_Interface[$2, [extends: $3, stmts: $5]]; }
| T_TRAIT T_STRING '{' class_statement_list '}'
{ $$ = Stmt_Trait[$2, $4]; }
;
class_entry_type:
@ -247,17 +250,17 @@ extends_from:
interface_extends_list:
/* empty */ { $$ = array(); }
| T_EXTENDS interface_list { $$ = $2; }
| T_EXTENDS name_list { $$ = $2; }
;
implements_list:
/* empty */ { $$ = array(); }
| T_IMPLEMENTS interface_list { $$ = $2; }
| T_IMPLEMENTS name_list { $$ = $2; }
;
interface_list:
name_list:
name { init($1); }
| interface_list ',' name { push($1, $3); }
| name_list ',' name { push($1, $3); }
;
for_statement:
@ -391,6 +394,36 @@ class_statement:
| T_CONST constant_declaration_list ';' { $$ = Stmt_ClassConst[$2]; }
| method_modifiers T_FUNCTION optional_ref T_STRING '(' parameter_list ')' method_body
{ $$ = Stmt_ClassMethod[$4, [type: $1, byRef: $3, params: $6, stmts: $8]]; }
| T_USE name_list trait_adaptations { $$ = Stmt_TraitUse[$2, $3]; }
;
trait_adaptations:
';' { $$ = array(); }
| '{' trait_adaptation_list '}' { $$ = $2; }
;
trait_adaptation_list:
/* empty */ { init(); }
| trait_adaptation_list trait_adaptation { push($1, $2); }
;
trait_adaptation:
trait_method_reference_fully_qualified T_INSTEADOF name_list ';'
{ $$ = Stmt_TraitUseAdaptation_Precedence[$1[0], $1[1], $3]; }
| trait_method_reference T_AS member_modifier T_STRING ';'
{ $$ = Stmt_TraitUseAdaptation_Alias[$1[0], $1[1], $3, $4]; }
| trait_method_reference T_AS member_modifier ';'
{ $$ = Stmt_TraitUseAdaptation_Alias[$1[0], $1[1], $3, null]; }
| trait_method_reference T_AS T_STRING ';'
{ $$ = Stmt_TraitUseAdaptation_Alias[$1[0], $1[1], null, $3]; }
;
trait_method_reference_fully_qualified:
name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = array($1, $3); }
;
trait_method_reference:
trait_method_reference_fully_qualified { $$ = $1; }
| T_STRING { $$ = array(null, $1); }
;
method_body:
@ -624,6 +657,7 @@ common_scalar:
| T_FILE { $$ = Scalar_FileConst[]; }
| T_DIR { $$ = Scalar_DirConst[]; }
| T_CLASS_C { $$ = Scalar_ClassConst[]; }
| T_TRAIT_C { $$ = Scalar_TraitConst[]; }
| T_METHOD_C { $$ = Scalar_MethodConst[]; }
| T_FUNC_C { $$ = Scalar_FuncConst[]; }
| T_NS_C { $$ = Scalar_NSConst[]; }

View File

@ -0,0 +1,14 @@
<?php
class PHPParser_Node_Scalar_TraitConst extends PHPParser_Node_Scalar
{
/**
* Constructs a __TRAIT__ const node
*
* @param int $line Line
* @param null|string $docComment Nearest doc comment
*/
public function __construct($line = -1, $docComment = null) {
parent::__construct(array(), $line, $docComment);
}
}

View File

@ -0,0 +1,26 @@
<?php
/**
* @property string $name Name
* @property PHPParser_Node[] $stmts Statements
*/
class PHPParser_Node_Stmt_Trait extends PHPParser_Node_Stmt
{
/**
* Constructs a trait node.
*
* @param string $name Name
* @param PHPParser_Node[] $stmts Statements
* @param int $line Line
* @param null|string $docComment Nearest doc comment
*/
public function __construct($name, array $stmts = array(), $line = -1, $docComment = null) {
parent::__construct(
array(
'name' => $name,
'stmts' => $stmts,
),
$line, $docComment
);
}
}

View File

@ -0,0 +1,26 @@
<?php
/**
* @property PHPParser_Node_Name[] $traits Traits
* @property PHPParser_Node_TraitUseAdaptation[] $adaptations Adaptations
*/
class PHPParser_Node_Stmt_TraitUse extends PHPParser_Node_Stmt
{
/**
* Constructs a trait use node.
*
* @param PHPParser_Node_Name[] $traits Traits
* @param PHPParser_Node_TraitUseAdaptation[] $adaptations Adaptations
* @param int $line Line
* @param null|string $docComment Nearest doc comment
*/
public function __construct(array $traits, array $adaptations = array(), $line = -1, $docComment = null) {
parent::__construct(
array(
'traits' => $traits,
'adaptations' => $adaptations,
),
$line, $docComment
);
}
}

View File

@ -0,0 +1,5 @@
<?php
abstract class PHPParser_Node_Stmt_TraitUseAdaptation extends PHPParser_Node_Stmt
{
}

View File

@ -0,0 +1,32 @@
<?php
/**
* @property null|PHPParser_Node_Name $trait Trait name
* @property string $method Method name
* @property null|int $newModifier New modifier
* @property null|string $newName New name
*/
class PHPParser_Node_Stmt_TraitUseAdaptation_Alias extends PHPParser_Node_Stmt_TraitUseAdaptation
{
/**
* Constructs a trait use precedence adaptation node.
*
* @param null|PHPParser_Node_Name $trait Trait name
* @param string $method Method name
* @param null|int $newModifier New modifier
* @param null|string $newName New name
* @param int $line Line
* @param null|string $docComment Nearest doc comment
*/
public function __construct($trait, $method, $newModifier, $newName, $line = -1, $docComment = null) {
parent::__construct(
array(
'trait' => $trait,
'method' => $method,
'newModifier' => $newModifier,
'newName' => $newName,
),
$line, $docComment
);
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* @property PHPParser_Node_Name $trait Trait name
* @property string $method Method name
* @property PHPParser_Node_Name[] $insteadof Overwritten traits
*/
class PHPParser_Node_Stmt_TraitUseAdaptation_Precedence extends PHPParser_Node_Stmt_TraitUseAdaptation
{
/**
* Constructs a trait use precedence adaptation node.
*
* @param PHPParser_Node_Name $trait Trait name
* @param string $method Method name
* @param PHPParser_Node_Name[] $insteadof Overwritten traits
* @param int $line Line
* @param null|string $docComment Nearest doc comment
*/
public function __construct(PHPParser_Node_Name $trait, $method, array $insteadof, $line = -1, $docComment = null) {
parent::__construct(
array(
'trait' => $trait,
'method' => $method,
'insteadof' => $insteadof,
),
$line, $docComment
);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -75,17 +75,18 @@ class PHPParser_Parser_Debug extends PHPParser_Parser
"function_declaration_statement : T_FUNCTION optional_ref T_STRING '(' parameter_list ')' '{' inner_statement_list '}'",
"class_declaration_statement : class_entry_type T_STRING extends_from implements_list '{' class_statement_list '}'",
"class_declaration_statement : T_INTERFACE T_STRING interface_extends_list '{' class_statement_list '}'",
"class_declaration_statement : T_TRAIT T_STRING '{' class_statement_list '}'",
"class_entry_type : T_CLASS",
"class_entry_type : T_ABSTRACT T_CLASS",
"class_entry_type : T_FINAL T_CLASS",
"extends_from : /* empty */",
"extends_from : T_EXTENDS name",
"interface_extends_list : /* empty */",
"interface_extends_list : T_EXTENDS interface_list",
"interface_extends_list : T_EXTENDS name_list",
"implements_list : /* empty */",
"implements_list : T_IMPLEMENTS interface_list",
"interface_list : name",
"interface_list : interface_list ',' name",
"implements_list : T_IMPLEMENTS name_list",
"name_list : name",
"name_list : name_list ',' name",
"for_statement : statement",
"for_statement : ':' inner_statement_list T_ENDFOR ';'",
"foreach_statement : statement",
@ -143,6 +144,18 @@ class PHPParser_Parser_Debug extends PHPParser_Parser
"class_statement : variable_modifiers class_variable_declaration ';'",
"class_statement : T_CONST constant_declaration_list ';'",
"class_statement : method_modifiers T_FUNCTION optional_ref T_STRING '(' parameter_list ')' method_body",
"class_statement : T_USE name_list trait_adaptations",
"trait_adaptations : ';'",
"trait_adaptations : '{' trait_adaptation_list '}'",
"trait_adaptation_list : /* empty */",
"trait_adaptation_list : trait_adaptation_list trait_adaptation",
"trait_adaptation : trait_method_reference_fully_qualified T_INSTEADOF name_list ';'",
"trait_adaptation : trait_method_reference T_AS member_modifier T_STRING ';'",
"trait_adaptation : trait_method_reference T_AS member_modifier ';'",
"trait_adaptation : trait_method_reference T_AS T_STRING ';'",
"trait_method_reference_fully_qualified : name T_PAAMAYIM_NEKUDOTAYIM T_STRING",
"trait_method_reference : trait_method_reference_fully_qualified",
"trait_method_reference : T_STRING",
"method_body : ';'",
"method_body : '{' inner_statement_list '}'",
"variable_modifiers : non_empty_member_modifiers",
@ -282,6 +295,7 @@ class PHPParser_Parser_Debug extends PHPParser_Parser
"common_scalar : T_FILE",
"common_scalar : T_DIR",
"common_scalar : T_CLASS_C",
"common_scalar : T_TRAIT_C",
"common_scalar : T_METHOD_C",
"common_scalar : T_FUNC_C",
"common_scalar : T_NS_C",

View File

@ -39,6 +39,10 @@ class PHPParser_PrettyPrinter_Zend extends PHPParser_PrettyPrinterAbstract
return '__CLASS__';
}
public function pScalar_TraitConst(PHPParser_Node_Scalar_TraitConst $node) {
return '__TRAIT__';
}
public function pScalar_DirConst(PHPParser_Node_Scalar_DirConst $node) {
return '__DIR__';
}
@ -464,6 +468,31 @@ class PHPParser_PrettyPrinter_Zend extends PHPParser_PrettyPrinterAbstract
. "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
}
public function pStmt_Trait(PHPParser_Node_Stmt_Trait $node) {
return 'trait ' . $node->name
. "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
}
public function pStmt_TraitUse(PHPParser_Node_Stmt_TraitUse $node) {
return 'use ' . $this->pCommaSeparated($node->traits)
. (empty($node->adaptations)
? ';'
: ' {' . "\n" . $this->pStmts($node->adaptations) . "\n" . '}');
}
public function pStmt_TraitUseAdaptation_Precedence(PHPParser_Node_Stmt_TraitUseAdaptation_Precedence $node) {
return $this->p($node->trait) . '::' . $node->method
. ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';';
}
public function pStmt_TraitUseAdaptation_Alias(PHPParser_Node_Stmt_TraitUseAdaptation_Alias $node) {
return (null !== $node->trait ? $this->p($node->trait) . '::' : '')
. $node->method . ' as'
. (null !== $node->newModifier ? ' ' . $this->pModifiers($node->newModifier) : '')
. (null !== $node->newName ? ' ' . $node->newName : '')
. ';';
}
public function pStmt_Property(PHPParser_Node_Stmt_Property $node) {
return $this->pModifiers($node->type) . $this->pCommaSeparated($node->props) . ';';
}