Multiple use of ast node in parser

php.internals

Christian Schneider

7 years ago
I'm playing around with some syntactic sugar for PHP and when trying to implement :$foo as equivalent for 'foo' => $foo I ran into the problem of having only having one ast node but trying to use it twice. My first attempt was | ':' T_VARIABLE { $$ = zend_ast_create(ZEND_AST_ARRAY_ELEM, zend_ast_create(ZEND_AST_VAR, $2), $2); } but that results in corrupted memory because of the double usage. Trying something like | ':' T_VARIABLE { $$ = zend_ast_create(ZEND_AST_ARRAY_ELEM, zend_ast_create(ZEND_AST_VAR, GC_AST(zend_ast_copy($2))), $2); } works but leaks memory because the copied ast is not freed. Is there a proper way to do something like this ast transformation inside the parser? Regards, - Chris