ParserFactory.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace PhpParser;
  3. class ParserFactory {
  4. const PREFER_PHP7 = 1;
  5. const PREFER_PHP5 = 2;
  6. const ONLY_PHP7 = 3;
  7. const ONLY_PHP5 = 4;
  8. /**
  9. * Creates a Parser instance, according to the provided kind.
  10. *
  11. * @param int $kind One of ::PREFER_PHP7, ::PREFER_PHP5, ::ONLY_PHP7 or ::ONLY_PHP5
  12. * @param Lexer|null $lexer Lexer to use. Defaults to emulative lexer when not specified
  13. * @param array $parserOptions Parser options. See ParserAbstract::__construct() argument
  14. *
  15. * @return Parser The parser instance
  16. */
  17. public function create($kind, Lexer $lexer = null, array $parserOptions = array()) {
  18. if (null === $lexer) {
  19. $lexer = new Lexer\Emulative();
  20. }
  21. switch ($kind) {
  22. case self::PREFER_PHP7:
  23. return new Parser\Multiple([
  24. new Parser\Php7($lexer, $parserOptions), new Parser\Php5($lexer, $parserOptions)
  25. ]);
  26. case self::PREFER_PHP5:
  27. return new Parser\Multiple([
  28. new Parser\Php5($lexer, $parserOptions), new Parser\Php7($lexer, $parserOptions)
  29. ]);
  30. case self::ONLY_PHP7:
  31. return new Parser\Php7($lexer, $parserOptions);
  32. case self::ONLY_PHP5:
  33. return new Parser\Php5($lexer, $parserOptions);
  34. default:
  35. throw new \LogicException(
  36. 'Kind must be one of ::PREFER_PHP7, ::PREFER_PHP5, ::ONLY_PHP7 or ::ONLY_PHP5'
  37. );
  38. }
  39. }
  40. }