File: extract_functions.php

package info (click to toggle)
php-elisp 1.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,268 kB
  • sloc: lisp: 7,468; php: 5,396; makefile: 42; sh: 10
file content (118 lines) | stat: -rwxr-xr-x 3,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env php
<?php

/**
 * Extract PHP Functions
 *
 * @copyright 2023 Friends of Emacs-PHP development
 * @author USAMI Kenta <tadsan@zonu.me>
 * @license FSFAP https://spdx.org/licenses/FSFAP.html
 */

// Copying and distribution of this file, with or without modification,
// are permitted in any medium without royalty provided the copyright
// notice and this notice are preserved.  This file is offered as-is,
// without any warranty.

declare(strict_types=1);

error_reporting(E_ALL);

$functions = json_decode(stream_get_contents(STDIN), true);

$command = $argv[0];
$subcommand = $argv[1] ?? null;

$subcommands = [
    'count' => function (array $extracted) {
        echo json_encode(array_map(count(...), $extracted), JSON_PRETTY_PRINT), PHP_EOL;
    },
    'modules' => function (array $extracted) {
        echo implode(PHP_EOL, array_keys($extracted)), PHP_EOL;
    },
    'functions' => function (array $extracted) {
        echo json_encode(array_map(array_keys(...), $extracted), JSON_PRETTY_PRINT), PHP_EOL;
    },
    'functions-txt' => function (array $extracted) {
        foreach ($extracted as $functions) {
            foreach ($functions as $name => $_) {
                echo $name, PHP_EOL;
            }
        }
    },
    'functions-sexp' => function (array $extracted) {
        echo "  '(";
        foreach ($extracted as $module => $functions) {
            echo "\n    ({$module}";
            //            ksort($functions);
            foreach ($functions as $name => $function) {
                $escaped_name = strtr($name, ['\\' => '\\\\']);
                echo "\n     \"{$escaped_name}\"";
            }
            echo ")";
        }
        echo ")";
    },
];

if (!isset($subcommands[$subcommand])) {
    $json_url = 'http://doc.php.net/downloads/json/php_manual_en.json';
    fwrite(STDERR, implode(PHP_EOL, [
        "[Extract PHP Functions]\n",
        "This script extract PHP function names from <{$json_url}>.\n",
        "Usage:",
        "\t{$command} count < php_manual_en.json",
        "\t{$command} modules < php_manual_en.json",
        "\t{$command} functions < php_manual_en.json > result.json",
        "\t{$command} functions-sexp < php_manual_en.json > result.el",
        '',
    ]));
    exit(1);
}

$module_id_patterns = array_map(
    fn($allowlist) => '/\A(?:' . implode('|', array_map(
        fn($preg) => strtr($preg, ['\.' => '.', '.' => '\.']) .
            (preg_match('/[0-9a-z]\z/', $preg) ? '\z' : '\b'),
        $allowlist)
    ) . ')/',
    include __DIR__ . '/data/module_id_prefixes.php'
);

$extracted = [];

foreach ($functions as $name => $function) {
    if (str_contains($name, '::')) {
        continue;
    }

    $module = get_module($function, $module_id_patterns);

    if ($module === null) {
        fwrite(STDERR, "{$name}: {$function['id']}\n");
    } else {
        $extracted[$module][$name] = $function;
    }
}

ksort($extracted);
array_walk($extracted, function (&$functions) {
    ksort($functions);
});

call_user_func($subcommands[$subcommand], $extracted);

/**
 * @param array{id: non-empty-string} $function
 * @param array<non-empty-string, non-empty-string> $function
 */
function get_module(array $function, array $patterns): ?string
{
    foreach ($patterns as $module => $pattern) {
        if (preg_match($pattern, $function['id'])) {
            return $module;
        }
    }

    return null;
}