| 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: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 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: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public function createForNewestSupportedVersion(): Parser { |
| 57: | return new Php7(new Emulative($this->getLexerOptions())); |
| 58: | } |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 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: | |