Skip to content

Commit

Permalink
. Added support for using static HEREDOCs to initialize static variab…
Browse files Browse the repository at this point in the history
…les and class members or constants. (Matt)

. Improved syntax highlighting and consistency for variables in double-quoted strings and literal text in HEREDOCs and backticks. (Matt)
. Optimized interpolated strings to use one less opcode. (Matt)
  • Loading branch information
dstogov committed Jul 26, 2008
1 parent 8b14236 commit 478acfd
Show file tree
Hide file tree
Showing 17 changed files with 3,616 additions and 4,050 deletions.
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ PHP NEWS
. Added "jump label" operator (limited "goto"). (Dmitry, Sara)
. Added NOWDOC syntax. (Gwynne Raskind, Stas, Dmitry)
. Added HEREDOC syntax with double quotes. (Lars Strojny, Felipe)
. Added support for using static HEREDOCs to initialize static variables and
class members or constants. (Matt)
. Improved syntax highlighting and consistency for variables in double-quoted
strings and literal text in HEREDOCs and backticks. (Matt)
. Added "?:" operator. (Marcus)
. Added support for namespaces. (Dmitry, Stas, Gregory)
. Added support for Late Static Binding. (Dmitry, Etienne Kneuss)
Expand Down Expand Up @@ -69,6 +73,7 @@ PHP NEWS
second usage. (Dmitry)
. Optimized ZEND_FETCH_CLASS + ZEND_ADD_INTERFACE into single
ZEND_ADD_INTERFACE opcode (Dmitry)
. Optimized interpolated strings to use one less opcode. (Matt)

- Improved php.ini handling: (Jani)
. Added ".htaccess" style user-defined php.ini files support for CGI/FastCGI
Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/bug35655.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ highlight_string($code);
?>
--EXPECT--
<code><span style="color: #000000">
<br /><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$x&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOT<br /></span><span style="color: #0000BB">some&nbsp;string&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #007700">EOT<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$y&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
<br /><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$x&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOT<br /></span><span style="color: #DD0000">some&nbsp;string&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #007700">EOT<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$y&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code>
12 changes: 6 additions & 6 deletions Zend/tests/heredoc_011.phpt
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
--TEST--
Heredocs can NOT be used as static scalars.
STATIC heredocs CAN be used as static scalars.
--FILE--
<?php

require_once 'nowdoc.inc';

class e {

const E = <<<THISMUSTERROR
If you see this, something's wrong.
THISMUSTERROR;
const E = <<<THISMUSTNOTERROR
If you DON'T see this, something's wrong.
THISMUSTNOTERROR;

};

print e::E . "\n";

?>
--EXPECTF--
Parse error: syntax error, unexpected T_START_HEREDOC in %sheredoc_011.php on line 8
--EXPECT--
If you DON'T see this, something's wrong.
2 changes: 1 addition & 1 deletion Zend/tests/nowdoc_013.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ highlight_string($code);
?>
--EXPECT--
<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$x&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;'EOT'<br /></span><span style="color: #0000BB">some&nbsp;string&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #007700">EOT<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$y&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$x&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;'EOT'<br /></span><span style="color: #DD0000">some&nbsp;string&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #007700">EOT<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$y&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code>
4 changes: 2 additions & 2 deletions Zend/tests/nowdoc_014.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Highliting empty nowdoc
Highlighting empty nowdoc
--INI--
highlight.string = #DD0000
highlight.comment = #FF8000
Expand All @@ -20,6 +20,6 @@ highlight_string($code);
?>
--EXPECT--
<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$x&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;'EOT'<br /></span><span style="color: #0000BB"></span><span style="color: #007700">EOT<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$y&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$x&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;'EOT'<br />EOT<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$y&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code>
60 changes: 18 additions & 42 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,19 +1022,6 @@ void zend_do_end_variable_parse(znode *variable, int type, int arg_offset TSRMLS
}


void zend_do_init_string(znode *result TSRMLS_DC)
{
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);

opline->opcode = ZEND_INIT_STRING;
opline->result.op_type = IS_TMP_VAR;
opline->result.u.var = get_temporary_variable(CG(active_op_array));
*result = opline->result;
SET_UNUSED(opline->op1);
SET_UNUSED(opline->op2);
}


void zend_do_add_string(znode *result, znode *op1, znode *op2 TSRMLS_DC)
{
zend_op *opline;
Expand All @@ -1054,45 +1041,34 @@ void zend_do_add_string(znode *result, znode *op1, znode *op2 TSRMLS_DC)
efree(Z_STRVAL(op2->u.constant));
return;
}
opline->op1 = *op1;

