-
install AstKit extension (requires PHP 7.0+)
$ pecl install astkit
-
enable AstKit for PHP
# in php.ini extension=astkit.so
-
run the script on a file
$ php ast_explore.php /path/to/file.php
Created
October 11, 2017 20:55
-
-
Save chingor13/84b7c198f5e25211f4a3ce8c56ffd15a to your computer and use it in GitHub Desktop.
Use AstKit extension to look at the AST from a given PHP file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
if ($argc < 2) { | |
echo "Usage: php {$argv[0]} <file>" . PHP_EOL; | |
exit(1); | |
} | |
function debug($msg, $depth) { | |
echo str_repeat(" ", $depth*2) . $msg . PHP_EOL; | |
} | |
function browseAst($ast, $depth = 0) { | |
debug(sprintf( | |
"%s (%d), line %d", | |
AstKit::kindName($ast->getId()), | |
$ast->getId(), | |
$ast->getLine() | |
), $depth); | |
for ($i = 0; $i < $ast->numChildren(); $i++) { | |
// stackdriver_debugger(); | |
$child = $ast->getChild($i); | |
if (is_string($child)) { | |
debug("command: $child", $depth + 1); | |
} elseif (is_int($child)) { | |
debug("int: $child", $depth + 1); | |
} elseif (is_float($child)) { | |
debug("float: $child", $depth + 1); | |
} elseif(is_null($child)) { | |
debug("child #{$i} is null!", $depth + 1); | |
} else { | |
browseAst($child, $depth + 1); | |
} | |
} | |
} | |
$ast = AstKit::parseFile($argv[1]); | |
browseAst($ast); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment