Use seperate Name node classes instead of type subnode

This commit is contained in:
nikic 2011-09-24 16:53:40 +02:00
parent 858545732c
commit df7cb44eed
9 changed files with 864 additions and 780 deletions

View File

@ -113,12 +113,8 @@ top_statement_list:
;
namespace_name:
namespace_name_sub { $$ = Name[$1, Name::NORMAL]; }
;
namespace_name_sub:
T_STRING { init($1); }
| namespace_name_sub T_NS_SEPARATOR T_STRING { push($1, $3); }
| namespace_name T_NS_SEPARATOR T_STRING { push($1, $3); }
;
top_statement:
@ -127,8 +123,8 @@ top_statement:
| class_declaration_statement { $$ = $1; }
| T_HALT_COMPILER
{ $$ = Stmt_HaltCompiler[$this->lexer->handleHaltCompiler()]; }
| T_NAMESPACE namespace_name ';' { $$ = Stmt_Namespace[name: $2, stmts: null]; }
| T_NAMESPACE namespace_name '{' top_statement_list '}' { $$ = Stmt_Namespace[name: $2, stmts: $4]; }
| T_NAMESPACE namespace_name ';' { $$ = Stmt_Namespace[name: Name[$2], stmts: null]; }
| T_NAMESPACE namespace_name '{' top_statement_list '}' { $$ = Stmt_Namespace[name: Name[$2], stmts: $4]; }
| T_NAMESPACE '{' top_statement_list '}' { $$ = Stmt_Namespace[name: null, stmts: $3]; }
| T_USE use_declarations ';' { $$ = Stmt_Use[$2]; }
| T_CONST constant_declaration_list ';' { $$ = Stmt_Const[$2]; }
@ -140,10 +136,10 @@ use_declarations:
;
use_declaration:
namespace_name { $$ = Stmt_UseUse[name: $1, alias: null]; }
| namespace_name T_AS T_STRING { $$ = Stmt_UseUse[name: $1, alias: $3]; }
| T_NS_SEPARATOR namespace_name { $$ = Stmt_UseUse[name: $2, alias: null]; }
| T_NS_SEPARATOR namespace_name T_AS T_STRING { $$ = Stmt_UseUse[name: $2, alias: $4]; }
namespace_name { $$ = Stmt_UseUse[name: Name[$1], alias: null]; }
| namespace_name T_AS T_STRING { $$ = Stmt_UseUse[name: Name[$1], alias: $3]; }
| T_NS_SEPARATOR namespace_name { $$ = Stmt_UseUse[name: Name[$2], alias: null]; }
| T_NS_SEPARATOR namespace_name T_AS T_STRING { $$ = Stmt_UseUse[name: Name[$2], alias: $4]; }
;
constant_declaration_list:
@ -562,14 +558,14 @@ function_call:
;
class_name:
T_STATIC { $$ = Name['static', Name::NORMAL]; }
T_STATIC { $$ = Name_Normal['static']; }
| name { $$ = $1; }
;
name:
namespace_name { $$ = $1; }
| T_NAMESPACE T_NS_SEPARATOR namespace_name { $3->type = Name::RELATIVE; $$ = $3; }
| T_NS_SEPARATOR namespace_name { $2->type = Name::FULLY_QUALIFIED; $$ = $2; }
namespace_name { $$ = Name_Normal[$1]; }
| T_NS_SEPARATOR namespace_name { $$ = Name_FullyQualified[$2]; }
| T_NAMESPACE T_NS_SEPARATOR namespace_name { $$ = Name_Relative[$3]; }
;
class_name_reference:

View File