if (op1) {
opline->op1 = *op1;
opline->result = *op1;
} else {
SET_UNUSED(opline->op1);
opline->result.op_type = IS_TMP_VAR;
opline->result.u.var = get_temporary_variable(CG(active_op_array));
}
opline->op2 = *op2;
opline->op2.op_type = IS_CONST;
opline->result = opline->op1;
*result = opline->result;
}


void zend_do_add_variable(znode *result, znode *op1, znode *op2 TSRMLS_DC)
{
zend_op *opline;
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);

if (op1->op_type == IS_CONST) {
opline = get_next_op(CG(active_op_array) TSRMLS_CC);
opline->opcode = ZEND_INIT_STRING;
opline->result.op_type = IS_TMP_VAR;
opline->result.u.var = get_temporary_variable(CG(active_op_array));
*result = opline->result;
SET_UNUSED(opline->op1);
SET_UNUSED(opline->op2);
opline->opcode = ZEND_ADD_VAR;

if (Z_STRLEN(op1->u.constant)>0) {
opline = get_next_op(CG(active_op_array) TSRMLS_CC);
opline->opcode = ZEND_ADD_STRING;
opline->result = *result;
opline->op1 = *result;
opline->op2 = *op1;
opline->result = opline->op1;
} else {
zval_dtor(&op1->u.constant);
}
if (op1) {
opline->op1 = *op1;
opline->result = *op1;
} else {
*result = *op1;
SET_UNUSED(opline->op1);
opline->result.op_type = IS_TMP_VAR;
opline->result.u.var = get_temporary_variable(CG(active_op_array));
}

opline = get_next_op(CG(active_op_array) TSRMLS_CC);
opline->opcode = ZEND_ADD_VAR;
opline->result = *result;
opline->op1 = *result;
opline->op2 = *op2;
*result = opline->result;
}
Expand Down Expand Up @@ -3949,6 +3925,7 @@ void zend_do_shell_exec(znode *result, znode *cmd TSRMLS_DC)
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);

switch (cmd->op_type) {
case IS_CONST:
case IS_TMP_VAR:
opline->opcode = ZEND_SEND_VAL;
break;
Expand Down Expand Up @@ -4925,7 +4902,6 @@ int zendlex(znode *zendlval TSRMLS_DC)
retval = T_ECHO;
break;
case T_END_HEREDOC:
case T_END_NOWDOC:
efree(Z_STRVAL(zendlval->u.constant));
break;
}
Expand Down
1 change: 0 additions & 1 deletion Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ void zend_check_writable_variable(znode *variable);

void zend_do_free(znode *op1 TSRMLS_DC);

void zend_do_init_string(znode *result TSRMLS_DC);
void zend_do_add_string(znode *result, znode *op1, znode *op2 TSRMLS_DC);
void zend_do_add_variable(znode *result, znode *op1, znode *op2 TSRMLS_DC);

Expand Down
22 changes: 5 additions & 17 deletions Zend/zend_highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini
int token_type;
char *last_color = syntax_highlighter_ini->highlight_html;
char *next_color;
int in_string=0;

zend_printf("<code>");
zend_printf("<span style=\"color: %s\">\n", last_color);
Expand All @@ -115,22 +114,18 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini
case T_CLOSE_TAG:
next_color = syntax_highlighter_ini->highlight_default;
break;
case '"':
case T_ENCAPSED_AND_WHITESPACE:
case T_CONSTANT_ENCAPSED_STRING:
next_color = syntax_highlighter_ini->highlight_string;
break;
case '"':
next_color = syntax_highlighter_ini->highlight_string;
in_string = !in_string;
break;
case T_WHITESPACE:
zend_html_puts(LANG_SCNG(yy_text), LANG_SCNG(yy_leng) TSRMLS_CC); /* no color needed */
token.type = 0;
continue;
break;
default:
if (in_string) {
next_color = syntax_highlighter_ini->highlight_string;
} else if (token.type == 0) {
if (token.type == 0) {
next_color = syntax_highlighter_ini->highlight_keyword;
} else {
next_color = syntax_highlighter_ini->highlight_default;
Expand All @@ -149,7 +144,6 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini
}
switch (token_type) {
case T_END_HEREDOC:
case T_END_NOWDOC:
zend_html_puts(token.value.str.val, token.value.str.len TSRMLS_CC);
break;
default:
Expand All @@ -159,8 +153,6 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini

if (token.type == IS_STRING) {
switch (token_type) {
case EOF:
goto done;
case T_OPEN_TAG:
case T_OPEN_TAG_WITH_ECHO:
case T_CLOSE_TAG:
Expand All @@ -172,7 +164,7 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini
efree(token.value.str.val);
break;
}
} else if (token_type == T_END_HEREDOC || token_type == T_END_NOWDOC) {
} else if (token_type == T_END_HEREDOC) {
efree(token.value.str.val);
}
token.type = 0;
Expand All @@ -190,7 +182,7 @@ ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini
}
zend_html_puts(LANG_SCNG(yy_text), (LANG_SCNG(yy_limit) - LANG_SCNG(yy_text)) TSRMLS_CC);
}
done:

