PHP: Fix parsing of anonymous functions using the "use" keyword

http://www.php.net/manual/en/language.namespaces.php#104136
This commit is contained in:
Colomban Wendling 2013-08-08 17:23:42 +02:00
parent fb7bd34592
commit cc9e56e9bc
3 changed files with 37 additions and 2 deletions

View File

@ -1085,7 +1085,8 @@ static boolean parseTrait (tokenInfo *const token)
*
* if @name is not NULL, parses an anonymous function with name @name
* $foo = function($foo, $bar) {}
* $foo = function&($foo, $bar) {} */
* $foo = function&($foo, $bar) {}
* $foo = function($foo, $bar) use ($x, &$y) {} */
static boolean parseFunction (tokenInfo *const token, const tokenInfo *name)
{
boolean readNext = TRUE;
@ -1176,8 +1177,33 @@ static boolean parseFunction (tokenInfo *const token, const tokenInfo *name)
makeFunctionTag (name, arglist, access, impl);
vStringDelete (arglist);
readToken (token); /* normally it's an open brace or a semicolon */
readToken (token); /* normally it's an open brace or "use" keyword */
}
/* skip use(...) */
if (token->type == TOKEN_KEYWORD && token->keyword == KEYWORD_use)
{
readToken (token);
if (token->type == TOKEN_OPEN_PAREN)
{
int depth = 1;
do
{
readToken (token);
switch (token->type)
{
case TOKEN_OPEN_PAREN: depth++; break;
case TOKEN_CLOSE_PAREN: depth--; break;
default: break;
}
}
while (token->type != TOKEN_EOF && depth > 0);
readToken (token);
}
}
if (token->type == TOKEN_OPEN_CURLY)
enterScope (token, name->string, K_FUNCTION);
else

View File

@ -22,3 +22,10 @@ $e = function&() {
global $_g;
return $_g;
};
$f = function&() use (&$_g) {
function f_sub() {}
return $_g;
};

View File

@ -5,3 +5,5 @@ b
cフ16ヘ()ヨ0
dフ16ヘ()ヨ0
eフ16ヘ()ヨ0
f<EFBFBD>16<EFBFBD>()<29>0
f_sub<EFBFBD>16<EFBFBD>()<29>f<EFBFBD>0