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