Blog Posts

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.

About the author

Comments [8]

sleek, 01.02.2005 05:37 CEST

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!

Christoph Frick, 16.03.2005 17:25 CEST

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)))
{

Kilian Spohr, 24.05.2005 22:24 CEST

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

jkelly, 06.10.2005 15:43 CEST

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.

robet, 09.03.2006 22:48 CEST

Thank you very much, it works great!.

Paul Holt, 10.05.2006 16:03 CEST

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

munkyboy, 06.12.2006 21:35 CEST

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

dAniel hAhler, 12.09.2007 01:04 CEST

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

Happy hacking!

Add a comment

Your email adress will never be published. Comment spam will be deleted!