Fix building with makefile.win32 from Windows command prompt, not MSYS
[geany-mirror.git] / tagmanager / c.c
blobfb8bbef4c839f134533da604f5cd5975ce271c9e
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, 0 } },
440 { "inout", KEYWORD_CONST, { 0, 0, 0, 0, 0, 0, 1 } }, /* treat like const */
441 { "input", KEYWORD_INPUT, { 0, 0, 0, 0, 1, 0, 0 } },
442 { "int", KEYWORD_INT, { 1, 1, 1, 1, 0, 1, 1 } },
443 { "integer", KEYWORD_INTEGER, { 0, 0, 0, 0, 1, 0, 0 } },
444 { "interface", KEYWORD_INTERFACE, { 0, 0, 1, 1, 1, 1, 1 } },
445 { "internal", KEYWORD_INTERNAL, { 0, 0, 1, 0, 0, 0, 0 } },
446 { "local", KEYWORD_LOCAL, { 0, 0, 0, 0, 1, 0, 0 } },
447 { "long", KEYWORD_LONG, { 1, 1, 1, 1, 0, 1, 1 } },
448 { "m_bad_state", KEYWORD_M_BAD_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
449 { "m_bad_trans", KEYWORD_M_BAD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
450 { "m_state", KEYWORD_M_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
451 { "m_trans", KEYWORD_M_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
452 { "mutable", KEYWORD_MUTABLE, { 0, 1, 0, 0, 0, 0, 0 } },
453 { "module", KEYWORD_MODULE, { 0, 0, 0, 0, 0, 0, 1 } },
454 { "namespace", KEYWORD_NAMESPACE, { 0, 1, 1, 0, 0, 1, 0 } },
455 { "native", KEYWORD_NATIVE, { 0, 0, 0, 1, 0, 0, 0 } },
456 { "new", KEYWORD_NEW, { 0, 1, 1, 1, 0, 1, 1 } },
457 { "newcov", KEYWORD_NEWCOV, { 0, 0, 0, 0, 1, 0, 0 } },
458 { "operator", KEYWORD_OPERATOR, { 0, 1, 1, 0, 0, 0, 0 } },
459 { "out", KEYWORD_OUT, { 0, 0, 0, 0, 0, 1, 1 } },
460 { "output", KEYWORD_OUTPUT, { 0, 0, 0, 0, 1, 0, 0 } },
461 { "overload", KEYWORD_OVERLOAD, { 0, 1, 0, 0, 0, 0, 0 } },
462 { "override", KEYWORD_OVERRIDE, { 0, 0, 1, 0, 0, 1, 1 } },
463 { "package", KEYWORD_PACKAGE, { 0, 0, 0, 1, 0, 0, 1 } },
464 { "packed", KEYWORD_PACKED, { 0, 0, 0, 0, 1, 0, 0 } },
465 { "port", KEYWORD_PORT, { 0, 0, 0, 0, 1, 0, 0 } },
466 { "private", KEYWORD_PRIVATE, { 0, 1, 1, 1, 0, 1, 1 } },
467 { "program", KEYWORD_PROGRAM, { 0, 0, 0, 0, 1, 0, 0 } },
468 { "protected", KEYWORD_PROTECTED, { 0, 1, 1, 1, 1, 1, 1 } },
469 { "public", KEYWORD_PUBLIC, { 0, 1, 1, 1, 1, 1, 1 } },
470 { "ref", KEYWORD_REF, { 0, 0, 0, 0, 0, 1, 1 } },
471 { "register", KEYWORD_REGISTER, { 1, 1, 0, 0, 0, 0, 0 } },
472 { "requires", KEYWORD_ATTRIBUTE, { 0, 0, 0, 0, 0, 1, 0 } }, /* ignore */
473 { "return", KEYWORD_RETURN, { 1, 1, 1, 1, 0, 1, 1 } },
474 { "set", KEYWORD_SET, { 0, 0, 0, 0, 0, 1, 0 } },
475 { "shadow", KEYWORD_SHADOW, { 0, 0, 0, 0, 1, 0, 0 } },
476 { "short", KEYWORD_SHORT, { 1, 1, 1, 1, 0, 1, 1 } },
477 { "signal", KEYWORD_SIGNAL, { 0, 0, 0, 0, 0, 1, 0 } },
478 { "signed", KEYWORD_SIGNED, { 1, 1, 0, 0, 0, 0, 0 } },
479 { "size_t", KEYWORD_SIZE_T, { 1, 1, 0, 0, 0, 1, 1 } },
480 { "state", KEYWORD_STATE, { 0, 0, 0, 0, 1, 0, 0 } },
481 { "static", KEYWORD_STATIC, { 1, 1, 1, 1, 1, 1, 1 } },
482 { "string", KEYWORD_STRING, { 0, 0, 1, 0, 1, 1, 0 } },
483 { "struct", KEYWORD_STRUCT, { 1, 1, 1, 0, 0, 1, 1 } },
484 { "switch", KEYWORD_SWITCH, { 1, 1, 1, 1, 0, 1, 1 } },
485 { "synchronized", KEYWORD_SYNCHRONIZED, { 0, 0, 0, 1, 0, 0, 1 } },
486 { "task", KEYWORD_TASK, { 0, 0, 0, 0, 1, 0, 0 } },
487 { "template", KEYWORD_TEMPLATE, { 0, 1, 0, 0, 0, 0, 0 } },
488 { "template", KEYWORD_NAMESPACE, { 0, 0, 0, 0, 0, 0, 1 } }, /* parse block */
489 { "this", KEYWORD_THIS, { 0, 0, 1, 1, 0, 1, 0 } }, /* 0 to allow D ctor tags */
490 { "throw", KEYWORD_THROW, { 0, 1, 1, 1, 0, 1, 1 } },
491 { "throws", KEYWORD_THROWS, { 0, 0, 0, 1, 0, 1, 0 } },
492 { "trans", KEYWORD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
493 { "transition", KEYWORD_TRANSITION, { 0, 0, 0, 0, 1, 0, 0 } },
494 { "transient", KEYWORD_TRANSIENT, { 0, 0, 0, 1, 0, 0, 0 } },
495 { "try", KEYWORD_TRY, { 0, 1, 1, 0, 0, 1, 1 } },
496 { "typedef", KEYWORD_TYPEDEF, { 1, 1, 1, 0, 1, 0, 1 } },
497 { "typename", KEYWORD_TYPENAME, { 0, 1, 0, 0, 0, 0, 0 } },
498 { "uint", KEYWORD_UINT, { 0, 0, 1, 0, 0, 1, 1 } },
499 { "ulong", KEYWORD_ULONG, { 0, 0, 1, 0, 0, 1, 1 } },
500 { "union", KEYWORD_UNION, { 1, 1, 0, 0, 0, 0, 1 } },
501 { "unittest", KEYWORD_BODY, { 0, 0, 0, 0, 0, 0, 1 } }, /* ignore */
502 { "unsigned", KEYWORD_UNSIGNED, { 1, 1, 1, 0, 0, 0, 1 } },
503 { "ushort", KEYWORD_USHORT, { 0, 0, 1, 0, 0, 1, 1 } },
504 { "using", KEYWORD_USING, { 0, 1, 1, 0, 0, 1, 0 } },
505 { "version", KEYWORD_NAMESPACE, { 0, 0, 0, 0, 0, 0, 1 } }, /* parse block */
506 { "virtual", KEYWORD_VIRTUAL, { 0, 1, 1, 0, 1, 1, 0 } },
507 { "void", KEYWORD_VOID, { 1, 1, 1, 1, 1, 1, 1 } },
508 { "volatile", KEYWORD_VOLATILE, { 1, 1, 1, 1, 0, 0, 1 } },
509 { "wchar_t", KEYWORD_WCHAR_T, { 1, 1, 1, 0, 0, 0, 1 } },
510 { "weak", KEYWORD_WEAK, { 0, 0, 0, 0, 0, 1, 0 } },
511 { "while", KEYWORD_WHILE, { 1, 1, 1, 1, 0, 1, 1 } }
516 * FUNCTION PROTOTYPES
518 static void createTags (const unsigned int nestLevel, statementInfo *const parent);
519 static void copyToken (tokenInfo *const dest, const tokenInfo *const src);
520 static const char *getVarType (const statementInfo *const st);
523 * FUNCTION DEFINITIONS
526 /* Debugging functions added by Biswa */
527 #if defined(DEBUG_C) && DEBUG_C
528 static char *tokenTypeName[] = {
529 "none", "args", "'}'", "'{'", "','", "'::'", "keyword", "name",
530 "package", "paren-name", "';'", "spec", "*", "[]", "count"
533 static char *tagScopeNames[] = {
534 "global", "static", "extern", "friend", "typedef", "count"};
536 static char *declTypeNames[] = {
537 "none", "base", "class", "enum", "function", "ignore", "interface",
538 "namespace", "nomangle", "package", "struct", "union", "count"};
540 static char *impTypeNames[] = {
541 "default", "abstract", "virtual", "pure-virtual", "count"};
543 void printToken(const tokenInfo *const token)
545 fprintf(stderr, "Type: %s, Keyword: %d, name: %s\n", tokenTypeName[token->type],
546 token->keyword, vStringValue(token->name));
549 void printTagEntry(const tagEntryInfo *tag)
551 fprintf(stderr, "Tag: %s (%s) [ impl: %s, scope: %s, type: %s\n", tag->name,
552 tag->kindName, tag->extensionFields.implementation, tag->extensionFields.scope[1],
553 tag->extensionFields.varType);
556 void printStatement(const statementInfo *const statement)
558 int i;
559 statementInfo *st = (statementInfo *) statement;
560 while (NULL != st)
562 fprintf(stderr, "Statement Info:\n------------------------\n");
563 fprintf(stderr, "scope: %s, decl: %s, impl: %s\n", tagScopeNames[st->scope],
564 declTypeNames[st->declaration], impTypeNames[st->implementation]);
565 for (i=0; i < NumTokens; ++i)
567 fprintf(stderr, "Token %d %s: ", i, (i == st->tokenIndex)?"(current)":"");
568 printToken(st->token[i]);
570 fprintf(stderr, "Context: ");
571 printToken(st->context);
572 fprintf(stderr, "Block: ");
573 printToken(st->blockName);
574 fprintf(stderr, "Parent classes: %s\n", vStringValue(st->parentClasses));
575 fprintf(stderr, "First token: ");
576 printToken(st->firstToken);
577 if (NULL != st->parent)
578 fprintf(stderr, "Printing Parent:\n");
579 st = st->parent;
581 fprintf(stderr, "-----------------------------------------------\n");
583 #endif
585 extern boolean includingDefineTags (void)
587 if (isLanguage(Lang_c) ||
588 isLanguage(Lang_cpp) ||
589 isLanguage(Lang_csharp) ||
590 isLanguage(Lang_ferite) ||
591 isLanguage(Lang_glsl) ||
592 isLanguage(Lang_vala))
593 return CKinds [CK_DEFINE].enabled;
595 return FALSE;
599 * Token management
602 static void initToken (tokenInfo* const token)
604 token->type = TOKEN_NONE;
605 token->keyword = KEYWORD_NONE;
606 token->lineNumber = getSourceLineNumber();
607 token->filePosition = getInputFilePosition();
608 vStringClear(token->name);
611 static void advanceToken (statementInfo* const st)
613 if (st->tokenIndex >= (unsigned int) NumTokens - 1)
614 st->tokenIndex = 0;
615 else
616 ++st->tokenIndex;
617 initToken(st->token[st->tokenIndex]);
620 static tokenInfo *prevToken (const statementInfo *const st, unsigned int n)
622 unsigned int tokenIndex;
623 unsigned int num = (unsigned int) NumTokens;
624 Assert(n < num);
625 tokenIndex = (st->tokenIndex + num - n) % num;
627 return st->token[tokenIndex];
630 static void setToken (statementInfo *const st, const tokenType type)
632 tokenInfo *token;
633 token = activeToken (st);
634 initToken(token);
635 token->type = type;
638 static void retardToken (statementInfo *const st)
640 if (st->tokenIndex == 0)
641 st->tokenIndex = (unsigned int) NumTokens - 1;
642 else
643 --st->tokenIndex;
644 setToken(st, TOKEN_NONE);
647 static tokenInfo *newToken (void)
649 tokenInfo *const token = xMalloc (1, tokenInfo);
650 token->name = vStringNew();
651 initToken(token);
652 return token;
655 static void deleteToken (tokenInfo *const token)
657 if (token != NULL)
659 vStringDelete(token->name);
660 eFree(token);
664 static const char *accessString (const accessType laccess)
666 static const char *const names [] = {
667 "?", "private", "protected", "public", "default"
669 Assert (sizeof (names) / sizeof (names [0]) == ACCESS_COUNT);
670 Assert ((int) laccess < ACCESS_COUNT);
671 return names[(int) laccess];
674 static const char *implementationString (const impType imp)
676 static const char *const names [] = {
677 "?", "abstract", "virtual", "pure virtual"
679 Assert (sizeof (names) / sizeof (names [0]) == IMP_COUNT);
680 Assert ((int) imp < IMP_COUNT);
681 return names [(int) imp];
685 * Debugging functions
688 #ifdef TM_DEBUG
690 #define boolString(c) ((c) ? "TRUE" : "FALSE")
692 static const char *tokenString (const tokenType type)
694 static const char *const names [] = {
695 "none", "args", "}", "{", "comma", "double colon", "keyword", "name",
696 "package", "paren-name", "semicolon", "specifier", "*", "[]"
698 Assert (sizeof (names) / sizeof (names [0]) == TOKEN_COUNT);
699 Assert ((int) type < TOKEN_COUNT);
700 return names[(int) type];
703 static const char *scopeString (const tagScope scope)
705 static const char *const names [] = {
706 "global", "static", "extern", "friend", "typedef"
708 Assert (sizeof (names) / sizeof (names [0]) == SCOPE_COUNT);
709 Assert ((int) scope < SCOPE_COUNT);
710 return names[(int) scope];
713 static const char *declString (const declType declaration)
715 static const char *const names [] = {
716 "?", "base", "class", "enum", "event", "signal", "function",
717 "function template", "ignore", "interface", "module", "namespace",
718 "no mangle", "package", "struct", "union",
720 Assert (sizeof (names) / sizeof (names [0]) == DECL_COUNT);
721 Assert ((int) declaration < DECL_COUNT);
722 return names[(int) declaration];
725 static const char *keywordString (const keywordId keyword)
727 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
728 const char *name = "none";
729 size_t i;
730 for (i = 0 ; i < count ; ++i)
732 const keywordDesc *p = &KeywordTable[i];
734 if (p->id == keyword)
736 name = p->name;
737 break;
740 return name;
743 static void __unused__ pt (tokenInfo *const token)
745 if (isType (token, TOKEN_NAME))
746 printf("type: %-12s: %-13s line: %lu\n",
747 tokenString (token->type), vStringValue (token->name),
748 token->lineNumber);
749 else if (isType (token, TOKEN_KEYWORD))
750 printf("type: %-12s: %-13s line: %lu\n",
751 tokenString (token->type), keywordString (token->keyword),
752 token->lineNumber);
753 else
754 printf("type: %-12s line: %lu\n",
755 tokenString (token->type), token->lineNumber);
758 static void __unused__ ps (statementInfo *const st)
760 unsigned int i;
761 printf("scope: %s decl: %s gotName: %s gotParenName: %s\n",
762 scopeString (st->scope), declString (st->declaration),
763 boolString (st->gotName), boolString (st->gotParenName));
764 printf("haveQualifyingName: %s\n", boolString (st->haveQualifyingName));
765 printf("access: %s default: %s\n", accessString (st->member.access),
766 accessString (st->member.accessDefault));
767 printf("token : ");
768 pt(activeToken (st));
769 for (i = 1 ; i < (unsigned int) NumTokens ; ++i)
771 printf("prev %u : ", i);
772 pt(prevToken (st, i));
774 printf("context: ");
775 pt(st->context);
778 #endif
781 * Statement management
784 static boolean isDataTypeKeyword (const tokenInfo *const token)
786 switch (token->keyword)
788 case KEYWORD_BOOLEAN:
789 case KEYWORD_BYTE:
790 case KEYWORD_CHAR:
791 case KEYWORD_DOUBLE:
792 case KEYWORD_FLOAT:
793 case KEYWORD_INT:
794 case KEYWORD_LONG:
795 case KEYWORD_SHORT:
796 case KEYWORD_VOID:
797 case KEYWORD_WCHAR_T:
798 case KEYWORD_SIZE_T:
799 return TRUE;
800 default:
801 return FALSE;
805 #if 0
806 static boolean isVariableKeyword (const tokenInfo *const token)
808 switch (token->keyword)
810 case KEYWORD_CONST:
811 case KEYWORD_EXTERN:
812 case KEYWORD_REGISTER:
813 case KEYWORD_STATIC:
814 case KEYWORD_VIRTUAL:
815 case KEYWORD_SIGNED:
816 case KEYWORD_UNSIGNED:
817 return TRUE;
818 default:
819 return FALSE;
822 #endif
824 static boolean isContextualKeyword (const tokenInfo *const token)
826 boolean result;
827 switch (token->keyword)
829 case KEYWORD_CLASS:
830 case KEYWORD_ENUM:
831 case KEYWORD_INTERFACE:
832 case KEYWORD_NAMESPACE:
833 case KEYWORD_STRUCT:
834 case KEYWORD_UNION:
836 result = TRUE;
837 break;
840 default:
842 result = FALSE;
843 break;
846 return result;
849 static boolean isContextualStatement (const statementInfo *const st)
851 boolean result = FALSE;
853 if (st != NULL)
855 if (isLanguage (Lang_vala))
857 /* All can be a contextual statment as properties can be of any type */
858 result = TRUE;
860 else
862 switch (st->declaration)
864 case DECL_CLASS:
865 case DECL_ENUM:
866 case DECL_INTERFACE:
867 case DECL_NAMESPACE:
868 case DECL_STRUCT:
869 case DECL_UNION:
871 result = TRUE;
872 break;
875 default:
877 result = FALSE;
878 break;
883 return result;
886 static boolean isMember (const statementInfo *const st)
888 boolean result;
889 if (isType (st->context, TOKEN_NAME))
890 result = TRUE;
891 else
892 result = isContextualStatement (st->parent);
893 return result;
896 static void initMemberInfo (statementInfo *const st)
898 accessType accessDefault = ACCESS_UNDEFINED;
900 if (st->parent != NULL) switch (st->parent->declaration)
902 case DECL_ENUM:
903 case DECL_NAMESPACE:
905 accessDefault = ACCESS_UNDEFINED;
906 break;
908 case DECL_CLASS:
910 if (isLanguage (Lang_java))
911 accessDefault = ACCESS_DEFAULT;
912 else
913 accessDefault = ACCESS_PRIVATE;
914 break;
916 case DECL_INTERFACE:
917 case DECL_STRUCT:
918 case DECL_UNION:
920 accessDefault = ACCESS_PUBLIC;
921 break;
923 default:
924 break;
926 st->member.accessDefault = accessDefault;
927 st->member.access = accessDefault;
930 static void reinitStatement (statementInfo *const st, const boolean partial)
932 unsigned int i;
934 if (! partial)
936 st->scope = SCOPE_GLOBAL;
937 if (isContextualStatement (st->parent))
938 st->declaration = DECL_BASE;
939 else
940 st->declaration = DECL_NONE;
942 st->gotParenName = FALSE;
943 st->implementation = IMP_DEFAULT;
944 st->gotArgs = FALSE;
945 st->gotName = FALSE;
946 st->haveQualifyingName = FALSE;
947 st->argEndPosition = 0;
949 st->tokenIndex = 0;
950 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
952 initToken (st->token [i]);
955 initToken (st->context);
956 initToken (st->blockName);
957 vStringClear (st->parentClasses);
959 /* Init member info. */
960 if (! partial)
961 st->member.access = st->member.accessDefault;
963 /* Init first token */
964 if (!partial)
965 initToken(st->firstToken);
968 static void reinitStatementWithToken (statementInfo *const st,
969 tokenInfo *token, const boolean partial)
971 tokenInfo *const save = newToken ();
972 /* given token can be part of reinit statementInfo */
973 copyToken (save, token);
974 reinitStatement (st, partial);
975 token = activeToken (st);
976 copyToken (token, save);
977 deleteToken (save);
978 ++st->tokenIndex; /* this is quite save becouse current tokenIndex = 0 */
981 static void initStatement (statementInfo *const st, statementInfo *const parent)
983 st->parent = parent;
984 initMemberInfo (st);
985 reinitStatement (st, FALSE);
986 if (parent)
988 const tokenInfo *const src = activeToken (parent);
989 tokenInfo *const dst = activeToken (st);
990 copyToken (dst, src);
991 st->tokenIndex++;
996 * Tag generation functions
998 static cKind cTagKind (const tagType type)
1000 cKind result = CK_UNDEFINED;
1001 switch (type)
1003 case TAG_CLASS: result = CK_CLASS; break;
1004 case TAG_ENUM: result = CK_ENUMERATION; break;
1005 case TAG_ENUMERATOR: result = CK_ENUMERATOR; break;
1006 case TAG_FUNCTION: result = CK_FUNCTION; break;
1007 case TAG_MEMBER: result = CK_MEMBER; break;
1008 case TAG_NAMESPACE: result = CK_NAMESPACE; break;
1009 case TAG_PROTOTYPE: result = CK_PROTOTYPE; break;
1010 case TAG_STRUCT: result = CK_STRUCT; break;
1011 case TAG_TYPEDEF: result = CK_TYPEDEF; break;
1012 case TAG_UNION: result = CK_UNION; break;
1013 case TAG_VARIABLE: result = CK_VARIABLE; break;
1014 case TAG_EXTERN_VAR: result = CK_EXTERN_VARIABLE; break;
1016 default: Assert ("Bad C tag type" == NULL); break;
1018 return result;
1021 static csharpKind csharpTagKind (const tagType type)
1023 csharpKind result = CSK_UNDEFINED;
1024 switch (type)
1026 case TAG_CLASS: result = CSK_CLASS; break;
1027 case TAG_ENUM: result = CSK_ENUMERATION; break;
1028 case TAG_ENUMERATOR: result = CSK_ENUMERATOR; break;
1029 case TAG_EVENT: result = CSK_EVENT; break;
1030 case TAG_FIELD: result = CSK_FIELD ; break;
1031 case TAG_INTERFACE: result = CSK_INTERFACE; break;
1032 case TAG_LOCAL: result = CSK_LOCAL; break;
1033 case TAG_METHOD: result = CSK_METHOD; break;
1034 case TAG_NAMESPACE: result = CSK_NAMESPACE; break;
1035 case TAG_PROPERTY: result = CSK_PROPERTY; break;
1036 case TAG_STRUCT: result = CSK_STRUCT; break;
1037 case TAG_TYPEDEF: result = CSK_TYPEDEF; break;
1039 default: Assert ("Bad C# tag type" == NULL); break;
1041 return result;
1044 static dKind dTagKind (const tagType type)
1046 dKind result = DK_UNDEFINED;
1047 switch (type)
1049 case TAG_CLASS: result = DK_CLASS; break;
1050 case TAG_ENUM: result = DK_ENUMERATION; break;
1051 case TAG_ENUMERATOR: result = DK_ENUMERATOR; break;
1052 case TAG_FUNCTION: result = DK_FUNCTION; break;
1053 case TAG_INTERFACE: result = DK_INTERFACE; break;
1054 case TAG_MEMBER: result = DK_MEMBER; break;
1055 case TAG_NAMESPACE: result = DK_NAMESPACE; break;
1056 case TAG_PROTOTYPE: result = DK_PROTOTYPE; break;
1057 case TAG_STRUCT: result = DK_STRUCT; break;
1058 case TAG_TYPEDEF: result = DK_TYPEDEF; break;
1059 case TAG_UNION: result = DK_UNION; break;
1060 case TAG_VARIABLE: result = DK_VARIABLE; break;
1061 case TAG_EXTERN_VAR: result = DK_EXTERN_VARIABLE; break;
1063 default: Assert ("Bad D tag type" == NULL); break;
1065 return result;
1068 static valaKind valaTagKind (const tagType type)
1070 valaKind result = VK_UNDEFINED;
1071 switch (type)
1073 case TAG_CLASS: result = VK_CLASS; break;
1074 case TAG_ENUM: result = VK_ENUMERATION; break;
1075 case TAG_ENUMERATOR: result = VK_ENUMERATOR; break;
1076 case TAG_SIGNAL: result = VK_SIGNAL; break;
1077 case TAG_FIELD: result = VK_FIELD ; break;
1078 case TAG_INTERFACE: result = VK_INTERFACE; break;
1079 case TAG_LOCAL: result = VK_LOCAL; break;
1080 case TAG_METHOD: result = VK_METHOD; break;
1081 case TAG_NAMESPACE: result = VK_NAMESPACE; break;
1082 case TAG_PROPERTY: result = VK_PROPERTY; break;
1083 case TAG_STRUCT: result = VK_STRUCT; break;
1085 default: Assert ("Bad Vala tag type" == NULL); break;
1087 return result;
1090 static javaKind javaTagKind (const tagType type)
1092 javaKind result = JK_UNDEFINED;
1093 switch (type)
1095 case TAG_CLASS: result = JK_CLASS; break;
1096 case TAG_FIELD: result = JK_FIELD; break;
1097 case TAG_INTERFACE: result = JK_INTERFACE; break;
1098 case TAG_METHOD: result = JK_METHOD; break;
1099 case TAG_PACKAGE: result = JK_PACKAGE; break;
1101 default: Assert ("Bad Java tag type" == NULL); break;
1103 return result;
1106 static const char *tagName (const tagType type)
1108 const char* result;
1109 if (isLanguage (Lang_java))
1110 result = JavaKinds [javaTagKind (type)].name;
1111 else if (isLanguage (Lang_csharp))
1112 result = CsharpKinds [csharpTagKind (type)].name;
1113 else if (isLanguage (Lang_d))
1114 result = DKinds [dTagKind (type)].name;
1115 else if (isLanguage (Lang_vala))
1116 result = ValaKinds [valaTagKind (type)].name;
1117 else
1118 result = CKinds [cTagKind (type)].name;
1119 return result;
1122 static int tagLetter (const tagType type)
1124 int result;
1125 if (isLanguage (Lang_csharp))
1126 result = CsharpKinds [csharpTagKind (type)].letter;
1127 else if (isLanguage (Lang_d))
1128 result = DKinds [dTagKind (type)].letter;
1129 else if (isLanguage (Lang_java))
1130 result = JavaKinds [javaTagKind (type)].letter;
1131 else if (isLanguage (Lang_vala))
1132 result = ValaKinds [valaTagKind (type)].letter;
1133 else
1134 result = CKinds [cTagKind (type)].letter;
1135 return result;
1139 static boolean includeTag (const tagType type, const boolean isFileScope)
1141 boolean result;
1142 if (isFileScope && ! Option.include.fileScope)
1143 result = FALSE;
1144 else if (isLanguage (Lang_java))
1145 result = JavaKinds [javaTagKind (type)].enabled;
1146 else
1147 result = CKinds [cTagKind (type)].enabled;
1148 return result;
1152 static tagType declToTagType (const declType declaration)
1154 tagType type = TAG_UNDEFINED;
1156 switch (declaration)
1158 case DECL_CLASS: type = TAG_CLASS; break;
1159 case DECL_ENUM: type = TAG_ENUM; break;
1160 case DECL_FUNCTION: type = TAG_FUNCTION; break;
1161 case DECL_FUNCTION_TEMPLATE: type = TAG_FUNCTION; break;
1162 case DECL_INTERFACE:type = TAG_INTERFACE; break;
1163 case DECL_NAMESPACE:type = TAG_NAMESPACE; break;
1164 case DECL_STRUCT: type = TAG_STRUCT; break;
1165 case DECL_UNION: type = TAG_UNION; break;
1167 default: Assert ("Unexpected declaration" == NULL); break;
1169 return type;
1172 static const char* accessField (const statementInfo *const st)
1174 const char* result = NULL;
1176 if ((isLanguage (Lang_cpp) || isLanguage (Lang_d) || isLanguage (Lang_ferite)) &&
1177 st->scope == SCOPE_FRIEND)
1178 result = "friend";
1179 else if (st->member.access != ACCESS_UNDEFINED)
1180 result = accessString (st->member.access);
1181 return result;
1184 static void addOtherFields (tagEntryInfo* const tag, const tagType type,
1185 const statementInfo *const st, vString *const scope)
1187 /* For selected tag types, append an extension flag designating the
1188 * parent object in which the tag is defined.
1190 switch (type)
1192 default: break;
1194 case TAG_CLASS:
1195 case TAG_ENUM:
1196 case TAG_ENUMERATOR:
1197 case TAG_FIELD:
1198 case TAG_FUNCTION:
1199 case TAG_INTERFACE:
1200 case TAG_MEMBER:
1201 case TAG_METHOD:
1202 case TAG_PROTOTYPE:
1203 case TAG_STRUCT:
1204 case TAG_TYPEDEF:
1205 case TAG_UNION:
1207 if (vStringLength (scope) > 0 &&
1208 (isMember (st) || st->parent->declaration == DECL_NAMESPACE))
1210 if (isType (st->context, TOKEN_NAME))
1211 tag->extensionFields.scope [0] = tagName (TAG_CLASS);
1212 else
1213 tag->extensionFields.scope [0] =
1214 tagName (declToTagType (parentDecl (st)));
1215 tag->extensionFields.scope [1] = vStringValue (scope);
1217 if ((type == TAG_CLASS || type == TAG_INTERFACE ||
1218 type == TAG_STRUCT) && vStringLength (st->parentClasses) > 0)
1220 tag->extensionFields.inheritance =
1221 vStringValue (st->parentClasses);
1223 if (st->implementation != IMP_DEFAULT &&
1224 (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) || isLanguage (Lang_vala) ||
1225 isLanguage (Lang_java) || isLanguage (Lang_d) || isLanguage (Lang_ferite)))
1227 tag->extensionFields.implementation =
1228 implementationString (st->implementation);
1230 if (isMember (st))
1232 tag->extensionFields.access = accessField (st);
1234 if ((TRUE == st->gotArgs) && (TRUE == Option.extensionFields.argList) &&
1235 ((TAG_FUNCTION == type) || (TAG_METHOD == type) || (TAG_PROTOTYPE == type)))
1237 tag->extensionFields.arglist = getArglistFromFilePos(
1238 tag->filePosition, tag->name);
1240 break;
1244 if ((TAG_FIELD == tag->type) || (TAG_MEMBER == tag->type) ||
1245 (TAG_EXTERN_VAR == tag->type) || (TAG_TYPEDEF == tag->type) ||
1246 (TAG_VARIABLE == tag->type) || (TAG_METHOD == tag->type) ||
1247 (TAG_PROTOTYPE == tag->type) || (TAG_FUNCTION == tag->type))
1249 if (((TOKEN_NAME == st->firstToken->type) || isDataTypeKeyword(st->firstToken))
1250 && (0 != strcmp(vStringValue(st->firstToken->name), tag->name)))
1252 tag->extensionFields.varType = getVarType(st);
1257 static const char *getVarType (const statementInfo *const st)
1259 static vString *vt = NULL;
1260 unsigned int i;
1262 if (! st->gotArgs)
1263 return vStringValue(st->firstToken->name); /* ignore non-functions */
1265 if (vt == NULL)
1266 vt = vStringNew();
1267 else
1268 vStringClear(vt);
1270 for (i = 0; i < st->tokenIndex; i++)
1272 tokenInfo *t = st->token[i];
1274 switch (t->type)
1276 case TOKEN_NAME: /* user typename */
1277 if (strcmp(vStringValue(t->name), vStringValue(st->firstToken->name)) != 0)
1278 continue;
1279 break;
1280 case TOKEN_KEYWORD:
1281 if (t->keyword != KEYWORD_EXTERN && t->keyword != KEYWORD_STATIC) /* uninteresting keywords */
1282 break;
1283 continue;
1284 case TOKEN_STAR: vStringCatS(vt, " *"); continue;
1285 case TOKEN_ARRAY: vStringCatS(vt, "[]"); continue;
1286 default: continue;
1288 if (vStringLength(vt) > 0)
1289 if (isalpha(vStringValue(vt)[vStringLength(vt) - 1]))
1290 vStringPut(vt, ' ');
1291 vStringCat(vt, t->name);
1293 vStringTerminate(vt);
1294 return vStringValue(vt);
1297 static void addContextSeparator (vString *const scope)
1299 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
1300 vStringCatS (scope, "::");
1301 else if (isLanguage (Lang_java) || isLanguage (Lang_d) || isLanguage (Lang_ferite) ||
1302 isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1303 vStringCatS (scope, ".");
1306 static void findScopeHierarchy (vString *const string,
1307 const statementInfo *const st)
1309 const char* const anon = "<anonymous>";
1310 boolean nonAnonPresent = FALSE;
1312 vStringClear (string);
1313 if (isType (st->context, TOKEN_NAME))
1315 vStringCopy (string, st->context->name);
1316 nonAnonPresent = TRUE;
1318 if (st->parent != NULL)
1320 vString *temp = vStringNew ();
1321 const statementInfo *s;
1323 for (s = st->parent ; s != NULL ; s = s->parent)
1325 if (isContextualStatement (s) ||
1326 s->declaration == DECL_NAMESPACE)
1328 vStringCopy (temp, string);
1329 vStringClear (string);
1330 if (isType (s->blockName, TOKEN_NAME))
1332 if (isType (s->context, TOKEN_NAME) &&
1333 vStringLength (s->context->name) > 0)
1335 vStringCat (string, s->context->name);
1336 addContextSeparator (string);
1338 vStringCat (string, s->blockName->name);
1339 nonAnonPresent = TRUE;
1341 else
1342 vStringCopyS (string, anon);
1343 if (vStringLength (temp) > 0)
1344 addContextSeparator (string);
1345 vStringCat (string, temp);
1348 vStringDelete (temp);
1350 if (! nonAnonPresent)
1351 vStringClear (string);
1355 static void makeExtraTagEntry (const tagType type, tagEntryInfo *const e,
1356 vString *const scope)
1358 if (Option.include.qualifiedTags &&
1359 scope != NULL && vStringLength (scope) > 0)
1361 vString *const scopedName = vStringNew ();
1363 if (type != TAG_ENUMERATOR)
1364 vStringCopy (scopedName, scope);
1365 else
1367 /* remove last component (i.e. enumeration name) from scope */
1368 const char* const sc = vStringValue (scope);
1369 const char* colon = strrchr (sc, ':');
1370 if (colon != NULL)
1372 while (*colon == ':' && colon > sc)
1373 --colon;
1374 vStringNCopy (scopedName, scope, colon + 1 - sc);
1377 if (vStringLength (scopedName) > 0)
1379 addContextSeparator (scopedName);
1380 vStringCatS (scopedName, e->name);
1381 e->name = vStringValue (scopedName);
1382 makeTagEntry (e);
1384 vStringDelete (scopedName);
1388 static void makeTag (const tokenInfo *const token,
1389 const statementInfo *const st,
1390 boolean isFileScope, const tagType type)
1392 #ifdef DEBUG_C
1393 printToken(token);
1394 fprintf(stderr, "<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>\n");
1395 printStatement(st);
1396 #endif
1397 /* Nothing is really of file scope when it appears in a header file.
1399 isFileScope = (boolean) (isFileScope && ! isHeaderFile ());
1401 if (isType (token, TOKEN_NAME) && vStringLength (token->name) > 0 /* &&
1402 includeTag (type, isFileScope) */)
1404 vString *scope = vStringNew ();
1405 tagEntryInfo e;
1407 /* take only functions which are introduced by "function ..." */
1408 if (type == TAG_FUNCTION && isLanguage (Lang_ferite) &&
1409 strncmp("function", st->firstToken->name->buffer, 8) != 0)
1411 return;
1414 initTagEntry (&e, vStringValue (token->name));
1416 e.lineNumber = token->lineNumber;
1417 e.filePosition = token->filePosition;
1418 e.isFileScope = isFileScope;
1419 e.kindName = tagName (type);
1420 e.kind = tagLetter (type);
1421 e.type = type;
1423 findScopeHierarchy (scope, st);
1424 addOtherFields (&e, type, st, scope);
1426 #ifdef DEBUG_C
1427 printTagEntry(&e);
1428 #endif
1429 makeTagEntry (&e);
1430 if (NULL != TagEntryFunction)
1431 makeExtraTagEntry (type, &e, scope);
1432 vStringDelete (scope);
1433 if (NULL != e.extensionFields.arglist)
1434 free((char *) e.extensionFields.arglist);
1438 static boolean isValidTypeSpecifier (const declType declaration)
1440 boolean result;
1441 switch (declaration)
1443 case DECL_BASE:
1444 case DECL_CLASS:
1445 case DECL_ENUM:
1446 case DECL_STRUCT:
1447 case DECL_UNION:
1448 result = TRUE;
1449 break;
1451 default:
1452 result = FALSE;
1453 break;
1455 return result;
1458 static void qualifyEnumeratorTag (const statementInfo *const st,
1459 const tokenInfo *const nameToken)
1461 if (isType (nameToken, TOKEN_NAME))
1462 makeTag (nameToken, st, TRUE, TAG_ENUMERATOR);
1465 static void qualifyFunctionTag (const statementInfo *const st,
1466 const tokenInfo *const nameToken)
1468 if (isType (nameToken, TOKEN_NAME))
1470 const tagType type = (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1471 ? TAG_METHOD : TAG_FUNCTION;
1472 const boolean isFileScope =
1473 (boolean) (st->member.access == ACCESS_PRIVATE ||
1474 (!isMember (st) && st->scope == SCOPE_STATIC));
1476 makeTag (nameToken, st, isFileScope, type);
1480 static void qualifyFunctionDeclTag (const statementInfo *const st,
1481 const tokenInfo *const nameToken)
1483 if (! isType (nameToken, TOKEN_NAME))
1485 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1486 qualifyFunctionTag (st, nameToken);
1487 else if (st->scope == SCOPE_TYPEDEF)
1488 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1489 else if (isValidTypeSpecifier (st->declaration) &&
1490 ! (isLanguage (Lang_csharp) || isLanguage (Lang_vala)))
1491 makeTag (nameToken, st, TRUE, TAG_PROTOTYPE);
1494 static void qualifyCompoundTag (const statementInfo *const st,
1495 const tokenInfo *const nameToken)
1497 if (isType (nameToken, TOKEN_NAME))
1499 const tagType type = declToTagType (st->declaration);
1501 if (type != TAG_UNDEFINED)
1502 makeTag (nameToken, st, (boolean) (! isLanguage (Lang_java) &&
1503 ! isLanguage (Lang_csharp) &&
1504 ! isLanguage (Lang_vala)), type);
1508 static void qualifyBlockTag (statementInfo *const st,
1509 const tokenInfo *const nameToken)
1511 switch (st->declaration)
1513 case DECL_CLASS:
1514 case DECL_ENUM:
1515 case DECL_INTERFACE:
1516 case DECL_NAMESPACE:
1517 case DECL_STRUCT:
1518 case DECL_UNION:
1519 qualifyCompoundTag (st, nameToken);
1520 break;
1521 default: break;
1525 static void qualifyVariableTag (const statementInfo *const st,
1526 const tokenInfo *const nameToken)
1528 /* We have to watch that we do not interpret a declaration of the
1529 * form "struct tag;" as a variable definition. In such a case, the
1530 * token preceding the name will be a keyword.
1532 if (! isType (nameToken, TOKEN_NAME))
1534 else if (st->declaration == DECL_IGNORE)
1536 else if (st->scope == SCOPE_TYPEDEF)
1537 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1538 else if (st->declaration == DECL_PACKAGE)
1539 makeTag (nameToken, st, FALSE, TAG_PACKAGE);
1540 else if (st->declaration == DECL_MODULE) /* handle modules in D as namespaces */
1541 makeTag (nameToken, st, FALSE, TAG_NAMESPACE);
1542 else if (isValidTypeSpecifier (st->declaration))
1544 if (isMember (st))
1546 if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1547 makeTag (nameToken, st, (boolean) (st->member.access == ACCESS_PRIVATE), TAG_FIELD);
1548 else if (st->scope == SCOPE_GLOBAL || st->scope == SCOPE_STATIC)
1549 makeTag (nameToken, st, TRUE, TAG_MEMBER);
1551 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1553 else
1555 if (st->scope == SCOPE_EXTERN || ! st->haveQualifyingName)
1556 makeTag (nameToken, st, FALSE, TAG_EXTERN_VAR);
1557 else
1558 makeTag (nameToken, st, (boolean) (st->scope == SCOPE_STATIC), TAG_VARIABLE);
1564 * Parsing functions
1567 static int skipToOneOf (const char *const chars)
1569 int c;
1571 c = cppGetc ();
1572 while (c != EOF && c != '\0' && strchr (chars, c) == NULL);
1574 return c;
1577 /* Skip to the next non-white character.
1579 static int skipToNonWhite (void)
1581 int c;
1585 c = cppGetc ();
1587 while (isspace (c));
1589 return c;
1592 /* Skips to the next brace in column 1. This is intended for cases where
1593 * preprocessor constructs result in unbalanced braces.
1595 static void skipToFormattedBraceMatch (void)
1597 int c, next;
1599 c = cppGetc ();
1600 next = cppGetc ();
1601 while (c != EOF && (c != '\n' || next != '}'))
1603 c = next;
1604 next = cppGetc ();
1608 /* Skip to the matching character indicated by the pair string. If skipping
1609 * to a matching brace and any brace is found within a different level of a
1610 * #if conditional statement while brace formatting is in effect, we skip to
1611 * the brace matched by its formatting. It is assumed that we have already
1612 * read the character which starts the group (i.e. the first character of
1613 * "pair").
1615 static void skipToMatch (const char *const pair)
1617 const boolean braceMatching = (boolean) (strcmp ("{}", pair) == 0);
1618 const boolean braceFormatting = (boolean) (isBraceFormat () && braceMatching);
1619 const unsigned int initialLevel = getDirectiveNestLevel ();
1620 const int begin = pair [0], end = pair [1];
1621 const unsigned long inputLineNumber = getInputLineNumber ();
1622 int matchLevel = 1;
1623 int c = '\0';
1624 while (matchLevel > 0 && (c = cppGetc ()) != EOF)
1626 if (c == begin)
1628 ++matchLevel;
1629 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1631 skipToFormattedBraceMatch ();
1632 break;
1635 else if (c == end)
1637 --matchLevel;
1638 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1640 skipToFormattedBraceMatch ();
1641 break;
1645 if (c == EOF)
1647 verbose ("%s: failed to find match for '%c' at line %lu\n",
1648 getInputFileName (), begin, inputLineNumber);
1649 if (braceMatching)
1650 longjmp (Exception, (int) ExceptionBraceFormattingError);
1651 else
1652 longjmp (Exception, (int) ExceptionFormattingError);
1656 static void skipParens (void)
1658 const int c = skipToNonWhite ();
1660 if (c == '(')
1661 skipToMatch ("()");
1662 else
1663 cppUngetc (c);
1666 static void skipBraces (void)
1668 const int c = skipToNonWhite ();
1670 if (c == '{')
1671 skipToMatch ("{}");
1672 else
1673 cppUngetc (c);
1676 static keywordId analyzeKeyword (const char *const name)
1678 const keywordId id = (keywordId) lookupKeyword (name, getSourceLanguage ());
1680 /* ignore D @attributes, but show them in function signatures */
1681 if (isLanguage(Lang_d) && id == KEYWORD_NONE && name[0] == '@')
1682 return KEYWORD_CONST;
1683 return id;
1686 static void analyzeIdentifier (tokenInfo *const token)
1688 char *const name = vStringValue (token->name);
1689 const char *replacement = NULL;
1690 boolean parensToo = FALSE;
1692 if (isLanguage (Lang_java) ||
1693 ! isIgnoreToken (name, &parensToo, &replacement))
1695 if (replacement != NULL)
1696 token->keyword = analyzeKeyword (replacement);
1697 else
1698 token->keyword = analyzeKeyword (vStringValue (token->name));
1700 if (token->keyword == KEYWORD_NONE)
1701 token->type = TOKEN_NAME;
1702 else
1703 token->type = TOKEN_KEYWORD;
1705 else
1707 initToken (token);
1708 if (parensToo)
1710 int c = skipToNonWhite ();
1712 if (c == '(')
1713 skipToMatch ("()");
1718 static void readIdentifier (tokenInfo *const token, const int firstChar)
1720 vString *const name = token->name;
1721 int c = firstChar;
1723 initToken (token);
1725 /* Bug #1585745 (CTags): strangely, C++ destructors allow whitespace between
1726 * the ~ and the class name. */
1727 if (isLanguage (Lang_cpp) && firstChar == '~')
1729 vStringPut (name, c);
1730 c = skipToNonWhite ();
1735 vStringPut (name, c);
1736 c = cppGetc ();
1737 } while (isident (c) || (isLanguage (Lang_vala) && '.' == c));
1738 vStringTerminate (name);
1739 cppUngetc (c); /* unget non-identifier character */
1741 /* Vala supports '?' at end of a type (with or without whitespace before) for nullable types */
1742 if (isLanguage (Lang_vala))
1744 c = skipToNonWhite ();
1745 if ('?' == c)
1746 vStringPut (name, c);
1747 else
1748 cppUngetc (c);
1751 analyzeIdentifier (token);
1754 static void readPackageName (tokenInfo *const token, const int firstChar)
1756 vString *const name = token->name;
1757 int c = firstChar;
1759 initToken (token);
1761 while (isident (c) || c == '.')
1763 vStringPut (name, c);
1764 c = cppGetc ();
1766 vStringTerminate (name);
1767 cppUngetc (c); /* unget non-package character */
1770 static void readPackageOrNamespace (statementInfo *const st, const declType declaration)
1772 st->declaration = declaration;
1774 if (declaration == DECL_NAMESPACE && !(isLanguage (Lang_csharp) || isLanguage (Lang_vala)))
1776 /* In C++ a namespace is specified one level at a time. */
1777 return;
1779 else
1781 /* In C#, a namespace can also be specified like a Java package name. */
1782 tokenInfo *const token = activeToken (st);
1783 Assert (isType (token, TOKEN_KEYWORD));
1784 readPackageName (token, skipToNonWhite ());
1785 token->type = TOKEN_NAME;
1786 st->gotName = TRUE;
1787 st->haveQualifyingName = TRUE;
1791 static void readPackage (statementInfo *const st)
1793 tokenInfo *const token = activeToken (st);
1794 Assert (isType (token, TOKEN_KEYWORD));
1795 readPackageName (token, skipToNonWhite ());
1796 token->type = TOKEN_NAME;
1797 if (isLanguage (Lang_d))
1798 st->declaration = DECL_MODULE;
1799 else
1800 st->declaration = DECL_PACKAGE;
1801 st->gotName = TRUE;
1802 st->haveQualifyingName = TRUE;
1805 static void processName (statementInfo *const st)
1807 Assert (isType (activeToken (st), TOKEN_NAME));
1808 if (st->gotName && st->declaration == DECL_NONE)
1809 st->declaration = DECL_BASE;
1810 st->gotName = TRUE;
1811 st->haveQualifyingName = TRUE;
1814 static void readOperator (statementInfo *const st)
1816 const char *const acceptable = "+-*/%^&|~!=<>,[]";
1817 const tokenInfo* const prev = prevToken (st,1);
1818 tokenInfo *const token = activeToken (st);
1819 vString *const name = token->name;
1820 int c = skipToNonWhite ();
1822 /* When we arrive here, we have the keyword "operator" in 'name'.
1824 if (isType (prev, TOKEN_KEYWORD) && (prev->keyword == KEYWORD_ENUM ||
1825 prev->keyword == KEYWORD_STRUCT || prev->keyword == KEYWORD_UNION))
1826 ; /* ignore "operator" keyword if preceded by these keywords */
1827 else if (c == '(')
1829 /* Verify whether this is a valid function call (i.e. "()") operator.
1831 if (cppGetc () == ')')
1833 vStringPut (name, ' '); /* always separate operator from keyword */
1834 c = skipToNonWhite ();
1835 if (c == '(')
1836 vStringCatS (name, "()");
1838 else
1840 skipToMatch ("()");
1841 c = cppGetc ();
1844 else if (isident1 (c))
1846 /* Handle "new" and "delete" operators, and conversion functions
1847 * (per 13.3.1.1.2 [2] of the C++ spec).
1849 boolean whiteSpace = TRUE; /* default causes insertion of space */
1852 if (isspace (c))
1853 whiteSpace = TRUE;
1854 else
1856 if (whiteSpace)
1858 vStringPut (name, ' ');
1859 whiteSpace = FALSE;
1861 vStringPut (name, c);
1863 c = cppGetc ();
1864 } while (! isOneOf (c, "(;") && c != EOF);
1865 vStringTerminate (name);
1867 else if (isOneOf (c, acceptable))
1869 vStringPut (name, ' '); /* always separate operator from keyword */
1872 vStringPut (name, c);
1873 c = cppGetc ();
1874 } while (isOneOf (c, acceptable));
1875 vStringTerminate (name);
1878 cppUngetc (c);
1880 token->type = TOKEN_NAME;
1881 token->keyword = KEYWORD_NONE;
1882 processName (st);
1885 static void copyToken (tokenInfo *const dest, const tokenInfo *const src)
1887 dest->type = src->type;
1888 dest->keyword = src->keyword;
1889 dest->filePosition = src->filePosition;
1890 dest->lineNumber = src->lineNumber;
1891 vStringCopy (dest->name, src->name);
1894 static void setAccess (statementInfo *const st, const accessType laccess)
1896 if (isMember (st))
1898 if (isLanguage (Lang_cpp) || isLanguage (Lang_d) || isLanguage (Lang_ferite))
1900 int c = skipToNonWhite ();
1902 if (c == ':')
1903 reinitStatementWithToken (st, prevToken (st, 1), FALSE);
1904 else
1905 cppUngetc (c);
1907 st->member.accessDefault = laccess;
1909 st->member.access = laccess;
1913 static void discardTypeList (tokenInfo *const token)
1915 int c = skipToNonWhite ();
1916 while (isident1 (c))
1918 readIdentifier (token, c);
1919 c = skipToNonWhite ();
1920 if (c == '.' || c == ',')
1921 c = skipToNonWhite ();
1923 cppUngetc (c);
1926 static void addParentClass (statementInfo *const st, tokenInfo *const token)
1928 if (vStringLength (token->name) > 0 &&
1929 vStringLength (st->parentClasses) > 0)
1931 vStringPut (st->parentClasses, ',');
1933 vStringCat (st->parentClasses, token->name);
1936 static void readParents (statementInfo *const st, const int qualifier)
1938 tokenInfo *const token = newToken ();
1939 tokenInfo *const parent = newToken ();
1940 int c;
1944 c = skipToNonWhite ();
1945 if (isident1 (c))
1947 readIdentifier (token, c);
1948 if (isType (token, TOKEN_NAME))
1949 vStringCat (parent->name, token->name);
1950 else
1952 addParentClass (st, parent);
1953 initToken (parent);
1956 else if (c == qualifier)
1957 vStringPut (parent->name, c);
1958 else if (c == '<')
1959 skipToMatch ("<>");
1960 else if (isType (token, TOKEN_NAME))
1962 addParentClass (st, parent);
1963 initToken (parent);
1965 } while (c != '{' && c != EOF);
1966 cppUngetc (c);
1967 deleteToken (parent);
1968 deleteToken (token);
1971 static void processToken (tokenInfo *const token, statementInfo *const st)
1973 switch (token->keyword) /* is it a reserved word? */
1975 default: break;
1977 case KEYWORD_NONE: processName (st); break;
1978 case KEYWORD_ABSTRACT: st->implementation = IMP_ABSTRACT; break;
1979 case KEYWORD_ATTRIBUTE: skipParens (); initToken (token); break;
1980 case KEYWORD_CATCH: skipParens (); skipBraces (); break;
1981 case KEYWORD_CHAR: st->declaration = DECL_BASE; break;
1982 case KEYWORD_CLASS: st->declaration = DECL_CLASS; break;
1983 case KEYWORD_CONST: st->declaration = DECL_BASE; break;
1984 case KEYWORD_DOUBLE: st->declaration = DECL_BASE; break;
1985 case KEYWORD_ENUM: st->declaration = DECL_ENUM; break;
1986 case KEYWORD_EXTENDS: readParents (st, '.');
1987 setToken (st, TOKEN_NONE); break;
1988 case KEYWORD_FLOAT: st->declaration = DECL_BASE; break;
1989 case KEYWORD_FRIEND: st->scope = SCOPE_FRIEND; break;
1990 case KEYWORD_IMPLEMENTS:readParents (st, '.');
1991 setToken (st, TOKEN_NONE); break;
1992 case KEYWORD_IMPORT: st->declaration = DECL_IGNORE; break;
1993 case KEYWORD_INT: st->declaration = DECL_BASE; break;
1994 case KEYWORD_BOOLEAN: st->declaration = DECL_BASE; break;
1995 case KEYWORD_WCHAR_T: st->declaration = DECL_BASE; break;
1996 case KEYWORD_SIZE_T: st->declaration = DECL_BASE; break;
1997 case KEYWORD_INTERFACE: st->declaration = DECL_INTERFACE; break;
1998 case KEYWORD_LONG: st->declaration = DECL_BASE; break;
1999 case KEYWORD_OPERATOR: readOperator (st); break;
2000 case KEYWORD_MODULE: readPackage (st); break;
2001 case KEYWORD_PRIVATE: setAccess (st, ACCESS_PRIVATE); break;
2002 case KEYWORD_PROTECTED: setAccess (st, ACCESS_PROTECTED); break;
2003 case KEYWORD_PUBLIC: setAccess (st, ACCESS_PUBLIC); break;
2004 case KEYWORD_SHORT: st->declaration = DECL_BASE; break;
2005 case KEYWORD_SIGNED: st->declaration = DECL_BASE; break;
2006 case KEYWORD_STRUCT: st->declaration = DECL_STRUCT; break;
2007 case KEYWORD_THROWS: discardTypeList (token); break;
2008 case KEYWORD_TYPEDEF: st->scope = SCOPE_TYPEDEF; break;
2009 case KEYWORD_UNION: st->declaration = DECL_UNION; break;
2010 case KEYWORD_UNSIGNED: st->declaration = DECL_BASE; break;
2011 case KEYWORD_USING: st->declaration = DECL_IGNORE; break;
2012 case KEYWORD_VOID: st->declaration = DECL_BASE; break;
2013 case KEYWORD_VOLATILE: st->declaration = DECL_BASE; break;
2014 case KEYWORD_VIRTUAL: st->implementation = IMP_VIRTUAL; break;
2016 case KEYWORD_NAMESPACE: readPackageOrNamespace (st, DECL_NAMESPACE); break;
2017 case KEYWORD_PACKAGE: readPackageOrNamespace (st, DECL_PACKAGE); break;
2018 case KEYWORD_EVENT:
2020 if (isLanguage (Lang_csharp))
2021 st->declaration = DECL_EVENT;
2022 break;
2024 case KEYWORD_SIGNAL:
2026 if (isLanguage (Lang_vala))
2027 st->declaration = DECL_SIGNAL;
2028 break;
2030 case KEYWORD_EXTERN:
2032 if (! isLanguage (Lang_csharp) || !st->gotName)
2034 /*reinitStatement (st, FALSE);*/
2035 st->scope = SCOPE_EXTERN;
2036 st->declaration = DECL_BASE;
2038 break;
2040 case KEYWORD_STATIC:
2042 if (! isLanguage (Lang_java) && ! isLanguage (Lang_csharp) && ! isLanguage (Lang_vala))
2044 /*reinitStatement (st, FALSE);*/
2045 st->scope = SCOPE_STATIC;
2046 st->declaration = DECL_BASE;
2048 break;
2050 case KEYWORD_IF:
2051 if (isLanguage (Lang_d))
2052 { /* static if (is(typeof(__traits(getMember, a, name)) == function)) */
2053 int c = skipToNonWhite ();
2054 if (c == '(')
2055 skipToMatch ("()");
2057 break;
2062 * Parenthesis handling functions
2065 static void restartStatement (statementInfo *const st)
2067 tokenInfo *const save = newToken ();
2068 tokenInfo *token = activeToken (st);
2070 copyToken (save, token);
2071 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>");)
2072 reinitStatement (st, FALSE);
2073 token = activeToken (st);
2074 copyToken (token, save);
2075 deleteToken (save);
2076 processToken (token, st);
2079 /* Skips over a the mem-initializer-list of a ctor-initializer, defined as:
2081 * mem-initializer-list:
2082 * mem-initializer, mem-initializer-list
2084 * mem-initializer:
2085 * [::] [nested-name-spec] class-name (...)
2086 * identifier
2088 static void skipMemIntializerList (tokenInfo *const token)
2090 int c;
2094 c = skipToNonWhite ();
2095 while (isident1 (c) || c == ':')
2097 if (c != ':')
2098 readIdentifier (token, c);
2099 c = skipToNonWhite ();
2101 if (c == '<')
2103 skipToMatch ("<>");
2104 c = skipToNonWhite ();
2106 if (c == '(')
2108 skipToMatch ("()");
2109 c = skipToNonWhite ();
2111 } while (c == ',');
2112 cppUngetc (c);
2115 static void skipMacro (statementInfo *const st)
2117 tokenInfo *const prev2 = prevToken (st, 2);
2119 if (isType (prev2, TOKEN_NAME))
2120 retardToken (st);
2121 skipToMatch ("()");
2124 static boolean isDPostArgumentToken(tokenInfo *const token)
2126 switch (token->keyword)
2128 /* Note: some other keywords e.g. immutable are parsed as
2129 * KEYWORD_CONST - see initializeDParser */
2130 case KEYWORD_CONST:
2131 /* template constraint */
2132 case KEYWORD_IF:
2133 /* contracts */
2134 case KEYWORD_IN:
2135 case KEYWORD_OUT:
2136 case KEYWORD_BODY:
2137 return TRUE;
2138 default:
2139 break;
2141 /* @attributes */
2142 if (vStringValue(token->name)[0] == '@')
2143 return TRUE;
2144 return FALSE;
2147 /* Skips over characters following the parameter list. This will be either
2148 * non-ANSI style function declarations or C++ stuff. Our choices:
2150 * C (K&R):
2151 * int func ();
2152 * int func (one, two) int one; float two; {...}
2153 * C (ANSI):
2154 * int func (int one, float two);
2155 * int func (int one, float two) {...}
2156 * C++:
2157 * int foo (...) [const|volatile] [throw (...)];
2158 * int foo (...) [const|volatile] [throw (...)] [ctor-initializer] {...}
2159 * int foo (...) [const|volatile] [throw (...)] try [ctor-initializer] {...}
2160 * catch (...) {...}
2162 static boolean skipPostArgumentStuff (statementInfo *const st,
2163 parenInfo *const info)
2165 tokenInfo *const token = activeToken (st);
2166 unsigned int parameters = info->parameterCount;
2167 unsigned int elementCount = 0;
2168 boolean restart = FALSE;
2169 boolean end = FALSE;
2170 int c = skipToNonWhite ();
2174 switch (c)
2176 case ')': break;
2177 case ':': skipMemIntializerList (token);break; /* ctor-initializer */
2178 case '[': skipToMatch ("[]"); break;
2179 case '=': cppUngetc (c); end = TRUE; break;
2180 case '{': cppUngetc (c); end = TRUE; break;
2181 case '}': cppUngetc (c); end = TRUE; break;
2183 case '(':
2185 if (elementCount > 0)
2186 ++elementCount;
2187 skipToMatch ("()");
2188 break;
2191 case ';':
2193 if (parameters == 0 || elementCount < 2)
2195 cppUngetc (c);
2196 end = TRUE;
2198 else if (--parameters == 0)
2199 end = TRUE;
2200 break;
2203 default:
2205 if (isident1 (c))
2207 readIdentifier (token, c);
2208 if (isLanguage(Lang_d) && isDPostArgumentToken(token))
2209 token->keyword = KEYWORD_CONST;
2211 switch (token->keyword)
2213 case KEYWORD_ATTRIBUTE: skipParens (); break;
2214 case KEYWORD_THROW: skipParens (); break;
2215 case KEYWORD_CONST: break;
2216 case KEYWORD_TRY: break;
2217 case KEYWORD_VOLATILE: break;
2219 case KEYWORD_CATCH: case KEYWORD_CLASS:
2220 case KEYWORD_EXPLICIT: case KEYWORD_EXTERN:
2221 case KEYWORD_FRIEND: case KEYWORD_INLINE:
2222 case KEYWORD_MUTABLE: case KEYWORD_NAMESPACE:
2223 case KEYWORD_NEW: case KEYWORD_OPERATOR:
2224 case KEYWORD_OVERLOAD: case KEYWORD_PRIVATE:
2225 case KEYWORD_PROTECTED: case KEYWORD_PUBLIC:
2226 case KEYWORD_STATIC: case KEYWORD_TEMPLATE:
2227 case KEYWORD_TYPEDEF: case KEYWORD_TYPENAME:
2228 case KEYWORD_USING: case KEYWORD_VIRTUAL:
2229 /* Never allowed within parameter declarations.
2231 restart = TRUE;
2232 end = TRUE;
2233 break;
2235 default:
2236 if (isType (token, TOKEN_NONE))
2238 else if (info->isKnrParamList && info->parameterCount > 0)
2239 ++elementCount;
2240 else
2242 /* If we encounter any other identifier immediately
2243 * following an empty parameter list, this is almost
2244 * certainly one of those Microsoft macro "thingies"
2245 * that the automatic source code generation sticks
2246 * in. Terminate the current statement.
2248 restart = TRUE;
2249 end = TRUE;
2251 break;
2256 if (! end)
2258 c = skipToNonWhite ();
2259 if (c == EOF)
2260 end = TRUE;
2262 } while (! end);
2264 if (restart)
2265 restartStatement (st);
2266 else
2267 setToken (st, TOKEN_NONE);
2268 return (boolean) (c != EOF);
2271 static void skipJavaThrows (statementInfo *const st)
2273 tokenInfo *const token = activeToken (st);
2274 int c = skipToNonWhite ();
2276 if (isident1 (c))
2278 readIdentifier (token, c);
2279 if (token->keyword == KEYWORD_THROWS)
2283 c = skipToNonWhite ();
2284 if (isident1 (c))
2286 readIdentifier (token, c);
2287 c = skipToNonWhite ();
2289 } while (c == '.' || c == ',');
2292 cppUngetc (c);
2293 setToken (st, TOKEN_NONE);
2296 static void skipValaPostParens (statementInfo *const st)
2298 tokenInfo *const token = activeToken (st);
2299 int c = skipToNonWhite ();
2301 while (isident1 (c))
2303 readIdentifier (token, c);
2304 if (token->keyword == KEYWORD_ATTRIBUTE)
2306 /* parse contracts */
2307 skipParens ();
2308 c = skipToNonWhite ();
2310 else if (token->keyword == KEYWORD_THROWS)
2314 c = skipToNonWhite ();
2315 if (isident1 (c))
2317 readIdentifier (token, c);
2318 c = skipToNonWhite ();
2320 } while (c == '.' || c == ',');
2322 else
2323 break;
2325 cppUngetc (c);
2326 setToken (st, TOKEN_NONE);
2329 static void analyzePostParens (statementInfo *const st, parenInfo *const info)
2331 const unsigned long inputLineNumber = getInputLineNumber ();
2332 int c = skipToNonWhite ();
2334 cppUngetc (c);
2335 if (isOneOf (c, "{;,="))
2337 else if (isLanguage (Lang_java))
2338 skipJavaThrows (st);
2339 else if (isLanguage (Lang_vala))
2340 skipValaPostParens(st);
2341 else
2343 if (! skipPostArgumentStuff (st, info))
2345 verbose (
2346 "%s: confusing argument declarations beginning at line %lu\n",
2347 getInputFileName (), inputLineNumber);
2348 longjmp (Exception, (int) ExceptionFormattingError);
2353 static int parseParens (statementInfo *const st, parenInfo *const info)
2355 tokenInfo *const token = activeToken (st);
2356 unsigned int identifierCount = 0;
2357 unsigned int depth = 1;
2358 boolean firstChar = TRUE;
2359 int nextChar = '\0';
2361 info->parameterCount = 1;
2364 int c = skipToNonWhite ();
2366 switch (c)
2368 case '&':
2369 case '*':
2371 /* DEBUG_PRINT("parseParens, po++\n"); */
2372 info->isKnrParamList = FALSE;
2373 if (identifierCount == 0)
2374 info->isParamList = FALSE;
2375 initToken (token);
2376 break;
2378 case ':':
2380 info->isKnrParamList = FALSE;
2381 break;
2383 case '.':
2385 info->isNameCandidate = FALSE;
2386 info->isKnrParamList = FALSE;
2387 break;
2389 case ',':
2391 info->isNameCandidate = FALSE;
2392 if (info->isKnrParamList)
2394 ++info->parameterCount;
2395 identifierCount = 0;
2397 break;
2399 case '=':
2401 info->isKnrParamList = FALSE;
2402 info->isNameCandidate = FALSE;
2403 if (firstChar)
2405 info->isParamList = FALSE;
2406 skipMacro (st);
2407 depth = 0;
2409 break;
2411 case '[':
2413 info->isKnrParamList = FALSE;
2414 skipToMatch ("[]");
2415 break;
2417 case '<':
2419 info->isKnrParamList = FALSE;
2420 skipToMatch ("<>");
2421 break;
2423 case ')':
2425 if (firstChar)
2426 info->parameterCount = 0;
2427 --depth;
2428 break;
2430 case '(':
2432 info->isKnrParamList = FALSE;
2433 if (firstChar)
2435 info->isNameCandidate = FALSE;
2436 cppUngetc (c);
2437 skipMacro (st);
2438 depth = 0;
2440 else if (isType (token, TOKEN_PAREN_NAME))
2442 c = skipToNonWhite ();
2443 if (c == '*') /* check for function pointer */
2445 skipToMatch ("()");
2446 c = skipToNonWhite ();
2447 if (c == '(')
2448 skipToMatch ("()");
2450 else
2452 cppUngetc (c);
2453 cppUngetc ('(');
2454 info->nestedArgs = TRUE;
2457 else
2458 ++depth;
2459 break;
2462 default:
2464 if (isident1 (c))
2466 if (++identifierCount > 1)
2467 info->isKnrParamList = FALSE;
2468 readIdentifier (token, c);
2469 if (isType (token, TOKEN_NAME) && info->isNameCandidate)
2470 token->type = TOKEN_PAREN_NAME;
2471 else if (isType (token, TOKEN_KEYWORD))
2473 info->isKnrParamList = FALSE;
2474 info->isNameCandidate = FALSE;
2477 else
2479 info->isParamList = FALSE;
2480 info->isKnrParamList = FALSE;
2481 info->isNameCandidate = FALSE;
2482 info->invalidContents = TRUE;
2484 break;
2487 firstChar = FALSE;
2488 } while (! info->nestedArgs && depth > 0 &&
2489 (info->isKnrParamList || info->isNameCandidate));
2491 if (! info->nestedArgs) while (depth > 0)
2493 skipToMatch ("()");
2494 --depth;
2496 if (st->argEndPosition == 0)
2497 st->argEndPosition = mio_tell (File.mio);
2499 if (! info->isNameCandidate)
2500 initToken (token);
2502 return nextChar;
2505 static void initParenInfo (parenInfo *const info)
2507 info->isParamList = TRUE;
2508 info->isKnrParamList = TRUE;
2509 info->isNameCandidate = TRUE;
2510 info->invalidContents = FALSE;
2511 info->nestedArgs = FALSE;
2512 info->parameterCount = 0;
2515 static void analyzeParens (statementInfo *const st)
2517 tokenInfo *const prev = prevToken (st, 1);
2519 if (! isType (prev, TOKEN_NONE)) /* in case of ignored enclosing macros */
2521 tokenInfo *const token = activeToken (st);
2522 parenInfo info;
2523 int c;
2525 initParenInfo (&info);
2526 parseParens (st, &info);
2528 c = skipToNonWhite ();
2530 cppUngetc (c);
2531 if (info.invalidContents)
2533 reinitStatement (st, FALSE);
2535 else if (info.isNameCandidate && isType (token, TOKEN_PAREN_NAME) &&
2536 ! st->gotParenName &&
2537 (! info.isParamList || ! st->haveQualifyingName ||
2538 c == '(' ||
2539 (c == '=' && st->implementation != IMP_VIRTUAL) ||
2540 (st->declaration == DECL_NONE && isOneOf (c, ",;"))))
2542 token->type = TOKEN_NAME;
2543 processName (st);
2544 st->gotParenName = TRUE;
2545 if (isLanguage(Lang_d) && c == '(' && isType (prev, TOKEN_NAME))
2547 st->declaration = DECL_FUNCTION_TEMPLATE;
2548 copyToken (st->blockName, prev);
2551 else if (! st->gotArgs && info.isParamList)
2553 st->gotArgs = TRUE;
2554 setToken (st, TOKEN_ARGS);
2555 advanceToken (st);
2556 analyzePostParens (st, &info);
2558 else
2560 setToken (st, TOKEN_NONE);
2566 * Token parsing functions
2569 static void addContext (statementInfo *const st, const tokenInfo* const token)
2571 if (isType (token, TOKEN_NAME))
2573 if (vStringLength (st->context->name) > 0)
2575 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
2576 vStringCatS (st->context->name, "::");
2577 else if (isLanguage (Lang_java) ||
2578 isLanguage (Lang_d) || isLanguage (Lang_ferite) ||
2579 isLanguage (Lang_csharp) || isLanguage (Lang_vala))
2580 vStringCatS (st->context->name, ".");
2582 vStringCat (st->context->name, token->name);
2583 st->context->type = TOKEN_NAME;
2587 static boolean inheritingDeclaration (declType decl)
2589 return (boolean) (decl == DECL_CLASS ||
2590 decl == DECL_STRUCT ||
2591 decl == DECL_INTERFACE);
2594 static void processColon (statementInfo *const st)
2596 int c = skipToNonWhite ();
2597 const boolean doubleColon = (boolean) (c == ':');
2599 if (doubleColon)
2601 setToken (st, TOKEN_DOUBLE_COLON);
2602 st->haveQualifyingName = FALSE;
2604 else
2606 cppUngetc (c);
2607 if ((((isLanguage (Lang_cpp) &&
2608 (st->declaration == DECL_CLASS || st->declaration == DECL_STRUCT)) ||
2609 isLanguage (Lang_csharp) || isLanguage (Lang_vala)) &&
2610 inheritingDeclaration (st->declaration)) ||
2611 isLanguage (Lang_d))
2613 readParents (st, ':');
2615 else if (parentDecl (st) == DECL_STRUCT || parentDecl (st) == DECL_CLASS)
2617 c = skipToOneOf (",;");
2618 if (c == ',')
2619 setToken (st, TOKEN_COMMA);
2620 else if (c == ';')
2621 setToken (st, TOKEN_SEMICOLON);
2623 else
2625 const tokenInfo *const prev = prevToken (st, 1);
2626 const tokenInfo *const prev2 = prevToken (st, 2);
2627 if (prev->keyword == KEYWORD_DEFAULT ||
2628 prev2->keyword == KEYWORD_CASE ||
2629 st->parent != NULL)
2631 reinitStatement (st, FALSE);
2637 /* Skips over any initializing value which may follow an '=' character in a
2638 * variable definition.
2640 static int skipInitializer (statementInfo *const st)
2642 boolean done = FALSE;
2643 int c;
2645 while (! done)
2647 c = skipToNonWhite ();
2649 if (c == EOF)
2650 longjmp (Exception, (int) ExceptionFormattingError);
2651 else switch (c)
2653 case ',':
2654 case ';': done = TRUE; break;
2656 case '0':
2657 if (st->implementation == IMP_VIRTUAL)
2658 st->implementation = IMP_PURE_VIRTUAL;
2659 break;
2661 case '[': skipToMatch ("[]"); break;
2662 case '(': skipToMatch ("()"); break;
2663 case '{': skipToMatch ("{}"); break;
2665 case '}':
2666 if (insideEnumBody (st))
2667 done = TRUE;
2668 else if (! isBraceFormat ())
2670 verbose ("%s: unexpected closing brace at line %lu\n",
2671 getInputFileName (), getInputLineNumber ());
2672 longjmp (Exception, (int) ExceptionBraceFormattingError);
2674 break;
2676 default: break;
2679 return c;
2682 static void processInitializer (statementInfo *const st)
2684 const boolean inEnumBody = insideEnumBody (st);
2685 const int c = skipInitializer (st);
2687 if (c == ';')
2688 setToken (st, TOKEN_SEMICOLON);
2689 else if (c == ',')
2690 setToken (st, TOKEN_COMMA);
2691 else if (c == '}' && inEnumBody)
2693 cppUngetc (c);
2694 setToken (st, TOKEN_COMMA);
2696 if (st->scope == SCOPE_EXTERN)
2697 st->scope = SCOPE_GLOBAL;
2700 static void parseIdentifier (statementInfo *const st, const int c)
2702 tokenInfo *const token = activeToken (st);
2704 readIdentifier (token, c);
2705 if (! isType (token, TOKEN_NONE))
2706 processToken (token, st);
2709 static void parseGeneralToken (statementInfo *const st, const int c)
2711 const tokenInfo *const prev = prevToken (st, 1);
2713 if (isident1(c))
2715 parseIdentifier (st, c);
2716 if (isType (st->context, TOKEN_NAME) &&
2717 isType (activeToken (st), TOKEN_NAME) && isType (prev, TOKEN_NAME))
2719 initToken (st->context);
2722 else if (isExternCDecl (st, c))
2724 st->declaration = DECL_NOMANGLE;
2725 st->scope = SCOPE_GLOBAL;
2729 /* Reads characters from the pre-processor and assembles tokens, setting
2730 * the current statement state.
2732 static void nextToken (statementInfo *const st)
2734 int c;
2735 tokenInfo *token = activeToken (st);
2738 c = skipToNonWhite();
2739 switch (c)
2741 case EOF: longjmp (Exception, (int) ExceptionEOF); break;
2742 case '(': analyzeParens (st); token = activeToken (st); break;
2743 case '*': setToken (st, TOKEN_STAR); break;
2744 case ',': setToken (st, TOKEN_COMMA); break;
2745 case ':': processColon (st); break;
2746 case ';': setToken (st, TOKEN_SEMICOLON); break;
2747 case '<': skipToMatch ("<>"); break;
2748 case '=': processInitializer (st); break;
2749 case '[':
2750 /* Hack for Vala: [..] can be a function attribute.
2751 * Seems not to have bad side effects, but have to test it more. */
2752 if (!isLanguage (Lang_vala))
2753 setToken (st, TOKEN_ARRAY);
2754 skipToMatch ("[]");
2755 break;
2756 case '{': setToken (st, TOKEN_BRACE_OPEN); break;
2757 case '}': setToken (st, TOKEN_BRACE_CLOSE); break;
2758 default: parseGeneralToken (st, c); break;
2760 } while (isType (token, TOKEN_NONE));
2762 /* We want to know about non-keyword variable types */
2763 if (TOKEN_NONE == st->firstToken->type)
2765 if ((TOKEN_NAME == token->type) || isDataTypeKeyword(token))
2766 copyToken(st->firstToken, token);
2771 * Scanning support functions
2773 static unsigned int contextual_fake_count = 0;
2774 static statementInfo *CurrentStatement = NULL;
2776 static statementInfo *newStatement (statementInfo *const parent)
2778 statementInfo *const st = xMalloc (1, statementInfo);
2779 unsigned int i;
2781 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2782 st->token [i] = newToken ();
2784 st->context = newToken ();
2785 st->blockName = newToken ();
2786 st->parentClasses = vStringNew ();
2787 st->firstToken = newToken();
2789 initStatement (st, parent);
2790 CurrentStatement = st;
2792 return st;
2795 static void deleteStatement (void)
2797 statementInfo *const st = CurrentStatement;
2798 statementInfo *const parent = st->parent;
2799 unsigned int i;
2801 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2803 deleteToken(st->token[i]); st->token[i] = NULL;
2805 deleteToken(st->blockName); st->blockName = NULL;
2806 deleteToken(st->context); st->context = NULL;
2807 vStringDelete(st->parentClasses); st->parentClasses = NULL;
2808 deleteToken(st->firstToken);
2809 eFree (st);
2810 CurrentStatement = parent;
2813 static void deleteAllStatements (void)
2815 while (CurrentStatement != NULL)
2816 deleteStatement ();
2819 static boolean isStatementEnd (const statementInfo *const st)
2821 const tokenInfo *const token = activeToken (st);
2822 boolean isEnd;
2824 if (isType (token, TOKEN_SEMICOLON))
2825 isEnd = TRUE;
2826 else if (isType (token, TOKEN_BRACE_CLOSE))
2827 /* Java, D, C#, Vala do not require semicolons to end a block. Neither do
2828 * C++ namespaces. All other blocks require a semicolon to terminate them.
2830 isEnd = (boolean) (isLanguage (Lang_java) || isLanguage (Lang_d) ||
2831 isLanguage (Lang_csharp) || isLanguage (Lang_vala) ||
2832 ! isContextualStatement (st));
2833 else
2834 isEnd = FALSE;
2836 return isEnd;
2839 static void checkStatementEnd (statementInfo *const st)
2841 const tokenInfo *const token = activeToken (st);
2842 boolean comma = isType (token, TOKEN_COMMA);
2844 if (comma || isStatementEnd (st))
2846 reinitStatementWithToken (st, activeToken (st), comma);
2848 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>"); )
2849 cppEndStatement ();
2851 else
2853 cppBeginStatement ();
2854 advanceToken (st);
2858 static void nest (statementInfo *const st, const unsigned int nestLevel)
2860 switch (st->declaration)
2862 case DECL_CLASS:
2863 case DECL_ENUM:
2864 case DECL_INTERFACE:
2865 case DECL_NAMESPACE:
2866 case DECL_NOMANGLE:
2867 case DECL_STRUCT:
2868 case DECL_UNION:
2869 createTags (nestLevel, st);
2870 break;
2871 default:
2872 skipToMatch ("{}");
2873 break;
2875 advanceToken (st);
2876 setToken (st, TOKEN_BRACE_CLOSE);
2879 static void tagCheck (statementInfo *const st)
2881 const tokenInfo *const token = activeToken (st);
2882 const tokenInfo *const prev = prevToken (st, 1);
2883 const tokenInfo *const prev2 = prevToken (st, 2);
2885 switch (token->type)
2887 case TOKEN_NAME:
2889 if (insideEnumBody (st))
2890 qualifyEnumeratorTag (st, token);
2891 break;
2893 #if 0
2894 case TOKEN_PACKAGE:
2896 if (st->haveQualifyingName)
2897 makeTag (token, st, FALSE, TAG_PACKAGE);
2898 break;
2900 #endif
2901 case TOKEN_BRACE_OPEN:
2903 if (isType (prev, TOKEN_ARGS))
2905 if (st->declaration == DECL_FUNCTION_TEMPLATE)
2906 qualifyFunctionTag (st, st->blockName);
2907 else if (st->haveQualifyingName)
2909 if (isType (prev2, TOKEN_NAME))
2910 copyToken (st->blockName, prev2);
2911 /* D structure templates */
2912 if (isLanguage (Lang_d) &&
2913 (st->declaration == DECL_CLASS || st->declaration == DECL_STRUCT ||
2914 st->declaration == DECL_INTERFACE || st->declaration == DECL_NAMESPACE))
2915 qualifyBlockTag (st, prev2);
2916 else
2918 st->declaration = DECL_FUNCTION;
2919 qualifyFunctionTag (st, prev2);
2923 else if (isContextualStatement (st))
2925 tokenInfo *name_token = (tokenInfo *)prev;
2926 boolean free_name_token = FALSE;
2928 if (isType (name_token, TOKEN_NAME))
2930 if (!isLanguage (Lang_vala))
2931 copyToken (st->blockName, name_token);
2932 else
2934 switch (st->declaration)
2936 case DECL_CLASS:
2937 case DECL_ENUM:
2938 case DECL_INTERFACE:
2939 case DECL_NAMESPACE:
2940 case DECL_STRUCT:
2941 copyToken (st->blockName, name_token);
2942 break;
2944 /* anything else can be a property */
2945 default:
2946 /* makeTag (prev, st, FALSE, TAG_PROPERTY); */
2947 /* FIXME: temporary hack to get properties shown */
2948 makeTag (prev, st, FALSE, TAG_FIELD);
2949 break;
2953 else if (isLanguage (Lang_csharp))
2954 makeTag (prev, st, FALSE, TAG_PROPERTY);
2955 else
2957 tokenInfo *contextual_token = (tokenInfo *)prev;
2958 if(isContextualKeyword (contextual_token))
2960 char buffer[64];
2962 name_token = newToken ();
2963 free_name_token = TRUE;
2964 copyToken (name_token, contextual_token);
2966 sprintf(buffer, "anon_%s_%d", name_token->name->buffer, contextual_fake_count++);
2967 vStringClear(name_token->name);
2968 vStringCatS(name_token->name, buffer);
2970 name_token->type = TOKEN_NAME;
2971 name_token->keyword = KEYWORD_NONE;
2973 advanceToken (st);
2974 contextual_token = activeToken (st);
2975 copyToken (contextual_token, token);
2976 copyToken ((tokenInfo *const)token, name_token);
2977 copyToken (st->blockName, name_token);
2978 copyToken (st->firstToken, name_token);
2981 qualifyBlockTag (st, name_token);
2982 if (free_name_token)
2983 deleteToken (name_token);
2985 break;
2987 case TOKEN_ARRAY:
2988 case TOKEN_SEMICOLON:
2989 case TOKEN_COMMA:
2991 if (insideEnumBody (st))
2993 else if (isType (prev, TOKEN_NAME))
2995 if (isContextualKeyword (prev2))
2996 makeTag (prev, st, TRUE, TAG_EXTERN_VAR);
2997 else
2998 qualifyVariableTag (st, prev);
3000 else if (isType (prev, TOKEN_ARGS) && isType (prev2, TOKEN_NAME))
3002 qualifyFunctionDeclTag (st, prev2);
3004 break;
3006 default:
3007 break;
3011 /* Parses the current file and decides whether to write out and tags that
3012 * are discovered.
3014 static void createTags (const unsigned int nestLevel,
3015 statementInfo *const parent)
3017 statementInfo *const st = newStatement (parent);
3019 DebugStatement ( if (nestLevel > 0) debugParseNest (TRUE, nestLevel); )
3020 while (TRUE)
3022 tokenInfo *token;
3024 nextToken (st);
3026 token = activeToken (st);
3028 if (isType (token, TOKEN_BRACE_CLOSE))
3030 if (nestLevel > 0)
3031 break;
3032 else
3034 verbose ("%s: unexpected closing brace at line %lu\n",
3035 getInputFileName (), getInputLineNumber ());
3036 longjmp (Exception, (int) ExceptionBraceFormattingError);
3039 else if (isType (token, TOKEN_DOUBLE_COLON))
3041 addContext (st, prevToken (st, 1));
3042 advanceToken (st);
3044 else
3046 tagCheck (st);/* this can add new token */
3047 if (isType (activeToken (st), TOKEN_BRACE_OPEN))
3048 nest (st, nestLevel + 1);
3049 checkStatementEnd (st);
3052 deleteStatement ();
3053 DebugStatement ( if (nestLevel > 0) debugParseNest (FALSE, nestLevel - 1); )
3056 static boolean findCTags (const unsigned int passCount)
3058 exception_t exception;
3059 boolean retry;
3061 contextual_fake_count = 0;
3063 Assert (passCount < 3);
3064 cppInit ((boolean) (passCount > 1), isLanguage (Lang_csharp));
3066 exception = (exception_t) setjmp (Exception);
3067 retry = FALSE;
3069 if (exception == ExceptionNone)
3071 createTags (0, NULL);
3073 else
3075 deleteAllStatements ();
3076 if (exception == ExceptionBraceFormattingError && passCount == 1)
3078 retry = TRUE;
3079 verbose ("%s: retrying file with fallback brace matching algorithm\n",
3080 getInputFileName ());
3083 cppTerminate ();
3084 return retry;
3087 static void buildKeywordHash (const langType language, unsigned int idx)
3089 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
3090 size_t i;
3091 for (i = 0 ; i < count ; ++i)
3093 const keywordDesc* const p = &KeywordTable [i];
3094 if (p->isValid [idx])
3095 addKeyword (p->name, language, (int) p->id);
3099 static void initializeCParser (const langType language)
3101 Lang_c = language;
3102 buildKeywordHash (language, 0);
3105 static void initializeCppParser (const langType language)
3107 Lang_cpp = language;
3108 buildKeywordHash (language, 1);
3111 static void initializeJavaParser (const langType language)
3113 Lang_java = language;
3114 buildKeywordHash (language, 3);
3117 static void initializeDParser (const langType language)
3119 /* keyword aliases - some are for parsing like const(Type), some are just
3120 * function attributes */
3121 char *const_aliases[] = {"immutable", "nothrow", "pure", "shared", NULL};
3122 char **s;
3124 Lang_d = language;
3125 buildKeywordHash (language, 6);
3127 for (s = const_aliases; *s != NULL; s++)
3129 addKeyword (*s, language, KEYWORD_CONST);
3133 static void initializeGLSLParser (const langType language)
3135 Lang_glsl = language;
3136 buildKeywordHash (language, 0); /* C keywords */
3139 static void initializeFeriteParser (const langType language)
3141 Lang_ferite = language;
3142 buildKeywordHash (language, 1); /* C++ keywords */
3145 static void initializeCsharpParser (const langType language)
3147 Lang_csharp = language;
3148 buildKeywordHash (language, 2);
3151 static void initializeValaParser (const langType language)
3153 Lang_vala = language;
3154 buildKeywordHash (language, 5);
3157 extern parserDefinition* CParser (void)
3159 static const char *const extensions [] = { "c", "pc", "sc", NULL };
3160 parserDefinition* def = parserNew ("C");
3161 def->kinds = CKinds;
3162 def->kindCount = KIND_COUNT (CKinds);
3163 def->extensions = extensions;
3164 def->parser2 = findCTags;
3165 def->initialize = initializeCParser;
3166 return def;
3169 extern parserDefinition* CppParser (void)
3171 static const char *const extensions [] = {
3172 "c++", "cc", "cp", "cpp", "cxx", "h", "h++", "hh", "hp", "hpp", "hxx",
3173 "i",
3174 #ifndef CASE_INSENSITIVE_FILENAMES
3175 "C", "H",
3176 #endif
3177 NULL
3179 parserDefinition* def = parserNew ("C++");
3180 def->kinds = CKinds;
3181 def->kindCount = KIND_COUNT (CKinds);
3182 def->extensions = extensions;
3183 def->parser2 = findCTags;
3184 def->initialize = initializeCppParser;
3185 return def;
3188 extern parserDefinition* JavaParser (void)
3190 static const char *const extensions [] = { "java", NULL };
3191 parserDefinition* def = parserNew ("Java");
3192 def->kinds = JavaKinds;
3193 def->kindCount = KIND_COUNT (JavaKinds);
3194 def->extensions = extensions;
3195 def->parser2 = findCTags;
3196 def->initialize = initializeJavaParser;
3197 return def;
3200 extern parserDefinition* DParser (void)
3202 static const char *const extensions [] = { "d", "di", NULL };
3203 parserDefinition* def = parserNew ("D");
3204 def->kinds = DKinds;
3205 def->kindCount = KIND_COUNT (DKinds);
3206 def->extensions = extensions;
3207 def->parser2 = findCTags;
3208 def->initialize = initializeDParser;
3209 return def;
3212 extern parserDefinition* GLSLParser (void)
3214 static const char *const extensions [] = { "glsl", "frag", "vert", NULL };
3215 parserDefinition* def = parserNew ("GLSL");
3216 def->kinds = CKinds;
3217 def->kindCount = KIND_COUNT (CKinds);
3218 def->extensions = extensions;
3219 def->parser2 = findCTags;
3220 def->initialize = initializeGLSLParser;
3221 return def;
3224 extern parserDefinition* FeriteParser (void)
3226 static const char *const extensions [] = { "fe", NULL };
3227 parserDefinition* def = parserNew ("Ferite");
3228 def->kinds = CKinds;
3229 def->kindCount = KIND_COUNT (CKinds);
3230 def->extensions = extensions;
3231 def->parser2 = findCTags;
3232 def->initialize = initializeFeriteParser;
3233 return def;
3236 extern parserDefinition* CsharpParser (void)
3238 static const char *const extensions [] = { "cs", NULL };
3239 parserDefinition* def = parserNew ("C#");
3240 def->kinds = CsharpKinds;
3241 def->kindCount = KIND_COUNT (CsharpKinds);
3242 def->extensions = extensions;
3243 def->parser2 = findCTags;
3244 def->initialize = initializeCsharpParser;
3245 return def;
3248 extern parserDefinition* ValaParser (void)
3250 static const char *const extensions [] = { "vala", NULL };
3251 parserDefinition* def = parserNew ("Vala");
3252 def->kinds = ValaKinds;
3253 def->kindCount = KIND_COUNT (ValaKinds);
3254 def->extensions = extensions;
3255 def->parser2 = findCTags;
3256 def->initialize = initializeValaParser;
3257 return def;
3259 /* vi:set tabstop=8 shiftwidth=4: */