Update for last 2 commits.
[geany-mirror.git] / tagmanager / php.c
blob4f031a176a86f8f25974aa484ca0c07d65179ce5
1 /*
2 * $Id$
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
11 * variables.
13 * Parsing PHP defines by Pavel Hlousek <pavel.hlousek@seznam.cz>, Apr 2003.
17 * INCLUDE FILES
19 #include "general.h" /* must always come first */
21 #include <string.h>
22 #include "main.h"
23 #include "parse.h"
24 #include "read.h"
25 #include "vstring.h"
28 * DATA DEFINITIONS
30 typedef enum {
31 K_CLASS, K_DEFINE, K_FUNCTION, K_VARIABLE
32 } phpKind;
34 #if 0
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" }
41 #endif
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 #ifdef __CYGWIN__
59 #define ALPHA "[:alpha:]"
60 #define ALNUM "[:alnum:]"
61 #else
62 #define ALPHA "A-Za-z\x7f-\xff"
63 #define ALNUM "0-9A-Za-z\x7f-\xff"
64 #endif
66 /* "A-Za-z\x7f-\xff" fails on other locales than "C" and so skip it */
67 #define ALPHA "[:alpha:]"
68 #define ALNUM "[:alnum:]"
70 static void function_cb(const char *line, const regexMatch *matches, unsigned int count);
72 static void installPHPRegex (const langType language)
74 addTagRegex(language, "^[ \t]*((final|abstract)[ \t]+)*class[ \t]+([" ALPHA "_][" ALNUM "_]*)",
75 "\\3", "c,class,classes", NULL);
76 addTagRegex(language, "^[ \t]*interface[ \t]+([" ALPHA "_][" ALNUM "_]*)",
77 "\\1", "i,interface,interfaces", NULL);
78 addTagRegex(language, "^[ \t]*define[ \t]*\\([ \t]*['\"]?([" ALPHA "_][" ALNUM "_]*)",
79 "\\1", "m,macro,macros", NULL);
80 addTagRegex(language, "^[ \t]*const[ \t]*([" ALPHA "_][" ALNUM "_]*)[ \t]*[=;]",
81 "\\1", "m,macro,macros", NULL);
82 addCallbackRegex(language,
83 "^[ \t]*[(public|protected|private|static)[ \t]*]*[ \t]*function[ \t]+&?[ \t]*([" ALPHA "_][" ALNUM "_]*)[[:space:]]*(\\(.*\\))",
84 NULL, function_cb);
85 addTagRegex(language, "^[ \t]*(\\$|::\\$|\\$this->)([" ALPHA "_][" ALNUM "_]*)[ \t]*=",
86 "\\2", "v,variable,variables", NULL);
87 addTagRegex(language, "^[ \t]*((var|public|protected|private|static)[ \t]+)+\\$([" ALPHA "_][" ALNUM "_]*)[ \t]*[=;]",
88 "\\3", "v,variable,variables", NULL);
90 /* function regex is covered by PHP regex */
91 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_]+)[ \t]*[=:][ \t]*function[ \t]*\\(",
92 "\\2", "j,jsfunction,javascript functions", NULL);
93 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_.]+)\\.([A-Za-z0-9_]+)[ \t]*=[ \t]*function[ \t]*\\(",
94 "\\2.\\3", "j,jsfunction,javascript functions", NULL);
95 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_.]+)\\.([A-Za-z0-9_]+)[ \t]*=[ \t]*function[ \t]*\\(",
96 "\\3", "j,jsfunction,javascript functions", NULL);
100 static void function_cb(const char *line, const regexMatch *matches, unsigned int count)
102 char *name, *arglist;
103 char kind = 'f';
104 static char *kindName = "function";
105 tagEntryInfo e;
106 const regexMatch *match_funcname = NULL;
107 const regexMatch *match_arglist = NULL;
109 if (count > 2)
111 match_funcname = &matches[count - 2];
112 match_arglist = &matches[count - 1];
115 if (match_funcname != NULL)
117 name = xMalloc(match_funcname->length + 1, char);
118 strncpy(name, line + match_funcname->start, match_funcname->length);
119 *(name+match_funcname->length) = '\x0';
120 arglist = xMalloc(match_arglist->length + 1, char);
121 strncpy(arglist, line + match_arglist->start, match_arglist->length);
122 *(arglist+match_arglist->length) = '\x0';
124 initTagEntry (&e, name);
125 e.kind = kind;
126 e.kindName = kindName;
127 e.extensionFields.arglist = arglist;
128 makeTagEntry (&e);
132 /* Create parser definition structure */
133 extern parserDefinition* PhpParser (void)
135 static const char *const extensions [] = { "php", "php3", "phtml", NULL };
136 parserDefinition* def = parserNew ("PHP");
137 def->extensions = extensions;
138 def->initialize = installPHPRegex;
139 def->regex = TRUE;
140 return def;
143 #if 0
145 static boolean isLetter(const int c)
147 return (boolean)(isalpha(c) || (c >= 127 && c <= 255));
150 static boolean isVarChar1(const int c)
152 return (boolean)(isLetter (c) || c == '_');
155 static boolean isVarChar(const int c)
157 return (boolean)(isVarChar1 (c) || isdigit (c));
160 static void findPhpTags (void)
162 vString *name = vStringNew ();
163 const unsigned char *line;
165 while ((line = fileReadLine ()) != NULL)
167 const unsigned char *cp = line;
168 const char* f;
170 while (isspace (*cp))
171 cp++;
173 if (*(const char*)cp == '$' && isVarChar1 (*(const char*)(cp+1)))
175 cp += 1;
176 vStringClear (name);
177 while (isVarChar ((int) *cp))
179 vStringPut (name, (int) *cp);
180 ++cp;
182 while (isspace ((int) *cp))
183 ++cp;
184 if (*(const char*) cp == '=')
186 vStringTerminate (name);
187 makeSimpleTag (name, PhpKinds, K_VARIABLE);
188 vStringClear (name);
191 else if ((f = strstr ((const char*) cp, "function")) != NULL &&
192 (f == (const char*) cp || isspace ((int) f [-1])) &&
193 isspace ((int) f [8]))
195 cp = ((const unsigned char *) f) + 8;
197 while (isspace ((int) *cp))
198 ++cp;
200 if (*cp == '&') /* skip reference character and following whitespace */
202 cp++;
204 while (isspace ((int) *cp))
205 ++cp;
208 vStringClear (name);
209 while (isalnum ((int) *cp) || *cp == '_')
211 vStringPut (name, (int) *cp);
212 ++cp;
214 vStringTerminate (name);
215 makeSimpleTag (name, PhpKinds, K_FUNCTION);
216 vStringClear (name);
218 else if (strncmp ((const char*) cp, "class", (size_t) 5) == 0 &&
219 isspace ((int) cp [5]))
221 cp += 5;
223 while (isspace ((int) *cp))
224 ++cp;
225 vStringClear (name);
226 while (isalnum ((int) *cp) || *cp == '_')
228 vStringPut (name, (int) *cp);
229 ++cp;
231 vStringTerminate (name);
232 makeSimpleTag (name, PhpKinds, K_CLASS);
233 vStringClear (name);
235 else if (strncmp ((const char*) cp, "define", (size_t) 6) == 0 &&
236 ! isalnum ((int) cp [6]))
238 cp += 6;
240 while (isspace ((int) *cp))
241 ++cp;
242 if (*cp != '(')
243 continue;
244 ++cp;
246 while (isspace ((int) *cp))
247 ++cp;
248 if ((*cp == '\'') || (*cp == '"'))
249 ++cp;
250 else if (! ((*cp == '_') || isalnum ((int) *cp)))
251 continue;
253 vStringClear (name);
254 while (isalnum ((int) *cp) || *cp == '_')
256 vStringPut (name, (int) *cp);
257 ++cp;
259 vStringTerminate (name);
260 makeSimpleTag (name, PhpKinds, K_DEFINE);
261 vStringClear (name);
264 vStringDelete (name);
267 extern parserDefinition* PhpParser (void)
269 static const char *const extensions [] = { "php", "php3", "phtml", NULL };
270 parserDefinition* def = parserNew ("PHP");
271 def->kinds = PhpKinds;
272 def->kindCount = KIND_COUNT (PhpKinds);
273 def->extensions = extensions;
274 def->parser = findPhpTags;
275 return def;
278 #endif
280 /* vi:set tabstop=4 shiftwidth=4: */