| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace PhpParser; |
| 4 |
|
| 5 |
use PhpParser\Lexer\Emulative; |
| 6 |
use PhpParser\Parser\Php7; |
| 7 |
|
| 8 |
class ParserFactory |
| 9 |
{ |
| 10 |
const PREFER_PHP7 = 1; |
| 11 |
const PREFER_PHP5 = 2; |
| 12 |
const ONLY_PHP7 = 3; |
| 13 |
const ONLY_PHP5 = 4; |
| 14 |
|
| 15 |
/** |
| 16 |
* Creates a Parser instance, according to the provided kind. |
| 17 |
* |
| 18 |
* @param int $kind One of ::PREFER_PHP7, ::PREFER_PHP5, ::ONLY_PHP7 or ::ONLY_PHP5 |
| 19 |
* @param Lexer|null $lexer Lexer to use. Defaults to emulative lexer when not specified |
| 20 |
* @param array $parserOptions Parser options. See ParserAbstract::__construct() argument |
| 21 |
* |
| 22 |
* @return Parser The parser instance |
| 23 |
*/ |
| 24 |
public function create(int $kind, ?Lexer $lexer = null, array $parserOptions = []) : Parser { |
| 25 |
if (null === $lexer) { |
| 26 |
$lexer = new Lexer\Emulative(); |
| 27 |
} |
| 28 |
switch ($kind) { |
| 29 |
case self::PREFER_PHP7: |
| 30 |
return new Parser\Multiple([ |
| 31 |
new Parser\Php7($lexer, $parserOptions), new Parser\Php5($lexer, $parserOptions) |
| 32 |
]); |
| 33 |
case self::PREFER_PHP5: |
| 34 |
return new Parser\Multiple([ |
| 35 |
new Parser\Php5($lexer, $parserOptions), new Parser\Php7($lexer, $parserOptions) |
| 36 |
]); |
| 37 |
case self::ONLY_PHP7: |
| 38 |
return new Parser\Php7($lexer, $parserOptions); |
| 39 |
case self::ONLY_PHP5: |
| 40 |
return new Parser\Php5($lexer, $parserOptions); |
| 41 |
default: |
| 42 |
throw new \LogicException( |
| 43 |
'Kind must be one of ::PREFER_PHP7, ::PREFER_PHP5, ::ONLY_PHP7 or ::ONLY_PHP5' |
| 44 |
); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Create a parser targeting the newest version supported by this library. Code for older |
| 50 |
* versions will be accepted if there have been no relevant backwards-compatibility breaks in |
| 51 |
* PHP. |
| 52 |
* |
| 53 |
* All supported lexer attributes (comments, startLine, endLine, startTokenPos, endTokenPos, |
| 54 |
* startFilePos, endFilePos) will be enabled. |
| 55 |
*/ |
| 56 |
public function createForNewestSupportedVersion(): Parser { |
| 57 |
return new Php7(new Emulative($this->getLexerOptions())); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Create a parser targeting the host PHP version, that is the PHP version we're currently |
| 62 |
* running on. This parser will not use any token emulation. |
| 63 |
* |
| 64 |
* All supported lexer attributes (comments, startLine, endLine, startTokenPos, endTokenPos, |
| 65 |
* startFilePos, endFilePos) will be enabled. |
| 66 |
*/ |
| 67 |
public function createForHostVersion(): Parser { |
| 68 |
return new Php7(new Lexer($this->getLexerOptions())); |
| 69 |
} |
| 70 |
|
| 71 |
private function getLexerOptions(): array { |
| 72 |
return ['usedAttributes' => [ |
| 73 |
'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos', 'startFilePos', 'endFilePos', |
| 74 |
]]; |
| 75 |
} |
| 76 |
} |
| 77 |
|