Generate AST of a PHP source file

前端 未结 5 1988
梦毁少年i
梦毁少年i 2021-01-01 17:26

I want to parse a PHP source file, into an AST (preferably as a nested array of instructions).

I basically want to convert things like

f($a, $b + 1)
         


        
5条回答
  •  清酒与你
    2021-01-01 17:57

    HipHop

    You can use Facebook's HHVM to dump the AST.

    apt-get install hhvm
    
    # parse specified file and dump the AST
    hhvm --parse arg  
    

    This worked for HipHop (the old PHP to C++ compiler) - back in the days of 2013!


    HHVM

    Update 2015

    --parse is not supported.

    You will get an error: HHVM The 'parse' command line option is not supported.

    See https://github.com/facebook/hhvm/blob/c494c3a145008f65d349611eb2d09d0c33f1ab23/hphp/runtime/base/program_functions.cpp#L1111

    Feature Request to support the CLI option again: https://github.com/facebook/hhvm/issues/4615


    PHP 7

    PHP 7 will have an AST, see the related RFC.

    There are two extensions, which provide access and expose the AST generated by PHP 7:

    • https://github.com/nikic/php-ast
    • https://github.com/sgolemon/astkit

提交回复
热议问题