if (last_color != syntax_highlighter_ini->highlight_html) {
zend_printf("</span>\n");
}
Expand All @@ -217,12 +209,8 @@ ZEND_API void zend_strip(TSRMLS_D)
case T_DOC_COMMENT:
token.type = 0;
continue;

case EOF:
return;

case T_END_HEREDOC:
case T_END_NOWDOC:
zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
efree(token.value.str.val);
/* read the following character, either newline or ; */
Expand Down
19 changes: 12 additions & 7 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/*
* LALR shift/reduce conflicts and how they are resolved:
*
* - 2 shift/reduce conflicts due to the dangeling elseif/else ambiguity. Solved by shift.
* - 2 shift/reduce conflicts due to the dangling elseif/else ambiguity. Solved by shift.
*
*/

Expand Down Expand Up @@ -147,8 +147,6 @@
%token T_NAMESPACE
%token T_NS_C
%token T_DIR
%token T_START_NOWDOC
%token T_END_NOWDOC

%% /* Rules */

Expand Down Expand Up @@ -641,7 +639,7 @@ expr_without_variable:
| '@' { zend_do_begin_silence(&$1 TSRMLS_CC); } expr { zend_do_end_silence(&$1 TSRMLS_CC); $$ = $3; }
| scalar { $$ = $1; }
| T_ARRAY '(' array_pair_list ')' { $$ = $3; }
| '`' encaps_list '`' { zend_do_shell_exec(&$$, &$2 TSRMLS_CC); }
| '`' backticks_expr '`' { zend_do_shell_exec(&$$, &$2 TSRMLS_CC); }
| T_PRINT expr { zend_do_print(&$$, &$2 TSRMLS_CC); }
| function is_reference '(' { zend_do_begin_lambda_function_declaration(&$$, &$1, $2.op_type, 0 TSRMLS_CC); }
parameter_list ')' lexical_vars '{' inner_statement_list '}' { zend_do_end_function_declaration(&$1 TSRMLS_CC); $$ = $4; }
Expand Down Expand Up @@ -736,6 +734,12 @@ exit_expr:
| '(' expr ')' { $$ = $2; }
;

backticks_expr:
/* empty */ { ZVAL_EMPTY_STRING(&$$.u.constant); INIT_PZVAL(&$$.u.constant); $$.op_type = IS_CONST; }
| T_ENCAPSED_AND_WHITESPACE { $$ = $1; }
| encaps_list { $$ = $1; }
;


ctor_arguments:
/* empty */ { Z_LVAL($$.u.constant)=0; }
Expand All @@ -754,7 +758,8 @@ common_scalar:
| T_METHOD_C { $$ = $1; }
| T_FUNC_C { $$ = $1; }
| T_NS_C { $$ = $1; }
| T_START_NOWDOC T_ENCAPSED_AND_WHITESPACE T_END_NOWDOC { $$ = $2; }
| T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC { $$ = $2; }
| T_START_HEREDOC T_END_HEREDOC { ZVAL_EMPTY_STRING(&$$.u.constant); INIT_PZVAL(&$$.u.constant); $$.op_type = IS_CONST; }
;


Expand Down Expand Up @@ -944,8 +949,8 @@ non_empty_array_pair_list:
encaps_list:
encaps_list encaps_var { zend_do_end_variable_parse(&$2, BP_VAR_R, 0 TSRMLS_CC); zend_do_add_variable(&$$, &$1, &$2 TSRMLS_CC); }
| encaps_list T_ENCAPSED_AND_WHITESPACE { zend_do_add_string(&$$, &$1, &$2 TSRMLS_CC); }
| /* empty */ { zend_do_init_string(&$$ TSRMLS_CC); }

| encaps_var { zend_do_end_variable_parse(&$1, BP_VAR_R, 0 TSRMLS_CC); zend_do_add_variable(&$$, NULL, &$1 TSRMLS_CC); }
| T_ENCAPSED_AND_WHITESPACE encaps_var { zend_do_add_string(&$$, NULL, &$1 TSRMLS_CC); zend_do_end_variable_parse(&$2, BP_VAR_R, 0 TSRMLS_CC); zend_do_add_variable(&$$, &$$, &$2 TSRMLS_CC); }
;


Expand Down
Loading

0 comments on commit 478acfd

Please sign in to comment.