Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace the $ in php variable declaration

I would like to extend the php syntax, in order to tell apart mutable and immutable variables.

$a should be declared mutable (as in standard php) and #b should be declared immutable.

I've read Hacking PHP syntax,

and I couldn't figure out where can I define that variables declared with # should also be tokenized as T_VARIABLE ?

like image 286
Uri Goren Avatar asked Jun 01 '15 10:06

Uri Goren


1 Answers

I was able to solve this issue, Two steps need to be taken:

Make PHP not parse '#' as comments:

Change:

<ST_IN_SCRIPTING>"#"|"//" {

To

<ST_IN_SCRIPTING>"//" {

In line 1901 in zend_language_scanner.l

Tokenize #a as a variable:

Change:

simple_variable:
T_VARIABLE { $$ = $1; }
| '$' '{' expr '}' { $$ = $3; }
| '$' simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
;

To:

simple_variable:
T_VARIABLE { $$ = $1; }
| '$' '{' expr '}' { $$ = $3; }
| '$' simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
| '#' '{' expr '}' { $$ = $3; }
| '#' simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
;

In line 1117 of zend_language_parser.y

That's it

Now this code works:

#a=1;
echo (#a);//1
like image 108
Uri Goren Avatar answered Oct 16 '22 08:10

Uri Goren