tokenizer_data_gen.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. $rootDir = __DIR__ . '/../..';
  3. $infile = $rootDir . '/Zend/zend_language_parser.y';
  4. $outfile = $rootDir . '/ext/tokenizer/tokenizer_data.c';
  5. if (!file_exists($infile)) {
  6. fwrite(STDERR, <<<ERROR
  7. $infile is missing.
  8. Please, generate the PHP parser files by scripts/dev/genfiles
  9. or by running the ./configure build step.
  10. ERROR
  11. );
  12. exit(1);
  13. }
  14. $result = '';
  15. $result .= <<<CODE
  16. /*
  17. +----------------------------------------------------------------------+
  18. | Copyright (c) The PHP Group |
  19. +----------------------------------------------------------------------+
  20. | This source file is subject to version 3.01 of the PHP license, |
  21. | that is bundled with this package in the file LICENSE, and is |
  22. | available through the world-wide-web at the following url: |
  23. | https://www.php.net/license/3_01.txt |
  24. | If you did not receive a copy of the PHP license and are unable to |
  25. | obtain it through the world-wide-web, please send a note to |
  26. | license@php.net so we can mail you a copy immediately. |
  27. +----------------------------------------------------------------------+
  28. | Author: Johannes Schlueter <johannes@php.net> |
  29. +----------------------------------------------------------------------+
  30. */
  31. /*
  32. DO NOT EDIT THIS FILE!
  33. This file is generated using tokenizer_data_gen.php
  34. */
  35. #include "php.h"
  36. #include "zend.h"
  37. #include <zend_language_parser.h>
  38. void tokenizer_register_constants(INIT_FUNC_ARGS) {
  39. CODE;
  40. $incontent = file_get_contents($infile);
  41. preg_match_all('(^%token.*\b(?<token_name>T_.*?)\b)m', $incontent, $matches);
  42. foreach ($matches['token_name'] as $tokenName) {
  43. if ($tokenName === 'T_NOELSE' || $tokenName === 'T_ERROR') {
  44. continue;
  45. }
  46. $result .= "\tREGISTER_LONG_CONSTANT(\"$tokenName\", $tokenName, CONST_CS | CONST_PERSISTENT);\n";
  47. }
  48. $result .= "\tREGISTER_LONG_CONSTANT(\"T_DOUBLE_COLON\", T_PAAMAYIM_NEKUDOTAYIM, CONST_CS | CONST_PERSISTENT);\n";
  49. $result .= <<<CODE
  50. }
  51. char *get_token_type_name(int token_type)
  52. {
  53. \tswitch (token_type) {
  54. CODE;
  55. foreach ($matches['token_name'] as $tokenName) {
  56. if ($tokenName === 'T_NOELSE' || $tokenName === 'T_ERROR') {
  57. continue;
  58. }
  59. if ($tokenName === 'T_PAAMAYIM_NEKUDOTAYIM') {
  60. $result .= "\t\tcase T_PAAMAYIM_NEKUDOTAYIM: return \"T_DOUBLE_COLON\";\n";
  61. } else {
  62. $result .= "\t\tcase $tokenName: return \"$tokenName\";\n";
  63. }
  64. }
  65. $result .= <<<CODE
  66. \t}
  67. \treturn NULL;
  68. }
  69. CODE;
  70. file_put_contents($outfile, $result);
  71. echo "Wrote $outfile\n";