|
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;
|
@@ -41,4 +44,33 @@ public function create(int $kind, Lexer $lexer = null, array $parserOptions = []
|
41 | 44 | );
|
42 | 45 | }
|
43 | 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 | + } |
44 | 76 | }
|
0 commit comments