12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php declare(strict_types=1);
- namespace PhpParser;
- use PhpParser\Lexer\Emulative;
- use PhpParser\Parser\Php7;
- class ParserFactory
- {
- const PREFER_PHP7 = 1;
- const PREFER_PHP5 = 2;
- const ONLY_PHP7 = 3;
- const ONLY_PHP5 = 4;
-
- public function create(int $kind, ?Lexer $lexer = null, array $parserOptions = []) : Parser {
- if (null === $lexer) {
- $lexer = new Lexer\Emulative();
- }
- switch ($kind) {
- case self::PREFER_PHP7:
- return new Parser\Multiple([
- new Parser\Php7($lexer, $parserOptions), new Parser\Php5($lexer, $parserOptions)
- ]);
- case self::PREFER_PHP5:
- return new Parser\Multiple([
- new Parser\Php5($lexer, $parserOptions), new Parser\Php7($lexer, $parserOptions)
- ]);
- case self::ONLY_PHP7:
- return new Parser\Php7($lexer, $parserOptions);
- case self::ONLY_PHP5:
- return new Parser\Php5($lexer, $parserOptions);
- default:
- throw new \LogicException(
- 'Kind must be one of ::PREFER_PHP7, ::PREFER_PHP5, ::ONLY_PHP7 or ::ONLY_PHP5'
- );
- }
- }
-
- public function createForNewestSupportedVersion(): Parser {
- return new Php7(new Emulative($this->getLexerOptions()));
- }
-
- public function createForHostVersion(): Parser {
- return new Php7(new Lexer($this->getLexerOptions()));
- }
- private function getLexerOptions(): array {
- return ['usedAttributes' => [
- 'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos', 'startFilePos', 'endFilePos',
- ]];
- }
- }
|