xref: /php-ast/scripts/generate_ast_stub.php (revision 7fe259ed)
1<?php
2
3$stubFile = __DIR__ . '/../ast_stub.php';
4if (!extension_loaded('ast')) {
5    fwrite(STDERR, __FILE__ . " requires that php-ast be enabled\n");
6    exit(1);
7}
8
9$stub = file_get_contents($stubFile);
10$stub = preg_replace_callback(
11    '~(?<=// AST KIND CONSTANTS\n).*(?=\n// END AST KIND CONSTANTS)~s',
12    function ($matches) {
13        $consts = "namespace ast;";
14        foreach (get_defined_constants(true)['ast'] as $name => $value) {
15            if (0 === strpos($name, 'ast\\AST_')) {
16                $consts .= "\nconst " . substr($name, 4) . " = $value;";
17            }
18        }
19        return $consts;
20    },
21    $stub
22);
23$stub = preg_replace_callback(
24    '~(?<=// AST FLAG CONSTANTS\n).*(?=\n// END AST FLAG CONSTANTS)~s',
25    function ($matches) {
26        $consts = "namespace ast\\flags;";
27        foreach (get_defined_constants(true)['ast'] as $name => $value) {
28            if (0 === strpos($name, 'ast\\flags\\')) {
29                $consts .= "\nconst " . substr($name, 10) . " = $value;";
30            }
31        }
32        return $consts;
33    },
34    $stub
35);
36file_put_contents($stubFile, $stub);
37