Fix one-off leak by allocating PropertyDialogElements on the stack
[geany-mirror.git] / tagmanager / c.c
blobcc55085efe4551783a14d0d98d6c68b76bb95526
1 /*
3 * Copyright (c) 1996-2001, Darren Hiebert
5 * This source code is released for free distribution under the terms of the
6 * GNU General Public License.
8 * This module contains functions for parsing and scanning C, C++, D and Java
9 * source files.
13 * INCLUDE FILES
15 #include "general.h" /* must always come first */
17 #include <string.h>
18 #include <setjmp.h>
19 #include <mio/mio.h>
21 #include "entry.h"
22 #include "get.h"
23 #include "keyword.h"
24 #include "main.h"
25 #include "options.h"
26 #include "parse.h"
27 #include "read.h"
30 * MACROS
33 #define activeToken(st) ((st)->token [(int) (st)->tokenIndex])
34 #define parentDecl(st) ((st)->parent == NULL ? \
35 DECL_NONE : (st)->parent->declaration)
36 #define isType(token,t) (boolean) ((token)->type == (t))
37 #define insideEnumBody(st) (boolean) ((st)->parent == NULL ? FALSE : \
38 ((st)->parent->declaration == DECL_ENUM))
39 #define isExternCDecl(st,c) (boolean) ((c) == STRING_SYMBOL && \
40 ! (st)->haveQualifyingName && \
41 (st)->scope == SCOPE_EXTERN)
43 #define isOneOf(c,s) (boolean) (strchr ((s), (c)) != NULL)
46 * DATA DECLARATIONS
49 enum { NumTokens = 12 };
51 typedef enum eException
53 ExceptionNone, ExceptionEOF, ExceptionFormattingError,
54 ExceptionBraceFormattingError
55 } exception_t;
57 /* Used to specify type of keyword.
59 typedef enum eKeywordId
61 KEYWORD_NONE = -1,
62 KEYWORD_ATTRIBUTE, KEYWORD_ABSTRACT, KEYWORD_ALIAS,
63 KEYWORD_BOOLEAN, KEYWORD_BYTE, KEYWORD_BAD_STATE, KEYWORD_BAD_TRANS,
64 KEYWORD_BIND, KEYWORD_BIND_VAR, KEYWORD_BIT, KEYWORD_BODY,
65 KEYWORD_CASE, KEYWORD_CATCH, KEYWORD_CHAR, KEYWORD_CLASS, KEYWORD_CONST,
66 KEYWORD_CONSTRAINT, KEYWORD_COVERAGE_BLOCK, KEYWORD_COVERAGE_DEF,
67 KEYWORD_DEFAULT, KEYWORD_DELEGATE, KEYWORD_DELETE, KEYWORD_DO,
68 KEYWORD_DOUBLE,
69 KEYWORD_ELSE, KEYWORD_ENUM, KEYWORD_EXPLICIT, KEYWORD_EXTERN,
70 KEYWORD_EXTENDS, KEYWORD_EVENT,
71 KEYWORD_FINAL, KEYWORD_FINALLY, KEYWORD_FLOAT, KEYWORD_FOR, KEYWORD_FRIEND, KEYWORD_FUNCTION,
72 KEYWORD_GET, KEYWORD_GOTO,
73 KEYWORD_IF, KEYWORD_IMPLEMENTS, KEYWORD_IMPORT, KEYWORD_IN, KEYWORD_INLINE, KEYWORD_INT,
74 KEYWORD_INOUT, KEYWORD_INPUT, KEYWORD_INTEGER, KEYWORD_INTERFACE,
75 KEYWORD_INTERNAL,
76 KEYWORD_LOCAL, KEYWORD_LONG,
77 KEYWORD_M_BAD_STATE, KEYWORD_M_BAD_TRANS, KEYWORD_M_STATE, KEYWORD_M_TRANS,
78 KEYWORD_MODULE, KEYWORD_MUTABLE,
79 KEYWORD_NAMESPACE, KEYWORD_NEW, KEYWORD_NEWCOV, KEYWORD_NATIVE,
80 KEYWORD_OPERATOR, KEYWORD_OUT, KEYWORD_OUTPUT, KEYWORD_OVERLOAD, KEYWORD_OVERRIDE,
81 KEYWORD_PACKED, KEYWORD_PORT, KEYWORD_PACKAGE, KEYWORD_PRIVATE,
82 KEYWORD_PROGRAM, KEYWORD_PROTECTED, KEYWORD_PUBLIC,
83 KEYWORD_REF, KEYWORD_REGISTER, KEYWORD_RETURN,
84 KEYWORD_SHADOW, KEYWORD_STATE,
85 KEYWORD_SET, KEYWORD_SHORT, KEYWORD_SIGNAL, KEYWORD_SIGNED, KEYWORD_SIZE_T, KEYWORD_STATIC, KEYWORD_STRING,
86 KEYWORD_STRUCT, KEYWORD_SWITCH, KEYWORD_SYNCHRONIZED,
87 KEYWORD_TASK, KEYWORD_TEMPLATE, KEYWORD_THIS, KEYWORD_THROW,
88 KEYWORD_THROWS, KEYWORD_TRANSIENT, KEYWORD_TRANS, KEYWORD_TRANSITION,
89 KEYWORD_TRY, KEYWORD_TYPEDEF, KEYWORD_TYPENAME,
90 KEYWORD_UINT, KEYWORD_ULONG, KEYWORD_UNION, KEYWORD_UNSIGNED, KEYWORD_USHORT,
91 KEYWORD_USING,
92 KEYWORD_VIRTUAL, KEYWORD_VOID, KEYWORD_VOLATILE,
93 KEYWORD_WCHAR_T, KEYWORD_WEAK, KEYWORD_WHILE
94 } keywordId;
96 /* Used to determine whether keyword is valid for the current language and
97 * what its ID is.
99 typedef struct sKeywordDesc
101 const char *name;
102 keywordId id;
103 short isValid [7]; /* indicates languages for which kw is valid */
104 } keywordDesc;
106 /* Used for reporting the type of object parsed by nextToken ().
108 typedef enum eTokenType
110 TOKEN_NONE, /* none */
111 TOKEN_ARGS, /* a parenthetical pair and its contents */
112 TOKEN_BRACE_CLOSE,
113 TOKEN_BRACE_OPEN,
114 TOKEN_COMMA, /* the comma character */
115 TOKEN_DOUBLE_COLON, /* double colon indicates nested-name-specifier */
116 TOKEN_KEYWORD,
117 TOKEN_NAME, /* an unknown name */
118 TOKEN_PACKAGE, /* a Java package name */
119 TOKEN_PAREN_NAME, /* a single name in parentheses */
120 TOKEN_SEMICOLON, /* the semicolon character */
121 TOKEN_SPEC, /* a storage class specifier, qualifier, type, etc. */
122 TOKEN_STAR, /* pointer detection */
123 TOKEN_ARRAY, /* array detection */
124 TOKEN_COUNT
125 } tokenType;
127 /* This describes the scoping of the current statement.
129 typedef enum eTagScope
131 SCOPE_GLOBAL, /* no storage class specified */
132 SCOPE_STATIC, /* static storage class */
133 SCOPE_EXTERN, /* external storage class */
134 SCOPE_FRIEND, /* declares access only */
135 SCOPE_TYPEDEF, /* scoping depends upon context */
136 SCOPE_COUNT
137 } tagScope;
139 typedef enum eDeclaration
141 DECL_NONE,
142 DECL_BASE, /* base type (default) */
143 DECL_CLASS,
144 DECL_ENUM,
145 DECL_EVENT,
146 DECL_SIGNAL,
147 DECL_FUNCTION,
148 DECL_FUNCTION_TEMPLATE,
149 DECL_IGNORE, /* non-taggable "declaration" */
150 DECL_INTERFACE,
151 DECL_MODULE,
152 DECL_NAMESPACE,
153 DECL_NOMANGLE, /* C++ name demangling block */
154 DECL_PACKAGE,
155 DECL_STRUCT,
156 DECL_UNION,
157 DECL_COUNT
158 } declType;
160 typedef enum eVisibilityType
162 ACCESS_UNDEFINED,
163 ACCESS_PRIVATE,
164 ACCESS_PROTECTED,
165 ACCESS_PUBLIC,
166 ACCESS_DEFAULT, /* Java-specific */
167 ACCESS_COUNT
168 } accessType;
170 /* Information about the parent class of a member (if any).
172 typedef struct sMemberInfo
174 accessType access; /* access of current statement */
175 accessType accessDefault; /* access default for current statement */
176 } memberInfo;
178 typedef struct sTokenInfo
180 tokenType type;
181 keywordId keyword;
182 vString* name; /* the name of the token */
183 unsigned long lineNumber; /* line number of tag */
184 MIOPos filePosition; /* file position of line containing name */
185 } tokenInfo;
187 typedef enum eImplementation
189 IMP_DEFAULT,
190 IMP_ABSTRACT,
191 IMP_VIRTUAL,
192 IMP_PURE_VIRTUAL,
193 IMP_COUNT
194 } impType;
196 /* Describes the statement currently undergoing analysis.
198 typedef struct sStatementInfo
200 tagScope scope;
201 declType declaration; /* specifier associated with TOKEN_SPEC */
202 boolean gotName; /* was a name parsed yet? */
203 boolean haveQualifyingName; /* do we have a name we are considering? */
204 boolean gotParenName; /* was a name inside parentheses parsed yet? */
205 boolean gotArgs; /* was a list of parameters parsed yet? */
206 impType implementation; /* abstract or concrete implementation? */
207 unsigned int tokenIndex; /* currently active token */
208 tokenInfo* token [((int) NumTokens)];
209 tokenInfo* context; /* accumulated scope of current statement */
210 tokenInfo* blockName; /* name of current block */
211 memberInfo member; /* information regarding parent class/struct */
212 vString* parentClasses; /* parent classes */
213 struct sStatementInfo *parent; /* statement we are nested within */
214 long argEndPosition; /* Position where argument list ended */
215 tokenInfo* firstToken; /* First token in the statement */
216 } statementInfo;
218 /* Describes the type of tag being generated.
220 typedef enum eTagType
222 TAG_UNDEFINED,
223 TAG_CLASS, /* class name */
224 TAG_ENUM, /* enumeration name */
225 TAG_ENUMERATOR, /* enumerator (enumeration value) */
226 TAG_FIELD, /* field (Java) */
227 TAG_FUNCTION, /* function definition */
228 TAG_INTERFACE, /* interface declaration */
229 TAG_MEMBER, /* structure, class or interface member */
230 TAG_METHOD, /* method declaration */
231 TAG_NAMESPACE, /* namespace name */
232 TAG_PACKAGE, /* package name */
233 TAG_PROTOTYPE, /* function prototype or declaration */
234 TAG_STRUCT, /* structure name */
235 TAG_TYPEDEF, /* typedef name */
236 TAG_UNION, /* union name */
237 TAG_VARIABLE, /* variable definition */
238 TAG_EXTERN_VAR, /* external variable declaration */
239 TAG_MACRO, /* #define s */
240 TAG_EVENT, /* event */
241 TAG_SIGNAL, /* signal */
242 TAG_LOCAL, /* local variable definition */
243 TAG_PROPERTY, /* property name */
244 TAG_COUNT /* must be last */
245 } tagType;
247 typedef struct sParenInfo
249 boolean isParamList;
250 boolean isKnrParamList;
251 boolean isNameCandidate;
252 boolean invalidContents;
253 boolean nestedArgs;
254 unsigned int parameterCount;
255 } parenInfo;
258 * DATA DEFINITIONS
261 static jmp_buf Exception;
263 static langType Lang_c;
264 static langType Lang_cpp;
265 static langType Lang_csharp;
266 static langType Lang_java;
267 static langType Lang_d;
268 static langType Lang_glsl;
269 static langType Lang_ferite;
270 static langType Lang_vala;
272 /* Used to index into the CKinds table. */
273 typedef enum
275 CK_UNDEFINED = -1,
276 CK_CLASS, CK_DEFINE, CK_ENUMERATOR, CK_FUNCTION,
277 CK_ENUMERATION, CK_MEMBER, CK_NAMESPACE, CK_PROTOTYPE,
278 CK_STRUCT, CK_TYPEDEF, CK_UNION, CK_VARIABLE,
279 CK_EXTERN_VARIABLE
280 } cKind;
282 static kindOption CKinds [] = {
283 { TRUE, 'c', "class", "classes"},
284 { TRUE, 'd', "macro", "macro definitions"},
285 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
286 { TRUE, 'f', "function", "function definitions"},
287 { TRUE, 'g', "enum", "enumeration names"},
288 { TRUE, 'm', "member", "class, struct, and union members"},
289 { TRUE, 'n', "namespace", "namespaces"},
290 { FALSE, 'p', "prototype", "function prototypes"},
291 { TRUE, 's', "struct", "structure names"},
292 { TRUE, 't', "typedef", "typedefs"},
293 { TRUE, 'u', "union", "union names"},
294 { TRUE, 'v', "variable", "variable definitions"},
295 { FALSE, 'x', "externvar", "external variable declarations"},
298 /* Used to index into the DKinds table. */
299 typedef enum
301 DK_UNDEFINED = -1,
302 DK_CLASS, DK_ENUMERATOR, DK_FUNCTION,
303 DK_ENUMERATION, DK_INTERFACE, DK_MEMBER, DK_NAMESPACE, DK_PROTOTYPE,
304 DK_STRUCT, DK_TYPEDEF, DK_UNION, DK_VARIABLE,
305 DK_EXTERN_VARIABLE
306 } dKind;
308 static kindOption DKinds [] = {
309 { TRUE, 'c', "class", "classes"},
310 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
311 { TRUE, 'f', "function", "function definitions"},
312 { TRUE, 'g', "enum", "enumeration names"},
313 { TRUE, 'i', "interface", "interfaces"},
314 { TRUE, 'm', "member", "class, struct, and union members"},
315 { TRUE, 'n', "namespace", "namespaces"},
316 { FALSE, 'p', "prototype", "function prototypes"},
317 { TRUE, 's', "struct", "structure names"},
318 { TRUE, 't', "typedef", "typedefs"},
319 { TRUE, 'u', "union", "union names"},
320 { TRUE, 'v', "variable", "variable definitions"},
321 { FALSE, 'x', "externvar", "external variable declarations"},
324 /* Used to index into the JavaKinds table. */
325 typedef enum
327 JK_UNDEFINED = -1,
328 JK_CLASS, JK_FIELD, JK_INTERFACE, JK_METHOD,
329 JK_PACKAGE
330 } javaKind;
332 static kindOption JavaKinds [] = {
333 { TRUE, 'c', "class", "classes"},
334 { TRUE, 'f', "field", "fields"},
335 { TRUE, 'i', "interface", "interfaces"},
336 { TRUE, 'm', "method", "methods"},
337 { TRUE, 'p', "package", "packages"},
340 typedef enum
342 CSK_UNDEFINED = -1,
343 CSK_CLASS, CSK_DEFINE, CSK_ENUMERATOR, CSK_EVENT, CSK_FIELD,
344 CSK_ENUMERATION, CSK_INTERFACE, CSK_LOCAL, CSK_METHOD,
345 CSK_NAMESPACE, CSK_PROPERTY, CSK_STRUCT, CSK_TYPEDEF
346 } csharpKind;
348 static kindOption CsharpKinds [] = {
349 { TRUE, 'c', "class", "classes"},
350 { TRUE, 'd', "macro", "macro definitions"},
351 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
352 { TRUE, 'E', "event", "events"},
353 { TRUE, 'f', "field", "fields"},
354 { TRUE, 'g', "enum", "enumeration names"},
355 { TRUE, 'i', "interface", "interfaces"},
356 { FALSE, 'l', "local", "local variables"},
357 { TRUE, 'm', "method", "methods"},
358 { TRUE, 'n', "namespace", "namespaces"},
359 { TRUE, 'p', "property", "properties"},
360 { TRUE, 's', "struct", "structure names"},
361 { TRUE, 't', "typedef", "typedefs"},
364 typedef enum {
365 VK_UNDEFINED = -1,
366 VK_CLASS, VK_DEFINE, VK_ENUMERATOR, VK_FIELD,
367 VK_ENUMERATION, VK_INTERFACE, VK_LOCAL, VK_METHOD,
368 VK_NAMESPACE, VK_PROPERTY, VK_SIGNAL, VK_STRUCT
369 } valaKind;
371 static kindOption ValaKinds [] = {
372 { TRUE, 'c', "class", "classes"},
373 { TRUE, 'd', "macro", "macro definitions"},
374 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
375 { TRUE, 'f', "field", "fields"},
376 { TRUE, 'g', "enum", "enumeration names"},
377 { TRUE, 'i', "interface", "interfaces"},
378 { FALSE, 'l', "local", "local variables"},
379 { TRUE, 'm', "method", "methods"},
380 { TRUE, 'n', "namespace", "namespaces"},
381 { TRUE, 'p', "property", "properties"},
382 { TRUE, 'S', "signal", "signals"},
383 { TRUE, 's', "struct", "structure names"},
386 static const keywordDesc KeywordTable [] = {
387 /* C++ */
388 /* ANSI C | C# Java */
389 /* | | | | Vera */
390 /* | | | | | Vala */
391 /* | | | | | | D */
392 /* keyword keyword ID | | | | | | | */
393 { "__attribute__", KEYWORD_ATTRIBUTE, { 1, 1, 1, 0, 0, 0, 1 } },
394 { "abstract", KEYWORD_ABSTRACT, { 0, 0, 1, 1, 0, 1, 1 } },
395 { "alias", KEYWORD_TYPEDEF, { 0, 0, 0, 0, 0, 0, 1 } }, /* handle like typedef */
396 { "bad_state", KEYWORD_BAD_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
397 { "bad_trans", KEYWORD_BAD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
398 { "bind", KEYWORD_BIND, { 0, 0, 0, 0, 1, 0, 0 } },
399 { "bind_var", KEYWORD_BIND_VAR, { 0, 0, 0, 0, 1, 0, 0 } },
400 { "bit", KEYWORD_BIT, { 0, 0, 0, 0, 1, 0, 0 } },
401 { "body", KEYWORD_BODY, { 0, 0, 0, 0, 0, 0, 1 } },
402 { "boolean", KEYWORD_BOOLEAN, { 0, 0, 0, 1, 0, 0, 0 } },
403 { "byte", KEYWORD_BYTE, { 0, 0, 0, 1, 0, 0, 1 } },
404 { "case", KEYWORD_CASE, { 1, 1, 1, 1, 0, 1, 1 } },
405 { "catch", KEYWORD_CATCH, { 0, 1, 1, 0, 0, 1, 1 } },
406 { "char", KEYWORD_CHAR, { 1, 1, 1, 1, 0, 1, 1 } },
407 { "class", KEYWORD_CLASS, { 0, 1, 1, 1, 1, 1, 1 } },
408 { "const", KEYWORD_CONST, { 1, 1, 1, 1, 0, 1, 1 } },
409 { "constraint", KEYWORD_CONSTRAINT, { 0, 0, 0, 0, 1, 0, 0 } },
410 { "coverage_block", KEYWORD_COVERAGE_BLOCK, { 0, 0, 0, 0, 1, 0, 0 } },
411 { "coverage_def", KEYWORD_COVERAGE_DEF, { 0, 0, 0, 0, 1, 0, 0 } },
412 { "do", KEYWORD_DO, { 1, 1, 1, 1, 0, 1, 1 } },
413 { "default", KEYWORD_DEFAULT, { 1, 1, 1, 1, 0, 1, 1 } },
414 { "delegate", KEYWORD_DELEGATE, { 0, 0, 1, 0, 0, 1, 1 } },
415 { "delete", KEYWORD_DELETE, { 0, 1, 0, 0, 0, 1, 1 } },
416 { "double", KEYWORD_DOUBLE, { 1, 1, 1, 1, 0, 1, 1 } },
417 { "else", KEYWORD_ELSE, { 1, 1, 0, 1, 0, 1, 1 } },
418 { "ensures", KEYWORD_ATTRIBUTE, { 0, 0, 0, 0, 0, 1, 0 } }, /* ignore */
419 { "enum", KEYWORD_ENUM, { 1, 1, 1, 1, 1, 1, 1 } },
420 { "errordomain", KEYWORD_ENUM, { 0, 0, 0, 0, 0, 1, 0 } }, /* errordomain behaves like enum */
421 { "event", KEYWORD_EVENT, { 0, 0, 1, 0, 1, 0, 0 } },
422 { "explicit", KEYWORD_EXPLICIT, { 0, 1, 1, 0, 0, 0, 1 } },
423 { "extends", KEYWORD_EXTENDS, { 0, 0, 0, 1, 1, 0, 0 } },
424 { "extern", KEYWORD_EXTERN, { 1, 1, 1, 0, 1, 1, 0 } },
425 { "extern", KEYWORD_NAMESPACE, { 0, 0, 0, 0, 0, 0, 1 } }, /* parse block */
426 { "final", KEYWORD_FINAL, { 0, 0, 0, 1, 0, 0, 1 } },
427 { "finally", KEYWORD_FINALLY, { 0, 0, 0, 0, 0, 1, 1 } },
428 { "float", KEYWORD_FLOAT, { 1, 1, 1, 1, 0, 1, 1 } },
429 { "for", KEYWORD_FOR, { 1, 1, 1, 1, 0, 1, 1 } },
430 { "friend", KEYWORD_FRIEND, { 0, 1, 0, 0, 0, 0, 0 } },
431 { "function", KEYWORD_FUNCTION, { 0, 0, 0, 0, 1, 0, 1 } },
432 { "get", KEYWORD_GET, { 0, 0, 0, 0, 0, 1, 0 } },
433 { "goto", KEYWORD_GOTO, { 1, 1, 1, 1, 0, 1, 1 } },
434 { "if", KEYWORD_IF, { 1, 1, 1, 1, 0, 1, 1 } },
435 { "implements", KEYWORD_IMPLEMENTS, { 0, 0, 0, 1, 0, 0, 0 } },
436 { "import", KEYWORD_IMPORT, { 0, 0, 0, 1, 0, 0, 1 } },
437 { "inline", KEYWORD_INLINE, { 0, 1, 0, 0, 0, 1, 0 } },
438 { "in", KEYWORD_IN, { 0, 0, 0, 0, 0, 0, 1 } },
439 { "inout", KEYWORD_INOUT, { 0, 0, 0, 0, 1, 0, 1 } },
440 { "input", KEYWORD_INPUT, { 0, 0, 0, 0, 1, 0, 0 } },
441 { "int", KEYWORD_INT, { 1, 1, 1, 1, 0, 1, 1 } },
442 { "integer", KEYWORD_INTEGER, { 0, 0, 0, 0, 1, 0, 0 } },
443 { "interface", KEYWORD_INTERFACE, { 0, 0, 1, 1, 1, 1, 1 } },
444 { "internal", KEYWORD_INTERNAL, { 0, 0, 1, 0, 0, 0, 0 } },
445 { "local", KEYWORD_LOCAL, { 0, 0, 0, 0, 1, 0, 0 } },
446 { "long", KEYWORD_LONG, { 1, 1, 1, 1, 0, 1, 1 } },
447 { "m_bad_state", KEYWORD_M_BAD_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
448 { "m_bad_trans", KEYWORD_M_BAD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
449 { "m_state", KEYWORD_M_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
450 { "m_trans", KEYWORD_M_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
451 { "mutable", KEYWORD_MUTABLE, { 0, 1, 0, 0, 0, 0, 0 } },
452 { "module", KEYWORD_MODULE, { 0, 0, 0, 0, 0, 0, 1 } },
453 { "namespace", KEYWORD_NAMESPACE, { 0, 1, 1, 0, 0, 1, 0 } },
454 { "native", KEYWORD_NATIVE, { 0, 0, 0, 1, 0, 0, 0 } },
455 { "new", KEYWORD_NEW, { 0, 1, 1, 1, 0, 1, 1 } },
456 { "newcov", KEYWORD_NEWCOV, { 0, 0, 0, 0, 1, 0, 0 } },
457 { "operator", KEYWORD_OPERATOR, { 0, 1, 1, 0, 0, 0, 0 } },
458 { "out", KEYWORD_OUT, { 0, 0, 0, 0, 0, 1, 1 } },
459 { "output", KEYWORD_OUTPUT, { 0, 0, 0, 0, 1, 0, 0 } },
460 { "overload", KEYWORD_OVERLOAD, { 0, 1, 0, 0, 0, 0, 0 } },
461 { "override", KEYWORD_OVERRIDE, { 0, 0, 1, 0, 0, 1, 1 } },
462 { "package", KEYWORD_PACKAGE, { 0, 0, 0, 1, 0, 0, 1 } },
463 { "packed", KEYWORD_PACKED, { 0, 0, 0, 0, 1, 0, 0 } },
464 { "port", KEYWORD_PORT, { 0, 0, 0, 0, 1, 0, 0 } },
465 { "private", KEYWORD_PRIVATE, { 0, 1, 1, 1, 0, 1, 1 } },
466 { "program", KEYWORD_PROGRAM, { 0, 0, 0, 0, 1, 0, 0 } },
467 { "protected", KEYWORD_PROTECTED, { 0, 1, 1, 1, 1, 1, 1 } },
468 { "public", KEYWORD_PUBLIC, { 0, 1, 1, 1, 1, 1, 1 } },
469 { "ref", KEYWORD_REF, { 0, 0, 0, 0, 0, 1, 1 } },
470 { "register", KEYWORD_REGISTER, { 1, 1, 0, 0, 0, 0, 0 } },
471 { "requires", KEYWORD_ATTRIBUTE, { 0, 0, 0, 0, 0, 1, 0 } }, /* ignore */
472 { "return", KEYWORD_RETURN, { 1, 1, 1, 1, 0, 1, 1 } },
473 { "set", KEYWORD_SET, { 0, 0, 0, 0, 0, 1, 0 } },
474 { "shadow", KEYWORD_SHADOW, { 0, 0, 0, 0, 1, 0, 0 } },
475 { "short", KEYWORD_SHORT, { 1, 1, 1, 1, 0, 1, 1 } },
476 { "signal", KEYWORD_SIGNAL, { 0, 0, 0, 0, 0, 1, 0 } },
477 { "signed", KEYWORD_SIGNED, { 1, 1, 0, 0, 0, 0, 0 } },
478 { "size_t", KEYWORD_SIZE_T, { 1, 1, 0, 0, 0, 1, 1 } },
479 { "state", KEYWORD_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
480 { "static", KEYWORD_STATIC, { 1, 1, 1, 1, 1, 1, 1 } },
481 { "string", KEYWORD_STRING, { 0, 0, 1, 0, 1, 1, 0 } },
482 { "struct", KEYWORD_STRUCT, { 1, 1, 1, 0, 0, 1, 1 } },
483 { "switch", KEYWORD_SWITCH, { 1, 1, 1, 1, 0, 1, 1 } },
484 { "synchronized", KEYWORD_SYNCHRONIZED, { 0, 0, 0, 1, 0, 0, 1 } },
485 { "task", KEYWORD_TASK, { 0, 0, 0, 0, 1, 0, 0 } },
486 { "template", KEYWORD_TEMPLATE, { 0, 1, 0, 0, 0, 0, 0 } },
487 { "template", KEYWORD_NAMESPACE, { 0, 0, 0, 0, 0, 0, 1 } }, /* parse block */
488 { "this", KEYWORD_THIS, { 0, 0, 1, 1, 0, 1, 0 } }, /* 0 to allow D ctor tags */
489 { "throw", KEYWORD_THROW, { 0, 1, 1, 1, 0, 1, 1 } },
490 { "throws", KEYWORD_THROWS, { 0, 0, 0, 1, 0, 1, 0 } },
491 { "trans", KEYWORD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
492 { "transition", KEYWORD_TRANSITION, { 0, 0, 0, 0, 1, 0, 0 } },
493 { "transient", KEYWORD_TRANSIENT, { 0, 0, 0, 1, 0, 0, 0 } },
494 { "try", KEYWORD_TRY, { 0, 1, 1, 0, 0, 1, 1 } },
495 { "typedef", KEYWORD_TYPEDEF, { 1, 1, 1, 0, 1, 0, 1 } },
496 { "typename", KEYWORD_TYPENAME, { 0, 1, 0, 0, 0, 0, 0 } },
497 { "uint", KEYWORD_UINT, { 0, 0, 1, 0, 0, 1, 1 } },
498 { "ulong", KEYWORD_ULONG, { 0, 0, 1, 0, 0, 1, 1 } },
499 { "union", KEYWORD_UNION, { 1, 1, 0, 0, 0, 0, 1 } },
500 { "unittest", KEYWORD_BODY, { 0, 0, 0, 0, 0, 0, 1 } }, /* ignore */
501 { "unsigned", KEYWORD_UNSIGNED, { 1, 1, 1, 0, 0, 0, 1 } },
502 { "ushort", KEYWORD_USHORT, { 0, 0, 1, 0, 0, 1, 1 } },
503 { "using", KEYWORD_USING, { 0, 1, 1, 0, 0, 1, 0 } },
504 { "version", KEYWORD_NAMESPACE, { 0, 0, 0, 0, 0, 0, 1 } }, /* parse block */
505 { "virtual", KEYWORD_VIRTUAL, { 0, 1, 1, 0, 1, 1, 0 } },
506 { "void", KEYWORD_VOID, { 1, 1, 1, 1, 1, 1, 1 } },
507 { "volatile", KEYWORD_VOLATILE, { 1, 1, 1, 1, 0, 0, 1 } },
508 { "wchar_t", KEYWORD_WCHAR_T, { 1, 1, 1, 0, 0, 0, 1 } },
509 { "weak", KEYWORD_WEAK, { 0, 0, 0, 0, 0, 1, 0 } },
510 { "while", KEYWORD_WHILE, { 1, 1, 1, 1, 0, 1, 1 } }
515 * FUNCTION PROTOTYPES
517 static void createTags (const unsigned int nestLevel, statementInfo *const parent);
518 static void copyToken (tokenInfo *const dest, const tokenInfo *const src);
519 static const char *getVarType (const statementInfo *const st);
522 * FUNCTION DEFINITIONS
525 /* Debugging functions added by Biswa */
526 #if defined(DEBUG_C) && DEBUG_C
527 static char *tokenTypeName[] = {
528 "none", "args", "'}'", "'{'", "','", "'::'", "keyword", "name",
529 "package", "paren-name", "';'", "spec", "*", "[]", "count"
532 static char *tagScopeNames[] = {
533 "global", "static", "extern", "friend", "typedef", "count"};
535 static char *declTypeNames[] = {
536 "none", "base", "class", "enum", "function", "ignore", "interface",
537 "namespace", "nomangle", "package", "struct", "union", "count"};
539 static char *impTypeNames[] = {
540 "default", "abstract", "virtual", "pure-virtual", "count"};
542 void printToken(const tokenInfo *const token)
544 fprintf(stderr, "Type: %s, Keyword: %d, name: %s\n", tokenTypeName[token->type],
545 token->keyword, vStringValue(token->name));
548 void printTagEntry(const tagEntryInfo *tag)
550 fprintf(stderr, "Tag: %s (%s) [ impl: %s, scope: %s, type: %s\n", tag->name,
551 tag->kindName, tag->extensionFields.implementation, tag->extensionFields.scope[1],
552 tag->extensionFields.varType);
555 void printStatement(const statementInfo *const statement)
557 int i;
558 statementInfo *st = (statementInfo *) statement;
559 while (NULL != st)
561 fprintf(stderr, "Statement Info:\n------------------------\n");
562 fprintf(stderr, "scope: %s, decl: %s, impl: %s\n", tagScopeNames[st->scope],
563 declTypeNames[st->declaration], impTypeNames[st->implementation]);
564 for (i=0; i < NumTokens; ++i)
566 fprintf(stderr, "Token %d %s: ", i, (i == st->tokenIndex)?"(current)":"");
567 printToken(st->token[i]);
569 fprintf(stderr, "Context: ");
570 printToken(st->context);
571 fprintf(stderr, "Block: ");
572 printToken(st->blockName);
573 fprintf(stderr, "Parent classes: %s\n", vStringValue(st->parentClasses));
574 fprintf(stderr, "First token: ");
575 printToken(st->firstToken);
576 if (NULL != st->parent)
577 fprintf(stderr, "Printing Parent:\n");
578 st = st->parent;
580 fprintf(stderr, "-----------------------------------------------\n");
582 #endif
584 extern boolean includingDefineTags (void)
586 if (isLanguage(Lang_c) ||
587 isLanguage(Lang_cpp) ||
588 isLanguage(Lang_csharp) ||
589 isLanguage(Lang_ferite) ||
590 isLanguage(Lang_glsl) ||
591 isLanguage(Lang_vala))
592 return CKinds [CK_DEFINE].enabled;
594 return FALSE;
598 * Token management
601 static void initToken (tokenInfo* const token)
603 token->type = TOKEN_NONE;
604 token->keyword = KEYWORD_NONE;
605 token->lineNumber = getSourceLineNumber();
606 token->filePosition = getInputFilePosition();
607 vStringClear(token->name);
610 static void advanceToken (statementInfo* const st)
612 if (st->tokenIndex >= (unsigned int) NumTokens - 1)
613 st->tokenIndex = 0;
614 else
615 ++st->tokenIndex;
616 initToken(st->token[st->tokenIndex]);
619 static tokenInfo *prevToken (const statementInfo *const st, unsigned int n)
621 unsigned int tokenIndex;
622 unsigned int num = (unsigned int) NumTokens;
623 Assert(n < num);
624 tokenIndex = (st->tokenIndex + num - n) % num;
626 return st->token[tokenIndex];
629 static void setToken (statementInfo *const st, const tokenType type)
631 tokenInfo *token;
632 token = activeToken (st);
633 initToken(token);
634 token->type = type;
637 static void retardToken (statementInfo *const st)
639 if (st->tokenIndex == 0)
640 st->tokenIndex = (unsigned int) NumTokens - 1;
641 else
642 --st->tokenIndex;
643 setToken(st, TOKEN_NONE);
646 static tokenInfo *newToken (void)
648 tokenInfo *const token = xMalloc (1, tokenInfo);
649 token->name = vStringNew();
650 initToken(token);
651 return token;
654 static void deleteToken (tokenInfo *const token)
656 if (token != NULL)
658 vStringDelete(token->name);
659 eFree(token);
663 static const char *accessString (const accessType laccess)
665 static const char *const names [] = {
666 "?", "private", "protected", "public", "default"
668 Assert (sizeof (names) / sizeof (names [0]) == ACCESS_COUNT);
669 Assert ((int) laccess < ACCESS_COUNT);
670 return names[(int) laccess];
673 static const char *implementationString (const impType imp)
675 static const char *const names [] = {
676 "?", "abstract", "virtual", "pure virtual"
678 Assert (sizeof (names) / sizeof (names [0]) == IMP_COUNT);
679 Assert ((int) imp < IMP_COUNT);
680 return names [(int) imp];
684 * Debugging functions
687 #ifdef TM_DEBUG
689 #define boolString(c) ((c) ? "TRUE" : "FALSE")
691 static const char *tokenString (const tokenType type)
693 static const char *const names [] = {
694 "none", "args", "}", "{", "comma", "double colon", "keyword", "name",
695 "package", "paren-name", "semicolon", "specifier", "*", "[]"
697 Assert (sizeof (names) / sizeof (names [0]) == TOKEN_COUNT);
698 Assert ((int) type < TOKEN_COUNT);
699 return names[(int) type];
702 static const char *scopeString (const tagScope scope)
704 static const char *const names [] = {
705 "global", "static", "extern", "friend", "typedef"
707 Assert (sizeof (names) / sizeof (names [0]) == SCOPE_COUNT);
708 Assert ((int) scope < SCOPE_COUNT);
709 return names[(int) scope];
712 static const char *declString (const declType declaration)
714 static const char *const names [] = {
715 "?", "base", "class", "enum", "event", "signal", "function",
716 "function template", "ignore", "interface", "module", "namespace",
717 "no mangle", "package", "struct", "union",
719 Assert (sizeof (names) / sizeof (names [0]) == DECL_COUNT);
720 Assert ((int) declaration < DECL_COUNT);
721 return names[(int) declaration];
724 static const char *keywordString (const keywordId keyword)
726 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
727 const char *name = "none";
728 size_t i;
729 for (i = 0 ; i < count ; ++i)
731 const keywordDesc *p = &KeywordTable[i];
733 if (p->id == keyword)
735 name = p->name;
736 break;
739 return name;
742 static void __unused__ pt (tokenInfo *const token)
744 if (isType (token, TOKEN_NAME))
745 printf("type: %-12s: %-13s line: %lu\n",
746 tokenString (token->type), vStringValue (token->name),
747 token->lineNumber);
748 else if (isType (token, TOKEN_KEYWORD))
749 printf("type: %-12s: %-13s line: %lu\n",
750 tokenString (token->type), keywordString (token->keyword),
751 token->lineNumber);
752 else
753 printf("type: %-12s line: %lu\n",
754 tokenString (token->type), token->lineNumber);
757 static void __unused__ ps (statementInfo *const st)
759 unsigned int i;
760 printf("scope: %s decl: %s gotName: %s gotParenName: %s\n",
761 scopeString (st->scope), declString (st->declaration),
762 boolString (st->gotName), boolString (st->gotParenName));
763 printf("haveQualifyingName: %s\n", boolString (st->haveQualifyingName));
764 printf("access: %s default: %s\n", accessString (st->member.access),
765 accessString (st->member.accessDefault));
766 printf("token : ");
767 pt(activeToken (st));
768 for (i = 1 ; i < (unsigned int) NumTokens ; ++i)
770 printf("prev %u : ", i);
771 pt(prevToken (st, i));
773 printf("context: ");
774 pt(st->context);
777 #endif
780 * Statement management
783 static boolean isDataTypeKeyword (const tokenInfo *const token)
785 switch (token->keyword)
787 case KEYWORD_BOOLEAN:
788 case KEYWORD_BYTE:
789 case KEYWORD_CHAR:
790 case KEYWORD_DOUBLE:
791 case KEYWORD_FLOAT:
792 case KEYWORD_INT:
793 case KEYWORD_LONG:
794 case KEYWORD_SHORT:
795 case KEYWORD_VOID:
796 case KEYWORD_WCHAR_T:
797 case KEYWORD_SIZE_T:
798 return TRUE;
799 default:
800 return FALSE;
804 #if 0
805 static boolean isVariableKeyword (const tokenInfo *const token)
807 switch (token->keyword)
809 case KEYWORD_CONST:
810 case KEYWORD_EXTERN:
811 case KEYWORD_REGISTER:
812 case KEYWORD_STATIC:
813 case KEYWORD_VIRTUAL:
814 case KEYWORD_SIGNED:
815 case KEYWORD_UNSIGNED:
816 return TRUE;
817 default:
818 return FALSE;
821 #endif
823 static boolean isContextualKeyword (const tokenInfo *const token)
825 boolean result;
826 switch (token->keyword)
828 case KEYWORD_CLASS:
829 case KEYWORD_ENUM:
830 case KEYWORD_INTERFACE:
831 case KEYWORD_NAMESPACE:
832 case KEYWORD_STRUCT:
833 case KEYWORD_UNION:
835 result = TRUE;
836 break;
839 default:
841 result = FALSE;
842 break;
845 return result;
848 static boolean isContextualStatement (const statementInfo *const st)
850 boolean result = FALSE;
852 if (st != NULL)
854 if (isLanguage (Lang_vala))
856 /* All can be a contextual statment as properties can be of any type */
857 result = TRUE;
859 else
861 switch (st->declaration)
863 case DECL_CLASS:
864 case DECL_ENUM:
865 case DECL_INTERFACE:
866 case DECL_NAMESPACE:
867 case DECL_STRUCT:
868 case DECL_UNION:
870 result = TRUE;
871 break;
874 default:
876 result = FALSE;
877 break;
882 return result;
885 static boolean isMember (const statementInfo *const st)
887 boolean result;
888 if (isType (st->context, TOKEN_NAME))
889 result = TRUE;
890 else
891 result = isContextualStatement (st->parent);
892 return result;
895 static void initMemberInfo (statementInfo *const st)
897 accessType accessDefault = ACCESS_UNDEFINED;
899 if (st->parent != NULL) switch (st->parent->declaration)
901 case DECL_ENUM:
902 case DECL_NAMESPACE:
904 accessDefault = ACCESS_UNDEFINED;
905 break;
907 case DECL_CLASS:
909 if (isLanguage (Lang_java))
910 accessDefault = ACCESS_DEFAULT;
911 else
912 accessDefault = ACCESS_PRIVATE;
913 break;
915 case DECL_INTERFACE:
916 case DECL_STRUCT:
917 case DECL_UNION:
919 accessDefault = ACCESS_PUBLIC;
920 break;
922 default:
923 break;
925 st->member.accessDefault = accessDefault;
926 st->member.access = accessDefault;
929 static void reinitStatement (statementInfo *const st, const boolean partial)
931 unsigned int i;
933 if (! partial)
935 st->scope = SCOPE_GLOBAL;
936 if (isContextualStatement (st->parent))
937 st->declaration = DECL_BASE;
938 else
939 st->declaration = DECL_NONE;
941 st->gotParenName = FALSE;
942 st->implementation = IMP_DEFAULT;
943 st->gotArgs = FALSE;
944 st->gotName = FALSE;
945 st->haveQualifyingName = FALSE;
946 st->argEndPosition = 0;
948 st->tokenIndex = 0;
949 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
951 initToken (st->token [i]);
954 initToken (st->context);
955 initToken (st->blockName);
956 vStringClear (st->parentClasses);
958 /* Init member info. */
959 if (! partial)
960 st->member.access = st->member.accessDefault;
962 /* Init first token */
963 if (!partial)
964 initToken(st->firstToken);
967 static void reinitStatementWithToken (statementInfo *const st,
968 tokenInfo *token, const boolean partial)
970 tokenInfo *const save = newToken ();
971 /* given token can be part of reinit statementInfo */
972 copyToken (save, token);
973 reinitStatement (st, partial);
974 token = activeToken (st);
975 copyToken (token, save);
976 deleteToken (save);
977 ++st->tokenIndex; /* this is quite save becouse current tokenIndex = 0 */
980 static void initStatement (statementInfo *const st, statementInfo *const parent)
982 st->parent = parent;
983 initMemberInfo (st);
984 reinitStatement (st, FALSE);
985 if (parent)
987 const tokenInfo *const src = activeToken (parent);
988 tokenInfo *const dst = activeToken (st);
989 copyToken (dst, src);
990 st->tokenIndex++;
995 * Tag generation functions
997 static cKind cTagKind (const tagType type)
999 cKind result = CK_UNDEFINED;
1000 switch (type)
1002 case TAG_CLASS: result = CK_CLASS; break;
1003 case TAG_ENUM: result = CK_ENUMERATION; break;
1004 case TAG_ENUMERATOR: result = CK_ENUMERATOR; break;
1005 case TAG_FUNCTION: result = CK_FUNCTION; break;
1006 case TAG_MEMBER: result = CK_MEMBER; break;
1007 case TAG_NAMESPACE: result = CK_NAMESPACE; break;
1008 case TAG_PROTOTYPE: result = CK_PROTOTYPE; break;
1009 case TAG_STRUCT: result = CK_STRUCT; break;
1010 case TAG_TYPEDEF: result = CK_TYPEDEF; break;
1011 case TAG_UNION: result = CK_UNION; break;
1012 case TAG_VARIABLE: result = CK_VARIABLE; break;
1013 case TAG_EXTERN_VAR: result = CK_EXTERN_VARIABLE; break;
1015 default: Assert ("Bad C tag type" == NULL); break;
1017 return result;
1020 static csharpKind csharpTagKind (const tagType type)
1022 csharpKind result = CSK_UNDEFINED;
1023 switch (type)
1025 case TAG_CLASS: result = CSK_CLASS; break;
1026 case TAG_ENUM: result = CSK_ENUMERATION; break;
1027 case TAG_ENUMERATOR: result = CSK_ENUMERATOR; break;
1028 case TAG_EVENT: result = CSK_EVENT; break;
1029 case TAG_FIELD: result = CSK_FIELD ; break;
1030 case TAG_INTERFACE: result = CSK_INTERFACE; break;
1031 case TAG_LOCAL: result = CSK_LOCAL; break;
1032 case TAG_METHOD: result = CSK_METHOD; break;
1033 case TAG_NAMESPACE: result = CSK_NAMESPACE; break;
1034 case TAG_PROPERTY: result = CSK_PROPERTY; break;
1035 case TAG_STRUCT: result = CSK_STRUCT; break;
1036 case TAG_TYPEDEF: result = CSK_TYPEDEF; break;
1038 default: Assert ("Bad C# tag type" == NULL); break;
1040 return result;
1043 static dKind dTagKind (const tagType type)
1045 dKind result = DK_UNDEFINED;
1046 switch (type)
1048 case TAG_CLASS: result = DK_CLASS; break;
1049 case TAG_ENUM: result = DK_ENUMERATION; break;
1050 case TAG_ENUMERATOR: result = DK_ENUMERATOR; break;
1051 case TAG_FUNCTION: result = DK_FUNCTION; break;
1052 case TAG_INTERFACE: result = DK_INTERFACE; break;
1053 case TAG_MEMBER: result = DK_MEMBER; break;
1054 case TAG_NAMESPACE: result = DK_NAMESPACE; break;
1055 case TAG_PROTOTYPE: result = DK_PROTOTYPE; break;
1056 case TAG_STRUCT: result = DK_STRUCT; break;
1057 case TAG_TYPEDEF: result = DK_TYPEDEF; break;
1058 case TAG_UNION: result = DK_UNION; break;
1059 case TAG_VARIABLE: result = DK_VARIABLE; break;
1060 case TAG_EXTERN_VAR: result = DK_EXTERN_VARIABLE; break;
1062 default: Assert ("Bad D tag type" == NULL); break;
1064 return result;
1067 static valaKind valaTagKind (const tagType type)
1069 valaKind result = VK_UNDEFINED;
1070 switch (type)
1072 case TAG_CLASS: result = VK_CLASS; break;
1073 case TAG_ENUM: result = VK_ENUMERATION; break;
1074 case TAG_ENUMERATOR: result = VK_ENUMERATOR; break;
1075 case TAG_SIGNAL: result = VK_SIGNAL; break;
1076 case TAG_FIELD: result = VK_FIELD ; break;
1077 case TAG_INTERFACE: result = VK_INTERFACE; break;
1078 case TAG_LOCAL: result = VK_LOCAL; break;
1079 case TAG_METHOD: result = VK_METHOD; break;
1080 case TAG_NAMESPACE: result = VK_NAMESPACE; break;
1081 case TAG_PROPERTY: result = VK_PROPERTY; break;
1082 case TAG_STRUCT: result = VK_STRUCT; break;
1084 default: Assert ("Bad Vala tag type" == NULL); break;
1086 return result;
1089 static javaKind javaTagKind (const tagType type)
1091 javaKind result = JK_UNDEFINED;
1092 switch (type)
1094 case TAG_CLASS: result = JK_CLASS; break;
1095 case TAG_FIELD: result = JK_FIELD; break;
1096 case TAG_INTERFACE: result = JK_INTERFACE; break;
1097 case TAG_METHOD: result = JK_METHOD; break;
1098 case TAG_PACKAGE: result = JK_PACKAGE; break;
1100 default: Assert ("Bad Java tag type" == NULL); break;
1102 return result;
1105 static const char *tagName (const tagType type)
1107 const char* result;
1108 if (isLanguage (Lang_java))
1109 result = JavaKinds [javaTagKind (type)].name;
1110 else if (isLanguage (Lang_csharp))
1111 result = CsharpKinds [csharpTagKind (type)].name;
1112 else if (isLanguage (Lang_d))
1113 result = DKinds [dTagKind (type)].name;
1114 else if (isLanguage (Lang_vala))
1115 result = ValaKinds [valaTagKind (type)].name;
1116 else
1117 result = CKinds [cTagKind (type)].name;
1118 return result;
1121 static int tagLetter (const tagType type)
1123 int result;
1124 if (isLanguage (Lang_csharp))
1125 result = CsharpKinds [csharpTagKind (type)].letter;
1126 else if (isLanguage (Lang_d))
1127 result = DKinds [dTagKind (type)].letter;
1128 else if (isLanguage (Lang_java))
1129 result = JavaKinds [javaTagKind (type)].letter;
1130 else if (isLanguage (Lang_vala))
1131 result = ValaKinds [valaTagKind (type)].letter;
1132 else
1133 result = CKinds [cTagKind (type)].letter;
1134 return result;
1138 static boolean includeTag (const tagType type, const boolean isFileScope)
1140 boolean result;
1141 if (isFileScope && ! Option.include.fileScope)
1142 result = FALSE;
1143 else if (isLanguage (Lang_java))
1144 result = JavaKinds [javaTagKind (type)].enabled;
1145 else
1146 result = CKinds [cTagKind (type)].enabled;
1147 return result;
1151 static tagType declToTagType (const declType declaration)
1153 tagType type = TAG_UNDEFINED;
1155 switch (declaration)
1157 case DECL_CLASS: type = TAG_CLASS; break;
1158 case DECL_ENUM: type = TAG_ENUM; break;
1159 case DECL_FUNCTION: type = TAG_FUNCTION; break;
1160 case DECL_FUNCTION_TEMPLATE: type = TAG_FUNCTION; break;
1161 case DECL_INTERFACE:type = TAG_INTERFACE; break;
1162 case DECL_NAMESPACE:type = TAG_NAMESPACE; break;
1163 case DECL_STRUCT: type = TAG_STRUCT; break;
1164 case DECL_UNION: type = TAG_UNION; break;
1166 default: Assert ("Unexpected declaration" == NULL); break;
1168 return type;
1171 static const char* accessField (const statementInfo *const st)
1173 const char* result = NULL;
1175 if ((isLanguage (Lang_cpp) || isLanguage (Lang_d) || isLanguage (Lang_ferite)) &&
1176 st->scope == SCOPE_FRIEND)
1177 result = "friend";
1178 else if (st->member.access != ACCESS_UNDEFINED)
1179 result = accessString (st->member.access);
1180 return result;
1183 static void addOtherFields (tagEntryInfo* const tag, const tagType type,
1184 const statementInfo *const st, vString *const scope)
1186 /* For selected tag types, append an extension flag designating the
1187 * parent object in which the tag is defined.
1189 switch (type)
1191 default: break;
1193 case TAG_CLASS:
1194 case TAG_ENUM:
1195 case TAG_ENUMERATOR:
1196 case TAG_FIELD:
1197 case TAG_FUNCTION:
1198 case TAG_INTERFACE:
1199 case TAG_MEMBER:
1200 case TAG_METHOD:
1201 case TAG_PROTOTYPE:
1202 case TAG_STRUCT:
1203 case TAG_TYPEDEF:
1204 case TAG_UNION:
1206 if (vStringLength (scope) > 0 &&
1207 (isMember (st) || st->parent->declaration == DECL_NAMESPACE))
1209 if (isType (st->context, TOKEN_NAME))
1210 tag->extensionFields.scope [0] = tagName (TAG_CLASS);
1211 else
1212 tag->extensionFields.scope [0] =
1213 tagName (declToTagType (parentDecl (st)));
1214 tag->extensionFields.scope [1] = vStringValue (scope);
1216 if ((type == TAG_CLASS || type == TAG_INTERFACE ||
1217 type == TAG_STRUCT) && vStringLength (st->parentClasses) > 0)
1219 tag->extensionFields.inheritance =
1220 vStringValue (st->parentClasses);
1222 if (st->implementation != IMP_DEFAULT &&
1223 (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) || isLanguage (Lang_vala) ||
1224 isLanguage (Lang_java) || isLanguage (Lang_d) || isLanguage (Lang_ferite)))
1226 tag->extensionFields.implementation =
1227 implementationString (st->implementation);
1229 if (isMember (st))
1231 tag->extensionFields.access = accessField (st);
1233 if ((TRUE == st->gotArgs) && (TRUE == Option.extensionFields.argList) &&
1234 ((TAG_FUNCTION == type) || (TAG_METHOD == type) || (TAG_PROTOTYPE == type)))
1236 tag->extensionFields.arglist = getArglistFromFilePos(
1237 tag->filePosition, tag->name);
1239 break;
1243 if ((TAG_FIELD == tag->type) || (TAG_MEMBER == tag->type) ||
1244 (TAG_EXTERN_VAR == tag->type) || (TAG_TYPEDEF == tag->type) ||
1245 (TAG_VARIABLE == tag->type) || (TAG_METHOD == tag->type) ||
1246 (TAG_PROTOTYPE == tag->type) || (TAG_FUNCTION == tag->type))
1248 if (((TOKEN_NAME == st->firstToken->type) || isDataTypeKeyword(st->firstToken))
1249 && (0 != strcmp(vStringValue(st->firstToken->name), tag->name)))
1251 tag->extensionFields.varType = getVarType(st);
1256 static const char *getVarType (const statementInfo *const st)
1258 static vString *vt = NULL;
1259 unsigned int i;
1261 if (! st->gotArgs)
1262 return vStringValue(st->firstToken->name); /* ignore non-functions */
1264 if (vt == NULL)
1265 vt = vStringNew();
1266 else
1267 vStringClear(vt);
1269 for (i = 0; i < st->tokenIndex; i++)
1271 tokenInfo *t = st->token[i];
1273 switch (t->type)
1275 case TOKEN_NAME: /* user typename */
1276 if (strcmp(vStringValue(t->name), vStringValue(st->firstToken->name)) != 0)
1277 continue;
1278 break;
1279 case TOKEN_KEYWORD:
1280 if (t->keyword != KEYWORD_EXTERN && t->keyword != KEYWORD_STATIC) /* uninteresting keywords */
1281 break;
1282 continue;
1283 case TOKEN_STAR: vStringCatS(vt, " *"); continue;
1284 case TOKEN_ARRAY: vStringCatS(vt, "[]"); continue;
1285 default: continue;
1287 if (vStringLength(vt) > 0)
1288 if (isalpha(vStringValue(vt)[vStringLength(vt) - 1]))
1289 vStringPut(vt, ' ');
1290 vStringCat(vt, t->name);
1292 vStringTerminate(vt);
1293 return vStringValue(vt);
1296 static void addContextSeparator (vString *const scope)
1298 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
1299 vStringCatS (scope, "::");
1300 else if (isLanguage (Lang_java) || isLanguage (Lang_d) || isLanguage (Lang_ferite) ||
1301 isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1302 vStringCatS (scope, ".");
1305 static void findScopeHierarchy (vString *const string,
1306 const statementInfo *const st)
1308 const char* const anon = "<anonymous>";
1309 boolean nonAnonPresent = FALSE;
1311 vStringClear (string);
1312 if (isType (st->context, TOKEN_NAME))
1314 vStringCopy (string, st->context->name);
1315 nonAnonPresent = TRUE;
1317 if (st->parent != NULL)
1319 vString *temp = vStringNew ();
1320 const statementInfo *s;
1322 for (s = st->parent ; s != NULL ; s = s->parent)
1324 if (isContextualStatement (s) ||
1325 s->declaration == DECL_NAMESPACE)
1327 vStringCopy (temp, string);
1328 vStringClear (string);
1329 if (isType (s->blockName, TOKEN_NAME))
1331 if (isType (s->context, TOKEN_NAME) &&
1332 vStringLength (s->context->name) > 0)
1334 vStringCat (string, s->context->name);
1335 addContextSeparator (string);
1337 vStringCat (string, s->blockName->name);
1338 nonAnonPresent = TRUE;
1340 else
1341 vStringCopyS (string, anon);
1342 if (vStringLength (temp) > 0)
1343 addContextSeparator (string);
1344 vStringCat (string, temp);
1347 vStringDelete (temp);
1349 if (! nonAnonPresent)
1350 vStringClear (string);
1354 static void makeExtraTagEntry (const tagType type, tagEntryInfo *const e,
1355 vString *const scope)
1357 if (Option.include.qualifiedTags &&
1358 scope != NULL && vStringLength (scope) > 0)
1360 vString *const scopedName = vStringNew ();
1362 if (type != TAG_ENUMERATOR)
1363 vStringCopy (scopedName, scope);
1364 else
1366 /* remove last component (i.e. enumeration name) from scope */
1367 const char* const sc = vStringValue (scope);
1368 const char* colon = strrchr (sc, ':');
1369 if (colon != NULL)
1371 while (*colon == ':' && colon > sc)
1372 --colon;
1373 vStringNCopy (scopedName, scope, colon + 1 - sc);
1376 if (vStringLength (scopedName) > 0)
1378 addContextSeparator (scopedName);
1379 vStringCatS (scopedName, e->name);
1380 e->name = vStringValue (scopedName);
1381 makeTagEntry (e);
1383 vStringDelete (scopedName);
1387 static void makeTag (const tokenInfo *const token,
1388 const statementInfo *const st,
1389 boolean isFileScope, const tagType type)
1391 #ifdef DEBUG_C
1392 printToken(token);
1393 fprintf(stderr, "<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>\n");
1394 printStatement(st);
1395 #endif
1396 /* Nothing is really of file scope when it appears in a header file.
1398 isFileScope = (boolean) (isFileScope && ! isHeaderFile ());
1400 if (isType (token, TOKEN_NAME) && vStringLength (token->name) > 0 /* &&
1401 includeTag (type, isFileScope) */)
1403 vString *scope = vStringNew ();
1404 tagEntryInfo e;
1406 /* take only functions which are introduced by "function ..." */
1407 if (type == TAG_FUNCTION && isLanguage (Lang_ferite) &&
1408 strncmp("function", st->firstToken->name->buffer, 8) != 0)
1410 return;
1413 initTagEntry (&e, vStringValue (token->name));
1415 e.lineNumber = token->lineNumber;
1416 e.filePosition = token->filePosition;
1417 e.isFileScope = isFileScope;
1418 e.kindName = tagName (type);
1419 e.kind = tagLetter (type);
1420 e.type = type;
1422 findScopeHierarchy (scope, st);
1423 addOtherFields (&e, type, st, scope);
1425 #ifdef DEBUG_C
1426 printTagEntry(&e);
1427 #endif
1428 makeTagEntry (&e);
1429 if (NULL != TagEntryFunction)
1430 makeExtraTagEntry (type, &e, scope);
1431 vStringDelete (scope);
1432 if (NULL != e.extensionFields.arglist)
1433 free((char *) e.extensionFields.arglist);
1437 static boolean isValidTypeSpecifier (const declType declaration)
1439 boolean result;
1440 switch (declaration)
1442 case DECL_BASE:
1443 case DECL_CLASS:
1444 case DECL_ENUM:
1445 case DECL_STRUCT:
1446 case DECL_UNION:
1447 result = TRUE;
1448 break;
1450 default:
1451 result = FALSE;
1452 break;
1454 return result;
1457 static void qualifyEnumeratorTag (const statementInfo *const st,
1458 const tokenInfo *const nameToken)
1460 if (isType (nameToken, TOKEN_NAME))
1461 makeTag (nameToken, st, TRUE, TAG_ENUMERATOR);
1464 static void qualifyFunctionTag (const statementInfo *const st,
1465 const tokenInfo *const nameToken)
1467 if (isType (nameToken, TOKEN_NAME))
1469 const tagType type = (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1470 ? TAG_METHOD : TAG_FUNCTION;
1471 const boolean isFileScope =
1472 (boolean) (st->member.access == ACCESS_PRIVATE ||
1473 (!isMember (st) && st->scope == SCOPE_STATIC));
1475 makeTag (nameToken, st, isFileScope, type);
1479 static void qualifyFunctionDeclTag (const statementInfo *const st,
1480 const tokenInfo *const nameToken)
1482 if (! isType (nameToken, TOKEN_NAME))
1484 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1485 qualifyFunctionTag (st, nameToken);
1486 else if (st->scope == SCOPE_TYPEDEF)
1487 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1488 else if (isValidTypeSpecifier (st->declaration) &&
1489 ! (isLanguage (Lang_csharp) || isLanguage (Lang_vala)))
1490 makeTag (nameToken, st, TRUE, TAG_PROTOTYPE);
1493 static void qualifyCompoundTag (const statementInfo *const st,
1494 const tokenInfo *const nameToken)
1496 if (isType (nameToken, TOKEN_NAME))
1498 const tagType type = declToTagType (st->declaration);
1500 if (type != TAG_UNDEFINED)
1501 makeTag (nameToken, st, (boolean) (! isLanguage (Lang_java) &&
1502 ! isLanguage (Lang_csharp) &&
1503 ! isLanguage (Lang_vala)), type);
1507 static void qualifyBlockTag (statementInfo *const st,
1508 const tokenInfo *const nameToken)
1510 switch (st->declaration)
1512 case DECL_CLASS:
1513 case DECL_ENUM:
1514 case DECL_INTERFACE:
1515 case DECL_NAMESPACE:
1516 case DECL_STRUCT:
1517 case DECL_UNION:
1518 qualifyCompoundTag (st, nameToken);
1519 break;
1520 default: break;
1524 static void qualifyVariableTag (const statementInfo *const st,
1525 const tokenInfo *const nameToken)
1527 /* We have to watch that we do not interpret a declaration of the
1528 * form "struct tag;" as a variable definition. In such a case, the
1529 * token preceding the name will be a keyword.
1531 if (! isType (nameToken, TOKEN_NAME))
1533 else if (st->declaration == DECL_IGNORE)
1535 else if (st->scope == SCOPE_TYPEDEF)
1536 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1537 else if (st->declaration == DECL_PACKAGE)
1538 makeTag (nameToken, st, FALSE, TAG_PACKAGE);
1539 else if (st->declaration == DECL_MODULE) /* handle modules in D as namespaces */
1540 makeTag (nameToken, st, FALSE, TAG_NAMESPACE);
1541 else if (isValidTypeSpecifier (st->declaration))
1543 if (isMember (st))
1545 if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1546 makeTag (nameToken, st, (boolean) (st->member.access == ACCESS_PRIVATE), TAG_FIELD);
1547 else if (st->scope == SCOPE_GLOBAL || st->scope == SCOPE_STATIC)
1548 makeTag (nameToken, st, TRUE, TAG_MEMBER);
1550 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1552 else
1554 if (st->scope == SCOPE_EXTERN || ! st->haveQualifyingName)
1555 makeTag (nameToken, st, FALSE, TAG_EXTERN_VAR);
1556 else
1557 makeTag (nameToken, st, (boolean) (st->scope == SCOPE_STATIC), TAG_VARIABLE);
1563 * Parsing functions
1566 static int skipToOneOf (const char *const chars)
1568 int c;
1570 c = cppGetc ();
1571 while (c != EOF && c != '\0' && strchr (chars, c) == NULL);
1573 return c;
1576 /* Skip to the next non-white character.
1578 static int skipToNonWhite (void)
1580 int c;
1584 c = cppGetc ();
1586 while (isspace (c));
1588 return c;
1591 /* Skips to the next brace in column 1. This is intended for cases where
1592 * preprocessor constructs result in unbalanced braces.
1594 static void skipToFormattedBraceMatch (void)
1596 int c, next;
1598 c = cppGetc ();
1599 next = cppGetc ();
1600 while (c != EOF && (c != '\n' || next != '}'))
1602 c = next;
1603 next = cppGetc ();
1607 /* Skip to the matching character indicated by the pair string. If skipping
1608 * to a matching brace and any brace is found within a different level of a
1609 * #if conditional statement while brace formatting is in effect, we skip to
1610 * the brace matched by its formatting. It is assumed that we have already
1611 * read the character which starts the group (i.e. the first character of
1612 * "pair").
1614 static void skipToMatch (const char *const pair)
1616 const boolean braceMatching = (boolean) (strcmp ("{}", pair) == 0);
1617 const boolean braceFormatting = (boolean) (isBraceFormat () && braceMatching);
1618 const unsigned int initialLevel = getDirectiveNestLevel ();
1619 const int begin = pair [0], end = pair [1];
1620 const unsigned long inputLineNumber = getInputLineNumber ();
1621 int matchLevel = 1;
1622 int c = '\0';
1623 while (matchLevel > 0 && (c = cppGetc ()) != EOF)
1625 if (c == begin)
1627 ++matchLevel;
1628 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1630 skipToFormattedBraceMatch ();
1631 break;
1634 else if (c == end)
1636 --matchLevel;
1637 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1639 skipToFormattedBraceMatch ();
1640 break;
1644 if (c == EOF)
1646 verbose ("%s: failed to find match for '%c' at line %lu\n",
1647 getInputFileName (), begin, inputLineNumber);
1648 if (braceMatching)
1649 longjmp (Exception, (int) ExceptionBraceFormattingError);
1650 else
1651 longjmp (Exception, (int) ExceptionFormattingError);
1655 static void skipParens (void)
1657 const int c = skipToNonWhite ();
1659 if (c == '(')
1660 skipToMatch ("()");
1661 else
1662 cppUngetc (c);
1665 static void skipBraces (void)
1667 const int c = skipToNonWhite ();
1669 if (c == '{')
1670 skipToMatch ("{}");
1671 else
1672 cppUngetc (c);
1675 static keywordId analyzeKeyword (const char *const name)
1677 const keywordId id = (keywordId) lookupKeyword (name, getSourceLanguage ());
1678 return id;
1681 static void analyzeIdentifier (tokenInfo *const token)
1683 char *const name = vStringValue (token->name);
1684 const char *replacement = NULL;
1685 boolean parensToo = FALSE;
1687 if (isLanguage (Lang_java) ||
1688 ! isIgnoreToken (name, &parensToo, &replacement))
1690 if (replacement != NULL)
1691 token->keyword = analyzeKeyword (replacement);
1692 else
1693 token->keyword = analyzeKeyword (vStringValue (token->name));
1695 if (token->keyword == KEYWORD_NONE)
1696 token->type = TOKEN_NAME;
1697 else
1698 token->type = TOKEN_KEYWORD;
1700 else
1702 initToken (token);
1703 if (parensToo)
1705 int c = skipToNonWhite ();
1707 if (c == '(')
1708 skipToMatch ("()");
1713 static void readIdentifier (tokenInfo *const token, const int firstChar)
1715 vString *const name = token->name;
1716 int c = firstChar;
1718 initToken (token);
1720 /* Bug #1585745 (CTags): strangely, C++ destructors allow whitespace between
1721 * the ~ and the class name. */
1722 if (isLanguage (Lang_cpp) && firstChar == '~')
1724 vStringPut (name, c);
1725 c = skipToNonWhite ();
1730 vStringPut (name, c);
1731 c = cppGetc ();
1732 } while (isident (c) || (isLanguage (Lang_vala) && '.' == c));
1733 vStringTerminate (name);
1734 cppUngetc (c); /* unget non-identifier character */
1736 /* Vala supports '?' at end of a type (with or without whitespace before) for nullable types */
1737 if (isLanguage (Lang_vala))
1739 c = skipToNonWhite ();
1740 if ('?' == c)
1741 vStringPut (name, c);
1742 else
1743 cppUngetc (c);
1746 analyzeIdentifier (token);
1749 static void readPackageName (tokenInfo *const token, const int firstChar)
1751 vString *const name = token->name;
1752 int c = firstChar;
1754 initToken (token);
1756 while (isident (c) || c == '.')
1758 vStringPut (name, c);
1759 c = cppGetc ();
1761 vStringTerminate (name);
1762 cppUngetc (c); /* unget non-package character */
1765 static void readPackageOrNamespace (statementInfo *const st, const declType declaration)
1767 st->declaration = declaration;
1769 if (declaration == DECL_NAMESPACE && !(isLanguage (Lang_csharp) || isLanguage (Lang_vala)))
1771 /* In C++ a namespace is specified one level at a time. */
1772 return;
1774 else
1776 /* In C#, a namespace can also be specified like a Java package name. */
1777 tokenInfo *const token = activeToken (st);
1778 Assert (isType (token, TOKEN_KEYWORD));
1779 readPackageName (token, skipToNonWhite ());
1780 token->type = TOKEN_NAME;
1781 st->gotName = TRUE;
1782 st->haveQualifyingName = TRUE;
1786 static void readPackage (statementInfo *const st)
1788 tokenInfo *const token = activeToken (st);
1789 Assert (isType (token, TOKEN_KEYWORD));
1790 readPackageName (token, skipToNonWhite ());
1791 token->type = TOKEN_NAME;
1792 if (isLanguage (Lang_d))
1793 st->declaration = DECL_MODULE;
1794 else
1795 st->declaration = DECL_PACKAGE;
1796 st->gotName = TRUE;
1797 st->haveQualifyingName = TRUE;
1800 static void processName (statementInfo *const st)
1802 Assert (isType (activeToken (st), TOKEN_NAME));
1803 if (st->gotName && st->declaration == DECL_NONE)
1804 st->declaration = DECL_BASE;
1805 st->gotName = TRUE;
1806 st->haveQualifyingName = TRUE;
1809 static void readOperator (statementInfo *const st)
1811 const char *const acceptable = "+-*/%^&|~!=<>,[]";
1812 const tokenInfo* const prev = prevToken (st,1);
1813 tokenInfo *const token = activeToken (st);
1814 vString *const name = token->name;
1815 int c = skipToNonWhite ();
1817 /* When we arrive here, we have the keyword "operator" in 'name'.
1819 if (isType (prev, TOKEN_KEYWORD) && (prev->keyword == KEYWORD_ENUM ||
1820 prev->keyword == KEYWORD_STRUCT || prev->keyword == KEYWORD_UNION))
1821 ; /* ignore "operator" keyword if preceded by these keywords */
1822 else if (c == '(')
1824 /* Verify whether this is a valid function call (i.e. "()") operator.
1826 if (cppGetc () == ')')
1828 vStringPut (name, ' '); /* always separate operator from keyword */
1829 c = skipToNonWhite ();
1830 if (c == '(')
1831 vStringCatS (name, "()");
1833 else
1835 skipToMatch ("()");
1836 c = cppGetc ();
1839 else if (isident1 (c))
1841 /* Handle "new" and "delete" operators, and conversion functions
1842 * (per 13.3.1.1.2 [2] of the C++ spec).
1844 boolean whiteSpace = TRUE; /* default causes insertion of space */
1847 if (isspace (c))
1848 whiteSpace = TRUE;
1849 else
1851 if (whiteSpace)
1853 vStringPut (name, ' ');
1854 whiteSpace = FALSE;
1856 vStringPut (name, c);
1858 c = cppGetc ();
1859 } while (! isOneOf (c, "(;") && c != EOF);
1860 vStringTerminate (name);
1862 else if (isOneOf (c, acceptable))
1864 vStringPut (name, ' '); /* always separate operator from keyword */
1867 vStringPut (name, c);
1868 c = cppGetc ();
1869 } while (isOneOf (c, acceptable));
1870 vStringTerminate (name);
1873 cppUngetc (c);
1875 token->type = TOKEN_NAME;
1876 token->keyword = KEYWORD_NONE;
1877 processName (st);
1880 static void copyToken (tokenInfo *const dest, const tokenInfo *const src)
1882 dest->type = src->type;
1883 dest->keyword = src->keyword;
1884 dest->filePosition = src->filePosition;
1885 dest->lineNumber = src->lineNumber;
1886 vStringCopy (dest->name, src->name);
1889 static void setAccess (statementInfo *const st, const accessType laccess)
1891 if (isMember (st))
1893 if (isLanguage (Lang_cpp) || isLanguage (Lang_d) || isLanguage (Lang_ferite))
1895 int c = skipToNonWhite ();
1897 if (c == ':')
1898 reinitStatementWithToken (st, prevToken (st, 1), FALSE);
1899 else
1900 cppUngetc (c);
1902 st->member.accessDefault = laccess;
1904 st->member.access = laccess;
1908 static void discardTypeList (tokenInfo *const token)
1910 int c = skipToNonWhite ();
1911 while (isident1 (c))
1913 readIdentifier (token, c);
1914 c = skipToNonWhite ();
1915 if (c == '.' || c == ',')
1916 c = skipToNonWhite ();
1918 cppUngetc (c);
1921 static void addParentClass (statementInfo *const st, tokenInfo *const token)
1923 if (vStringLength (token->name) > 0 &&
1924 vStringLength (st->parentClasses) > 0)
1926 vStringPut (st->parentClasses, ',');
1928 vStringCat (st->parentClasses, token->name);
1931 static void readParents (statementInfo *const st, const int qualifier)
1933 tokenInfo *const token = newToken ();
1934 tokenInfo *const parent = newToken ();
1935 int c;
1939 c = skipToNonWhite ();
1940 if (isident1 (c))
1942 readIdentifier (token, c);
1943 if (isType (token, TOKEN_NAME))
1944 vStringCat (parent->name, token->name);
1945 else
1947 addParentClass (st, parent);
1948 initToken (parent);
1951 else if (c == qualifier)
1952 vStringPut (parent->name, c);
1953 else if (c == '<')
1954 skipToMatch ("<>");
1955 else if (isType (token, TOKEN_NAME))
1957 addParentClass (st, parent);
1958 initToken (parent);
1960 } while (c != '{' && c != EOF);
1961 cppUngetc (c);
1962 deleteToken (parent);
1963 deleteToken (token);
1966 static void processToken (tokenInfo *const token, statementInfo *const st)
1968 switch (token->keyword) /* is it a reserved word? */
1970 default: break;
1972 case KEYWORD_NONE: processName (st); break;
1973 case KEYWORD_ABSTRACT: st->implementation = IMP_ABSTRACT; break;
1974 case KEYWORD_ATTRIBUTE: skipParens (); initToken (token); break;
1975 case KEYWORD_CATCH: skipParens (); skipBraces (); break;
1976 case KEYWORD_CHAR: st->declaration = DECL_BASE; break;
1977 case KEYWORD_CLASS: st->declaration = DECL_CLASS; break;
1978 case KEYWORD_CONST: st->declaration = DECL_BASE; break;
1979 case KEYWORD_DOUBLE: st->declaration = DECL_BASE; break;
1980 case KEYWORD_ENUM: st->declaration = DECL_ENUM; break;
1981 case KEYWORD_EXTENDS: readParents (st, '.');
1982 setToken (st, TOKEN_NONE); break;
1983 case KEYWORD_FLOAT: st->declaration = DECL_BASE; break;
1984 case KEYWORD_FRIEND: st->scope = SCOPE_FRIEND; break;
1985 case KEYWORD_IMPLEMENTS:readParents (st, '.');
1986 setToken (st, TOKEN_NONE); break;
1987 case KEYWORD_IMPORT: st->declaration = DECL_IGNORE; break;
1988 case KEYWORD_INT: st->declaration = DECL_BASE; break;
1989 case KEYWORD_BOOLEAN: st->declaration = DECL_BASE; break;
1990 case KEYWORD_WCHAR_T: st->declaration = DECL_BASE; break;
1991 case KEYWORD_SIZE_T: st->declaration = DECL_BASE; break;
1992 case KEYWORD_INTERFACE: st->declaration = DECL_INTERFACE; break;
1993 case KEYWORD_LONG: st->declaration = DECL_BASE; break;
1994 case KEYWORD_OPERATOR: readOperator (st); break;
1995 case KEYWORD_MODULE: readPackage (st); break;
1996 case KEYWORD_PRIVATE: setAccess (st, ACCESS_PRIVATE); break;
1997 case KEYWORD_PROTECTED: setAccess (st, ACCESS_PROTECTED); break;
1998 case KEYWORD_PUBLIC: setAccess (st, ACCESS_PUBLIC); break;
1999 case KEYWORD_SHORT: st->declaration = DECL_BASE; break;
2000 case KEYWORD_SIGNED: st->declaration = DECL_BASE; break;
2001 case KEYWORD_STRUCT: st->declaration = DECL_STRUCT; break;
2002 case KEYWORD_THROWS: discardTypeList (token); break;
2003 case KEYWORD_TYPEDEF: st->scope = SCOPE_TYPEDEF; break;
2004 case KEYWORD_UNION: st->declaration = DECL_UNION; break;
2005 case KEYWORD_UNSIGNED: st->declaration = DECL_BASE; break;
2006 case KEYWORD_USING: st->declaration = DECL_IGNORE; break;
2007 case KEYWORD_VOID: st->declaration = DECL_BASE; break;
2008 case KEYWORD_VOLATILE: st->declaration = DECL_BASE; break;
2009 case KEYWORD_VIRTUAL: st->implementation = IMP_VIRTUAL; break;
2011 case KEYWORD_NAMESPACE: readPackageOrNamespace (st, DECL_NAMESPACE); break;
2012 case KEYWORD_PACKAGE: readPackageOrNamespace (st, DECL_PACKAGE); break;
2013 case KEYWORD_EVENT:
2015 if (isLanguage (Lang_csharp))
2016 st->declaration = DECL_EVENT;
2017 break;
2019 case KEYWORD_SIGNAL:
2021 if (isLanguage (Lang_vala))
2022 st->declaration = DECL_SIGNAL;
2023 break;
2025 case KEYWORD_EXTERN:
2027 if (! isLanguage (Lang_csharp) || !st->gotName)
2029 /*reinitStatement (st, FALSE);*/
2030 st->scope = SCOPE_EXTERN;
2031 st->declaration = DECL_BASE;
2033 break;
2035 case KEYWORD_STATIC:
2037 if (! isLanguage (Lang_java) && ! isLanguage (Lang_csharp) && ! isLanguage (Lang_vala))
2039 /*reinitStatement (st, FALSE);*/
2040 st->scope = SCOPE_STATIC;
2041 st->declaration = DECL_BASE;
2043 break;
2049 * Parenthesis handling functions
2052 static void restartStatement (statementInfo *const st)
2054 tokenInfo *const save = newToken ();
2055 tokenInfo *token = activeToken (st);
2057 copyToken (save, token);
2058 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>");)
2059 reinitStatement (st, FALSE);
2060 token = activeToken (st);
2061 copyToken (token, save);
2062 deleteToken (save);
2063 processToken (token, st);
2066 /* Skips over a the mem-initializer-list of a ctor-initializer, defined as:
2068 * mem-initializer-list:
2069 * mem-initializer, mem-initializer-list
2071 * mem-initializer:
2072 * [::] [nested-name-spec] class-name (...)
2073 * identifier
2075 static void skipMemIntializerList (tokenInfo *const token)
2077 int c;
2081 c = skipToNonWhite ();
2082 while (isident1 (c) || c == ':')
2084 if (c != ':')
2085 readIdentifier (token, c);
2086 c = skipToNonWhite ();
2088 if (c == '<')
2090 skipToMatch ("<>");
2091 c = skipToNonWhite ();
2093 if (c == '(')
2095 skipToMatch ("()");
2096 c = skipToNonWhite ();
2098 } while (c == ',');
2099 cppUngetc (c);
2102 static void skipMacro (statementInfo *const st)
2104 tokenInfo *const prev2 = prevToken (st, 2);
2106 if (isType (prev2, TOKEN_NAME))
2107 retardToken (st);
2108 skipToMatch ("()");
2111 /* Skips over characters following the parameter list. This will be either
2112 * non-ANSI style function declarations or C++ stuff. Our choices:
2114 * C (K&R):
2115 * int func ();
2116 * int func (one, two) int one; float two; {...}
2117 * C (ANSI):
2118 * int func (int one, float two);
2119 * int func (int one, float two) {...}
2120 * C++:
2121 * int foo (...) [const|volatile] [throw (...)];
2122 * int foo (...) [const|volatile] [throw (...)] [ctor-initializer] {...}
2123 * int foo (...) [const|volatile] [throw (...)] try [ctor-initializer] {...}
2124 * catch (...) {...}
2126 static boolean skipPostArgumentStuff (statementInfo *const st,
2127 parenInfo *const info)
2129 tokenInfo *const token = activeToken (st);
2130 unsigned int parameters = info->parameterCount;
2131 unsigned int elementCount = 0;
2132 boolean restart = FALSE;
2133 boolean end = FALSE;
2134 int c = skipToNonWhite ();
2138 switch (c)
2140 case ')': break;
2141 case ':': skipMemIntializerList (token);break; /* ctor-initializer */
2142 case '[': skipToMatch ("[]"); break;
2143 case '=': cppUngetc (c); end = TRUE; break;
2144 case '{': cppUngetc (c); end = TRUE; break;
2145 case '}': cppUngetc (c); end = TRUE; break;
2147 case '(':
2149 if (elementCount > 0)
2150 ++elementCount;
2151 skipToMatch ("()");
2152 break;
2155 case ';':
2157 if (parameters == 0 || elementCount < 2)
2159 cppUngetc (c);
2160 end = TRUE;
2162 else if (--parameters == 0)
2163 end = TRUE;
2164 break;
2167 default:
2169 if (isident1 (c))
2171 readIdentifier (token, c);
2172 if (isLanguage(Lang_d))
2174 switch (token->keyword)
2176 /* template constraint */
2177 case KEYWORD_IF:
2178 /* contracts */
2179 case KEYWORD_IN:
2180 case KEYWORD_OUT:
2181 case KEYWORD_BODY:
2182 token->keyword = KEYWORD_CONST;
2183 default:
2184 break;
2187 switch (token->keyword)
2189 case KEYWORD_ATTRIBUTE: skipParens (); break;
2190 case KEYWORD_THROW: skipParens (); break;
2191 case KEYWORD_CONST: break;
2192 case KEYWORD_TRY: break;
2193 case KEYWORD_VOLATILE: break;
2195 case KEYWORD_CATCH: case KEYWORD_CLASS:
2196 case KEYWORD_EXPLICIT: case KEYWORD_EXTERN:
2197 case KEYWORD_FRIEND: case KEYWORD_INLINE:
2198 case KEYWORD_MUTABLE: case KEYWORD_NAMESPACE:
2199 case KEYWORD_NEW: case KEYWORD_OPERATOR:
2200 case KEYWORD_OVERLOAD: case KEYWORD_PRIVATE:
2201 case KEYWORD_PROTECTED: case KEYWORD_PUBLIC:
2202 case KEYWORD_STATIC: case KEYWORD_TEMPLATE:
2203 case KEYWORD_TYPEDEF: case KEYWORD_TYPENAME:
2204 case KEYWORD_USING: case KEYWORD_VIRTUAL:
2205 /* Never allowed within parameter declarations.
2207 restart = TRUE;
2208 end = TRUE;
2209 break;
2211 default:
2212 if (isType (token, TOKEN_NONE))
2214 else if (info->isKnrParamList && info->parameterCount > 0)
2215 ++elementCount;
2216 else
2218 /* If we encounter any other identifier immediately
2219 * following an empty parameter list, this is almost
2220 * certainly one of those Microsoft macro "thingies"
2221 * that the automatic source code generation sticks
2222 * in. Terminate the current statement.
2224 restart = TRUE;
2225 end = TRUE;
2227 break;
2232 if (! end)
2234 c = skipToNonWhite ();
2235 if (c == EOF)
2236 end = TRUE;
2238 } while (! end);
2240 if (restart)
2241 restartStatement (st);
2242 else
2243 setToken (st, TOKEN_NONE);
2244 return (boolean) (c != EOF);
2247 static void skipJavaThrows (statementInfo *const st)
2249 tokenInfo *const token = activeToken (st);
2250 int c = skipToNonWhite ();
2252 if (isident1 (c))
2254 readIdentifier (token, c);
2255 if (token->keyword == KEYWORD_THROWS)
2259 c = skipToNonWhite ();
2260 if (isident1 (c))
2262 readIdentifier (token, c);
2263 c = skipToNonWhite ();
2265 } while (c == '.' || c == ',');
2268 cppUngetc (c);
2269 setToken (st, TOKEN_NONE);
2272 static void skipValaPostParens (statementInfo *const st)
2274 tokenInfo *const token = activeToken (st);
2275 int c = skipToNonWhite ();
2277 while (isident1 (c))
2279 readIdentifier (token, c);
2280 if (token->keyword == KEYWORD_ATTRIBUTE)
2282 /* parse contracts */
2283 skipParens ();
2284 c = skipToNonWhite ();
2286 else if (token->keyword == KEYWORD_THROWS)
2290 c = skipToNonWhite ();
2291 if (isident1 (c))
2293 readIdentifier (token, c);
2294 c = skipToNonWhite ();
2296 } while (c == '.' || c == ',');
2298 else
2299 break;
2301 cppUngetc (c);
2302 setToken (st, TOKEN_NONE);
2305 static void analyzePostParens (statementInfo *const st, parenInfo *const info)
2307 const unsigned long inputLineNumber = getInputLineNumber ();
2308 int c = skipToNonWhite ();
2310 cppUngetc (c);
2311 if (isOneOf (c, "{;,="))
2313 else if (isLanguage (Lang_java))
2314 skipJavaThrows (st);
2315 else if (isLanguage (Lang_vala))
2316 skipValaPostParens(st);
2317 else
2319 if (! skipPostArgumentStuff (st, info))
2321 verbose (
2322 "%s: confusing argument declarations beginning at line %lu\n",
2323 getInputFileName (), inputLineNumber);
2324 longjmp (Exception, (int) ExceptionFormattingError);
2329 static int parseParens (statementInfo *const st, parenInfo *const info)
2331 tokenInfo *const token = activeToken (st);
2332 unsigned int identifierCount = 0;
2333 unsigned int depth = 1;
2334 boolean firstChar = TRUE;
2335 int nextChar = '\0';
2337 info->parameterCount = 1;
2340 int c = skipToNonWhite ();
2342 switch (c)
2344 case '&':
2345 case '*':
2347 /* DEBUG_PRINT("parseParens, po++\n"); */
2348 info->isKnrParamList = FALSE;
2349 if (identifierCount == 0)
2350 info->isParamList = FALSE;
2351 initToken (token);
2352 break;
2354 case ':':
2356 info->isKnrParamList = FALSE;
2357 break;
2359 case '.':
2361 info->isNameCandidate = FALSE;
2362 info->isKnrParamList = FALSE;
2363 break;
2365 case ',':
2367 info->isNameCandidate = FALSE;
2368 if (info->isKnrParamList)
2370 ++info->parameterCount;
2371 identifierCount = 0;
2373 break;
2375 case '=':
2377 info->isKnrParamList = FALSE;
2378 info->isNameCandidate = FALSE;
2379 if (firstChar)
2381 info->isParamList = FALSE;
2382 skipMacro (st);
2383 depth = 0;
2385 break;
2387 case '[':
2389 info->isKnrParamList = FALSE;
2390 skipToMatch ("[]");
2391 break;
2393 case '<':
2395 info->isKnrParamList = FALSE;
2396 skipToMatch ("<>");
2397 break;
2399 case ')':
2401 if (firstChar)
2402 info->parameterCount = 0;
2403 --depth;
2404 break;
2406 case '(':
2408 info->isKnrParamList = FALSE;
2409 if (firstChar)
2411 info->isNameCandidate = FALSE;
2412 cppUngetc (c);
2413 skipMacro (st);
2414 depth = 0;
2416 else if (isType (token, TOKEN_PAREN_NAME))
2418 c = skipToNonWhite ();
2419 if (c == '*') /* check for function pointer */
2421 skipToMatch ("()");
2422 c = skipToNonWhite ();
2423 if (c == '(')
2424 skipToMatch ("()");
2426 else
2428 cppUngetc (c);
2429 cppUngetc ('(');
2430 info->nestedArgs = TRUE;
2433 else
2434 ++depth;
2435 break;
2438 default:
2440 if (isident1 (c))
2442 if (++identifierCount > 1)
2443 info->isKnrParamList = FALSE;
2444 readIdentifier (token, c);
2445 if (isType (token, TOKEN_NAME) && info->isNameCandidate)
2446 token->type = TOKEN_PAREN_NAME;
2447 else if (isType (token, TOKEN_KEYWORD))
2449 info->isKnrParamList = FALSE;
2450 info->isNameCandidate = FALSE;
2453 else
2455 info->isParamList = FALSE;
2456 info->isKnrParamList = FALSE;
2457 info->isNameCandidate = FALSE;
2458 info->invalidContents = TRUE;
2460 break;
2463 firstChar = FALSE;
2464 } while (! info->nestedArgs && depth > 0 &&
2465 (info->isKnrParamList || info->isNameCandidate));
2467 if (! info->nestedArgs) while (depth > 0)
2469 skipToMatch ("()");
2470 --depth;
2472 if (st->argEndPosition == 0)
2473 st->argEndPosition = mio_tell (File.mio);
2475 if (! info->isNameCandidate)
2476 initToken (token);
2478 return nextChar;
2481 static void initParenInfo (parenInfo *const info)
2483 info->isParamList = TRUE;
2484 info->isKnrParamList = TRUE;
2485 info->isNameCandidate = TRUE;
2486 info->invalidContents = FALSE;
2487 info->nestedArgs = FALSE;
2488 info->parameterCount = 0;
2491 static void analyzeParens (statementInfo *const st)
2493 tokenInfo *const prev = prevToken (st, 1);
2495 if (! isType (prev, TOKEN_NONE)) /* in case of ignored enclosing macros */
2497 tokenInfo *const token = activeToken (st);
2498 parenInfo info;
2499 int c;
2501 initParenInfo (&info);
2502 parseParens (st, &info);
2504 c = skipToNonWhite ();
2506 cppUngetc (c);
2507 if (info.invalidContents)
2509 reinitStatement (st, FALSE);
2511 else if (info.isNameCandidate && isType (token, TOKEN_PAREN_NAME) &&
2512 ! st->gotParenName &&
2513 (! info.isParamList || ! st->haveQualifyingName ||
2514 c == '(' ||
2515 (c == '=' && st->implementation != IMP_VIRTUAL) ||
2516 (st->declaration == DECL_NONE && isOneOf (c, ",;"))))
2518 token->type = TOKEN_NAME;
2519 processName (st);
2520 st->gotParenName = TRUE;
2521 if (isLanguage(Lang_d) && c == '(' && isType (prev, TOKEN_NAME))
2523 st->declaration = DECL_FUNCTION_TEMPLATE;
2524 copyToken (st->blockName, prev);
2527 else if (! st->gotArgs && info.isParamList)
2529 st->gotArgs = TRUE;
2530 setToken (st, TOKEN_ARGS);
2531 advanceToken (st);
2532 analyzePostParens (st, &info);
2534 else
2536 setToken (st, TOKEN_NONE);
2542 * Token parsing functions
2545 static void addContext (statementInfo *const st, const tokenInfo* const token)
2547 if (isType (token, TOKEN_NAME))
2549 if (vStringLength (st->context->name) > 0)
2551 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
2552 vStringCatS (st->context->name, "::");
2553 else if (isLanguage (Lang_java) ||
2554 isLanguage (Lang_d) || isLanguage (Lang_ferite) ||
2555 isLanguage (Lang_csharp) || isLanguage (Lang_vala))
2556 vStringCatS (st->context->name, ".");
2558 vStringCat (st->context->name, token->name);
2559 st->context->type = TOKEN_NAME;
2563 static boolean inheritingDeclaration (declType decl)
2565 return (boolean) (decl == DECL_CLASS ||
2566 decl == DECL_STRUCT ||
2567 decl == DECL_INTERFACE);
2570 static void processColon (statementInfo *const st)
2572 int c = skipToNonWhite ();
2573 const boolean doubleColon = (boolean) (c == ':');
2575 if (doubleColon)
2577 setToken (st, TOKEN_DOUBLE_COLON);
2578 st->haveQualifyingName = FALSE;
2580 else
2582 cppUngetc (c);
2583 if ((((isLanguage (Lang_cpp) &&
2584 (st->declaration == DECL_CLASS || st->declaration == DECL_STRUCT)) ||
2585 isLanguage (Lang_csharp) || isLanguage (Lang_vala)) &&
2586 inheritingDeclaration (st->declaration)) ||
2587 isLanguage (Lang_d))
2589 readParents (st, ':');
2591 else if (parentDecl (st) == DECL_STRUCT || parentDecl (st) == DECL_CLASS)
2593 c = skipToOneOf (",;");
2594 if (c == ',')
2595 setToken (st, TOKEN_COMMA);
2596 else if (c == ';')
2597 setToken (st, TOKEN_SEMICOLON);
2599 else
2601 const tokenInfo *const prev = prevToken (st, 1);
2602 const tokenInfo *const prev2 = prevToken (st, 2);
2603 if (prev->keyword == KEYWORD_DEFAULT ||
2604 prev2->keyword == KEYWORD_CASE ||
2605 st->parent != NULL)
2607 reinitStatement (st, FALSE);
2613 /* Skips over any initializing value which may follow an '=' character in a
2614 * variable definition.
2616 static int skipInitializer (statementInfo *const st)
2618 boolean done = FALSE;
2619 int c;
2621 while (! done)
2623 c = skipToNonWhite ();
2625 if (c == EOF)
2626 longjmp (Exception, (int) ExceptionFormattingError);
2627 else switch (c)
2629 case ',':
2630 case ';': done = TRUE; break;
2632 case '0':
2633 if (st->implementation == IMP_VIRTUAL)
2634 st->implementation = IMP_PURE_VIRTUAL;
2635 break;
2637 case '[': skipToMatch ("[]"); break;
2638 case '(': skipToMatch ("()"); break;
2639 case '{': skipToMatch ("{}"); break;
2641 case '}':
2642 if (insideEnumBody (st))
2643 done = TRUE;
2644 else if (! isBraceFormat ())
2646 verbose ("%s: unexpected closing brace at line %lu\n",
2647 getInputFileName (), getInputLineNumber ());
2648 longjmp (Exception, (int) ExceptionBraceFormattingError);
2650 break;
2652 default: break;
2655 return c;
2658 static void processInitializer (statementInfo *const st)
2660 const boolean inEnumBody = insideEnumBody (st);
2661 const int c = skipInitializer (st);
2663 if (c == ';')
2664 setToken (st, TOKEN_SEMICOLON);
2665 else if (c == ',')
2666 setToken (st, TOKEN_COMMA);
2667 else if (c == '}' && inEnumBody)
2669 cppUngetc (c);
2670 setToken (st, TOKEN_COMMA);
2672 if (st->scope == SCOPE_EXTERN)
2673 st->scope = SCOPE_GLOBAL;
2676 static void parseIdentifier (statementInfo *const st, const int c)
2678 tokenInfo *const token = activeToken (st);
2680 readIdentifier (token, c);
2681 if (! isType (token, TOKEN_NONE))
2682 processToken (token, st);
2685 static void parseGeneralToken (statementInfo *const st, const int c)
2687 const tokenInfo *const prev = prevToken (st, 1);
2689 if (isident1(c))
2691 parseIdentifier (st, c);
2692 if (isType (st->context, TOKEN_NAME) &&
2693 isType (activeToken (st), TOKEN_NAME) && isType (prev, TOKEN_NAME))
2695 initToken (st->context);
2698 else if (isExternCDecl (st, c))
2700 st->declaration = DECL_NOMANGLE;
2701 st->scope = SCOPE_GLOBAL;
2705 /* Reads characters from the pre-processor and assembles tokens, setting
2706 * the current statement state.
2708 static void nextToken (statementInfo *const st)
2710 int c;
2711 tokenInfo *token = activeToken (st);
2714 c = skipToNonWhite();
2715 switch (c)
2717 case EOF: longjmp (Exception, (int) ExceptionEOF); break;
2718 case '(': analyzeParens (st); token = activeToken (st); break;
2719 case '*': setToken (st, TOKEN_STAR); break;
2720 case ',': setToken (st, TOKEN_COMMA); break;
2721 case ':': processColon (st); break;
2722 case ';': setToken (st, TOKEN_SEMICOLON); break;
2723 case '<': skipToMatch ("<>"); break;
2724 case '=': processInitializer (st); break;
2725 case '[':
2726 /* Hack for Vala: [..] can be a function attribute.
2727 * Seems not to have bad side effects, but have to test it more. */
2728 if (!isLanguage (Lang_vala))
2729 setToken (st, TOKEN_ARRAY);
2730 skipToMatch ("[]");
2731 break;
2732 case '{': setToken (st, TOKEN_BRACE_OPEN); break;
2733 case '}': setToken (st, TOKEN_BRACE_CLOSE); break;
2734 default: parseGeneralToken (st, c); break;
2736 } while (isType (token, TOKEN_NONE));
2738 /* We want to know about non-keyword variable types */
2739 if (TOKEN_NONE == st->firstToken->type)
2741 if ((TOKEN_NAME == token->type) || isDataTypeKeyword(token))
2742 copyToken(st->firstToken, token);
2747 * Scanning support functions
2749 static unsigned int contextual_fake_count = 0;
2750 static statementInfo *CurrentStatement = NULL;
2752 static statementInfo *newStatement (statementInfo *const parent)
2754 statementInfo *const st = xMalloc (1, statementInfo);
2755 unsigned int i;
2757 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2758 st->token [i] = newToken ();
2760 st->context = newToken ();
2761 st->blockName = newToken ();
2762 st->parentClasses = vStringNew ();
2763 st->firstToken = newToken();
2765 initStatement (st, parent);
2766 CurrentStatement = st;
2768 return st;
2771 static void deleteStatement (void)
2773 statementInfo *const st = CurrentStatement;
2774 statementInfo *const parent = st->parent;
2775 unsigned int i;
2777 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2779 deleteToken(st->token[i]); st->token[i] = NULL;
2781 deleteToken(st->blockName); st->blockName = NULL;
2782 deleteToken(st->context); st->context = NULL;
2783 vStringDelete(st->parentClasses); st->parentClasses = NULL;
2784 deleteToken(st->firstToken);
2785 eFree (st);
2786 CurrentStatement = parent;
2789 static void deleteAllStatements (void)
2791 while (CurrentStatement != NULL)
2792 deleteStatement ();
2795 static boolean isStatementEnd (const statementInfo *const st)
2797 const tokenInfo *const token = activeToken (st);
2798 boolean isEnd;
2800 if (isType (token, TOKEN_SEMICOLON))
2801 isEnd = TRUE;
2802 else if (isType (token, TOKEN_BRACE_CLOSE))
2803 /* Java, D, C#, Vala do not require semicolons to end a block. Neither do
2804 * C++ namespaces. All other blocks require a semicolon to terminate them.
2806 isEnd = (boolean) (isLanguage (Lang_java) || isLanguage (Lang_d) ||
2807 isLanguage (Lang_csharp) || isLanguage (Lang_vala) ||
2808 ! isContextualStatement (st));
2809 else
2810 isEnd = FALSE;
2812 return isEnd;
2815 static void checkStatementEnd (statementInfo *const st)
2817 const tokenInfo *const token = activeToken (st);
2818 boolean comma = isType (token, TOKEN_COMMA);
2820 if (comma || isStatementEnd (st))
2822 reinitStatementWithToken (st, activeToken (st), comma);
2824 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>"); )
2825 cppEndStatement ();
2827 else
2829 cppBeginStatement ();
2830 advanceToken (st);
2834 static void nest (statementInfo *const st, const unsigned int nestLevel)
2836 switch (st->declaration)
2838 case DECL_CLASS:
2839 case DECL_ENUM:
2840 case DECL_INTERFACE:
2841 case DECL_NAMESPACE:
2842 case DECL_NOMANGLE:
2843 case DECL_STRUCT:
2844 case DECL_UNION:
2845 createTags (nestLevel, st);
2846 break;
2847 default:
2848 skipToMatch ("{}");
2849 break;
2851 advanceToken (st);
2852 setToken (st, TOKEN_BRACE_CLOSE);
2855 static void tagCheck (statementInfo *const st)
2857 const tokenInfo *const token = activeToken (st);
2858 const tokenInfo *const prev = prevToken (st, 1);
2859 const tokenInfo *const prev2 = prevToken (st, 2);
2861 switch (token->type)
2863 case TOKEN_NAME:
2865 if (insideEnumBody (st))
2866 qualifyEnumeratorTag (st, token);
2867 break;
2869 #if 0
2870 case TOKEN_PACKAGE:
2872 if (st->haveQualifyingName)
2873 makeTag (token, st, FALSE, TAG_PACKAGE);
2874 break;
2876 #endif
2877 case TOKEN_BRACE_OPEN:
2879 if (isType (prev, TOKEN_ARGS))
2881 if (st->declaration == DECL_FUNCTION_TEMPLATE)
2882 qualifyFunctionTag (st, st->blockName);
2883 else if (st->haveQualifyingName)
2885 if (isType (prev2, TOKEN_NAME))
2886 copyToken (st->blockName, prev2);
2887 /* D structure templates */
2888 if (isLanguage (Lang_d) &&
2889 (st->declaration == DECL_CLASS || st->declaration == DECL_STRUCT ||
2890 st->declaration == DECL_INTERFACE || st->declaration == DECL_NAMESPACE))
2891 qualifyBlockTag (st, prev2);
2892 else
2894 st->declaration = DECL_FUNCTION;
2895 qualifyFunctionTag (st, prev2);
2899 else if (isContextualStatement (st))
2901 tokenInfo *name_token = (tokenInfo *)prev;
2902 boolean free_name_token = FALSE;
2904 if (isType (name_token, TOKEN_NAME))
2906 if (!isLanguage (Lang_vala))
2907 copyToken (st->blockName, name_token);
2908 else
2910 switch (st->declaration)
2912 case DECL_CLASS:
2913 case DECL_ENUM:
2914 case DECL_INTERFACE:
2915 case DECL_NAMESPACE:
2916 case DECL_STRUCT:
2917 copyToken (st->blockName, name_token);
2918 break;
2920 /* anything else can be a property */
2921 default:
2922 /* makeTag (prev, st, FALSE, TAG_PROPERTY); */
2923 /* FIXME: temporary hack to get properties shown */
2924 makeTag (prev, st, FALSE, TAG_FIELD);
2925 break;
2929 else if (isLanguage (Lang_csharp))
2930 makeTag (prev, st, FALSE, TAG_PROPERTY);
2931 else
2933 tokenInfo *contextual_token = (tokenInfo *)prev;
2934 if(isContextualKeyword (contextual_token))
2936 char buffer[64];
2938 name_token = newToken ();
2939 free_name_token = TRUE;
2940 copyToken (name_token, contextual_token);
2942 sprintf(buffer, "anon_%s_%d", name_token->name->buffer, contextual_fake_count++);
2943 vStringClear(name_token->name);
2944 vStringCatS(name_token->name, buffer);
2946 name_token->type = TOKEN_NAME;
2947 name_token->keyword = KEYWORD_NONE;
2949 advanceToken (st);
2950 contextual_token = activeToken (st);
2951 copyToken (contextual_token, token);
2952 copyToken ((tokenInfo *const)token, name_token);
2953 copyToken (st->blockName, name_token);
2954 copyToken (st->firstToken, name_token);
2957 qualifyBlockTag (st, name_token);
2958 if (free_name_token)
2959 deleteToken (name_token);
2961 break;
2963 case TOKEN_ARRAY:
2964 case TOKEN_SEMICOLON:
2965 case TOKEN_COMMA:
2967 if (insideEnumBody (st))
2969 else if (isType (prev, TOKEN_NAME))
2971 if (isContextualKeyword (prev2))
2972 makeTag (prev, st, TRUE, TAG_EXTERN_VAR);
2973 else
2974 qualifyVariableTag (st, prev);
2976 else if (isType (prev, TOKEN_ARGS) && isType (prev2, TOKEN_NAME))
2978 qualifyFunctionDeclTag (st, prev2);
2980 break;
2982 default:
2983 break;
2987 /* Parses the current file and decides whether to write out and tags that
2988 * are discovered.
2990 static void createTags (const unsigned int nestLevel,
2991 statementInfo *const parent)
2993 statementInfo *const st = newStatement (parent);
2995 DebugStatement ( if (nestLevel > 0) debugParseNest (TRUE, nestLevel); )
2996 while (TRUE)
2998 tokenInfo *token;
3000 nextToken (st);
3002 token = activeToken (st);
3004 if (isType (token, TOKEN_BRACE_CLOSE))
3006 if (nestLevel > 0)
3007 break;
3008 else
3010 verbose ("%s: unexpected closing brace at line %lu\n",
3011 getInputFileName (), getInputLineNumber ());
3012 longjmp (Exception, (int) ExceptionBraceFormattingError);
3015 else if (isType (token, TOKEN_DOUBLE_COLON))
3017 addContext (st, prevToken (st, 1));
3018 advanceToken (st);
3020 else
3022 tagCheck (st);/* this can add new token */
3023 if (isType (activeToken (st), TOKEN_BRACE_OPEN))
3024 nest (st, nestLevel + 1);
3025 checkStatementEnd (st);
3028 deleteStatement ();
3029 DebugStatement ( if (nestLevel > 0) debugParseNest (FALSE, nestLevel - 1); )
3032 static boolean findCTags (const unsigned int passCount)
3034 exception_t exception;
3035 boolean retry;
3037 contextual_fake_count = 0;
3039 Assert (passCount < 3);
3040 cppInit ((boolean) (passCount > 1), isLanguage (Lang_csharp));
3042 exception = (exception_t) setjmp (Exception);
3043 retry = FALSE;
3045 if (exception == ExceptionNone)
3047 createTags (0, NULL);
3049 else
3051 deleteAllStatements ();
3052 if (exception == ExceptionBraceFormattingError && passCount == 1)
3054 retry = TRUE;
3055 verbose ("%s: retrying file with fallback brace matching algorithm\n",
3056 getInputFileName ());
3059 cppTerminate ();
3060 return retry;
3063 static void buildKeywordHash (const langType language, unsigned int idx)
3065 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
3066 size_t i;
3067 for (i = 0 ; i < count ; ++i)
3069 const keywordDesc* const p = &KeywordTable [i];
3070 if (p->isValid [idx])
3071 addKeyword (p->name, language, (int) p->id);
3075 static void initializeCParser (const langType language)
3077 Lang_c = language;
3078 buildKeywordHash (language, 0);
3081 static void initializeCppParser (const langType language)
3083 Lang_cpp = language;
3084 buildKeywordHash (language, 1);
3087 static void initializeJavaParser (const langType language)
3089 Lang_java = language;
3090 buildKeywordHash (language, 3);
3093 static void initializeDParser (const langType language)
3095 Lang_d = language;
3096 buildKeywordHash (language, 6);
3099 static void initializeGLSLParser (const langType language)
3101 Lang_glsl = language;
3102 buildKeywordHash (language, 0); /* C keywords */
3105 static void initializeFeriteParser (const langType language)
3107 Lang_ferite = language;
3108 buildKeywordHash (language, 1); /* C++ keywords */
3111 static void initializeCsharpParser (const langType language)
3113 Lang_csharp = language;
3114 buildKeywordHash (language, 2);
3117 static void initializeValaParser (const langType language)
3119 Lang_vala = language;
3120 buildKeywordHash (language, 5);
3123 extern parserDefinition* CParser (void)
3125 static const char *const extensions [] = { "c", "pc", "sc", NULL };
3126 parserDefinition* def = parserNew ("C");
3127 def->kinds = CKinds;
3128 def->kindCount = KIND_COUNT (CKinds);
3129 def->extensions = extensions;
3130 def->parser2 = findCTags;
3131 def->initialize = initializeCParser;
3132 return def;
3135 extern parserDefinition* CppParser (void)
3137 static const char *const extensions [] = {
3138 "c++", "cc", "cp", "cpp", "cxx", "h", "h++", "hh", "hp", "hpp", "hxx",
3139 "i",
3140 #ifndef CASE_INSENSITIVE_FILENAMES
3141 "C", "H",
3142 #endif
3143 NULL
3145 parserDefinition* def = parserNew ("C++");
3146 def->kinds = CKinds;
3147 def->kindCount = KIND_COUNT (CKinds);
3148 def->extensions = extensions;
3149 def->parser2 = findCTags;
3150 def->initialize = initializeCppParser;
3151 return def;
3154 extern parserDefinition* JavaParser (void)
3156 static const char *const extensions [] = { "java", NULL };
3157 parserDefinition* def = parserNew ("Java");
3158 def->kinds = JavaKinds;
3159 def->kindCount = KIND_COUNT (JavaKinds);
3160 def->extensions = extensions;
3161 def->parser2 = findCTags;
3162 def->initialize = initializeJavaParser;
3163 return def;
3166 extern parserDefinition* DParser (void)
3168 static const char *const extensions [] = { "d", "di", NULL };
3169 parserDefinition* def = parserNew ("D");
3170 def->kinds = DKinds;
3171 def->kindCount = KIND_COUNT (DKinds);
3172 def->extensions = extensions;
3173 def->parser2 = findCTags;
3174 def->initialize = initializeDParser;
3175 return def;
3178 extern parserDefinition* GLSLParser (void)
3180 static const char *const extensions [] = { "glsl", "frag", "vert", NULL };
3181 parserDefinition* def = parserNew ("GLSL");
3182 def->kinds = CKinds;
3183 def->kindCount = KIND_COUNT (CKinds);
3184 def->extensions = extensions;
3185 def->parser2 = findCTags;
3186 def->initialize = initializeGLSLParser;
3187 return def;
3190 extern parserDefinition* FeriteParser (void)
3192 static const char *const extensions [] = { "fe", NULL };
3193 parserDefinition* def = parserNew ("Ferite");
3194 def->kinds = CKinds;
3195 def->kindCount = KIND_COUNT (CKinds);
3196 def->extensions = extensions;
3197 def->parser2 = findCTags;
3198 def->initialize = initializeFeriteParser;
3199 return def;
3202 extern parserDefinition* CsharpParser (void)
3204 static const char *const extensions [] = { "cs", NULL };
3205 parserDefinition* def = parserNew ("C#");
3206 def->kinds = CsharpKinds;
3207 def->kindCount = KIND_COUNT (CsharpKinds);
3208 def->extensions = extensions;
3209 def->parser2 = findCTags;
3210 def->initialize = initializeCsharpParser;
3211 return def;
3214 extern parserDefinition* ValaParser (void)
3216 static const char *const extensions [] = { "vala", NULL };
3217 parserDefinition* def = parserNew ("Vala");
3218 def->kinds = ValaKinds;
3219 def->kindCount = KIND_COUNT (ValaKinds);
3220 def->extensions = extensions;
3221 def->parser2 = findCTags;
3222 def->initialize = initializeValaParser;
3223 return def;
3225 /* vi:set tabstop=8 shiftwidth=4: */