eht16's changes
[geany-mirror.git] / tagmanager / basic.c
blob6ef6bec24c0a2d2f7230615106aa105d66590287
1 /*
2 * $Id$
4 * Copyright (c) 2000-2006, Darren Hiebert, Elias Pschernig
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 BlitzBasic
10 * (BlitzMax), PureBasic and FreeBasic language files. For now, this is kept
11 * quite simple - but feel free to ask for more things added any time -
12 * patches are of course most welcome.
16 * INCLUDE FILES
18 #include "general.h" /* must always come first */
20 #include <string.h>
22 #include "parse.h"
23 #include "read.h"
24 #include "vstring.h"
27 * DATA DEFINITIONS
29 typedef enum {
30 K_CONST,
31 K_FUNCTION,
32 K_LABEL,
33 K_TYPE,
34 K_VARIABLE,
35 K_ENUM
36 } BasicKind;
38 typedef struct {
39 char const *token;
40 BasicKind kind;
41 } KeyWord;
43 static kindOption BasicKinds[] = {
44 {TRUE, 'c', "macro", "constants"},
45 {TRUE, 'f', "function", "functions"},
46 {TRUE, 'l', "namespace", "labels"},
47 {TRUE, 't', "struct", "types"},
48 {TRUE, 'v', "variable", "variables"},
49 {TRUE, 'g', "externvar", "enumerations"}
52 static KeyWord freebasic_keywords[] = {
53 {"dim", K_VARIABLE}, /* must always be the first */
54 {"common", K_VARIABLE}, /* must always be the second */
55 {"const", K_CONST}, /* must always be the third */
56 {"function", K_FUNCTION},
57 {"sub", K_FUNCTION},
58 {"property", K_FUNCTION},
59 {"constructor", K_FUNCTION},
60 {"destructor", K_FUNCTION},
61 {"private sub", K_FUNCTION},
62 {"public sub", K_FUNCTION},
63 {"private function", K_FUNCTION},
64 {"public function", K_FUNCTION},
65 {"type", K_TYPE},
66 {"enum", K_ENUM},
67 {NULL, 0}
71 * FUNCTION DEFINITIONS
74 /* Match the name of a dim or const starting at pos. */
75 static int extract_dim (char const *pos, vString * name, BasicKind kind)
77 const char *old_pos = pos;
78 while (isspace (*pos))
79 pos++;
81 /* create tags only if there is some space between the keyword and the identifier */
82 if (old_pos == pos)
83 return 0;
85 vStringClear (name);
87 if (strncasecmp (pos, "shared", 6) == 0)
88 pos += 6; /* skip keyword "shared" */
90 while (isspace (*pos))
91 pos++;
93 /* capture "dim as String str" */
94 if (strncasecmp (pos, "as", 2) == 0)
96 pos += 2; /* skip keyword "as" */
98 while (isspace (*pos))
99 pos++;
100 while (!isspace (*pos)) /* skip next part which is a type */
101 pos++;
102 while (isspace (*pos))
103 pos++;
104 /* now we are at the name */
106 /* capture "dim as foo ptr bar" */
107 if (strncasecmp (pos, "ptr", 3) == 0 && isspace(*(pos+4)))
109 pos += 3; /* skip keyword "ptr" */
110 while (isspace (*pos))
111 pos++;
113 /* capture "dim as string * 4096 chunk" */
114 if (strncmp (pos, "*", 1) == 0)
116 pos += 1; /* skip "*" */
117 while (isspace (*pos) || isdigit(*pos) || ispunct(*pos))
118 pos++;
121 for (; *pos && !isspace (*pos) && *pos != '(' && *pos != ',' && *pos != '='; pos++)
122 vStringPut (name, *pos);
123 vStringTerminate (name);
124 makeSimpleTag (name, BasicKinds, kind);
126 /* if the line contains a ',', we have multiple declarations */
127 while (*pos && strchr (pos, ','))
129 /* skip all we don't need(e.g. "..., new_array(5), " we skip "(5)") */
130 while (*pos != ',' && *pos != '\'')
131 pos++;
133 if (*pos == '\'')
134 return 0; /* break if we are in a comment */
136 while (isspace (*pos) || *pos == ',')
137 pos++;
139 if (*pos == '\'')
140 return 0; /* break if we are in a comment */
142 vStringClear (name);
143 for (; *pos && !isspace (*pos) && *pos != '(' && *pos != ',' && *pos != '='; pos++)
144 vStringPut (name, *pos);
145 vStringTerminate (name);
146 makeSimpleTag (name, BasicKinds, kind);
149 vStringDelete (name);
150 return 1;
153 /* Match the name of a tag (function, variable, type, ...) starting at pos. */
154 static char const *extract_name (char const *pos, vString * name)
156 while (isspace (*pos))
157 pos++;
158 vStringClear (name);
159 for (; *pos && !isspace (*pos) && *pos != '(' && *pos != ',' && *pos != '='; pos++)
160 vStringPut (name, *pos);
161 vStringTerminate (name);
162 return pos;
165 /* Match a keyword starting at p (case insensitive). */
166 static int match_keyword (const char *p, KeyWord const *kw)
168 vString *name;
169 size_t i;
170 int j;
171 const char *old_p;
172 for (i = 0; i < strlen (kw->token); i++)
174 if (tolower (p[i]) != kw->token[i])
175 return 0;
177 name = vStringNew ();
178 p += i;
179 if (kw == &freebasic_keywords[0] ||
180 kw == &freebasic_keywords[1] ||
181 kw == &freebasic_keywords[2])
182 return extract_dim (p, name, kw->kind); /* extract_dim adds the found tag(s) */
184 old_p = p;
185 while (isspace (*p))
186 p++;
188 /* create tags only if there is some space between the keyword and the identifier */
189 if (old_p == p)
191 vStringDelete (name);
192 return 0;
195 for (j = 0; j < 1; j++)
197 p = extract_name (p, name);
199 makeSimpleTag (name, BasicKinds, kw->kind);
200 vStringDelete (name);
202 return 1;
205 /* Match a "label:" style label. */
206 static void match_colon_label (char const *p)
208 char const *end = p + strlen (p) - 1;
209 while (isspace (*end))
210 end--;
211 if (*end == ':')
213 vString *name = vStringNew ();
214 vStringNCatS (name, p, end - p);
215 makeSimpleTag (name, BasicKinds, K_LABEL);
216 vStringDelete (name);
220 static void findBasicTags (void)
222 const char *line;
223 KeyWord *keywords;
225 keywords = freebasic_keywords;
227 while ((line = (const char *) fileReadLine ()) != NULL)
229 const char *p = line;
230 KeyWord const *kw;
232 while (isspace (*p))
233 p++;
235 /* Empty line or comment? */
236 if (!*p || *p == '\'')
237 continue;
239 /* In Basic, keywords always are at the start of the line. */
240 for (kw = keywords; kw->token; kw++)
241 if (match_keyword (p, kw)) break;
243 /* Is it a label? */
244 match_colon_label (p);
248 parserDefinition *FreeBasicParser (void)
250 static char const *extensions[] = { "bas", "bi", "bb", "pb", NULL };
251 parserDefinition *def = parserNew ("FreeBasic");
252 def->kinds = BasicKinds;
253 def->kindCount = KIND_COUNT (BasicKinds);
254 def->extensions = extensions;
255 def->parser = findBasicTags;
256 return def;
259 /* vi:set tabstop=4 shiftwidth=4: */