You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.6 KiB

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