Parsing Hack code into Abstract Syntax Tree

537 views Asked by At

I would like my Hack code to be broken down to Abstract Syntax Tree. Please advise me on available tools/libraries/github repositories which will help me to parse Hack code into AST. I have found "h2tp" (hack to php transpiler written by Facebook), however it doesn't parse the code into AST. I also tried this project which does what I want however it is not recognising many of the operators and requires a significant rework, which will quite a lot of time to do.

2

There are 2 answers

0
Janos Pasztor On

The HHVM itself provides a lot of tools to dump the structure of a PHP file. However, the AST dump was removed: https://github.com/facebook/hhvm/issues/1268

What you can do is dump the HHVM assembly language: http://hhvm.com/blog/6323/the-journey-of-a-thousand-bytecodes

HHVM also has a PHP transpiler which may help:https://docs.hhvm.com/hack/tools/transpiler

You could also try to port this extension over to the HHVM: https://github.com/nikic/php-ast

2
fjsnogueira On

hhast (HHAST: Low-Level AST Library) lets you do that, you may invoke hh_parse like this : hh_parse --full-fidelity-json $file | jq

taken from (https://github.com/hhvm/hhast/blob/master/docs/ast-lib.md)