2 * $Id: php.c 734 2009-08-20 23:33:54Z jafl $
4 * Copyright (c) 2000, Jesus Castagnetto <jmcastagnetto@zkey.com>
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
9 * This module contains functions for generating tags for the PHP web page
10 * scripting language. Only recognizes functions and classes, not methods or
13 * Parsing PHP defines by Pavel Hlousek <pavel.hlousek@seznam.cz>, Apr 2003.
19 #include "general.h" /* must always come first */
31 K_CLASS
, K_DEFINE
, K_FUNCTION
, K_VARIABLE
35 static kindOption PhpKinds
[] = {
36 { TRUE
, 'c', "class", "classes" },
37 { TRUE
, 'd', "define", "constant definitions" },
38 { TRUE
, 'f', "function", "functions" },
39 { TRUE
, 'v', "variable", "variables" }
44 * FUNCTION DEFINITIONS
47 /* JavaScript patterns are duplicated in jscript.c */
50 * Cygwin doesn't support non-ASCII characters in character classes.
51 * This isn't a good solution to the underlying problem, because we're still
52 * making assumptions about the character encoding.
53 * Really, these regular expressions need to concentrate on what marks the
54 * end of an identifier, and we need something like iconv to take into
55 * account the user's locale (or an override on the command-line.)
58 #define ALPHA "[:alpha:]"
59 #define ALNUM "[:alnum:]"
61 #define ALPHA "A-Za-z\x7f-\xff"
62 #define ALNUM "0-9A-Za-z\x7f-\xff"
65 static void installPHPRegex (const langType language
)
67 addTagRegex(language
, "^[ \t]*((final|abstract)[ \t]+)*class[ \t]+([" ALPHA
"_][" ALNUM
"_]*)",
68 "\\3", "c,class,classes", NULL
);
69 addTagRegex(language
, "^[ \t]*interface[ \t]+([" ALPHA
"_][" ALNUM
"_]*)",
70 "\\1", "i,interface,interfaces", NULL
);
71 addTagRegex(language
, "^[ \t]*define[ \t]*\\([ \t]*['\"]?([" ALPHA
"_][" ALNUM
"_]*)",
72 "\\1", "d,define,constant definitions", NULL
);
73 addTagRegex(language
, "^[ \t]*((static|public|protected|private)[ \t]+)*function[ \t]+&?[ \t]*([" ALPHA
"_][" ALNUM
"_]*)",
74 "\\3", "f,function,functions", NULL
);
75 addTagRegex(language
, "^[ \t]*(\\$|::\\$|\\$this->)([" ALPHA
"_][" ALNUM
"_]*)[ \t]*=",
76 "\\2", "v,variable,variables", NULL
);
77 addTagRegex(language
, "^[ \t]*((var|public|protected|private|static)[ \t]+)+\\$([" ALPHA
"_][" ALNUM
"_]*)[ \t]*[=;]",
78 "\\3", "v,variable,variables", NULL
);
80 /* function regex is covered by PHP regex */
81 addTagRegex (language
, "(^|[ \t])([A-Za-z0-9_]+)[ \t]*[=:][ \t]*function[ \t]*\\(",
82 "\\2", "j,jsfunction,javascript functions", NULL
);
83 addTagRegex (language
, "(^|[ \t])([A-Za-z0-9_.]+)\\.([A-Za-z0-9_]+)[ \t]*=[ \t]*function[ \t]*\\(",
84 "\\2.\\3", "j,jsfunction,javascript functions", NULL
);
85 addTagRegex (language
, "(^|[ \t])([A-Za-z0-9_.]+)\\.([A-Za-z0-9_]+)[ \t]*=[ \t]*function[ \t]*\\(",
86 "\\3", "j,jsfunction,javascript functions", NULL
);
89 /* Create parser definition structure */
90 extern parserDefinition
* PhpParser (void)
92 static const char *const extensions
[] = { "php", "php3", "phtml", NULL
};
93 parserDefinition
* def
= parserNew ("PHP");
94 def
->extensions
= extensions
;
95 def
->initialize
= installPHPRegex
;
102 static boolean
isLetter(const int c
)
104 return (boolean
)(isalpha(c
) || (c
>= 127 && c
<= 255));
107 static boolean
isVarChar1(const int c
)
109 return (boolean
)(isLetter (c
) || c
== '_');
112 static boolean
isVarChar(const int c
)
114 return (boolean
)(isVarChar1 (c
) || isdigit (c
));
117 static void findPhpTags (void)
119 vString
*name
= vStringNew ();
120 const unsigned char *line
;
122 while ((line
= fileReadLine ()) != NULL
)
124 const unsigned char *cp
= line
;
127 while (isspace (*cp
))
130 if (*(const char*)cp
== '$' && isVarChar1 (*(const char*)(cp
+1)))
134 while (isVarChar ((int) *cp
))
136 vStringPut (name
, (int) *cp
);
139 while (isspace ((int) *cp
))
141 if (*(const char*) cp
== '=')
143 vStringTerminate (name
);
144 makeSimpleTag (name
, PhpKinds
, K_VARIABLE
);
148 else if ((f
= strstr ((const char*) cp
, "function")) != NULL
&&
149 (f
== (const char*) cp
|| isspace ((int) f
[-1])) &&
150 isspace ((int) f
[8]))
152 cp
= ((const unsigned char *) f
) + 8;
154 while (isspace ((int) *cp
))
157 if (*cp
== '&') /* skip reference character and following whitespace */
161 while (isspace ((int) *cp
))
166 while (isalnum ((int) *cp
) || *cp
== '_')
168 vStringPut (name
, (int) *cp
);
171 vStringTerminate (name
);
172 makeSimpleTag (name
, PhpKinds
, K_FUNCTION
);
175 else if (strncmp ((const char*) cp
, "class", (size_t) 5) == 0 &&
176 isspace ((int) cp
[5]))
180 while (isspace ((int) *cp
))
183 while (isalnum ((int) *cp
) || *cp
== '_')
185 vStringPut (name
, (int) *cp
);
188 vStringTerminate (name
);
189 makeSimpleTag (name
, PhpKinds
, K_CLASS
);
192 else if (strncmp ((const char*) cp
, "define", (size_t) 6) == 0 &&
193 ! isalnum ((int) cp
[6]))
197 while (isspace ((int) *cp
))
203 while (isspace ((int) *cp
))
205 if ((*cp
== '\'') || (*cp
== '"'))
207 else if (! ((*cp
== '_') || isalnum ((int) *cp
)))
211 while (isalnum ((int) *cp
) || *cp
== '_')
213 vStringPut (name
, (int) *cp
);
216 vStringTerminate (name
);
217 makeSimpleTag (name
, PhpKinds
, K_DEFINE
);
221 vStringDelete (name
);
224 extern parserDefinition
* PhpParser (void)
226 static const char *const extensions
[] = { "php", "php3", "phtml", NULL
};
227 parserDefinition
* def
= parserNew ("PHP");
228 def
->kinds
= PhpKinds
;
229 def
->kindCount
= KIND_COUNT (PhpKinds
);
230 def
->extensions
= extensions
;
231 def
->parser
= findPhpTags
;
237 /* vi:set tabstop=4 shiftwidth=4: */