| 1 |
/* |
| 2 |
* $Id:$ |
| 3 |
* |
| 4 |
* Copyright (c) 2000-2006, Darren Hiebert, Elias Pschernig |
| 5 |
* |
| 6 |
* This source code is released for free distribution under the terms of the |
| 7 |
* GNU General Public License. |
| 8 |
* |
| 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. |
| 13 |
*/ |
| 14 |
|
| 15 |
/* |
| 16 |
* INCLUDE FILES |
| 17 |
*/ |
| 18 |
#include "general.h" /* must always come first */ |
| 19 |
|
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include "options.h" |
| 23 |
#include "parse.h" |
| 24 |
#include "read.h" |
| 25 |
#include "routines.h" |
| 26 |
#include "vstring.h" |
| 27 |
|
| 28 |
/* |
| 29 |
* DATA DEFINITIONS |
| 30 |
*/ |
| 31 |
typedef enum { |
| 32 |
K_CONST, |
| 33 |
K_FUNCTION, |
| 34 |
K_LABEL, |
| 35 |
K_TYPE, |
| 36 |
K_VARIABLE, |
| 37 |
K_ENUM |
| 38 |
} BasicKind; |
| 39 |
|
| 40 |
typedef struct { |
| 41 |
char const *token; |
| 42 |
BasicKind kind; |
| 43 |
int skip; |
| 44 |
} KeyWord; |
| 45 |
|
| 46 |
static kindOption BasicKinds[] = { |
| 47 |
{TRUE, 'c', "constant", "constants"}, |
| 48 |
{TRUE, 'f', "function", "functions"}, |
| 49 |
{TRUE, 'l', "label", "labels"}, |
| 50 |
{TRUE, 't', "type", "types"}, |
| 51 |
{TRUE, 'v', "variable", "variables"}, |
| 52 |
{TRUE, 'g', "enum", "enumerations"} |
| 53 |
}; |
| 54 |
|
| 55 |
static KeyWord blitzbasic_keywords[] = { |
| 56 |
{"const", K_CONST, 0}, |
| 57 |
{"global", K_VARIABLE, 0}, |
| 58 |
{"dim", K_VARIABLE, 0}, |
| 59 |
{"function", K_FUNCTION, 0}, |
| 60 |
{"type", K_TYPE, 0}, |
| 61 |
{NULL, 0, 0} |
| 62 |
}; |
| 63 |
|
| 64 |
static KeyWord purebasic_keywords[] = { |
| 65 |
{"newlist", K_VARIABLE, 0}, |
| 66 |
{"global", K_VARIABLE, 0}, |
| 67 |
{"dim", K_VARIABLE, 0}, |
| 68 |
{"procedure", K_FUNCTION, 0}, |
| 69 |
{"interface", K_TYPE, 0}, |
| 70 |
{"structure", K_TYPE, 0}, |
| 71 |
{NULL, 0, 0} |
| 72 |
}; |
| 73 |
|
| 74 |
static KeyWord freebasic_keywords[] = { |
| 75 |
{"const", K_CONST, 0}, |
| 76 |
{"dim as", K_VARIABLE, 1}, |
| 77 |
{"dim", K_VARIABLE, 0}, |
| 78 |
{"common", K_VARIABLE, 0}, |
| 79 |
{"function", K_FUNCTION, 0}, |
| 80 |
{"sub", K_FUNCTION, 0}, |
| 81 |
{"private sub", K_FUNCTION, 0}, |
| 82 |
{"public sub", K_FUNCTION, 0}, |
| 83 |
{"private function", K_FUNCTION, 0}, |
| 84 |
{"public function", K_FUNCTION, 0}, |
| 85 |
{"type", K_TYPE, 0}, |
| 86 |
{"enum", K_ENUM, 0}, |
| 87 |
{NULL, 0, 0} |
| 88 |
}; |
| 89 |
|
| 90 |
/* |
| 91 |
* FUNCTION DEFINITIONS |
| 92 |
*/ |
| 93 |
|
| 94 |
/* Match the name of a tag (function, variable, type, ...) starting at pos. */ |
| 95 |
static char const *extract_name (char const *pos, vString * name) |
| 96 |
{ |
| 97 |
while (isspace (*pos)) |
| 98 |
pos++; |
| 99 |
vStringClear (name); |
| 100 |
for (; *pos && !isspace (*pos) && *pos != '(' && *pos != ','; pos++) |
| 101 |
vStringPut (name, *pos); |
| 102 |
vStringTerminate (name); |
| 103 |
return pos; |
| 104 |
} |
| 105 |
|
| 106 |
/* Match a keyword starting at p (case insensitive). */ |
| 107 |
static int match_keyword (const char *p, KeyWord const *kw) |
| 108 |
{ |
| 109 |
vString *name; |
| 110 |
size_t i; |
| 111 |
int j; |
| 112 |
for (i = 0; i < strlen (kw->token); i++) |
| 113 |
{ |
| 114 |
if (tolower (p[i]) != kw->token[i]) |
| 115 |
return 0; |
| 116 |
} |
| 117 |
name = vStringNew (); |
| 118 |
p += i; |
| 119 |
for (j = 0; j < 1 + kw->skip; j++) |
| 120 |
{ |
| 121 |
p = extract_name (p, name); |
| 122 |
} |
| 123 |
makeSimpleTag (name, BasicKinds, kw->kind); |
| 124 |
vStringDelete (name); |
| 125 |
return 1; |
| 126 |
} |
| 127 |
|
| 128 |
/* Match a "label:" style label. */ |
| 129 |
static void match_colon_label (char const *p) |
| 130 |
{ |
| 131 |
char const *end = p + strlen (p) - 1; |
| 132 |
while (isspace (*end)) |
| 133 |
end--; |
| 134 |
if (*end == ':') |
| 135 |
{ |
| 136 |
vString *name = vStringNew (); |
| 137 |
vStringNCatS (name, p, end - p); |
| 138 |
makeSimpleTag (name, BasicKinds, K_LABEL); |
| 139 |
vStringDelete (name); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/* Match a ".label" style label. */ |
| 144 |
static void match_dot_label (char const *p) |
| 145 |
{ |
| 146 |
if (*p == '.') |
| 147 |
{ |
| 148 |
vString *name = vStringNew (); |
| 149 |
extract_name (p + 1, name); |
| 150 |
makeSimpleTag (name, BasicKinds, K_LABEL); |
| 151 |
vStringDelete (name); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
static void findBasicTags (void) |
| 156 |
{ |
| 157 |
const char *line; |
| 158 |
const char *extension = fileExtension (vStringValue (File.name)); |
| 159 |
KeyWord *keywords; |
| 160 |
|
| 161 |
if (strcmp (extension, "bb") == 0) |
| 162 |
keywords = blitzbasic_keywords; |
| 163 |
else if (strcmp (extension, "pb") == 0) |
| 164 |
keywords = purebasic_keywords; |
| 165 |
else |
| 166 |
keywords = freebasic_keywords; |
| 167 |
|
| 168 |
while ((line = (const char *) fileReadLine ()) != NULL) |
| 169 |
{ |
| 170 |
const char *p = line; |
| 171 |
KeyWord const *kw; |
| 172 |
|
| 173 |
while (isspace (*p)) |
| 174 |
p++; |
| 175 |
|
| 176 |
/* Empty line? */ |
| 177 |
if (!*p) |
| 178 |
continue; |
| 179 |
|
| 180 |
/* In Basic, keywords always are at the start of the line. */ |
| 181 |
for (kw = keywords; kw->token; kw++) |
| 182 |
if (match_keyword (p, kw)) break; |
| 183 |
|
| 184 |
/* Is it a label? */ |
| 185 |
if (strcmp (extension, "bb") == 0) |
| 186 |
match_dot_label (p); |
| 187 |
else |
| 188 |
match_colon_label (p); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
parserDefinition *BasicParser (void) |
| 193 |
{ |
| 194 |
static char const *extensions[] = { "bas", "bi", "bb", "pb", NULL }; |
| 195 |
parserDefinition *def = parserNew ("Basic"); |
| 196 |
def->kinds = BasicKinds; |
| 197 |
def->kindCount = KIND_COUNT (BasicKinds); |
| 198 |
def->extensions = extensions; |
| 199 |
def->parser = findBasicTags; |
| 200 |
return def; |
| 201 |
} |
| 202 |
|
| 203 |
/* vi:set tabstop=4 shiftwidth=4: */ |