← All changes
|
vendor/nikic/php-parser/lib/PhpParser/ParserFactory.php
+33
-1
3.0
→
3.5.9
View file →
| @@ -1,8 +1,11 @@ | ||
| 1 | 1 | <?php declare(strict_types=1); |
| 2 | 2 | |
| 3 | 3 | namespace PhpParser; |
| 4 | 4 | |
| 5 | +use PhpParser\Lexer\Emulative; | |
| 6 | +use PhpParser\Parser\Php7; | |
| 7 | + | |
| 5 | 8 | class ParserFactory |
| 6 | 9 | { |
| 7 | 10 | const PREFER_PHP7 = 1; |
| 8 | 11 | const PREFER_PHP5 = 2; |
| @@ -17,9 +20,9 @@ | ||
| 17 | 20 | * @param array $parserOptions Parser options. See ParserAbstract::__construct() argument |
| 18 | 21 | * |
| 19 | 22 | * @return Parser The parser instance |
| 20 | 23 | */ |
| 21 | - public function create(int $kind, Lexer $lexer = null, array $parserOptions = []) : Parser { | |
| 24 | + public function create(int $kind, ?Lexer $lexer = null, array $parserOptions = []) : Parser { | |
| 22 | 25 | if (null === $lexer) { |
| 23 | 26 | $lexer = new Lexer\Emulative(); |
| 24 | 27 | } |
| 25 | 28 | switch ($kind) { |
| @@ -39,6 +42,35 @@ | ||
| 39 | 42 | throw new \LogicException( |
| 40 | 43 | 'Kind must be one of ::PREFER_PHP7, ::PREFER_PHP5, ::ONLY_PHP7 or ::ONLY_PHP5' |
| 41 | 44 | ); |
| 42 | 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 | + ]]; | |
| 43 | 75 | } |
| 44 | 76 | } |