2 * Copyright (c) 2001-2002, Darren Hiebert
4 * This source code is released for free distribution under the terms of the
5 * GNU General Public License version 2 or (at your option) any later version.
7 * This module contains functions for generating tags for the Pascal language,
8 * including some extensions for Object Pascal.
14 #include "general.h" /* must always come first */
28 K_FUNCTION
, K_PROCEDURE
31 static kindOption PascalKinds
[] = {
32 { true, 'f', "function", "functions"},
33 { true, 'p', "procedure", "procedures"}
37 * FUNCTION DEFINITIONS
40 static void createPascalTag (tagEntryInfo
* const tag
,
41 const vString
* const name
, const int kind
,
42 const char *arglist
, const char *vartype
)
44 if (PascalKinds
[kind
].enabled
&& name
!= NULL
&& vStringLength (name
) > 0)
46 initTagEntry (tag
, vStringValue (name
), &(PascalKinds
[kind
]));
48 tag
->extensionFields
.signature
= arglist
;
49 tag
->extensionFields
.varType
= vartype
;
52 /* TODO: Passing NULL as name makes an assertion behind initTagEntry failure */
53 initTagEntry (tag
, NULL
, NULL
);
56 static void makePascalTag (const tagEntryInfo
* const tag
)
58 if (tag
->name
!= NULL
)
62 static const unsigned char* dbp
;
64 #define starttoken(c) (isalpha ((int) c) || (int) c == '_')
65 #define intoken(c) (isalnum ((int) c) || (int) c == '_' || (int) c == '.')
66 #define endtoken(c) (! intoken (c) && ! isdigit ((int) c))
68 static bool tail (const char *cp
)
73 while (*cp
!= '\0' && tolower ((int) *cp
) == tolower ((int) dbp
[len
]))
75 if (*cp
== '\0' && !intoken (dbp
[len
]))
83 static void parseArglist(const char *buf
, char **arglist
, char **vartype
)
85 char *c
, *start
, *end
;
88 if (NULL
== buf
|| NULL
== arglist
)
92 /* parse argument list which can be missing like in "function ginit:integer;" */
93 if (NULL
!= (start
= strchr(c
, '(')))
95 for (level
= 1, end
= start
+ 1; level
> 0; ++end
)
101 else if (')' == *end
)
105 else /* if no argument list was found, continue looking for a return value */
111 /* parse return type if requested by passing a non-NULL vartype argument */
114 char *var
, *var_start
;
118 if (NULL
!= (var
= strchr(end
, ':')))
120 var
++; /* skip ':' */
121 while (isspace((int) *var
))
124 if (starttoken(*var
))
128 while (intoken(*var
))
133 *vartype
= strdup(var_start
);
140 *arglist
= strdup(start
);
146 /* Algorithm adapted from from GNU etags.
147 * Locates tags for procedures & functions. Doesn't do any type- or
148 * var-definitions. It does look for the keyword "extern" or "forward"
149 * immediately following the procedure statement; if found, the tag is
152 static void findPascalTags (void)
154 vString
*name
= vStringNew ();
156 char *arglist
= NULL
;
157 char *vartype
= NULL
;
158 pascalKind kind
= K_FUNCTION
;
159 /* each of these flags is true iff: */
160 bool incomment
= false; /* point is inside a comment */
161 int comment_char
= '\0'; /* type of current comment */
162 bool inquote
= false; /* point is inside '..' string */
163 bool get_tagname
= false;/* point is after PROCEDURE/FUNCTION
164 keyword, so next item = potential tag */
165 bool found_tag
= false; /* point is after a potential tag */
166 bool inparms
= false; /* point is within parameter-list */
167 bool verify_tag
= false;
168 /* point has passed the parm-list, so the next token will determine
169 * whether this is a FORWARD/EXTERN to be ignored, or whether it is a
173 dbp
= readLineFromInputFile ();
178 if (c
== '\0') /* if end of line */
180 dbp
= readLineFromInputFile ();
181 if (dbp
== NULL
|| *dbp
== '\0')
183 if (!((found_tag
&& verify_tag
) || get_tagname
))
185 /* only if don't need *dbp pointing to the beginning of
186 * the name of the procedure or function
191 if (comment_char
== '{' && c
== '}')
193 else if (comment_char
== '(' && c
== '*' && *dbp
== ')')
209 inquote
= true; /* found first quote */
211 case '{': /* found open { comment */
216 if (*dbp
== '*') /* found open (* comment */
222 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
225 case ')': /* end of parms list */
230 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
237 if (found_tag
&& verify_tag
&& *dbp
!= ' ')
239 /* check if this is an "extern" declaration */
242 if (tolower ((int) *dbp
== 'e'))
244 if (tail ("extern")) /* superfluous, really! */
250 else if (tolower ((int) *dbp
) == 'f')
252 if (tail ("forward")) /* check for forward reference */
258 else if (tolower ((int) *dbp
) == 't')
260 if (tail ("type")) /* check for forward reference */
266 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
270 makePascalTag (&tag
);
274 if (get_tagname
) /* grab name of proc or fn */
276 const unsigned char *cp
;
281 /* grab block name */
282 while (isspace ((int) *dbp
))
284 for (cp
= dbp
; *cp
!= '\0' && !endtoken (*cp
) ; cp
++)
286 vStringNCopyS (name
, (const char*) dbp
, cp
- dbp
);
289 if (kind
== K_FUNCTION
&& vartype
!= NULL
)
291 parseArglist((const char*) cp
, &arglist
, (kind
== K_FUNCTION
) ? &vartype
: NULL
);
292 createPascalTag (&tag
, name
, kind
, arglist
, (kind
== K_FUNCTION
) ? vartype
: NULL
);
293 dbp
= cp
; /* set dbp to e-o-token */
296 /* and proceed to check for "extern" */
298 else if (!incomment
&& !inquote
&& !found_tag
)
300 switch (tolower ((int) c
))
303 if (tail ("onstructor"))
310 if (tail ("estructor"))
317 if (tail ("rocedure"))
324 if (tail ("unction"))
338 } /* while not eof */
344 vStringDelete (name
);
347 extern parserDefinition
* PascalParser (void)
349 static const char *const extensions
[] = { "p", "pas", NULL
};
350 parserDefinition
* def
= parserNew ("Pascal");
351 def
->extensions
= extensions
;
352 def
->kinds
= PascalKinds
;
353 def
->kindCount
= ARRAY_SIZE (PascalKinds
);
354 def
->parser
= findPascalTags
;