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
11 * Parsing PHP defines by Pavel Hlousek <pavel.hlousek@seznam.cz>, Apr 2003.
17 #include "general.h" /* must always come first */
29 K_CLASS
, K_DEFINE
, K_FUNCTION
, K_VARIABLE
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" }
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.)
57 #define ALPHA "[:alpha:]"
58 #define ALNUM "[:alnum:]"
60 #define ALPHA "A-Za-z\x7f-\xff"
61 #define ALNUM "0-9A-Za-z\x7f-\xff"
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:]]*(\\(.*\\)|\\(.*)",
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
;
102 static const char *kindName
= "function";
104 const regexMatch
*match_funcname
= NULL
;
105 const regexMatch
*match_arglist
= NULL
;
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
);
124 e
.kindName
= kindName
;
125 e
.extensionFields
.arglist
= 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
;
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
;
171 while (isspace (*cp
))
174 if (*(const char*)cp
== '$' && isVarChar1 (*(const char*)(cp
+1)))
178 while (isVarChar ((int) *cp
))
180 vStringPut (name
, (int) *cp
);
183 while (isspace ((int) *cp
))
185 if (*(const char*) cp
== '=')
187 vStringTerminate (name
);
188 makeSimpleTag (name
, PhpKinds
, K_VARIABLE
);
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
))
201 if (*cp
== '&') /* skip reference character and following whitespace */
205 while (isspace ((int) *cp
))
210 while (isalnum ((int) *cp
) || *cp
== '_')
212 vStringPut (name
, (int) *cp
);
215 vStringTerminate (name
);
216 makeSimpleTag (name
, PhpKinds
, K_FUNCTION
);
219 else if (strncmp ((const char*) cp
, "class", (size_t) 5) == 0 &&
220 isspace ((int) cp
[5]))
224 while (isspace ((int) *cp
))
227 while (isalnum ((int) *cp
) || *cp
== '_')
229 vStringPut (name
, (int) *cp
);
232 vStringTerminate (name
);
233 makeSimpleTag (name
, PhpKinds
, K_CLASS
);
236 else if (strncmp ((const char*) cp
, "define", (size_t) 6) == 0 &&
237 ! isalnum ((int) cp
[6]))
241 while (isspace ((int) *cp
))
247 while (isspace ((int) *cp
))
249 if ((*cp
== '\'') || (*cp
== '"'))
251 else if (! ((*cp
== '_') || isalnum ((int) *cp
)))
255 while (isalnum ((int) *cp
) || *cp
== '_')
257 vStringPut (name
, (int) *cp
);
260 vStringTerminate (name
);
261 makeSimpleTag (name
, PhpKinds
, K_DEFINE
);
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
;
281 /* vi:set tabstop=4 shiftwidth=4: */