ctags for PHP 5

While developing a new application for PHP 5, it was quite annoying that ctags couldn’t parse the new function definitions (if you use for example public/private/protected). Therefore I took my rusty c-knowledge and patched the php parser within ctags and all runs well now. Here’s the patch and I will soon submit it to the ctags people and hopefully it will get included in the next release. The patch is against ctags 5.5.4, but it should work with older releases as well.

KICKASS! Thanks a ton! You have no idea how long I’ve been searching for something like this. It works perfectly. I even got it working great with Vim and TagList.

Thanks again!

The patch does not handle the final keyword; here is a more generic one:

diff -cr ctags-5.5.4.orig/php.c ctags-5.5.4/php.c
*** ctags-5.5.4.orig/php.c Wed Feb 18 06:30:11 2004
— ctags-5.5.4/php.c Wed Mar 16 15:19:20 2005
***************
*** 38,43 ****
— 38,53 —-
{ TRUE, ‘v’, “variable”, “variables” }
};

+ static const unsigned char *keywords[] = {
+ “abstract”,
+ “public”,
+ “private”,
+ “protected”,
+ “static”,
+ “final”,
+ NULL
+ };
+
/*
* FUNCTION DEFINITIONS
*/
***************
*** 57,65 ****
— 67,90 —-
return (boolean)(isVarChar1 (c) || isdigit (c));
}

+ static boolean isKeyword(const unsigned char *cp) {
+ int i=0;
+ size_t sl;
+ unsigned const char *kw;
+
+ while (kw = keywords[i++]) {
+ sl = strlen(kw);
+ if (strncmp(cp, kw, sl)==0 && isspace(cp[sl])) {
+ return sl;
+ }
+ }
+ return 0;
+ }
+
static void findPhpTags (void)
{
vString *name = vStringNew ();
+ int cpa;
const unsigned char *line;

while ((line = fileReadLine ()) != NULL)
***************
*** 68,73 ****
— 93,105 —-

while (isspace (*cp))
cp++;
+
+ while (cpa = isKeyword(cp)) {
+ cp += cpa;
+ while (isspace (*cp))
+ cp++;
+ }
+

if (*(const char*)cp == ‘$’ && isVarChar1 (*(const char*)(cp+1)))
{

Hello

I just wanted to ask, if there is perhaps someone who would be so kind to offer a compiled ctags 554 for Windows with this patch ?

Thank you.

Greets

Kilian Spohr

A year and a half later, still with nothing but v5.5.4 around, this patch is a breath of fresh air for somebody like me who’s getting a bit late into the PHP 5 circuit. Thanks so much for posting your patch.

Thank you very much, it works great!.

I modified the Christoph Frick patch to remove warnings, and formatted it as a patch file at
http://templestowe.org/ctags-5.5.4-php5.patch

thank you so much! I can’t tell you how much frustration I’ve had with ctags because of this.

JFI: PHP support should be much better with ctags 5.7 (just released).

Happy hacking!