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.)
59 #define ALPHA "[:alpha:]"
60 #define ALNUM "[:alnum:]"
62 #define ALPHA "A-Za-z\x7f-\xff"
63 #define ALNUM "0-9A-Za-z\x7f-\xff"
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:]]*(\\(.*\\))",
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
;
105 static char *kindName
= "function";
107 const regexMatch
*match_funcname
= NULL
;
108 const regexMatch
*match_arglist
= NULL
;
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
);
127 e
.kindName
= kindName
;
128 e
.extensionFields
.arglist
= 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
;
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
;
174 while (isspace (*cp
))
177 if (*(const char*)cp
== '$' && isVarChar1 (*(const char*)(cp
+1)))
181 while (isVarChar ((int) *cp
))
183 vStringPut (name
, (int) *cp
);
186 while (isspace ((int) *cp
))
188 if (*(const char*) cp
== '=')
190 vStringTerminate (name
);
191 makeSimpleTag (name
, PhpKinds
, K_VARIABLE
);
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
))
204 if (*cp
== '&') /* skip reference character and following whitespace */
208 while (isspace ((int) *cp
))
213 while (isalnum ((int) *cp
) || *cp
== '_')
215 vStringPut (name
, (int) *cp
);
218 vStringTerminate (name
);
219 makeSimpleTag (name
, PhpKinds
, K_FUNCTION
);
222 else if (strncmp ((const char*) cp
, "class", (size_t) 5) == 0 &&
223 isspace ((int) cp
[5]))
227 while (isspace ((int) *cp
))
230 while (isalnum ((int) *cp
) || *cp
== '_')
232 vStringPut (name
, (int) *cp
);
235 vStringTerminate (name
);
236 makeSimpleTag (name
, PhpKinds
, K_CLASS
);
239 else if (strncmp ((const char*) cp
, "define", (size_t) 6) == 0 &&
240 ! isalnum ((int) cp
[6]))
244 while (isspace ((int) *cp
))
250 while (isspace ((int) *cp
))
252 if ((*cp
== '\'') || (*cp
== '"'))
254 else if (! ((*cp
== '_') || isalnum ((int) *cp
)))
258 while (isalnum ((int) *cp
) || *cp
== '_')
260 vStringPut (name
, (int) *cp
);
263 vStringTerminate (name
);
264 makeSimpleTag (name
, PhpKinds
, K_DEFINE
);
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
;
284 /* vi:set tabstop=4 shiftwidth=4: */