1 <?php declare(strict_types=1);
3 namespace PhpParser\Parser;
6 use PhpParser\ErrorHandler;
9 class Multiple implements Parser
11 /** @var Parser[] List of parsers to try, in order of preference */
15 * Create a parser which will try multiple parsers in an order of preference.
17 * Parsers will be invoked in the order they're provided to the constructor. If one of the
18 * parsers runs without throwing, it's output is returned. Otherwise the exception that the
19 * first parser generated is thrown.
21 * @param Parser[] $parsers
23 public function __construct(array $parsers) {
24 $this->parsers = $parsers;
27 public function parse(string $code, ErrorHandler $errorHandler = null) {
28 if (null === $errorHandler) {
29 $errorHandler = new ErrorHandler\Throwing;
32 list($firstStmts, $firstError) = $this->tryParse($this->parsers[0], $errorHandler, $code);
33 if ($firstError === null) {
37 for ($i = 1, $c = count($this->parsers); $i < $c; ++$i) {
38 list($stmts, $error) = $this->tryParse($this->parsers[$i], $errorHandler, $code);
39 if ($error === null) {
47 private function tryParse(Parser $parser, ErrorHandler $errorHandler, $code) {
51 $stmts = $parser->parse($code, $errorHandler);
52 } catch (Error $error) {}
53 return [$stmts, $error];