-
Notifications
You must be signed in to change notification settings - Fork 0
/
beta.c
321 lines (304 loc) · 8.61 KB
/
beta.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* $Id: beta.c 536 2007-06-02 06:09:00Z elliotth $
*
* Copyright (c) 1999-2000, Mjølner Informatics
*
* Written by Erik Corry <[email protected]>
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
* This module contains functions for generating tags for BETA language
* files.
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include <string.h>
#include "entry.h"
#include "parse.h"
#include "read.h"
#include "routines.h"
#include "vstring.h"
/*
* MACROS
*/
#define isbident(c) (identarray [(unsigned char) (c)])
/*
* DATA DEFINITIONS
*/
typedef enum {
K_FRAGMENT, K_PATTERN, K_SLOT, K_VIRTUAL
} betaKind;
static kindOption BetaKinds [] = {
{ TRUE, 'f', "fragment", "fragment definitions"},
{ FALSE, 'p', "pattern", "all patterns"},
{ TRUE, 's', "slot", "slots (fragment uses)"},
{ TRUE, 'v', "virtual", "patterns (virtual or rebound)"}
};
/* [A-Z_a-z0-9] */
static const char identarray [256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0-15 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16-31 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 32-47 !"#$%&'()*+'-./ */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 48-63 0123456789:;<=>? */
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 64-79 @ABCDEFGHIJKLMNO */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 80-95 PQRSTUVWXYZ [\]^_ */
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 96-111 `abcdefghijklmno */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 112-127 pqrstuvwxyz{|}~ */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 128- */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; /* -255 */
/*
* FUNCTION DEFINITIONS
*/
static void makeBetaTag (const char* const name, const betaKind kind)
{
if (BetaKinds [kind].enabled)
{
tagEntryInfo e;
initTagEntry (&e, name);
e.kindName = BetaKinds [kind].name;
e.kind = BetaKinds [kind].letter;
makeTagEntry (&e);
}
}
static void findBetaTags (void)
{
vString *line = vStringNew ();
boolean incomment = FALSE;
boolean inquote = FALSE;
boolean dovirtuals = BetaKinds [K_VIRTUAL].enabled;
boolean dopatterns = BetaKinds [K_PATTERN].enabled;
do
{
boolean foundfragmenthere = FALSE;
/* find fragment definition (line that starts and ends with --) */
int last;
int first;
int c;
vStringClear (line);
while ((c = fileGetc ()) != EOF && c != '\n' && c != '\r')
vStringPut (line, c);
vStringTerminate (line);
last = vStringLength (line) - 1;
first = 0;
/* skip white space at start and end of line */
while (last && isspace ((int) vStringChar (line, last))) last--;
while (first < last && isspace ((int) vStringChar (line, first))) first++;
/* if line still has a reasonable length and ... */
if (last - first > 4 &&
(vStringChar (line, first) == '-' &&
vStringChar (line, first + 1) == '-' &&
vStringChar (line, last) == '-' &&
vStringChar (line, last - 1) == '-'))
{
if (!incomment && !inquote)
{
foundfragmenthere = TRUE;
/* skip past -- and whitespace. Also skip back past 'dopart'
or 'attributes' to the :. We have to do this because there
is no sensible way to include whitespace in a ctags token
so the conventional space after the ':' would mess us up */
last -= 2;
first += 2;
while (last && vStringChar (line, last) != ':') last--;
while (last && (isspace ((int) vStringChar (line, last-1)))) last--;
while (first < last &&
(isspace ((int) vStringChar (line, first)) ||
vStringChar (line, first) == '-'))
first++;
/* If there's anything left it is a fragment title */
if (first < last - 1)
{
vStringChar (line, last) = 0;
if (strcasecmp ("LIB", vStringValue (line) + first) &&
strcasecmp ("PROGRAM", vStringValue (line) + first))
{
makeBetaTag (vStringValue (line) + first, K_FRAGMENT);
}
}
}
} else {
int pos = 0;
int len = vStringLength (line);
if (inquote) goto stringtext;
if (incomment) goto commenttext;
programtext:
for ( ; pos < len; pos++)
{
if (vStringChar (line, pos) == '\'')
{
pos++;
inquote = TRUE;
goto stringtext;
}
if (vStringChar (line, pos) == '{')
{
pos++;
incomment = TRUE;
goto commenttext;
}
if (vStringChar (line, pos) == '(' && pos < len - 1 &&
vStringChar (line, pos+1) == '*')
{
pos +=2;
incomment = TRUE;
goto commenttext;
}
/*
* SLOT definition looks like this:
* <<SLOT nameofslot: dopart>>
* or
* <<SLOT nameofslot: descriptor>>
*/
if (!foundfragmenthere &&
vStringChar (line, pos) == '<' &&
pos+1 < len &&
vStringChar (line, pos+1) == '<' &&
strstr (vStringValue (line) + pos, ">>"))
{
/* Found slot name, get start and end */
int eoname;
char c2;
pos += 2; /* skip past << */
/* skip past space before SLOT */
while (pos < len && isspace ((int) vStringChar (line, pos)))
pos++;
/* skip past SLOT */
if (pos+4 <= len &&
!strncasecmp (vStringValue(line) + pos, "SLOT", (size_t)4))
pos += 4;
/* skip past space after SLOT */
while (pos < len && isspace ((int) vStringChar (line, pos)))
pos++;
eoname = pos;
/* skip to end of name */
while (eoname < len &&
(c2 = vStringChar (line, eoname)) != '>' &&
c2 != ':' &&
!isspace ((int) c2))
eoname++;
if (eoname < len)
{
vStringChar (line, eoname) = 0;
if (strcasecmp ("LIB", vStringValue (line) + pos) &&
strcasecmp ("PROGRAM", vStringValue (line) + pos) &&
strcasecmp ("SLOT", vStringValue (line) + pos))
{
makeBetaTag (vStringValue (line) + pos, K_SLOT);
}
}
if (eoname+1 < len) {
pos = eoname + 1;
} else {
pos = len;
continue;
}
}
/* Only patterns that are virtual, extensions of virtuals or
* final bindings are normally included so as not to overload
* totally.
* That means one of the forms name:: name:< or name::<
*/
if (!foundfragmenthere &&
vStringChar (line, pos) == ':' &&
(dopatterns ||
(dovirtuals &&
(vStringChar (line, pos+1) == ':' ||
vStringChar (line, pos+1) == '<')
)
)
)
{
/* Found pattern name, get start and end */
int eoname = pos;
int soname;
while (eoname && isspace ((int) vStringChar (line, eoname-1)))
eoname--;
foundanothername:
/* terminate right after name */
vStringChar (line, eoname) = 0;
soname = eoname;
while (soname &&
isbident (vStringChar (line, soname-1)))
{
soname--;
}
if (soname != eoname)
{
makeBetaTag (vStringValue (line) + soname, K_PATTERN);
/* scan back past white space */
while (soname &&
isspace ((int) vStringChar (line, soname-1)))
soname--;
if (soname && vStringChar (line, soname-1) == ',')
{
/* we found a new pattern name before comma */
eoname = soname;
goto foundanothername;
}
}
}
}
goto endofline;
commenttext:
for ( ; pos < len; pos++)
{
if (vStringChar (line, pos) == '*' && pos < len - 1 &&
vStringChar (line, pos+1) == ')')
{
pos += 2;
incomment = FALSE;
goto programtext;
}
if (vStringChar (line, pos) == '}')
{
pos++;
incomment = FALSE;
goto programtext;
}
}
goto endofline;
stringtext:
for ( ; pos < len; pos++)
{
if (vStringChar (line, pos) == '\\')
{
if (pos < len - 1) pos++;
}
else if (vStringChar (line, pos) == '\'')
{
pos++;
/* support obsolete '' syntax */
if (pos < len && vStringChar (line, pos) == '\'')
{
continue;
}
inquote = FALSE;
goto programtext;
}
}
}
endofline:
inquote = FALSE; /* This shouldn't really make a difference */
} while (!feof (File.fp));
vStringDelete (line);
}
extern parserDefinition* BetaParser (void)
{
static const char *const extensions [] = { "bet", NULL };
parserDefinition* def = parserNew ("BETA");
def->kinds = BetaKinds;
def->kindCount = KIND_COUNT (BetaKinds);
def->extensions = extensions;
def->parser = findBetaTags;
return def;
}
/* vi:set tabstop=4 shiftwidth=4: */