xref: /Universal-ctags/parsers/haxe.c (revision 19f4ba65dd36917a67a2fed1fe6f1ef61f3e74c5)
1 /*
2  *       Copyright (c) 2007, Ritchie Turner
3  *
4  *       This source code is released for free distribution under the terms of the
5  *       GNU General Public License version 2 or (at your opinion) any later version.
6  *
7  *       This module contains functions for generating tags for Haxe files
8  *       (https://en.wikipedia.org/wiki/Haxe).
9  *
10  *       Borrowed from PHP.
11  */
12 
13 /*
14  *       INCLUDE FILES
15  */
16 #include "general.h"    /* must always come first */
17 #include <ctype.h>      /* to define isalpha () */
18 #ifdef DEBUG
19 #include <stdio.h>
20 #endif
21 #include <string.h>
22 #include "entry.h"
23 #include "keyword.h"
24 #include "parse.h"
25 #include "read.h"
26 #include "vstring.h"
27 #include "routines.h"
28 
29 /*
30  *       MACROS
31  */
32 #define isType(token,t)         (bool) ((token)->type == (t))
33 #define isKeyword(token,k)      (bool) ((token)->keyword == (k))
34 
35 /*
36  *      DATA DEFINITIONS
37  */
38 
39 /*static jmp_buf Exception;*/
40 
41 typedef enum {
42 	HXTAG_METHODS,
43 	HXTAG_CLASS,
44 	HXTAG_ENUM,
45 	HXTAG_VARIABLE,
46 	HXTAG_INTERFACE,
47 	HXTAG_TYPEDEF,
48 	HXTAG_COUNT
49 } hxKind;
50 
51 static kindDefinition HxKinds [] = {
52 	{ true,  'm', "method",         "methods" },
53 	{ true,  'c', "class",          "classes" },
54 	{ true,  'e', "enum",           "enumerations" },
55 	{ true,  'v', "variable",       "variables" },
56 	{ true,  'i', "interface",      "interfaces" },
57 	{ true,  't', "typedef",        "typedefs" },
58 };
59 
findHxTags(void)60 static void findHxTags (void)
61 {
62 	vString *name = vStringNew ();
63 	vString *clsName = vStringNew();
64 	vString *scope2 = vStringNew();
65 	vString *laccess = vStringNew();
66 	const char *const priv = "private";
67 	const char *const pub = "public";
68 
69 	const unsigned char *line;
70 
71 	while ((line = readLineFromInputFile ()) != NULL)
72 	{
73 		const unsigned char *cp = line;
74 another:
75 		while (isspace (*cp))
76 			cp++;
77 
78 		vStringCopyS(laccess,priv);
79 
80 		if (strncmp ((const char*) cp, "var", (size_t) 3) == 0  &&
81 			isspace ((int) cp [3]))
82 		{
83 			cp += 3;
84 
85 			while (isspace ((int) *cp))
86 				++cp;
87 
88 			vStringClear (name);
89 			while (isalnum ((int) *cp)  ||  *cp == '_')
90 			{
91 				vStringPut (name, (int) *cp);
92 				++cp;
93 			}
94 			makeSimpleTag (name, HXTAG_VARIABLE);
95 
96 			vStringClear (name);
97 		}
98 		else if (strncmp ((const char*) cp, "function", (size_t) 8) == 0  &&
99 			isspace ((int) cp [8]))
100 		{
101 			cp += 8;
102 
103 			while (isspace ((int) *cp))
104 				++cp;
105 
106 			vStringClear (name);
107 			while (isalnum ((int) *cp)  ||  *cp == '_')
108 			{
109 				vStringPut (name, (int) *cp);
110 				++cp;
111 			}
112 			makeSimpleTag (name, HXTAG_METHODS);
113 
114 			vStringClear (name);
115 		}
116 		else if (strncmp ((const char*) cp, "class", (size_t) 5) == 0 &&
117 				 isspace ((int) cp [5]))
118 		{
119 			cp += 5;
120 
121 			while (isspace ((int) *cp))
122 				++cp;
123 			vStringClear (name);
124 			while (isalnum ((int) *cp)  ||  *cp == '_')
125 			{
126 				vStringPut (name, (int) *cp);
127 				++cp;
128 			}
129 			makeSimpleTag (name, HXTAG_CLASS);
130 			vStringCopy(clsName,name);
131 			vStringClear (name);
132 		}
133 		else if (strncmp ((const char*) cp, "enum", (size_t) 4) == 0 &&
134 				  isspace ((int) cp [4]))
135 		{
136 			cp += 4;
137 
138 			while (isspace ((int) *cp))
139 				++cp;
140 			vStringClear (name);
141 			while (isalnum ((int) *cp)  ||  *cp == '_')
142 			{
143 				vStringPut (name, (int) *cp);
144 				++cp;
145 			}
146 			makeSimpleTag (name, HXTAG_ENUM);
147 			vStringClear (name);
148 		} else if (strncmp ((const char*) cp, "public", (size_t) 6) == 0 &&
149 				 isspace((int) cp [6]))
150 		{
151 			cp += 6;
152 			while (isspace ((int) *cp))
153 				++cp;
154 			vStringCopyS(laccess,pub);
155 			goto another;
156 		} else if (strncmp ((const char*) cp, "static", (size_t) 6) == 0 &&
157 				 isspace((int) cp [6]))
158 		{
159 			cp += 6;
160 			while (isspace ((int) *cp))
161 				++cp;
162 			goto another;
163 		} else if (strncmp ((const char*) cp, "interface", (size_t) 9) == 0 &&
164 			isspace((int) cp [9]))
165 		{
166 			cp += 9;
167 
168 			while (isspace ((int) *cp))
169 				++cp;
170 			vStringClear (name);
171 			while (isalnum ((int) *cp)  ||  *cp == '_') {
172 				vStringPut (name, (int) *cp);
173 				++cp;
174 			}
175 			makeSimpleTag (name, HXTAG_INTERFACE);
176 			vStringClear (name);
177 		} else if (strncmp ((const char *) cp,"typedef",(size_t) 7) == 0 && isspace(((int) cp[7]))) {
178 			cp += 7;
179 
180 			while (isspace ((int) *cp))
181 				++cp;
182 			vStringClear (name);
183 			while (isalnum ((int) *cp)  ||  *cp == '_') {
184 				vStringPut (name, (int) *cp);
185 				++cp;
186 			}
187 			makeSimpleTag (name, HXTAG_TYPEDEF);
188 			vStringClear (name);
189 		}
190 
191 
192 	}
193 
194 	vStringDelete(name);
195 	vStringDelete(clsName);
196 	vStringDelete(scope2);
197 	vStringDelete(laccess);
198 }
199 
200 
201 /* Create parser definition structure */
HaxeParser(void)202 extern parserDefinition* HaxeParser (void)
203 {
204 	static const char *const extensions [] = { "hx", NULL };
205 
206 	parserDefinition *const def = parserNew ("Haxe");
207 	def->extensions = extensions;
208 	/*
209 	 * New definitions for parsing instead of regex
210 	 */
211 	def->kindTable          = HxKinds;
212 	def->kindCount  = ARRAY_SIZE (HxKinds);
213 	def->parser             = findHxTags;
214 	/*def->initialize = initialize;*/
215 	return def;
216 }
217