Skip to content

Commit

Permalink
PHP: Fix parsing of anonymous functions using the "use" keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
b4n committed Aug 8, 2013
1 parent fb7bd34 commit cc9e56e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
30 changes: 28 additions & 2 deletions tagmanager/ctags/php.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/ctags/anonymous_functions.php
Expand Up @@ -22,3 +22,10 @@
global $_g;
return $_g;
};

$f = function&() use (&$_g) {

function f_sub() {}

return $_g;
};
2 changes: 2 additions & 0 deletions tests/ctags/anonymous_functions.php.tags
Expand Up @@ -5,3 +5,5 @@ b
c�16�()�0
d�16�()�0
e�16�()�0
f�16�()�0
f_sub�16�()�f�0

0 comments on commit cc9e56e

Please sign in to comment.