Merge pull request #11 from esorton/bugfix/add-constexpr-keyword-to-arduino-ctags
[arduino-ctags.git] / php.c
blob90e096a705cf85f27c67ffe7786cb39eb611dba3
1 /*
2 * $Id: php.c 734 2009-08-20 23:33:54Z jafl $
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>
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.)
57 #ifdef __CYGWIN__
58 #define ALPHA "[:alpha:]"
59 #define ALNUM "[:alnum:]"
60 #else
61 #define ALPHA "A-Za-z\x7f-\xff"
62 #define ALNUM "0-9A-Za-z\x7f-\xff"
63 #endif
65 static void installPHPRegex (const langType language)
67 addTagRegex(language, "^[ \t]*((final|abstract)[ \t]+)*class[ \t]+([" ALPHA "_][" ALNUM "_]*)",
68 "\\3", "c,class,classes", NULL);
69 addTagRegex(language, "^[ \t]*interface[ \t]+([" ALPHA "_][" ALNUM "_]*)",
70 "\\1", "i,interface,interfaces", NULL);
71 addTagRegex(language, "^[ \t]*define[ \t]*\\([ \t]*['\"]?([" ALPHA "_][" ALNUM "_]*)",
72 "\\1", "d,define,constant definitions", NULL);
73 addTagRegex(language, "^[ \t]*((static|public|protected|private)[ \t]+)*function[ \t]+&?[ \t]*([" ALPHA "_][" ALNUM "_]*)",
74 "\\3", "f,function,functions", NULL);
75 addTagRegex(language, "^[ \t]*(\\$|::\\$|\\$this->)([" ALPHA "_][" ALNUM "_]*)[ \t]*=",
76 "\\2", "v,variable,variables", NULL);
77 addTagRegex(language, "^[ \t]*((var|public|protected|private|static)[ \t]+)+\\$([" ALPHA "_][" ALNUM "_]*)[ \t]*[=;]",
78 "\\3", "v,variable,variables", NULL);
80 /* function regex is covered by PHP regex */
81 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_]+)[ \t]*[=:][ \t]*function[ \t]*\\(",
82 "\\2", "j,jsfunction,javascript functions", NULL);
83 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_.]+)\\.([A-Za-z0-9_]+)[ \t]*=[ \t]*function[ \t]*\\(",
84 "\\2.\\3", "j,jsfunction,javascript functions", NULL);
85 addTagRegex (language, "(^|[ \t])([A-Za-z0-9_.]+)\\.([A-Za-z0-9_]+)[ \t]*=[ \t]*function[ \t]*\\(",
86 "\\3", "j,jsfunction,javascript functions", NULL);
89 /* Create parser definition structure */
90 extern parserDefinition* PhpParser (void)
92 static const char *const extensions [] = { "php", "php3", "phtml", NULL };
93 parserDefinition* def = parserNew ("PHP");
94 def->extensions = extensions;
95 def->initialize = installPHPRegex;
96 def->regex = TRUE;
97 return def;
100 #if 0
102 static boolean isLetter(const int c)
104 return (boolean)(isalpha(c) || (c >= 127 && c <= 255));
107 static boolean isVarChar1(const int c)
109 return (boolean)(isLetter (c) || c == '_');
112 static boolean isVarChar(const int c)
114 return (boolean)(isVarChar1 (c) || isdigit (c));
117 static void findPhpTags (void)
119 vString *name = vStringNew ();
120 const unsigned char *line;
122 while ((line = fileReadLine ()) != NULL)
124 const unsigned char *cp = line;
125 const char* f;
127 while (isspace (*cp))
128 cp++;
130 if (*(const char*)cp == '$' && isVarChar1 (*(const char*)(cp+1)))
132 cp += 1;
133 vStringClear (name);
134 while (isVarChar ((int) *cp))
136 vStringPut (name, (int) *cp);
137 ++cp;
139 while (isspace ((int) *cp))
140 ++cp;
141 if (*(const char*) cp == '=')
143 vStringTerminate (name);
144 makeSimpleTag (name, PhpKinds, K_VARIABLE);
145 vStringClear (name);
148 else if ((f = strstr ((const char*) cp, "function")) != NULL &&
149 (f == (const char*) cp || isspace ((int) f [-1])) &&
150 isspace ((int) f [8]))
152 cp = ((const unsigned char *) f) + 8;
154 while (isspace ((int) *cp))
155 ++cp;
157 if (*cp == '&') /* skip reference character and following whitespace */
159 cp++;
161 while (isspace ((int) *cp))
162 ++cp;
165 vStringClear (name);
166 while (isalnum ((int) *cp) || *cp == '_')
168 vStringPut (name, (int) *cp);
169 ++cp;
171 vStringTerminate (name);
172 makeSimpleTag (name, PhpKinds, K_FUNCTION);
173 vStringClear (name);
175 else if (strncmp ((const char*) cp, "class", (size_t) 5) == 0 &&
176 isspace ((int) cp [5]))
178 cp += 5;
180 while (isspace ((int) *cp))
181 ++cp;
182 vStringClear (name);
183 while (isalnum ((int) *cp) || *cp == '_')
185 vStringPut (name, (int) *cp);
186 ++cp;
188 vStringTerminate (name);
189 makeSimpleTag (name, PhpKinds, K_CLASS);
190 vStringClear (name);
192 else if (strncmp ((const char*) cp, "define", (size_t) 6) == 0 &&
193 ! isalnum ((int) cp [6]))
195 cp += 6;
197 while (isspace ((int) *cp))
198 ++cp;
199 if (*cp != '(')
200 continue;
201 ++cp;
203 while (isspace ((int) *cp))
204 ++cp;
205 if ((*cp == '\'') || (*cp == '"'))
206 ++cp;
207 else if (! ((*cp == '_') || isalnum ((int) *cp)))
208 continue;
210 vStringClear (name);
211 while (isalnum ((int) *cp) || *cp == '_')
213 vStringPut (name, (int) *cp);
214 ++cp;
216 vStringTerminate (name);
217 makeSimpleTag (name, PhpKinds, K_DEFINE);
218 vStringClear (name);
221 vStringDelete (name);
224 extern parserDefinition* PhpParser (void)
226 static const char *const extensions [] = { "php", "php3", "phtml", NULL };
227 parserDefinition* def = parserNew ("PHP");
228 def->kinds = PhpKinds;
229 def->kindCount = KIND_COUNT (PhpKinds);
230 def->extensions = extensions;
231 def->parser = findPhpTags;
232 return def;
235 #endif
237 /* vi:set tabstop=4 shiftwidth=4: */