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 addCallbackRegex(language
,
83 "^[ \t]*[(public|protected|private|static)[ \t]*]*[ \t]*function[ \t]+&?[ \t]*([" ALPHA
"_][" ALNUM
"_]*)[[:space:]]*(\\(.*\\))",
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
;
104 static char *kindName
= "function";
106 const regexMatch
*match_funcname
= NULL
;
107 const regexMatch
*match_arglist
= NULL
;
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
);
126 e
.kindName
= kindName
;
127 e
.extensionFields
.arglist
= arglist
;
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
;
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
;
170 while (isspace (*cp
))
173 if (*(const char*)cp
== '$' && isVarChar1 (*(const char*)(cp
+1)))
177 while (isVarChar ((int) *cp
))
179 vStringPut (name
, (int) *cp
);
182 while (isspace ((int) *cp
))
184 if (*(const char*) cp
== '=')
186 vStringTerminate (name
);
187 makeSimpleTag (name
, PhpKinds
, K_VARIABLE
);
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
))
200 if (*cp
== '&') /* skip reference character and following whitespace */
204 while (isspace ((int) *cp
))
209 while (isalnum ((int) *cp
) || *cp
== '_')
211 vStringPut (name
, (int) *cp
);
214 vStringTerminate (name
);
215 makeSimpleTag (name
, PhpKinds
, K_FUNCTION
);
218 else if (strncmp ((const char*) cp
, "class", (size_t) 5) == 0 &&
219 isspace ((int) cp
[5]))
223 while (isspace ((int) *cp
))
226 while (isalnum ((int) *cp
) || *cp
== '_')
228 vStringPut (name
, (int) *cp
);
231 vStringTerminate (name
);
232 makeSimpleTag (name
, PhpKinds
, K_CLASS
);
235 else if (strncmp ((const char*) cp
, "define", (size_t) 6) == 0 &&
236 ! isalnum ((int) cp
[6]))
240 while (isspace ((int) *cp
))
246 while (isspace ((int) *cp
))
248 if ((*cp
== '\'') || (*cp
== '"'))
250 else if (! ((*cp
== '_') || isalnum ((int) *cp
)))
254 while (isalnum ((int) *cp
) || *cp
== '_')
256 vStringPut (name
, (int) *cp
);
259 vStringTerminate (name
);
260 makeSimpleTag (name
, PhpKinds
, K_DEFINE
);
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
;
280 /* vi:set tabstop=4 shiftwidth=4: */