forked from geany/geany
/
pascal.c
340 lines (313 loc) · 7.83 KB
/
pascal.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (c) 2001-2002, Darren Hiebert
*
* This source code is released for free distribution under the terms of the
* GNU General Public License version 2 or (at your option) any later version.
*
* This module contains functions for generating tags for the Pascal language,
* including some extensions for Object Pascal.
*/
/*
* 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"
/*
* DATA DEFINITIONS
*/
typedef enum {
K_FUNCTION, K_PROCEDURE
} pascalKind;
static kindDefinition PascalKinds [] = {
{ true, 'f', "function", "functions"},
{ true, 'p', "procedure", "procedures"}
};
/*
* FUNCTION DEFINITIONS
*/
static void createPascalTag (
tagEntryInfo* const tag, const vString* const name, const int kind,
const vString *arglist, const vString *vartype)
{
if (PascalKinds [kind].enabled && name != NULL && vStringLength (name) > 0)
{
initTagEntry (tag, vStringValue (name), kind);
if (arglist != NULL && !vStringIsEmpty (arglist))
{
tag->extensionFields.signature = vStringValue (arglist);
}
if (vartype && !vStringIsEmpty (vartype))
{
tag->extensionFields.typeRef[0] = "typename";
tag->extensionFields.typeRef[1] = vStringValue (vartype);
}
}
else
/* TODO: Passing NULL as name makes an assertion behind initTagEntry failure */
initTagEntry (tag, NULL, KIND_GHOST_INDEX);
}
static void makePascalTag (const tagEntryInfo* const tag)
{
if (tag->name != NULL)
makeTagEntry (tag);
}
static const unsigned char* dbp;
#define starttoken(c) (isalpha ((int) c) || (int) c == '_')
#define intoken(c) (isalnum ((int) c) || (int) c == '_' || (int) c == '.')
#define endtoken(c) (! intoken (c) && ! isdigit ((int) c))
static bool tail (const char *cp)
{
bool result = false;
register int len = 0;
while (*cp != '\0' && tolower ((int) *cp) == tolower ((int) dbp [len]))
cp++, len++;
if (*cp == '\0' && !intoken (dbp [len]))
{
dbp += len;
result = true;
}
return result;
}
static void parseArglist (const char *buf, vString *arglist, vString *vartype)
{
const char *start, *end;
int level;
if (NULL == buf || arglist == NULL)
return;
/* parse argument list which can be missing like in "function ginit:integer;" */
if (NULL != (start = strchr (buf, '(')))
{
for (level = 1, end = start + 1; level > 0; ++end)
{
if ('\0' == *end)
break;
else if ('(' == *end)
++ level;
else if (')' == *end)
-- level;
}
}
else /* if no argument list was found, continue looking for a return value */
{
start = NULL;
end = buf;
}
/* parse return type if requested by passing a non-NULL vartype argument */
if (NULL != vartype)
{
char *var, *var_start;
if (NULL != (var = strchr (end, ':')))
{
var++; /* skip ':' */
while (isspace ((int) *var))
++var;
if (starttoken (*var))
{
var_start = var;
var++;
while (intoken (*var))
var++;
if (endtoken (*var))
{
vStringNCatS (vartype, var_start, var - var_start);
}
}
}
}
if (NULL == start) /* no argument list */
vStringCatS (arglist, "()");
else
vStringNCatS (arglist, start, end - start);
}
/* Algorithm adapted from from GNU etags.
* Locates tags for procedures & functions. Doesn't do any type- or
* var-definitions. It does look for the keyword "extern" or "forward"
* immediately following the procedure statement; if found, the tag is
* skipped.
*/
static void findPascalTags (void)
{
vString *name = vStringNew ();
vString *arglist = vStringNew ();
vString *vartype = vStringNew ();
tagEntryInfo tag;
pascalKind kind = K_FUNCTION;
/* each of these flags is true iff: */
bool incomment = false; /* point is inside a comment */
int comment_char = '\0'; /* type of current comment */
bool inquote = false; /* point is inside '..' string */
bool get_tagname = false;/* point is after PROCEDURE/FUNCTION
keyword, so next item = potential tag */
bool found_tag = false; /* point is after a potential tag */
bool inparms = false; /* point is within parameter-list */
bool verify_tag = false;
/* point has passed the parm-list, so the next token will determine
* whether this is a FORWARD/EXTERN to be ignored, or whether it is a
* real tag
*/
dbp = readLineFromInputFile ();
while (dbp != NULL)
{
int c = *dbp++;
if (c == '\0') /* if end of line */
{
dbp = readLineFromInputFile ();
if (dbp == NULL || *dbp == '\0')
continue;
if (!((found_tag && verify_tag) || get_tagname))
c = *dbp++;
/* only if don't need *dbp pointing to the beginning of
* the name of the procedure or function
*/
}
if (incomment)
{
if (comment_char == '{' && c == '}')
incomment = false;
else if (comment_char == '(' && c == '*' && *dbp == ')')
{
dbp++;
incomment = false;
}
continue;
}
else if (inquote)
{
if (c == '\'')
inquote = false;
continue;
}
else switch (c)
{
case '\'':
inquote = true; /* found first quote */
continue;
case '{': /* found open { comment */
incomment = true;
comment_char = c;
continue;
case '(':
if (*dbp == '*') /* found open (* comment */
{
incomment = true;
comment_char = c;
dbp++;
}
else if (found_tag) /* found '(' after tag, i.e., parm-list */
inparms = true;
continue;
case ')': /* end of parms list */
if (inparms)
inparms = false;
continue;
case ';':
if (found_tag && !inparms) /* end of proc or fn stmt */
{
verify_tag = true;
break;
}
continue;
}
if (found_tag && verify_tag && *dbp != ' ')
{
/* check if this is an "extern" declaration */
if (*dbp == '\0')
continue;
if (tolower ((int) *dbp == 'e'))
{
if (tail ("extern")) /* superfluous, really! */
{
found_tag = false;
verify_tag = false;
}
}
else if (tolower ((int) *dbp) == 'f')
{
if (tail ("forward")) /* check for forward reference */
{
found_tag = false;
verify_tag = false;
}
}
if (found_tag && verify_tag) /* not external proc, so make tag */
{
found_tag = false;
verify_tag = false;
makePascalTag (&tag);
continue;
}
}
if (get_tagname) /* grab name of proc or fn */
{
const unsigned char *cp;
if (*dbp == '\0')
continue;
/* grab block name */
while (isspace ((int) *dbp))
++dbp;
for (cp = dbp ; *cp != '\0' && !endtoken (*cp) ; cp++)
continue;
vStringNCopyS (name, (const char*) dbp, cp - dbp);
vStringClear (arglist);
vStringClear (vartype);
parseArglist ((const char*) cp, arglist, (kind == K_FUNCTION) ? vartype : NULL);
createPascalTag (&tag, name, kind, arglist, (kind == K_FUNCTION) ? vartype : NULL);
dbp = cp; /* set dbp to e-o-token */
get_tagname = false;
found_tag = true;
/* and proceed to check for "extern" */
}
else if (!incomment && !inquote && !found_tag)
{
switch (tolower ((int) c))
{
case 'c':
if (tail ("onstructor"))
{
get_tagname = true;
kind = K_PROCEDURE;
}
break;
case 'd':
if (tail ("estructor"))
{
get_tagname = true;
kind = K_PROCEDURE;
}
break;
case 'p':
if (tail ("rocedure"))
{
get_tagname = true;
kind = K_PROCEDURE;
}
break;
case 'f':
if (tail ("unction"))
{
get_tagname = true;
kind = K_FUNCTION;
}
break;
}
} /* while not eof */
}
vStringDelete (arglist);
vStringDelete (vartype);
vStringDelete (name);
}
extern parserDefinition* PascalParser (void)
{
static const char *const extensions [] = { "p", "pas", NULL };
parserDefinition* def = parserNew ("Pascal");
def->extensions = extensions;
def->kindTable = PascalKinds;
def->kindCount = ARRAY_SIZE (PascalKinds);
def->parser = findPascalTags;
return def;
}