@ -2,23 +2,17 @@
/**
* @property array $parts Parts of the name
* @property int $type Resolve type (self::NORMAL, self::FULLY_QUALIFIED or self::RELATIVE)
*/
class PHPParser_Node_Name extends PHPParser_NodeAbstract
{
const NORMAL = 0;
const FULLY_QUALIFIED = 1;
const RELATIVE = 2;
/**
* Constructs a name node.
*
* @param string|array $parts Parts of the name (or name as string)
* @param int $type Resolve type
* @param int $line Line
* @param null|string $docComment Nearest doc comment
*/
public function __construct($parts, $type = self::NORMAL, $line = -1, $docComment = null) {
public function __construct($parts, $line = -1, $docComment = null) {
if (!is_array($parts)) {
$parts = explode('\\', $parts);
}
@ -26,7 +20,6 @@ class PHPParser_Node_Name extends PHPParser_NodeAbstract
parent::__construct(
array(
'parts' => $parts,
'type' => $type
),
$line, $docComment
);
@ -50,42 +43,6 @@ class PHPParser_Node_Name extends PHPParser_NodeAbstract
return $this->parts[count($this->parts) - 1];
}
/**
* Checks whether the name is unqualified. (E.g. Name)
*
* @return bool Whether the name is unqualified
*/
public function isUnqualified() {
return self::NORMAL == $this->type && 1 == count($this->parts);
}
/**
* Checks whether the name is qualified. (E.g. Name\Name)
*
* @return bool Whether the name is qualified
*/
public function isQualified() {
return self::NORMAL == $this->type && 1 < count($this->parts);
}
/**
* Checks whether the name is fully qualified. (E.g. \Name)
*
* @return bool Whether the name is fully qualified
*/
public function isFullyQualified() {
return self::FULLY_QUALIFIED == $this->type;
}
/**
* Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
*
* @return bool Whether the name is fully qualified
*/
public function isRelative() {
return self::RELATIVE == $this->type;
}
/**
* Returns a string representation of the name by imploding the namespace parts with a separator.
*
@ -104,7 +61,7 @@ class PHPParser_Node_Name extends PHPParser_NodeAbstract
* @return string String representation
*/
public function __toString() {
return $this->toString('\\');
return implode('\\', $this->parts);
}
/**
@ -167,7 +124,7 @@ class PHPParser_Node_Name extends PHPParser_NodeAbstract
return $name->parts;
} elseif (!is_array($name)) {
throw new InvalidArgumentException(
'When changing a name you need to pass either a string and array or a Name node'
'When changing a name you need to pass either a string, an array or a Name node'
);
}
}

View File

@ -0,0 +1,40 @@
<?php
class PHPParser_Node_Name_FullyQualified extends PHPParser_Node_Name
{
/**
* Checks whether the name is unqualified. (E.g. Name)
*
* @return bool Whether the name is unqualified
*/
public function isUnqualified() {
return false;
}
/**
* Checks whether the name is qualified. (E.g. Name\Name)
*
* @return bool Whether the name is qualified
*/
public function isQualified() {
return false;
}
/**
* Checks whether the name is fully qualified. (E.g. \Name)
*
* @return bool Whether the name is fully qualified
*/
public function isFullyQualified() {
return true;
}
/**
* Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
*
* @return bool Whether the name is relative
*/
public function isRelative() {
return false;
}
}

View File

@ -0,0 +1,40 @@
<?php
class PHPParser_Node_Name_Normal extends PHPParser_Node_Name
{
/**
* Checks whether the name is unqualified. (E.g. Name)
*
* @return bool Whether the name is unqualified
*/
public function isUnqualified() {
return 1 == count($this->parts);
}
/**
* Checks whether the name is qualified. (E.g. Name\Name)
*
* @return bool Whether the name is qualified
*/
public function isQualified() {
return 1 < count($this->parts);
}
/**
* Checks whether the name is fully qualified. (E.g. \Name)
*
* @return bool Whether the name is fully qualified
*/
public function isFullyQualified() {
return false;
}
/**
* Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
*
* @return bool Whether the name is relative
*/
public function isRelative() {
return false;
}
}

View File

@ -0,0 +1,40 @@
<?php
class PHPParser_Node_Name_Relative extends PHPParser_Node_Name
{
/**
* Checks whether the name is unqualified. (E.g. Name)
*
* @return bool Whether the name is unqualified
*/
public function isUnqualified() {
return false;
}
/**
* Checks whether the name is qualified. (E.g. Name\Name)
*
* @return bool Whether the name is qualified
*/
public function isQualified() {
return false;
}
/**
* Checks whether the name is fully qualified. (E.g. \Name)
*
* @return bool Whether the name is fully qualified
*/
public function isFullyQualified() {
return false;
}
/**
* Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
*
* @return bool Whether the name is relative
*/
public function isRelative() {
return true;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -11,9 +11,8 @@ class PHPParser_Parser_Debug extends PHPParser_Parser
"start : top_statement_list",
"top_statement_list : top_statement_list top_statement",
"top_statement_list : /* empty */",
"namespace_name : namespace_name_sub",
"namespace_name_sub : T_STRING",
"namespace_name_sub : namespace_name_sub T_NS_SEPARATOR T_STRING",
"namespace_name : T_STRING",
"namespace_name : namespace_name T_NS_SEPARATOR T_STRING",
"top_statement : statement",
"top_statement : function_declaration_statement",
"top_statement : class_declaration_statement",
@ -252,8 +251,8 @@ class PHPParser_Parser_Debug extends PHPParser_Parser
"class_name : T_STATIC",
"class_name : name",
"name : namespace_name",
"name : T_NAMESPACE T_NS_SEPARATOR namespace_name",
"name : T_NS_SEPARATOR namespace_name",
"name : T_NAMESPACE T_NS_SEPARATOR namespace_name",
"class_name_reference : class_name",
"class_name_reference : dynamic_class_name_reference",
"dynamic_class_name_reference : object_access_for_dcnr",

View File

@ -4,11 +4,6 @@ class PHPParser_PrettyPrinter_Zend extends PHPParser_PrettyPrinterAbstract
{
// Special nodes
public function pName(PHPParser_Node_Name $node) {
return ($node->isFullyQualified() ? '\\' : ($node->isRelative() ? 'namespace\\' : ''))
. implode('\\', $node->parts);
}
public function pParam(PHPParser_Node_Param $node) {
return ($node->type ? ('array' == $node->type ? 'array' : $this->p($node->type)) . ' ' : '')
. ($node->byRef ? '&' : '')
@ -24,6 +19,24 @@ class PHPParser_PrettyPrinter_Zend extends PHPParser_PrettyPrinterAbstract
return $node->name . ' = ' . $this->p($node->value);
}
// Names
public function pName(PHPParser_Node_Name $node) {
return implode('\\', $node->parts);
}
public function pName_Normal(PHPParser_Node_Name_Normal $node) {
return implode('\\', $node->parts);
}
public function pName_FullyQualified(PHPParser_Node_Name_FullyQualified $node) {
return '\\' . implode('\\', $node->parts);
}
public function pName_Relative(PHPParser_Node_Name_Relative $node) {
return 'namespace\\' . implode('\\', $node->parts);
}
// Magic Constants
public function pScalar_ClassConst(PHPParser_Node_Scalar_ClassConst $node) {

View File

@ -33,7 +33,6 @@ class PHPParser_Tests_NodeDumperTest extends PHPUnit_Framework_TestCase
0: Hallo
1: World
)
type: 0
)'
),
array(