mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-30 04:29:15 +01:00
Put statements belonging to a namespace statement into its stmt property, add some further checks against invalid namespace usage and fix the pretty printer to print global namespaces
This commit is contained in:
parent
1478ae9c54
commit
fb45e8a30c
@ -19432,4 +19432,4 @@ Statistics for zend_language_parser.phpy:
|
||||
3815 items
|
||||
1124 lookahead sets used
|
||||
13442+794=14236 action entries
|
||||
229512 bytes used
|
||||
229536 bytes used
|
||||
|
@ -104,7 +104,7 @@
|
||||
%%
|
||||
|
||||
start:
|
||||
top_statement_list { $$ = $1; }
|
||||
top_statement_list { $$ = PHPParser_Node_Stmt_Namespace::postprocess($1); }
|
||||
;
|
||||
|
||||
top_statement_list:
|
||||
@ -127,9 +127,9 @@ top_statement:
|
||||
| class_declaration_statement { $$ = $1; }
|
||||
| T_HALT_COMPILER
|
||||
{ $$ = new PHPParser_Node_Stmt_HaltCompiler(array('remaining' => #this->lexer->handleHaltCompiler()), #this->line); }
|
||||
| T_NAMESPACE namespace_name ';' { $$ = new PHPParser_Node_Stmt_Namespace(array('ns' => $2), #this->line); }
|
||||
| T_NAMESPACE namespace_name '{' top_statement_list '}' { $$ = array(new PHPParser_Node_Stmt_Namespace(array('ns' => $2), #this->line), $4); }
|
||||
| T_NAMESPACE '{' top_statement_list '}' { $$ = array(new PHPParser_Node_Stmt_Namespace(array('ns' => null), #this->line), $3); }
|
||||
| T_NAMESPACE namespace_name ';' { $$ = new PHPParser_Node_Stmt_Namespace(array('name' => $2, 'stmts' => null), #this->line); }
|
||||
| T_NAMESPACE namespace_name '{' top_statement_list '}' { $$ = new PHPParser_Node_Stmt_Namespace(array('name' => $2, 'stmts' => $4), #this->line); }
|
||||
| T_NAMESPACE '{' top_statement_list '}' { $$ = new PHPParser_Node_Stmt_Namespace(array('name' => null, 'stmts' => $3), #this->line); }
|
||||
| T_USE use_declarations ';' { $$ = new PHPParser_Node_Stmt_Use(array('uses' => $2), #this->line); }
|
||||
| constant_declaration ';' { $$ = new PHPParser_Node_Stmt_Const(array('consts' => $1), #this->line); }
|
||||
;
|
||||
|
@ -104,7 +104,7 @@
|
||||
%%
|
||||
|
||||
start:
|
||||
top_statement_list { $$ = $1; }
|
||||
top_statement_list { $$ = Stmt_Namespace::postprocess($1); }
|
||||
;
|
||||
|
||||
top_statement_list:
|
||||
@ -127,9 +127,9 @@ top_statement:
|
||||
| class_declaration_statement { $$ = $1; }
|
||||
| T_HALT_COMPILER
|
||||
{ $$ = Stmt_HaltCompiler[remaining: #this->lexer->handleHaltCompiler()]; }
|
||||
| T_NAMESPACE namespace_name ';' { $$ = Stmt_Namespace[ns: $2]; }
|
||||
| T_NAMESPACE namespace_name '{' top_statement_list '}' { $$ = array(Stmt_Namespace[ns: $2], $4); }
|
||||
| T_NAMESPACE '{' top_statement_list '}' { $$ = array(Stmt_Namespace[ns: null], $3); }
|
||||
| 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 '{' top_statement_list '}' { $$ = Stmt_Namespace[name: null, stmts: $3]; }
|
||||
| T_USE use_declarations ';' { $$ = Stmt_Use[uses: $2]; }
|
||||
| constant_declaration ';' { $$ = Stmt_Const[consts: $1]; }
|
||||
;
|
||||
|
@ -1,8 +1,98 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property PHPParser_Node_Name $ns Namespace
|
||||
* @property PHPParser_Node_Name $name Name
|
||||
* @property array $stmts Statements
|
||||
*/
|
||||
class PHPParser_Node_Stmt_Namespace extends PHPParser_Node_Stmt
|
||||
{
|
||||
public function __construct(array $subNodes, $line) {
|
||||
parent::__construct($subNodes, $line);
|
||||
|
||||
if ('self' === $this->name || 'parent' === $this->name) {
|
||||
throw new PHPParser_Error(sprintf('Cannot use "%s" as namespace name', $this->name), $line);
|
||||
}
|
||||
|
||||
if (null !== $this->stmts) {
|
||||
foreach ($this->stmts as $stmt) {
|
||||
if ($stmt instanceof PHPParser_Node_Stmt_Namespace) {
|
||||
throw new PHPParser_Error('Namespace declarations cannot be nested', $stmt->getLine());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function postprocess(array $stmts) {
|
||||
// null = not in namespace, false = semicolon style, true = bracket style
|
||||
$bracketed = null;
|
||||
|
||||
// whether any statements that aren't allowed before a namespace declaration are encountered
|
||||
// (the only valid statement currently is a declare)
|
||||
$hasNotAllowedStmts = false;
|
||||
|
||||
// offsets for semicolon style namespaces
|
||||
// (required for transplanting the following statements into their ->stmts property)
|
||||
$nsOffsets = array();
|
||||
|
||||
foreach ($stmts as $i => $stmt) {
|
||||
if ($stmt instanceof PHPParser_Node_Stmt_Namespace) {
|
||||
// ->stmts is null if semicolon style is used
|
||||
$currentBracketed = null !== $stmt->stmts;
|
||||
|
||||
// if no namespace statement has been encountered yet
|
||||
if (!isset($bracketed)) {
|
||||
// set the namespacing style
|
||||
$bracketed = $currentBracketed;
|
||||
|
||||
// and ensure that it isn't preceded by a not allowed statement
|
||||
if ($hasNotAllowedStmts) {
|
||||
throw new PHPParser_Error('Namespace declaration statement has to be the very first statement in the script', $stmt->getLine());
|
||||
}
|
||||
// otherwise ensure that the style of the current namespace matches the style of
|
||||
// namespaceing used before in this document
|
||||
} elseif ($bracketed !== $currentBracketed) {
|
||||
throw new PHPParser_Error('Cannot mix bracketed namespace declarations with unbracketed namespace declarations', $stmt->getLine());
|
||||
}
|
||||
|
||||
// for semicolon style namespaces remember the offset
|
||||
if (!$bracketed) {
|
||||
$nsOffsets[] = $i;
|
||||
}
|
||||
// declare() is the only valid statement before a namespace
|
||||
} elseif (!$stmt instanceof PHPParser_Node_Stmt_Declare) {
|
||||
if (true === $bracketed) {
|
||||
throw new PHPParser_Error('No code may exist outside of namespace {}', $stmt->getLine());
|
||||
}
|
||||
|
||||
$hasNotAllowedStmts = true;
|
||||
}
|
||||
}
|
||||
|
||||
// if bracketed namespaces were used or no namespaces were used at all just return the
|
||||
// original statements
|
||||
if (!isset($bracketed) || true === $bracketed) {
|
||||
return $stmts;
|
||||
// for semicolon style transplant statements
|
||||
} else {
|
||||
// take all statements preceding the first namespace
|
||||
$newStmts = array_slice($stmts, 0, $nsOffsets[0]);
|
||||
|
||||
// iterate over all following namespaces
|
||||
for ($i = 0, $c = count($nsOffsets); $i < $c; ++$i) {
|
||||
$nsStmt = $stmts[$nsOffsets[$i]];
|
||||
|
||||
// the last namespace takes all statements after it
|
||||
if ($c === $i + 1) {
|
||||
$nsStmt->stmts = array_slice($stmts, $nsOffsets[$i] + 1);
|
||||
// and all the others take all statements between the current and the following one
|
||||
} else {
|
||||
$nsStmt->stmts = array_slice($stmts, $nsOffsets[$i] + 1, $nsOffsets[$i + 1] - $nsOffsets[$i] - 1);
|
||||
}
|
||||
|
||||
$newStmts[] = $nsStmt;
|
||||
}
|
||||
|
||||
return $newStmts;
|
||||
}
|
||||
}
|
||||
}
|
@ -898,7 +898,6 @@ class PHPParser_Parser
|
||||
$yystate = 0;
|
||||
$yychar = -1;
|
||||
|
||||
//$yylval = null;
|
||||
$yysstk[$this->yysp] = 0;
|
||||
/*$yyerrflag = 0;*/
|
||||
|
||||
@ -1030,7 +1029,7 @@ class PHPParser_Parser
|
||||
}
|
||||
|
||||
private function yyn1() {
|
||||
$this->yyval = $this->yyastk[$this->yysp-(1-1)];
|
||||
$this->yyval = PHPParser_Node_Stmt_Namespace::postprocess($this->yyastk[$this->yysp-(1-1)]);
|
||||
}
|
||||
|
||||
private function yyn2() {
|
||||
@ -1070,15 +1069,15 @@ class PHPParser_Parser
|
||||
}
|
||||
|
||||
private function yyn11() {
|
||||
$this->yyval = new PHPParser_Node_Stmt_Namespace(array('ns' => $this->yyastk[$this->yysp-(3-2)]), $this->line);
|
||||
$this->yyval = new PHPParser_Node_Stmt_Namespace(array('name' => $this->yyastk[$this->yysp-(3-2)], 'stmts' => null), $this->line);
|
||||
}
|
||||
|
||||
private function yyn12() {
|
||||
$this->yyval = array(new PHPParser_Node_Stmt_Namespace(array('ns' => $this->yyastk[$this->yysp-(5-2)]), $this->line), $this->yyastk[$this->yysp-(5-4)]);
|
||||
$this->yyval = new PHPParser_Node_Stmt_Namespace(array('name' => $this->yyastk[$this->yysp-(5-2)], 'stmts' => $this->yyastk[$this->yysp-(5-4)]), $this->line);
|
||||
}
|
||||
|
||||
private function yyn13() {
|
||||
$this->yyval = array(new PHPParser_Node_Stmt_Namespace(array('ns' => null), $this->line), $this->yyastk[$this->yysp-(4-3)]);
|
||||
$this->yyval = new PHPParser_Node_Stmt_Namespace(array('name' => null, 'stmts' => $this->yyastk[$this->yysp-(4-3)]), $this->line);
|
||||
}
|
||||
|
||||
private function yyn14() {
|
||||
|
@ -1292,7 +1292,6 @@ class PHPParser_ParserDebug
|
||||
$yystate = 0;
|
||||
$yychar = -1;
|
||||
|
||||
//$yylval = null;
|
||||
$yysstk[$this->yysp] = 0;
|
||||
/*$yyerrflag = 0;*/
|
||||
|
||||
@ -1432,7 +1431,7 @@ class PHPParser_ParserDebug
|
||||
}
|
||||
|
||||
private function yyn1() {
|
||||
$this->yyval = $this->yyastk[$this->yysp-(1-1)];
|
||||
$this->yyval = PHPParser_Node_Stmt_Namespace::postprocess($this->yyastk[$this->yysp-(1-1)]);
|
||||
}
|
||||
|
||||
private function yyn2() {
|
||||
@ -1472,15 +1471,15 @@ class PHPParser_ParserDebug
|
||||
}
|
||||
|
||||
private function yyn11() {
|
||||
$this->yyval = new PHPParser_Node_Stmt_Namespace(array('ns' => $this->yyastk[$this->yysp-(3-2)]), $this->line);
|
||||
$this->yyval = new PHPParser_Node_Stmt_Namespace(array('name' => $this->yyastk[$this->yysp-(3-2)], 'stmts' => null), $this->line);
|
||||
}
|
||||
|
||||
private function yyn12() {
|
||||
$this->yyval = array(new PHPParser_Node_Stmt_Namespace(array('ns' => $this->yyastk[$this->yysp-(5-2)]), $this->line), $this->yyastk[$this->yysp-(5-4)]);
|
||||
$this->yyval = new PHPParser_Node_Stmt_Namespace(array('name' => $this->yyastk[$this->yysp-(5-2)], 'stmts' => $this->yyastk[$this->yysp-(5-4)]), $this->line);
|
||||
}
|
||||
|
||||
private function yyn13() {
|
||||
$this->yyval = array(new PHPParser_Node_Stmt_Namespace(array('ns' => null), $this->line), $this->yyastk[$this->yysp-(4-3)]);
|
||||
$this->yyval = new PHPParser_Node_Stmt_Namespace(array('name' => null, 'stmts' => $this->yyastk[$this->yysp-(4-3)]), $this->line);
|
||||
}
|
||||
|
||||
private function yyn14() {
|
||||
|
@ -424,7 +424,8 @@ class PHPParser_PrettyPrinter_Zend extends PHPParser_PrettyPrinterAbstract
|
||||
// Declarations
|
||||
|
||||
public function pStmt_Namespace(PHPParser_Node_Stmt_Namespace $node) {
|
||||
return 'namespace ' . $this->p($node->ns);
|
||||
return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '')
|
||||
. ' {' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
|
||||
}
|
||||
|
||||
public function pStmt_Use(PHPParser_Node_Stmt_Use $node) {
|
||||
|
@ -71,6 +71,7 @@ abstract class PHPParser_PrettyPrinterAbstract
|
||||
'Stmt_TryCatch' => true,
|
||||
'Stmt_Label' => true,
|
||||
'Stmt_HaltCompiler' => true,
|
||||
'Stmt_Namespace' => true,
|
||||
);
|
||||
|
||||
protected $precedenceStack;
|
||||
|
Loading…
Reference in New Issue
Block a user