Properly save and restore the encoding state in Open File dialog (oops)
[geany-mirror.git] / tagmanager / php.c
blob7958e548bacef088a9a71668ea156d26e33b28ed
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 /* Note: Using [] to match words is wrong, but using () doesn't seem to match 'function' on its own */
83 addCallbackRegex(language,
84 "^[ \t]*[(public|protected|private|static|final)[ \t]*]*[ \t]*function[ \t]+&?[ \t]*([" ALPHA "_][" ALNUM "_]*)[[:space:]]*(\\(.*\\))",
85 NULL, function_cb);
86 addTagRegex(language, "^[ \t]*(\\$|::\\$|\\$this->)([" ALPHA "_][" ALNUM "_]*)[ \t]*=",
87 "\\2", "v,variable,variables", NULL);
88 addTagRegex(language, "^[ \t]*((var|public|protected|private|static)[ \t]+)+\\$([" ALPHA "_][" ALNUM "_]*)[ \t]*[=;]",
89 "\\3", "v,variable,variables", NULL);
91 /* function regex is covered by PHP regex */
92 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_]+)[ \t]*[=:][ \t]*function[ \t]*\\(",
93 "\\2", "j,jsfunction,javascript functions", NULL);
94 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_.]+)\\.([A-Za-z0-9_]+)[ \t]*=[ \t]*function[ \t]*\\(",
95 "\\2.\\3", "j,jsfunction,javascript functions", NULL);
96 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_.]+)\\.([A-Za-z0-9_]+)[ \t]*=[ \t]*function[ \t]*\\(",
97 "\\3", "j,jsfunction,javascript functions", NULL);
101 static void function_cb(const char *line, const regexMatch *matches, unsigned int count)
103 char *name, *arglist;
104 char kind = 'f';
105 static char *kindName = "function";
106 tagEntryInfo e;
107 const regexMatch *match_funcname = NULL;
108 const regexMatch *match_arglist = NULL;
110 if (count > 2)
112 match_funcname = &matches[count - 2];
113 match_arglist = &matches[count - 1];
116 if (match_funcname != NULL)
118 name = xMalloc(match_funcname->length + 1, char);
119 strncpy(name, line + match_funcname->start, match_funcname->length);
120 *(name+match_funcname->length) = '\x0';
121 arglist = xMalloc(match_arglist->length + 1, char);
122 strncpy(arglist, line + match_arglist->start, match_arglist->length);
123 *(arglist+match_arglist->length) = '\x0';
125 initTagEntry (&e, name);
126 e.kind = kind;
127 e.kindName = kindName;
128 e.extensionFields.arglist = arglist;
129 makeTagEntry (&e);
131 eFree(name);
132 eFree(arglist);
136 /* Create parser definition structure */
137 extern parserDefinition* PhpParser (void)
139 static const char *const extensions [] = { "php", "php3", "phtml", NULL };
140 parserDefinition* def = parserNew ("PHP");
141 def->extensions = extensions;
142 def->initialize = installPHPRegex;
143 def->regex = TRUE;
144 return def;
147 #if 0
149 static boolean isLetter(const int c)
151 return (boolean)(isalpha(c) || (c >= 127 && c <= 255));
154 static boolean isVarChar1(const int c)
156 return (boolean)(isLetter (c) || c == '_');
159 static boolean isVarChar(const int c)
161 return (boolean)(isVarChar1 (c) || isdigit (c));
164 static void findPhpTags (void)
166 vString *name = vStringNew ();
167 const unsigned char *line;
169 while ((line = fileReadLine ()) != NULL)
171 const unsigned char *cp = line;
172 const char* f;
174 while (isspace (*cp))
175 cp++;
177 if (*(const char*)cp == '$' && isVarChar1 (*(const char*)(cp+1)))
179 cp += 1;
180 vStringClear (name);
181 while (isVarChar ((int) *cp))
183 vStringPut (name, (int) *cp);
184 ++cp;
186 while (isspace ((int) *cp))
187 ++cp;
188 if (*(const char*) cp == '=')
190 vStringTerminate (name);
191 makeSimpleTag (name, PhpKinds, K_VARIABLE);
192 vStringClear (name);
195 else if ((f = strstr ((const char*) cp, "function")) != NULL &&
196 (f == (const char*) cp || isspace ((int) f [-1])) &&
197 isspace ((int) f [8]))
199 cp = ((const unsigned char *) f) + 8;
201 while (isspace ((int) *cp))
202 ++cp;
204 if (*cp == '&') /* skip reference character and following whitespace */
206 cp++;
208 while (isspace ((int) *cp))
209 ++cp;
212 vStringClear (name);
213 while (isalnum ((int) *cp) || *cp == '_')
215 vStringPut (name, (int) *cp);
216 ++cp;
218 vStringTerminate (name);
219 makeSimpleTag (name, PhpKinds, K_FUNCTION);
220 vStringClear (name);
222 else if (strncmp ((const char*) cp, "class", (size_t) 5) == 0 &&
223 isspace ((int) cp [5]))
225 cp += 5;
227 while (isspace ((int) *cp))
228 ++cp;
229 vStringClear (name);
230 while (isalnum ((int) *cp) || *cp == '_')
232 vStringPut (name, (int) *cp);
233 ++cp;
235 vStringTerminate (name);
236 makeSimpleTag (name, PhpKinds, K_CLASS);
237 vStringClear (name);
239 else if (strncmp ((const char*) cp, "define", (size_t) 6) == 0 &&
240 ! isalnum ((int) cp [6]))
242 cp += 6;
244 while (isspace ((int) *cp))
245 ++cp;
246 if (*cp != '(')
247 continue;
248 ++cp;
250 while (isspace ((int) *cp))
251 ++cp;
252 if ((*cp == '\'') || (*cp == '"'))
253 ++cp;
254 else if (! ((*cp == '_') || isalnum ((int) *cp)))
255 continue;
257 vStringClear (name);
258 while (isalnum ((int) *cp) || *cp == '_')
260 vStringPut (name, (int) *cp);
261 ++cp;
263 vStringTerminate (name);
264 makeSimpleTag (name, PhpKinds, K_DEFINE);
265 vStringClear (name);
268 vStringDelete (name);
271 extern parserDefinition* PhpParser (void)
273 static const char *const extensions [] = { "php", "php3", "phtml", NULL };
274 parserDefinition* def = parserNew ("PHP");
275 def->kinds = PhpKinds;
276 def->kindCount = KIND_COUNT (PhpKinds);
277 def->extensions = extensions;
278 def->parser = findPhpTags;
279 return def;
282 #endif
284 /* vi:set tabstop=4 shiftwidth=4: */