~ubuntu-branches/ubuntu/vivid/php-parser/vivid

« back to all changes in this revision

Viewing changes to .pc/0001-Workaround-removed-bootstrap.php.patch/bin/php-parse.php

  • Committer: Package Import Robot
  • Author(s): David Prévot, nikic, David Prévot
  • Date: 2014-10-16 22:04:02 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20141016220402-kbqssv91y2t8ouk3
Tags: 1.0.1-1
[ nikic ]
* Disallow new without a class name
* Fix var_dump truncation with xdebug in php-parse.php
* Add ability to pass code directly to php-parse.php
* Release version 1.0.1

[ David Prévot ]
* Improve manual page generation

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
require __DIR__ . '/../lib/bootstrap.php';
4
 
 
5
 
ini_set('xdebug.max_nesting_level', 2000);
6
 
 
7
 
/* The fancy var_dump function provided by XDebug will cut off the output way too
8
 
 * early to be of use. */
9
 
ini_set('xdebug.overload_var_dump', 0);
10
 
 
11
 
list($operations, $files) = parseArgs($argv);
12
 
 
13
 
/* Dump nodes by default */
14
 
if (empty($operations)) {
15
 
    $operations[] = 'dump';
16
 
}
17
 
 
18
 
if (empty($files)) {
19
 
    showHelp("Must specify at least one file.");
20
 
}
21
 
 
22
 
$parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative);
23
 
$dumper = new PhpParser\NodeDumper;
24
 
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
25
 
$serializer = new PhpParser\Serializer\XML;
26
 
 
27
 
$traverser = new PhpParser\NodeTraverser();
28
 
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
29
 
 
30
 
foreach ($files as $file) {
31
 
    if (!file_exists($file)) {
32
 
        die("File $file does not exist.\n");
33
 
    }
34
 
 
35
 
    echo "====> File $file:\n";
36
 
 
37
 
    $code = file_get_contents($file);
38
 
    try {
39
 
        $stmts = $parser->parse($code);
40
 
    } catch (PhpParser\Error $e) {
41
 
        die("==> Parse Error: {$e->getMessage()}\n");
42
 
    }
43
 
 
44
 
    foreach ($operations as $operation) {
45
 
        if ('dump' === $operation) {
46
 
            echo "==> Node dump:\n";
47
 
            echo $dumper->dump($stmts), "\n";
48
 
        } elseif ('pretty-print' === $operation) {
49
 
            echo "==> Pretty print:\n";
50
 
            echo $prettyPrinter->prettyPrintFile($stmts), "\n";
51
 
        } elseif ('serialize-xml' === $operation) {
52
 
            echo "==> Serialized XML:\n";
53
 
            echo $serializer->serialize($stmts), "\n";
54
 
        } elseif ('var-dump' === $operation) {
55
 
            echo "==> var_dump():\n";
56
 
            var_dump($stmts);
57
 
        } elseif ('resolve-names' === $operation) {
58
 
            echo "==> Resolved names.\n";
59
 
            $stmts = $traverser->traverse($stmts);
60
 
        }
61
 
    }
62
 
}
63
 
 
64
 
function showHelp($error) {
65
 
    die($error . "\n\n" .
66
 
        <<<OUTPUT
67
 
Usage:
68
 
 
69
 
    php php-parse.php [operations] file1.php [file2.php ...]
70
 
 
71
 
Operations is a list of the following options (--dump by default):
72
 
 
73
 
    --dump           -d  Dump nodes using NodeDumper
74
 
    --pretty-print   -p  Pretty print file using PrettyPrinter\Standard
75
 
    --serialize-xml      Serialize nodes using Serializer\XML
76
 
    --var-dump           var_dump() nodes (for exact structure)
77
 
    --resolve-names  -N  Resolve names using NodeVisitor\NameResolver
78
 
 
79
 
Example:
80
 
 
81
 
    php php-parse.php -d -p -N -d file.php
82
 
 
83
 
    Dumps nodes, pretty prints them, then resolves names and dumps them again.
84
 
OUTPUT
85
 
    );
86
 
}
87
 
 
88
 
function parseArgs($args) {
89
 
    $operations = array();
90
 
    $files = array();
91
 
 
92
 
    array_shift($args);
93
 
    $parseOptions = true;
94
 
    foreach ($args as $arg) {
95
 
        if (!$parseOptions) {
96
 
            $files[] = $arg;
97
 
            continue;
98
 
        }
99
 
 
100
 
        switch ($arg) {
101
 
            case '--dump':
102
 
            case '-d':
103
 
                $operations[] = 'dump';
104
 
                break;
105
 
            case '--pretty-print':
106
 
            case '-p':
107
 
                $operations[] = 'pretty-print';
108
 
                break;
109
 
            case '--serialize-xml':
110
 
                $operations[] = 'serialize-xml';
111
 
                break;
112
 
            case '--var-dump':
113
 
                $operations[] = 'var-dump';
114
 
                break;
115
 
            case '--resolve-names':
116
 
            case '-N';
117
 
                $operations[] = 'resolve-names';
118
 
                break;
119
 
            case '--':
120
 
                $parseOptions = false;
121
 
                break;
122
 
            default:
123
 
                if ($arg[0] === '-') {
124
 
                    showHelp("Invalid operation $arg.");
125
 
                } else {
126
 
                    $files[] = $arg;
127
 
                }
128
 
        }
129
 
    }
130
 
 
131
 
    return array($operations, $files);
132
 
}
 
 
b'\\ No newline at end of file'