Fix building with makefile.win32 from Windows command prompt, not MSYS
[geany-mirror.git] / tagmanager / php.c
blob4233d489ca8efc4be44e080b3e0d63109e186e83
1 /*
2 * Copyright (c) 2000, Jesus Castagnetto <jmcastagnetto@zkey.com>
4 * This source code is released for free distribution under the terms of the
5 * GNU General Public License.
7 * This module contains functions for generating tags for the PHP web page
8 * scripting language. Only recognizes functions and classes, not methods or
9 * variables.
11 * Parsing PHP defines by Pavel Hlousek <pavel.hlousek@seznam.cz>, Apr 2003.
15 * INCLUDE FILES
17 #include "general.h" /* must always come first */
19 #include <string.h>
20 #include "main.h"
21 #include "parse.h"
22 #include "read.h"
23 #include "vstring.h"
26 * DATA DEFINITIONS
28 typedef enum {
29 K_CLASS, K_DEFINE, K_FUNCTION, K_VARIABLE
30 } phpKind;
32 #if 0
33 static kindOption PhpKinds [] = {
34 { TRUE, 'c', "class", "classes" },
35 { TRUE, 'd', "define", "constant definitions" },
36 { TRUE, 'f', "function", "functions" },
37 { TRUE, 'v', "variable", "variables" }
39 #endif
42 * FUNCTION DEFINITIONS
45 /* JavaScript patterns are duplicated in jscript.c */
48 * Cygwin doesn't support non-ASCII characters in character classes.
49 * This isn't a good solution to the underlying problem, because we're still
50 * making assumptions about the character encoding.
51 * Really, these regular expressions need to concentrate on what marks the
52 * end of an identifier, and we need something like iconv to take into
53 * account the user's locale (or an override on the command-line.)
56 #ifdef __CYGWIN__
57 #define ALPHA "[:alpha:]"
58 #define ALNUM "[:alnum:]"
59 #else
60 #define ALPHA "A-Za-z\x7f-\xff"
61 #define ALNUM "0-9A-Za-z\x7f-\xff"
62 #endif
64 /* "A-Za-z\x7f-\xff" fails on other locales than "C" and so skip it */
65 #define ALPHA "[:alpha:]"
66 #define ALNUM "[:alnum:]"
68 static void function_cb(const char *line, const regexMatch *matches, unsigned int count);
70 static void installPHPRegex (const langType language)
72 addTagRegex(language, "^[ \t]*((final|abstract)[ \t]+)*class[ \t]+([" ALPHA "_][" ALNUM "_]*)",
73 "\\3", "c,class,classes", NULL);
74 addTagRegex(language, "^[ \t]*interface[ \t]+([" ALPHA "_][" ALNUM "_]*)",
75 "\\1", "i,interface,interfaces", NULL);
76 addTagRegex(language, "^[ \t]*define[ \t]*\\([ \t]*['\"]?([" ALPHA "_][" ALNUM "_]*)",
77 "\\1", "m,macro,macros", NULL);
78 addTagRegex(language, "^[ \t]*const[ \t]*([" ALPHA "_][" ALNUM "_]*)[ \t]*[=;]",
79 "\\1", "m,macro,macros", NULL);
80 addCallbackRegex(language,
81 "^[ \t]*((public|protected|private|static|final)[ \t]+)*function[ \t]+&?[ \t]*([" ALPHA "_][" ALNUM "_]*)[[:space:]]*(\\(.*\\)|\\(.*)",
82 NULL, function_cb);
83 addTagRegex(language, "^[ \t]*(\\$|::\\$|\\$this->)([" ALPHA "_][" ALNUM "_]*)[ \t]*=",
84 "\\2", "v,variable,variables", NULL);
85 addTagRegex(language, "^[ \t]*((var|public|protected|private|static)[ \t]+)+\\$([" ALPHA "_][" ALNUM "_]*)[ \t]*[=;]",
86 "\\3", "v,variable,variables", NULL);
88 /* function regex is covered by PHP regex */
89 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_]+)[ \t]*[=:][ \t]*function[ \t]*\\(",
90 "\\2", "j,jsfunction,javascript functions", NULL);
91 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_.]+)\\.([A-Za-z0-9_]+)[ \t]*=[ \t]*function[ \t]*\\(",
92 "\\2.\\3", "j,jsfunction,javascript functions", NULL);
93 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_.]+)\\.([A-Za-z0-9_]+)[ \t]*=[ \t]*function[ \t]*\\(",
94 "\\3", "j,jsfunction,javascript functions", NULL);
98 static void function_cb(const char *line, const regexMatch *matches, unsigned int count)
100 char *name, *arglist;
101 char kind = 'f';
102 static const char *kindName = "function";
103 tagEntryInfo e;
104 const regexMatch *match_funcname = NULL;
105 const regexMatch *match_arglist = NULL;
107 if (count > 2)
109 match_funcname = &matches[count - 2];
110 match_arglist = &matches[count - 1];
113 if (match_funcname != NULL)
115 name = xMalloc(match_funcname->length + 1, char);
116 strncpy(name, line + match_funcname->start, match_funcname->length);
117 *(name+match_funcname->length) = '\x0';
118 arglist = xMalloc(match_arglist->length + 1, char);
119 strncpy(arglist, line + match_arglist->start, match_arglist->length);
120 *(arglist+match_arglist->length) = '\x0';
122 initTagEntry (&e, name);
123 e.kind = kind;
124 e.kindName = kindName;
125 e.extensionFields.arglist = arglist;
126 makeTagEntry (&e);
128 eFree(name);
129 eFree(arglist);
133 /* Create parser definition structure */
134 extern parserDefinition* PhpParser (void)
136 static const char *const extensions [] = { "php", "php3", "phtml", NULL };
137 parserDefinition* def = parserNew ("PHP");
138 def->extensions = extensions;
139 def->initialize = installPHPRegex;
140 def->regex = TRUE;
141 return def;
144 #if 0
146 static boolean isLetter(const int c)
148 return (boolean)(isalpha(c) || (c >= 127 && c <= 255));
151 static boolean isVarChar1(const int c)
153 return (boolean)(isLetter (c) || c == '_');
156 static boolean isVarChar(const int c)
158 return (boolean)(isVarChar1 (c) || isdigit (c));
161 static void findPhpTags (void)
163 vString *name = vStringNew ();
164 const unsigned char *line;
166 while ((line = fileReadLine ()) != NULL)
168 const unsigned char *cp = line;
169 const char* f;
171 while (isspace (*cp))
172 cp++;
174 if (*(const char*)cp == '$' && isVarChar1 (*(const char*)(cp+1)))
176 cp += 1;
177 vStringClear (name);
178 while (isVarChar ((int) *cp))
180 vStringPut (name, (int) *cp);
181 ++cp;
183 while (isspace ((int) *cp))
184 ++cp;
185 if (*(const char*) cp == '=')
187 vStringTerminate (name);
188 makeSimpleTag (name, PhpKinds, K_VARIABLE);
189 vStringClear (name);
192 else if ((f = strstr ((const char*) cp, "function")) != NULL &&
193 (f == (const char*) cp || isspace ((int) f [-1])) &&
194 isspace ((int) f [8]))
196 cp = ((const unsigned char *) f) + 8;
198 while (isspace ((int) *cp))
199 ++cp;
201 if (*cp == '&') /* skip reference character and following whitespace */
203 cp++;
205 while (isspace ((int) *cp))
206 ++cp;
209 vStringClear (name);
210 while (isalnum ((int) *cp) || *cp == '_')
212 vStringPut (name, (int) *cp);
213 ++cp;
215 vStringTerminate (name);
216 makeSimpleTag (name, PhpKinds, K_FUNCTION);
217 vStringClear (name);
219 else if (strncmp ((const char*) cp, "class", (size_t) 5) == 0 &&
220 isspace ((int) cp [5]))
222 cp += 5;
224 while (isspace ((int) *cp))
225 ++cp;
226 vStringClear (name);
227 while (isalnum ((int) *cp) || *cp == '_')
229 vStringPut (name, (int) *cp);
230 ++cp;
232 vStringTerminate (name);
233 makeSimpleTag (name, PhpKinds, K_CLASS);
234 vStringClear (name);
236 else if (strncmp ((const char*) cp, "define", (size_t) 6) == 0 &&
237 ! isalnum ((int) cp [6]))
239 cp += 6;
241 while (isspace ((int) *cp))
242 ++cp;
243 if (*cp != '(')
244 continue;
245 ++cp;
247 while (isspace ((int) *cp))
248 ++cp;
249 if ((*cp == '\'') || (*cp == '"'))
250 ++cp;
251 else if (! ((*cp == '_') || isalnum ((int) *cp)))
252 continue;
254 vStringClear (name);
255 while (isalnum ((int) *cp) || *cp == '_')
257 vStringPut (name, (int) *cp);
258 ++cp;
260 vStringTerminate (name);
261 makeSimpleTag (name, PhpKinds, K_DEFINE);
262 vStringClear (name);
265 vStringDelete (name);
268 extern parserDefinition* PhpParser (void)
270 static const char *const extensions [] = { "php", "php3", "phtml", NULL };
271 parserDefinition* def = parserNew ("PHP");
272 def->kinds = PhpKinds;
273 def->kindCount = KIND_COUNT (PhpKinds);
274 def->extensions = extensions;
275 def->parser = findPhpTags;
276 return def;
279 #endif
281 /* vi:set tabstop=4 shiftwidth=4: */