2
* $Id: basic.c 2287 2008-02-27 13:17:29Z eht16 $
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.
18
#include "general.h" /* must always come first */
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},
58
{"private sub", K_FUNCTION},
59
{"public sub", K_FUNCTION},
60
{"private function", K_FUNCTION},
61
{"public function", K_FUNCTION},
68
* FUNCTION DEFINITIONS
71
/* Match the name of a dim or const starting at pos. */
72
static int extract_dim (char const *pos, vString * name, BasicKind kind)
74
while (isspace (*pos))
79
if (strncasecmp (pos, "shared", 6) == 0)
80
pos += 6; /* skip keyword "shared" */
82
while (isspace (*pos))
85
/* capture "dim as String str" */
86
if (strncasecmp (pos, "as", 2) == 0)
88
pos += 2; /* skip keyword "as" */
90
while (isspace (*pos))
92
while (!isspace (*pos)) /* skip next part which is a type */
94
while (isspace (*pos))
96
/* now we are at the name */
99
/* capture "dim as foo ptr bar" */
100
if (strncasecmp (pos, "ptr", 3) == 0)
102
pos += 3; /* skip keyword "ptr" */
104
while (isspace (*pos))
108
for (; *pos && !isspace (*pos) && *pos != '(' && *pos != ',' && *pos != '='; pos++)
109
vStringPut (name, *pos);
110
vStringTerminate (name);
111
makeSimpleTag (name, BasicKinds, kind);
113
/* if the line contains a ',', we have multiple declarations */
114
while (*pos && strchr (pos, ','))
116
/* skip all we don't need(e.g. "..., new_array(5), " we skip "(5)") */
117
while (*pos != ',' && *pos != '\'')
121
return 0; /* break if we are in a comment */
123
while (isspace (*pos) || *pos == ',')
127
return 0; /* break if we are in a comment */
130
for (; *pos && !isspace (*pos) && *pos != '(' && *pos != ',' && *pos != '='; pos++)
131
vStringPut (name, *pos);
132
vStringTerminate (name);
133
makeSimpleTag (name, BasicKinds, kind);
136
vStringDelete (name);
140
/* Match the name of a tag (function, variable, type, ...) starting at pos. */
141
static char const *extract_name (char const *pos, vString * name)
143
while (isspace (*pos))
146
for (; *pos && !isspace (*pos) && *pos != '(' && *pos != ',' && *pos != '='; pos++)
147
vStringPut (name, *pos);
148
vStringTerminate (name);
152
/* Match a keyword starting at p (case insensitive). */
153
static int match_keyword (const char *p, KeyWord const *kw)
158
for (i = 0; i < strlen (kw->token); i++)
160
if (tolower (p[i]) != kw->token[i])
163
name = vStringNew ();
165
if (kw == &freebasic_keywords[0] ||
166
kw == &freebasic_keywords[1] ||
167
kw == &freebasic_keywords[2])
168
return extract_dim (p, name, kw->kind); /* extract_dim adds the found tag(s) */
170
for (j = 0; j < 1; j++)
172
p = extract_name (p, name);
174
makeSimpleTag (name, BasicKinds, kw->kind);
175
vStringDelete (name);
180
/* Match a "label:" style label. */
181
static void match_colon_label (char const *p)
183
char const *end = p + strlen (p) - 1;
184
while (isspace (*end))
188
vString *name = vStringNew ();
189
vStringNCatS (name, p, end - p);
190
makeSimpleTag (name, BasicKinds, K_LABEL);
191
vStringDelete (name);
195
static void findBasicTags (void)
200
keywords = freebasic_keywords;
202
while ((line = (const char *) fileReadLine ()) != NULL)
204
const char *p = line;
214
/* In Basic, keywords always are at the start of the line. */
215
for (kw = keywords; kw->token; kw++)
216
if (match_keyword (p, kw)) break;
219
match_colon_label (p);
223
parserDefinition *FreeBasicParser (void)
225
static char const *extensions[] = { "bas", "bi", "bb", "pb", NULL };
226
parserDefinition *def = parserNew ("FreeBasic");
227
def->kinds = BasicKinds;
228
def->kindCount = KIND_COUNT (BasicKinds);
229
def->extensions = extensions;
230
def->parser = findBasicTags;
234
/* vi:set tabstop=4 shiftwidth=4: */