Patch: Nested comments

From: Date: Tue, 27 Nov 2001 10:48:51 +0000
Subject: Patch: Nested comments
Groups: php.dev 
Request: Send a blank email to php-dev+get-71774@lists.php.net to get a copy of this message
This patch allows for nested 'C-style' comments, which can be useful especially while debugging. <?php /* comments /* now /* nest */ */ /*/*/*/* */*/*/*/ */ ?> Since comments are handled purely lexical, there should be virtually no performance hit. The following two examples show how errors are handled: 1) <?php /* */ */ ?> 2) <?php /* /* */ ?> Ad 1) This will yield a zend_error(E_COMPILE_ERROR,"Invalid nesting of comments") on the last line. Unpatched, a parse error is the most likely result. Ad 2) A zend_error(E_COMPILE_WARNING, "unterminated comment starting line %d", CG(comment_start_line)) is raised. The attached patch is against latest CVS Best regards, Anders Johannsen -- aj@php.net Index: zend_globals.h =================================================================== RCS file: /repository/Zend/zend_globals.h,v retrieving revision 1.80 diff -u -r1.80 zend_globals.h --- zend_globals.h 2001/10/23 01:19:16 1.80 +++ zend_globals.h 2001/11/27 10:08:06 @@ -82,6 +82,7 @@ int comment_start_line; char *heredoc; int heredoc_len; + unsigned int comment_nest_level; zend_op_array *active_op_array; Index: zend_language_scanner.l =================================================================== RCS file: /repository/Zend/zend_language_scanner.l,v retrieving revision 1.40 diff -u -r1.40 zend_language_scanner.l --- zend_language_scanner.l 2001/09/22 00:06:27 1.40 +++ zend_language_scanner.l 2001/11/27 10:08:07 @@ -1,5 +1,4 @@ %{ - /* +----------------------------------------------------------------------+ | Zend Engine | @@ -125,6 +124,7 @@ { CG(heredoc) = NULL; CG(heredoc_len)=0; + CG(comment_nest_level)=0; } @@ -1057,24 +1057,39 @@ } } +<ST_IN_SCRIPTING>"*/" { + zend_error(E_COMPILE_ERROR,"Invalid nesting of comments"); +} + <ST_IN_SCRIPTING>"/*" { CG(comment_start_line) = CG(zend_lineno); BEGIN(ST_COMMENT); + CG(comment_nest_level) = 1; yymore(); } - -<ST_COMMENT>[^*]+ { +<ST_COMMENT>[^/*]+ { yymore(); } +<ST_COMMENT>"/*" { + CG(comment_nest_level)++; + yymore(); +} + <ST_COMMENT>"*/" { - HANDLE_NEWLINES(yytext, yyleng); - BEGIN(ST_IN_SCRIPTING); - return T_COMMENT; + CG(comment_nest_level)--; + + if (CG(comment_nest_level) == 0) { + HANDLE_NEWLINES(yytext, yyleng); + BEGIN(ST_IN_SCRIPTING); + return T_COMMENT; + } else { + yymore(); + } } -<ST_COMMENT>"*" { +<ST_COMMENT>"*"|"/" { yymore(); }

« previous php.dev (#71774) next »