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

PHP 7.1: Support multi-catch

Catch::$type is now an array Catch::$types.
This commit is contained in:
Nikita Popov 2016-07-22 15:40:00 +02:00
parent 7ff12b8fcb
commit 537b59d4d1
10 changed files with 1089 additions and 983 deletions

View File

@ -201,7 +201,7 @@ catches:
catch:
T_CATCH '(' name T_VARIABLE ')' '{' inner_statement_list '}'
{ $$ = Stmt\Catch_[$3, parseVar($4), $7]; }
{ $$ = Stmt\Catch_[array($3), parseVar($4), $7]; }
;
optional_finally:

View File

@ -195,8 +195,13 @@ catches:
| catches catch { push($1, $2); }
;
name_union:
name { init($1); }
| name_union '|' name { push($1, $3); }
;
catch:
T_CATCH '(' name T_VARIABLE ')' '{' inner_statement_list '}'
T_CATCH '(' name_union T_VARIABLE ')' '{' inner_statement_list '}'
{ $$ = Stmt\Catch_[$3, parseVar($4), $7]; }
;

View File

@ -6,8 +6,8 @@ use PhpParser\Node;
class Catch_ extends Node\Stmt
{
/** @var Node\Name Class of exception */
public $type;
/** @var Node\Name[] Types of exceptions to catch */
public $types;
/** @var string Variable for exception */
public $var;
/** @var Node[] Statements */
@ -16,19 +16,19 @@ class Catch_ extends Node\Stmt
/**
* Constructs a catch node.
*
* @param Node\Name $type Class of exception
* @param string $var Variable for exception
* @param Node[] $stmts Statements
* @param array $attributes Additional attributes
* @param Node\Name[] $types Types of exceptions to catch
* @param string $var Variable for exception
* @param Node[] $stmts Statements
* @param array $attributes Additional attributes
*/
public function __construct(Node\Name $type, $var, array $stmts = array(), array $attributes = array()) {
public function __construct(array $types, $var, array $stmts = array(), array $attributes = array()) {
parent::__construct($attributes);
$this->type = $type;
$this->types = $types;
$this->var = $var;
$this->stmts = $stmts;
}
public function getSubNodeNames() {
return array('type', 'var', 'stmts');
return array('types', 'var', 'stmts');
}
}

View File

@ -74,7 +74,9 @@ class NameResolver extends NodeVisitorAbstract
$node->class = $this->resolveClassName($node->class);
}
} elseif ($node instanceof Stmt\Catch_) {
$node->type = $this->resolveClassName($node->type);
foreach ($node->types as &$type) {
$type = $this->resolveClassName($type);
}
} elseif ($node instanceof Expr\FuncCall) {
if ($node->name instanceof Name) {
$node->name = $this->resolveOtherName($node->name, Stmt\Use_::TYPE_FUNCTION);

View File

@ -1553,7 +1553,7 @@ class Php5 extends \PhpParser\ParserAbstract
}
protected function reduceRule160() {
$this->semValue = new Stmt\Catch_($this->semStack[$this->stackPos-(8-3)], substr($this->semStack[$this->stackPos-(8-4)], 1), $this->semStack[$this->stackPos-(8-7)], $this->startAttributeStack[$this->stackPos-(8-1)] + $this->endAttributes);
$this->semValue = new Stmt\Catch_(array($this->semStack[$this->stackPos-(8-3)]), substr($this->semStack[$this->stackPos-(8-4)], 1), $this->semStack[$this->stackPos-(8-7)], $this->startAttributeStack[$this->stackPos-(8-1)] + $this->endAttributes);
}
protected function reduceRule161() {

File diff suppressed because it is too large Load Diff

View File

@ -735,7 +735,7 @@ class Standard extends PrettyPrinterAbstract
}
public function pStmt_Catch(Stmt\Catch_ $node) {
return ' catch (' . $this->p($node->type) . ' $' . $node->var . ') {'
return ' catch (' . $this->pImplode($node->types, '|') . ' $' . $node->var . ') {'
. $this->pStmts($node->stmts) . "\n" . '}';
}

View File

@ -0,0 +1,65 @@
Try/catch with multiple classes
-----
<?php
try {
$x;
} catch (X|Y $e1) {
$y;
} catch (\A|B\C $e2) {
$z;
}
-----
!!php7
array(
0: Stmt_TryCatch(
stmts: array(
0: Expr_Variable(
name: x
)
)
catches: array(
0: Stmt_Catch(
types: array(
0: Name(
parts: array(
0: X
)
)
1: Name(
parts: array(
0: Y
)
)
)
var: e1
stmts: array(
0: Expr_Variable(
name: y
)
)
)
1: Stmt_Catch(
types: array(
0: Name_FullyQualified(
parts: array(
0: A
)
)
1: Name(
parts: array(
0: B
1: C
)
)
)
var: e2
stmts: array(
0: Expr_Variable(
name: z
)
)
)
)
finallyStmts: null
)
)

View File

@ -36,9 +36,11 @@ array(
)
catches: array(
0: Stmt_Catch(
type: Name(
parts: array(
0: A
types: array(
0: Name(
parts: array(
0: A
)
)
)
var: b
@ -55,9 +57,11 @@ array(
)
)
1: Stmt_Catch(
type: Name(
parts: array(
0: B
types: array(
0: Name(
parts: array(
0: B
)
)
)
var: c
@ -91,9 +95,11 @@ array(
)
catches: array(
0: Stmt_Catch(
type: Name(
parts: array(
0: A
types: array(
0: Name(
parts: array(
0: A
)
)
)
var: b

View File

@ -0,0 +1,19 @@
Multi catch
-----
<?php
try {
$x;
} catch (X|Y $e1) {
$y;
} catch (\A|B\C $e2) {
$z;
}
-----
!!php7
try {
$x;
} catch (X|Y $e1) {
$y;
} catch (\A|B\C $e2) {
$z;
}