Make editor menu initially hidden
[geany-mirror.git] / tagmanager / c.c
blobb73b722415ae3832f4a14158ed4880ae09c7154d
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, 1 } },
487 { "this", KEYWORD_THIS, { 0, 0, 1, 1, 0, 1, 0 } }, /* 0 to allow D ctor tags */
488 { "throw", KEYWORD_THROW, { 0, 1, 1, 1, 0, 1, 1 } },
489 { "throws", KEYWORD_THROWS, { 0, 0, 0, 1, 0, 1, 0 } },
490 { "trans", KEYWORD_TRANS, { 0, 0, 0, 0, 1, 0, 0 } },
491 { "transition", KEYWORD_TRANSITION, { 0, 0, 0, 0, 1, 0, 0 } },
492 { "transient", KEYWORD_TRANSIENT, { 0, 0, 0, 1, 0, 0, 0 } },
493 { "try", KEYWORD_TRY, { 0, 1, 1, 0, 0, 1, 1 } },
494 { "typedef", KEYWORD_TYPEDEF, { 1, 1, 1, 0, 1, 0, 1 } },
495 { "typename", KEYWORD_TYPENAME, { 0, 1, 0, 0, 0, 0, 0 } },
496 { "uint", KEYWORD_UINT, { 0, 0, 1, 0, 0, 1, 1 } },
497 { "ulong", KEYWORD_ULONG, { 0, 0, 1, 0, 0, 1, 1 } },
498 { "union", KEYWORD_UNION, { 1, 1, 0, 0, 0, 0, 1 } },
499 { "unittest", KEYWORD_BODY, { 0, 0, 0, 0, 0, 0, 1 } }, /* ignore */
500 { "unsigned", KEYWORD_UNSIGNED, { 1, 1, 1, 0, 0, 0, 1 } },
501 { "ushort", KEYWORD_USHORT, { 0, 0, 1, 0, 0, 1, 1 } },
502 { "using", KEYWORD_USING, { 0, 1, 1, 0, 0, 1, 0 } },
503 { "version", KEYWORD_NAMESPACE, { 0, 0, 0, 0, 0, 0, 1 } }, /* parse block */
504 { "virtual", KEYWORD_VIRTUAL, { 0, 1, 1, 0, 1, 1, 0 } },
505 { "void", KEYWORD_VOID, { 1, 1, 1, 1, 1, 1, 1 } },
506 { "volatile", KEYWORD_VOLATILE, { 1, 1, 1, 1, 0, 0, 1 } },
507 { "wchar_t", KEYWORD_WCHAR_T, { 1, 1, 1, 0, 0, 0, 1 } },
508 { "weak", KEYWORD_WEAK, { 0, 0, 0, 0, 0, 1, 0 } },
509 { "while", KEYWORD_WHILE, { 1, 1, 1, 1, 0, 1, 1 } }
514 * FUNCTION PROTOTYPES
516 static void createTags (const unsigned int nestLevel, statementInfo *const parent);
517 static void copyToken (tokenInfo *const dest, const tokenInfo *const src);
518 static const char *getVarType (const statementInfo *const st);
521 * FUNCTION DEFINITIONS
524 /* Debugging functions added by Biswa */
525 #if defined(DEBUG_C) && DEBUG_C
526 static char *tokenTypeName[] = {
527 "none", "args", "'}'", "'{'", "','", "'::'", "keyword", "name",
528 "package", "paren-name", "';'", "spec", "*", "[]", "count"
531 static char *tagScopeNames[] = {
532 "global", "static", "extern", "friend", "typedef", "count"};
534 static char *declTypeNames[] = {
535 "none", "base", "class", "enum", "function", "ignore", "interface",
536 "namespace", "nomangle", "package", "struct", "union", "count"};
538 static char *impTypeNames[] = {
539 "default", "abstract", "virtual", "pure-virtual", "count"};
541 void printToken(const tokenInfo *const token)
543 fprintf(stderr, "Type: %s, Keyword: %d, name: %s\n", tokenTypeName[token->type],
544 token->keyword, vStringValue(token->name));
547 void printTagEntry(const tagEntryInfo *tag)
549 fprintf(stderr, "Tag: %s (%s) [ impl: %s, scope: %s, type: %s\n", tag->name,
550 tag->kindName, tag->extensionFields.implementation, tag->extensionFields.scope[1],
551 tag->extensionFields.varType);
554 void printStatement(const statementInfo *const statement)
556 int i;
557 statementInfo *st = (statementInfo *) statement;
558 while (NULL != st)
560 fprintf(stderr, "Statement Info:\n------------------------\n");
561 fprintf(stderr, "scope: %s, decl: %s, impl: %s\n", tagScopeNames[st->scope],
562 declTypeNames[st->declaration], impTypeNames[st->implementation]);
563 for (i=0; i < NumTokens; ++i)
565 fprintf(stderr, "Token %d %s: ", i, (i == st->tokenIndex)?"(current)":"");
566 printToken(st->token[i]);
568 fprintf(stderr, "Context: ");
569 printToken(st->context);
570 fprintf(stderr, "Block: ");
571 printToken(st->blockName);
572 fprintf(stderr, "Parent classes: %s\n", vStringValue(st->parentClasses));
573 fprintf(stderr, "First token: ");
574 printToken(st->firstToken);
575 if (NULL != st->parent)
576 fprintf(stderr, "Printing Parent:\n");
577 st = st->parent;
579 fprintf(stderr, "-----------------------------------------------\n");
581 #endif
583 extern boolean includingDefineTags (void)
585 if (isLanguage(Lang_c) ||
586 isLanguage(Lang_cpp) ||
587 isLanguage(Lang_csharp) ||
588 isLanguage(Lang_ferite) ||
589 isLanguage(Lang_glsl) ||
590 isLanguage(Lang_vala))
591 return CKinds [CK_DEFINE].enabled;
593 return FALSE;
597 * Token management
600 static void initToken (tokenInfo* const token)
602 token->type = TOKEN_NONE;
603 token->keyword = KEYWORD_NONE;
604 token->lineNumber = getSourceLineNumber();
605 token->filePosition = getInputFilePosition();
606 vStringClear(token->name);
609 static void advanceToken (statementInfo* const st)
611 if (st->tokenIndex >= (unsigned int) NumTokens - 1)
612 st->tokenIndex = 0;
613 else
614 ++st->tokenIndex;
615 initToken(st->token[st->tokenIndex]);
618 static tokenInfo *prevToken (const statementInfo *const st, unsigned int n)
620 unsigned int tokenIndex;
621 unsigned int num = (unsigned int) NumTokens;
622 Assert(n < num);
623 tokenIndex = (st->tokenIndex + num - n) % num;
625 return st->token[tokenIndex];
628 static void setToken (statementInfo *const st, const tokenType type)
630 tokenInfo *token;
631 token = activeToken (st);
632 initToken(token);
633 token->type = type;
636 static void retardToken (statementInfo *const st)
638 if (st->tokenIndex == 0)
639 st->tokenIndex = (unsigned int) NumTokens - 1;
640 else
641 --st->tokenIndex;
642 setToken(st, TOKEN_NONE);
645 static tokenInfo *newToken (void)
647 tokenInfo *const token = xMalloc (1, tokenInfo);
648 token->name = vStringNew();
649 initToken(token);
650 return token;
653 static void deleteToken (tokenInfo *const token)
655 if (token != NULL)
657 vStringDelete(token->name);
658 eFree(token);
662 static const char *accessString (const accessType laccess)
664 static const char *const names [] = {
665 "?", "private", "protected", "public", "default"
667 Assert (sizeof (names) / sizeof (names [0]) == ACCESS_COUNT);
668 Assert ((int) laccess < ACCESS_COUNT);
669 return names[(int) laccess];
672 static const char *implementationString (const impType imp)
674 static const char *const names [] = {
675 "?", "abstract", "virtual", "pure virtual"
677 Assert (sizeof (names) / sizeof (names [0]) == IMP_COUNT);
678 Assert ((int) imp < IMP_COUNT);
679 return names [(int) imp];
683 * Debugging functions
686 #ifdef TM_DEBUG
688 #define boolString(c) ((c) ? "TRUE" : "FALSE")
690 static const char *tokenString (const tokenType type)
692 static const char *const names [] = {
693 "none", "args", "}", "{", "comma", "double colon", "keyword", "name",
694 "package", "paren-name", "semicolon", "specifier", "*", "[]"
696 Assert (sizeof (names) / sizeof (names [0]) == TOKEN_COUNT);
697 Assert ((int) type < TOKEN_COUNT);
698 return names[(int) type];
701 static const char *scopeString (const tagScope scope)
703 static const char *const names [] = {
704 "global", "static", "extern", "friend", "typedef"
706 Assert (sizeof (names) / sizeof (names [0]) == SCOPE_COUNT);
707 Assert ((int) scope < SCOPE_COUNT);
708 return names[(int) scope];
711 static const char *declString (const declType declaration)
713 static const char *const names [] = {
714 "?", "base", "class", "enum", "event", "signal", "function",
715 "function template", "ignore", "interface", "module", "namespace",
716 "no mangle", "package", "struct", "union",
718 Assert (sizeof (names) / sizeof (names [0]) == DECL_COUNT);
719 Assert ((int) declaration < DECL_COUNT);
720 return names[(int) declaration];
723 static const char *keywordString (const keywordId keyword)
725 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
726 const char *name = "none";
727 size_t i;
728 for (i = 0 ; i < count ; ++i)
730 const keywordDesc *p = &KeywordTable[i];
732 if (p->id == keyword)
734 name = p->name;
735 break;
738 return name;
741 static void __unused__ pt (tokenInfo *const token)
743 if (isType (token, TOKEN_NAME))
744 printf("type: %-12s: %-13s line: %lu\n",
745 tokenString (token->type), vStringValue (token->name),
746 token->lineNumber);
747 else if (isType (token, TOKEN_KEYWORD))
748 printf("type: %-12s: %-13s line: %lu\n",
749 tokenString (token->type), keywordString (token->keyword),
750 token->lineNumber);
751 else
752 printf("type: %-12s line: %lu\n",
753 tokenString (token->type), token->lineNumber);
756 static void __unused__ ps (statementInfo *const st)
758 unsigned int i;
759 printf("scope: %s decl: %s gotName: %s gotParenName: %s\n",
760 scopeString (st->scope), declString (st->declaration),
761 boolString (st->gotName), boolString (st->gotParenName));
762 printf("haveQualifyingName: %s\n", boolString (st->haveQualifyingName));
763 printf("access: %s default: %s\n", accessString (st->member.access),
764 accessString (st->member.accessDefault));
765 printf("token : ");
766 pt(activeToken (st));
767 for (i = 1 ; i < (unsigned int) NumTokens ; ++i)
769 printf("prev %u : ", i);
770 pt(prevToken (st, i));
772 printf("context: ");
773 pt(st->context);
776 #endif
779 * Statement management
782 static boolean isDataTypeKeyword (const tokenInfo *const token)
784 switch (token->keyword)
786 case KEYWORD_BOOLEAN:
787 case KEYWORD_BYTE:
788 case KEYWORD_CHAR:
789 case KEYWORD_DOUBLE:
790 case KEYWORD_FLOAT:
791 case KEYWORD_INT:
792 case KEYWORD_LONG:
793 case KEYWORD_SHORT:
794 case KEYWORD_VOID:
795 case KEYWORD_WCHAR_T:
796 case KEYWORD_SIZE_T:
797 return TRUE;
798 default:
799 return FALSE;
803 #if 0
804 static boolean isVariableKeyword (const tokenInfo *const token)
806 switch (token->keyword)
808 case KEYWORD_CONST:
809 case KEYWORD_EXTERN:
810 case KEYWORD_REGISTER:
811 case KEYWORD_STATIC:
812 case KEYWORD_VIRTUAL:
813 case KEYWORD_SIGNED:
814 case KEYWORD_UNSIGNED:
815 return TRUE;
816 default:
817 return FALSE;
820 #endif
822 static boolean isContextualKeyword (const tokenInfo *const token)
824 boolean result;
825 switch (token->keyword)
827 case KEYWORD_CLASS:
828 case KEYWORD_ENUM:
829 case KEYWORD_INTERFACE:
830 case KEYWORD_NAMESPACE:
831 case KEYWORD_STRUCT:
832 case KEYWORD_UNION:
834 result = TRUE;
835 break;
838 default:
840 result = FALSE;
841 break;
844 return result;
847 static boolean isContextualStatement (const statementInfo *const st)
849 boolean result = FALSE;
851 if (st != NULL)
853 if (isLanguage (Lang_vala))
855 /* All can be a contextual statment as properties can be of any type */
856 result = TRUE;
858 else
860 switch (st->declaration)
862 case DECL_CLASS:
863 case DECL_ENUM:
864 case DECL_INTERFACE:
865 case DECL_NAMESPACE:
866 case DECL_STRUCT:
867 case DECL_UNION:
869 result = TRUE;
870 break;
873 default:
875 result = FALSE;
876 break;
881 return result;
884 static boolean isMember (const statementInfo *const st)
886 boolean result;
887 if (isType (st->context, TOKEN_NAME))
888 result = TRUE;
889 else
890 result = isContextualStatement (st->parent);
891 return result;
894 static void initMemberInfo (statementInfo *const st)
896 accessType accessDefault = ACCESS_UNDEFINED;
898 if (st->parent != NULL) switch (st->parent->declaration)
900 case DECL_ENUM:
901 case DECL_NAMESPACE:
903 accessDefault = ACCESS_UNDEFINED;
904 break;
906 case DECL_CLASS:
908 if (isLanguage (Lang_java))
909 accessDefault = ACCESS_DEFAULT;
910 else
911 accessDefault = ACCESS_PRIVATE;
912 break;
914 case DECL_INTERFACE:
915 case DECL_STRUCT:
916 case DECL_UNION:
918 accessDefault = ACCESS_PUBLIC;
919 break;
921 default:
922 break;
924 st->member.accessDefault = accessDefault;
925 st->member.access = accessDefault;
928 static void reinitStatement (statementInfo *const st, const boolean partial)
930 unsigned int i;
932 if (! partial)
934 st->scope = SCOPE_GLOBAL;
935 if (isContextualStatement (st->parent))
936 st->declaration = DECL_BASE;
937 else
938 st->declaration = DECL_NONE;
940 st->gotParenName = FALSE;
941 st->implementation = IMP_DEFAULT;
942 st->gotArgs = FALSE;
943 st->gotName = FALSE;
944 st->haveQualifyingName = FALSE;
945 st->argEndPosition = 0;
947 st->tokenIndex = 0;
948 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
950 initToken (st->token [i]);
953 initToken (st->context);
954 initToken (st->blockName);
955 vStringClear (st->parentClasses);
957 /* Init member info. */
958 if (! partial)
959 st->member.access = st->member.accessDefault;
961 /* Init first token */
962 if (!partial)
963 initToken(st->firstToken);
966 static void reinitStatementWithToken (statementInfo *const st,
967 tokenInfo *token, const boolean partial)
969 tokenInfo *const save = newToken ();
970 /* given token can be part of reinit statementInfo */
971 copyToken (save, token);
972 reinitStatement (st, partial);
973 token = activeToken (st);
974 copyToken (token, save);
975 deleteToken (save);
976 ++st->tokenIndex; /* this is quite save becouse current tokenIndex = 0 */
979 static void initStatement (statementInfo *const st, statementInfo *const parent)
981 st->parent = parent;
982 initMemberInfo (st);
983 reinitStatement (st, FALSE);
984 if (parent)
986 const tokenInfo *const src = activeToken (parent);
987 tokenInfo *const dst = activeToken (st);
988 copyToken (dst, src);
989 st->tokenIndex++;
994 * Tag generation functions
996 static cKind cTagKind (const tagType type)
998 cKind result = CK_UNDEFINED;
999 switch (type)
1001 case TAG_CLASS: result = CK_CLASS; break;
1002 case TAG_ENUM: result = CK_ENUMERATION; break;
1003 case TAG_ENUMERATOR: result = CK_ENUMERATOR; break;
1004 case TAG_FUNCTION: result = CK_FUNCTION; break;
1005 case TAG_MEMBER: result = CK_MEMBER; break;
1006 case TAG_NAMESPACE: result = CK_NAMESPACE; break;
1007 case TAG_PROTOTYPE: result = CK_PROTOTYPE; break;
1008 case TAG_STRUCT: result = CK_STRUCT; break;
1009 case TAG_TYPEDEF: result = CK_TYPEDEF; break;
1010 case TAG_UNION: result = CK_UNION; break;
1011 case TAG_VARIABLE: result = CK_VARIABLE; break;
1012 case TAG_EXTERN_VAR: result = CK_EXTERN_VARIABLE; break;
1014 default: Assert ("Bad C tag type" == NULL); break;
1016 return result;
1019 static csharpKind csharpTagKind (const tagType type)
1021 csharpKind result = CSK_UNDEFINED;
1022 switch (type)
1024 case TAG_CLASS: result = CSK_CLASS; break;
1025 case TAG_ENUM: result = CSK_ENUMERATION; break;
1026 case TAG_ENUMERATOR: result = CSK_ENUMERATOR; break;
1027 case TAG_EVENT: result = CSK_EVENT; break;
1028 case TAG_FIELD: result = CSK_FIELD ; break;
1029 case TAG_INTERFACE: result = CSK_INTERFACE; break;
1030 case TAG_LOCAL: result = CSK_LOCAL; break;
1031 case TAG_METHOD: result = CSK_METHOD; break;
1032 case TAG_NAMESPACE: result = CSK_NAMESPACE; break;
1033 case TAG_PROPERTY: result = CSK_PROPERTY; break;
1034 case TAG_STRUCT: result = CSK_STRUCT; break;
1035 case TAG_TYPEDEF: result = CSK_TYPEDEF; break;
1037 default: Assert ("Bad C# tag type" == NULL); break;
1039 return result;
1042 static dKind dTagKind (const tagType type)
1044 dKind result = DK_UNDEFINED;
1045 switch (type)
1047 case TAG_CLASS: result = DK_CLASS; break;
1048 case TAG_ENUM: result = DK_ENUMERATION; break;
1049 case TAG_ENUMERATOR: result = DK_ENUMERATOR; break;
1050 case TAG_FUNCTION: result = DK_FUNCTION; break;
1051 case TAG_INTERFACE: result = DK_INTERFACE; break;
1052 case TAG_MEMBER: result = DK_MEMBER; break;
1053 case TAG_NAMESPACE: result = DK_NAMESPACE; break;
1054 case TAG_PROTOTYPE: result = DK_PROTOTYPE; break;
1055 case TAG_STRUCT: result = DK_STRUCT; break;
1056 case TAG_TYPEDEF: result = DK_TYPEDEF; break;
1057 case TAG_UNION: result = DK_UNION; break;
1058 case TAG_VARIABLE: result = DK_VARIABLE; break;
1059 case TAG_EXTERN_VAR: result = DK_EXTERN_VARIABLE; break;
1061 default: Assert ("Bad D tag type" == NULL); break;
1063 return result;
1066 static valaKind valaTagKind (const tagType type)
1068 valaKind result = VK_UNDEFINED;
1069 switch (type)
1071 case TAG_CLASS: result = VK_CLASS; break;
1072 case TAG_ENUM: result = VK_ENUMERATION; break;
1073 case TAG_ENUMERATOR: result = VK_ENUMERATOR; break;
1074 case TAG_SIGNAL: result = VK_SIGNAL; break;
1075 case TAG_FIELD: result = VK_FIELD ; break;
1076 case TAG_INTERFACE: result = VK_INTERFACE; break;
1077 case TAG_LOCAL: result = VK_LOCAL; break;
1078 case TAG_METHOD: result = VK_METHOD; break;
1079 case TAG_NAMESPACE: result = VK_NAMESPACE; break;
1080 case TAG_PROPERTY: result = VK_PROPERTY; break;
1081 case TAG_STRUCT: result = VK_STRUCT; break;
1083 default: Assert ("Bad Vala tag type" == NULL); break;
1085 return result;
1088 static javaKind javaTagKind (const tagType type)
1090 javaKind result = JK_UNDEFINED;
1091 switch (type)
1093 case TAG_CLASS: result = JK_CLASS; break;
1094 case TAG_FIELD: result = JK_FIELD; break;
1095 case TAG_INTERFACE: result = JK_INTERFACE; break;
1096 case TAG_METHOD: result = JK_METHOD; break;
1097 case TAG_PACKAGE: result = JK_PACKAGE; break;
1099 default: Assert ("Bad Java tag type" == NULL); break;
1101 return result;
1104 static const char *tagName (const tagType type)
1106 const char* result;
1107 if (isLanguage (Lang_java))
1108 result = JavaKinds [javaTagKind (type)].name;
1109 else if (isLanguage (Lang_csharp))
1110 result = CsharpKinds [csharpTagKind (type)].name;
1111 else if (isLanguage (Lang_d))
1112 result = DKinds [dTagKind (type)].name;
1113 else if (isLanguage (Lang_vala))
1114 result = ValaKinds [valaTagKind (type)].name;
1115 else
1116 result = CKinds [cTagKind (type)].name;
1117 return result;
1120 static int tagLetter (const tagType type)
1122 int result;
1123 if (isLanguage (Lang_csharp))
1124 result = CsharpKinds [csharpTagKind (type)].letter;
1125 else if (isLanguage (Lang_d))
1126 result = DKinds [dTagKind (type)].letter;
1127 else if (isLanguage (Lang_java))
1128 result = JavaKinds [javaTagKind (type)].letter;
1129 else if (isLanguage (Lang_vala))
1130 result = ValaKinds [valaTagKind (type)].letter;
1131 else
1132 result = CKinds [cTagKind (type)].letter;
1133 return result;
1137 static boolean includeTag (const tagType type, const boolean isFileScope)
1139 boolean result;
1140 if (isFileScope && ! Option.include.fileScope)
1141 result = FALSE;
1142 else if (isLanguage (Lang_java))
1143 result = JavaKinds [javaTagKind (type)].enabled;
1144 else
1145 result = CKinds [cTagKind (type)].enabled;
1146 return result;
1150 static tagType declToTagType (const declType declaration)
1152 tagType type = TAG_UNDEFINED;
1154 switch (declaration)
1156 case DECL_CLASS: type = TAG_CLASS; break;
1157 case DECL_ENUM: type = TAG_ENUM; break;
1158 case DECL_FUNCTION: type = TAG_FUNCTION; break;
1159 case DECL_FUNCTION_TEMPLATE: type = TAG_FUNCTION; break;
1160 case DECL_INTERFACE:type = TAG_INTERFACE; break;
1161 case DECL_NAMESPACE:type = TAG_NAMESPACE; break;
1162 case DECL_STRUCT: type = TAG_STRUCT; break;
1163 case DECL_UNION: type = TAG_UNION; break;
1165 default: Assert ("Unexpected declaration" == NULL); break;
1167 return type;
1170 static const char* accessField (const statementInfo *const st)
1172 const char* result = NULL;
1174 if ((isLanguage (Lang_cpp) || isLanguage (Lang_d) || isLanguage (Lang_ferite)) &&
1175 st->scope == SCOPE_FRIEND)
1176 result = "friend";
1177 else if (st->member.access != ACCESS_UNDEFINED)
1178 result = accessString (st->member.access);
1179 return result;
1182 static void addOtherFields (tagEntryInfo* const tag, const tagType type,
1183 const statementInfo *const st, vString *const scope)
1185 /* For selected tag types, append an extension flag designating the
1186 * parent object in which the tag is defined.
1188 switch (type)
1190 default: break;
1192 case TAG_CLASS:
1193 case TAG_ENUM:
1194 case TAG_ENUMERATOR:
1195 case TAG_FIELD:
1196 case TAG_FUNCTION:
1197 case TAG_INTERFACE:
1198 case TAG_MEMBER:
1199 case TAG_METHOD:
1200 case TAG_PROTOTYPE:
1201 case TAG_STRUCT:
1202 case TAG_TYPEDEF:
1203 case TAG_UNION:
1205 if (vStringLength (scope) > 0 &&
1206 (isMember (st) || st->parent->declaration == DECL_NAMESPACE))
1208 if (isType (st->context, TOKEN_NAME))
1209 tag->extensionFields.scope [0] = tagName (TAG_CLASS);
1210 else
1211 tag->extensionFields.scope [0] =
1212 tagName (declToTagType (parentDecl (st)));
1213 tag->extensionFields.scope [1] = vStringValue (scope);
1215 if ((type == TAG_CLASS || type == TAG_INTERFACE ||
1216 type == TAG_STRUCT) && vStringLength (st->parentClasses) > 0)
1218 tag->extensionFields.inheritance =
1219 vStringValue (st->parentClasses);
1221 if (st->implementation != IMP_DEFAULT &&
1222 (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) || isLanguage (Lang_vala) ||
1223 isLanguage (Lang_java) || isLanguage (Lang_d) || isLanguage (Lang_ferite)))
1225 tag->extensionFields.implementation =
1226 implementationString (st->implementation);
1228 if (isMember (st))
1230 tag->extensionFields.access = accessField (st);
1232 if ((TRUE == st->gotArgs) && (TRUE == Option.extensionFields.argList) &&
1233 ((TAG_FUNCTION == type) || (TAG_METHOD == type) || (TAG_PROTOTYPE == type)))
1235 tag->extensionFields.arglist = getArglistFromFilePos(
1236 tag->filePosition, tag->name);
1238 break;
1242 if ((TAG_FIELD == tag->type) || (TAG_MEMBER == tag->type) ||
1243 (TAG_EXTERN_VAR == tag->type) || (TAG_TYPEDEF == tag->type) ||
1244 (TAG_VARIABLE == tag->type) || (TAG_METHOD == tag->type) ||
1245 (TAG_PROTOTYPE == tag->type) || (TAG_FUNCTION == tag->type))
1247 if (((TOKEN_NAME == st->firstToken->type) || isDataTypeKeyword(st->firstToken))
1248 && (0 != strcmp(vStringValue(st->firstToken->name), tag->name)))
1250 tag->extensionFields.varType = getVarType(st);
1255 static const char *getVarType (const statementInfo *const st)
1257 static vString *vt = NULL;
1258 unsigned int i;
1260 if (! st->gotArgs)
1261 return vStringValue(st->firstToken->name); /* ignore non-functions */
1263 if (vt == NULL)
1264 vt = vStringNew();
1265 else
1266 vStringClear(vt);
1268 for (i = 0; i < st->tokenIndex; i++)
1270 tokenInfo *t = st->token[i];
1272 switch (t->type)
1274 case TOKEN_NAME: /* user typename */
1275 if (strcmp(vStringValue(t->name), vStringValue(st->firstToken->name)) != 0)
1276 continue;
1277 break;
1278 case TOKEN_KEYWORD:
1279 if (t->keyword != KEYWORD_EXTERN && t->keyword != KEYWORD_STATIC) /* uninteresting keywords */
1280 break;
1281 continue;
1282 case TOKEN_STAR: vStringCatS(vt, " *"); continue;
1283 case TOKEN_ARRAY: vStringCatS(vt, "[]"); continue;
1284 default: continue;
1286 if (vStringLength(vt) > 0)
1287 if (isalpha(vStringValue(vt)[vStringLength(vt) - 1]))
1288 vStringPut(vt, ' ');
1289 vStringCat(vt, t->name);
1291 vStringTerminate(vt);
1292 return vStringValue(vt);
1295 static void addContextSeparator (vString *const scope)
1297 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
1298 vStringCatS (scope, "::");
1299 else if (isLanguage (Lang_java) || isLanguage (Lang_d) || isLanguage (Lang_ferite) ||
1300 isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1301 vStringCatS (scope, ".");
1304 static void findScopeHierarchy (vString *const string,
1305 const statementInfo *const st)
1307 const char* const anon = "<anonymous>";
1308 boolean nonAnonPresent = FALSE;
1310 vStringClear (string);
1311 if (isType (st->context, TOKEN_NAME))
1313 vStringCopy (string, st->context->name);
1314 nonAnonPresent = TRUE;
1316 if (st->parent != NULL)
1318 vString *temp = vStringNew ();
1319 const statementInfo *s;
1321 for (s = st->parent ; s != NULL ; s = s->parent)
1323 if (isContextualStatement (s) ||
1324 s->declaration == DECL_NAMESPACE)
1326 vStringCopy (temp, string);
1327 vStringClear (string);
1328 if (isType (s->blockName, TOKEN_NAME))
1330 if (isType (s->context, TOKEN_NAME) &&
1331 vStringLength (s->context->name) > 0)
1333 vStringCat (string, s->context->name);
1334 addContextSeparator (string);
1336 vStringCat (string, s->blockName->name);
1337 nonAnonPresent = TRUE;
1339 else
1340 vStringCopyS (string, anon);
1341 if (vStringLength (temp) > 0)
1342 addContextSeparator (string);
1343 vStringCat (string, temp);
1346 vStringDelete (temp);
1348 if (! nonAnonPresent)
1349 vStringClear (string);
1353 static void makeExtraTagEntry (const tagType type, tagEntryInfo *const e,
1354 vString *const scope)
1356 if (Option.include.qualifiedTags &&
1357 scope != NULL && vStringLength (scope) > 0)
1359 vString *const scopedName = vStringNew ();
1361 if (type != TAG_ENUMERATOR)
1362 vStringCopy (scopedName, scope);
1363 else
1365 /* remove last component (i.e. enumeration name) from scope */
1366 const char* const sc = vStringValue (scope);
1367 const char* colon = strrchr (sc, ':');
1368 if (colon != NULL)
1370 while (*colon == ':' && colon > sc)
1371 --colon;
1372 vStringNCopy (scopedName, scope, colon + 1 - sc);
1375 if (vStringLength (scopedName) > 0)
1377 addContextSeparator (scopedName);
1378 vStringCatS (scopedName, e->name);
1379 e->name = vStringValue (scopedName);
1380 makeTagEntry (e);
1382 vStringDelete (scopedName);
1386 static void makeTag (const tokenInfo *const token,
1387 const statementInfo *const st,
1388 boolean isFileScope, const tagType type)
1390 #ifdef DEBUG_C
1391 printToken(token);
1392 fprintf(stderr, "<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>\n");
1393 printStatement(st);
1394 #endif
1395 /* Nothing is really of file scope when it appears in a header file.
1397 isFileScope = (boolean) (isFileScope && ! isHeaderFile ());
1399 if (isType (token, TOKEN_NAME) && vStringLength (token->name) > 0 /* &&
1400 includeTag (type, isFileScope) */)
1402 vString *scope = vStringNew ();
1403 tagEntryInfo e;
1405 /* take only functions which are introduced by "function ..." */
1406 if (type == TAG_FUNCTION && isLanguage (Lang_ferite) &&
1407 strncmp("function", st->firstToken->name->buffer, 8) != 0)
1409 return;
1412 initTagEntry (&e, vStringValue (token->name));
1414 e.lineNumber = token->lineNumber;
1415 e.filePosition = token->filePosition;
1416 e.isFileScope = isFileScope;
1417 e.kindName = tagName (type);
1418 e.kind = tagLetter (type);
1419 e.type = type;
1421 findScopeHierarchy (scope, st);
1422 addOtherFields (&e, type, st, scope);
1424 #ifdef DEBUG_C
1425 printTagEntry(&e);
1426 #endif
1427 makeTagEntry (&e);
1428 if (NULL != TagEntryFunction)
1429 makeExtraTagEntry (type, &e, scope);
1430 vStringDelete (scope);
1431 if (NULL != e.extensionFields.arglist)
1432 free((char *) e.extensionFields.arglist);
1436 static boolean isValidTypeSpecifier (const declType declaration)
1438 boolean result;
1439 switch (declaration)
1441 case DECL_BASE:
1442 case DECL_CLASS:
1443 case DECL_ENUM:
1444 case DECL_STRUCT:
1445 case DECL_UNION:
1446 result = TRUE;
1447 break;
1449 default:
1450 result = FALSE;
1451 break;
1453 return result;
1456 static void qualifyEnumeratorTag (const statementInfo *const st,
1457 const tokenInfo *const nameToken)
1459 if (isType (nameToken, TOKEN_NAME))
1460 makeTag (nameToken, st, TRUE, TAG_ENUMERATOR);
1463 static void qualifyFunctionTag (const statementInfo *const st,
1464 const tokenInfo *const nameToken)
1466 if (isType (nameToken, TOKEN_NAME))
1468 const tagType type = (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1469 ? TAG_METHOD : TAG_FUNCTION;
1470 const boolean isFileScope =
1471 (boolean) (st->member.access == ACCESS_PRIVATE ||
1472 (!isMember (st) && st->scope == SCOPE_STATIC));
1474 makeTag (nameToken, st, isFileScope, type);
1478 static void qualifyFunctionDeclTag (const statementInfo *const st,
1479 const tokenInfo *const nameToken)
1481 if (! isType (nameToken, TOKEN_NAME))
1483 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1484 qualifyFunctionTag (st, nameToken);
1485 else if (st->scope == SCOPE_TYPEDEF)
1486 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1487 else if (isValidTypeSpecifier (st->declaration) &&
1488 ! (isLanguage (Lang_csharp) || isLanguage (Lang_vala)))
1489 makeTag (nameToken, st, TRUE, TAG_PROTOTYPE);
1492 static void qualifyCompoundTag (const statementInfo *const st,
1493 const tokenInfo *const nameToken)
1495 if (isType (nameToken, TOKEN_NAME))
1497 const tagType type = declToTagType (st->declaration);
1499 if (type != TAG_UNDEFINED)
1500 makeTag (nameToken, st, (boolean) (! isLanguage (Lang_java) &&
1501 ! isLanguage (Lang_csharp) &&
1502 ! isLanguage (Lang_vala)), type);
1506 static void qualifyBlockTag (statementInfo *const st,
1507 const tokenInfo *const nameToken)
1509 switch (st->declaration)
1511 case DECL_CLASS:
1512 case DECL_ENUM:
1513 case DECL_INTERFACE:
1514 case DECL_NAMESPACE:
1515 case DECL_STRUCT:
1516 case DECL_UNION:
1517 qualifyCompoundTag (st, nameToken);
1518 break;
1519 default: break;
1523 static void qualifyVariableTag (const statementInfo *const st,
1524 const tokenInfo *const nameToken)
1526 /* We have to watch that we do not interpret a declaration of the
1527 * form "struct tag;" as a variable definition. In such a case, the
1528 * token preceding the name will be a keyword.
1530 if (! isType (nameToken, TOKEN_NAME))
1532 else if (st->declaration == DECL_IGNORE)
1534 else if (st->scope == SCOPE_TYPEDEF)
1535 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1536 else if (st->declaration == DECL_PACKAGE)
1537 makeTag (nameToken, st, FALSE, TAG_PACKAGE);
1538 else if (st->declaration == DECL_MODULE) /* handle modules in D as namespaces */
1539 makeTag (nameToken, st, FALSE, TAG_NAMESPACE);
1540 else if (isValidTypeSpecifier (st->declaration))
1542 if (isMember (st))
1544 if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1545 makeTag (nameToken, st, (boolean) (st->member.access == ACCESS_PRIVATE), TAG_FIELD);
1546 else if (st->scope == SCOPE_GLOBAL || st->scope == SCOPE_STATIC)
1547 makeTag (nameToken, st, TRUE, TAG_MEMBER);
1549 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp) || isLanguage (Lang_vala))
1551 else
1553 if (st->scope == SCOPE_EXTERN || ! st->haveQualifyingName)
1554 makeTag (nameToken, st, FALSE, TAG_EXTERN_VAR);
1555 else
1556 makeTag (nameToken, st, (boolean) (st->scope == SCOPE_STATIC), TAG_VARIABLE);
1562 * Parsing functions
1565 static int skipToOneOf (const char *const chars)
1567 int c;
1569 c = cppGetc ();
1570 while (c != EOF && c != '\0' && strchr (chars, c) == NULL);
1572 return c;
1575 /* Skip to the next non-white character.
1577 static int skipToNonWhite (void)
1579 int c;
1583 c = cppGetc ();
1585 while (isspace (c));
1587 return c;
1590 /* Skips to the next brace in column 1. This is intended for cases where
1591 * preprocessor constructs result in unbalanced braces.
1593 static void skipToFormattedBraceMatch (void)
1595 int c, next;
1597 c = cppGetc ();
1598 next = cppGetc ();
1599 while (c != EOF && (c != '\n' || next != '}'))
1601 c = next;
1602 next = cppGetc ();
1606 /* Skip to the matching character indicated by the pair string. If skipping
1607 * to a matching brace and any brace is found within a different level of a
1608 * #if conditional statement while brace formatting is in effect, we skip to
1609 * the brace matched by its formatting. It is assumed that we have already
1610 * read the character which starts the group (i.e. the first character of
1611 * "pair").
1613 static void skipToMatch (const char *const pair)
1615 const boolean braceMatching = (boolean) (strcmp ("{}", pair) == 0);
1616 const boolean braceFormatting = (boolean) (isBraceFormat () && braceMatching);
1617 const unsigned int initialLevel = getDirectiveNestLevel ();
1618 const int begin = pair [0], end = pair [1];
1619 const unsigned long inputLineNumber = getInputLineNumber ();
1620 int matchLevel = 1;
1621 int c = '\0';
1622 while (matchLevel > 0 && (c = cppGetc ()) != EOF)
1624 if (c == begin)
1626 ++matchLevel;
1627 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1629 skipToFormattedBraceMatch ();
1630 break;
1633 else if (c == end)
1635 --matchLevel;
1636 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1638 skipToFormattedBraceMatch ();
1639 break;
1643 if (c == EOF)
1645 verbose ("%s: failed to find match for '%c' at line %lu\n",
1646 getInputFileName (), begin, inputLineNumber);
1647 if (braceMatching)
1648 longjmp (Exception, (int) ExceptionBraceFormattingError);
1649 else
1650 longjmp (Exception, (int) ExceptionFormattingError);
1654 static void skipParens (void)
1656 const int c = skipToNonWhite ();
1658 if (c == '(')
1659 skipToMatch ("()");
1660 else
1661 cppUngetc (c);
1664 static void skipBraces (void)
1666 const int c = skipToNonWhite ();
1668 if (c == '{')
1669 skipToMatch ("{}");
1670 else
1671 cppUngetc (c);
1674 static keywordId analyzeKeyword (const char *const name)
1676 const keywordId id = (keywordId) lookupKeyword (name, getSourceLanguage ());
1677 return id;
1680 static void analyzeIdentifier (tokenInfo *const token)
1682 char *const name = vStringValue (token->name);
1683 const char *replacement = NULL;
1684 boolean parensToo = FALSE;
1686 if (isLanguage (Lang_java) ||
1687 ! isIgnoreToken (name, &parensToo, &replacement))
1689 if (replacement != NULL)
1690 token->keyword = analyzeKeyword (replacement);
1691 else
1692 token->keyword = analyzeKeyword (vStringValue (token->name));
1694 if (token->keyword == KEYWORD_NONE)
1695 token->type = TOKEN_NAME;
1696 else
1697 token->type = TOKEN_KEYWORD;
1699 else
1701 initToken (token);
1702 if (parensToo)
1704 int c = skipToNonWhite ();
1706 if (c == '(')
1707 skipToMatch ("()");
1712 static void readIdentifier (tokenInfo *const token, const int firstChar)
1714 vString *const name = token->name;
1715 int c = firstChar;
1717 initToken (token);
1719 /* Bug #1585745 (CTags): strangely, C++ destructors allow whitespace between
1720 * the ~ and the class name. */
1721 if (isLanguage (Lang_cpp) && firstChar == '~')
1723 vStringPut (name, c);
1724 c = skipToNonWhite ();
1729 vStringPut (name, c);
1730 c = cppGetc ();
1731 } while (isident (c) || (isLanguage (Lang_vala) && '.' == c));
1732 vStringTerminate (name);
1733 cppUngetc (c); /* unget non-identifier character */
1735 /* Vala supports '?' at end of a type (with or without whitespace before) for nullable types */
1736 if (isLanguage (Lang_vala))
1738 c = skipToNonWhite ();
1739 if ('?' == c)
1740 vStringPut (name, c);
1741 else
1742 cppUngetc (c);
1745 analyzeIdentifier (token);
1748 static void readPackageName (tokenInfo *const token, const int firstChar)
1750 vString *const name = token->name;
1751 int c = firstChar;
1753 initToken (token);
1755 while (isident (c) || c == '.')
1757 vStringPut (name, c);
1758 c = cppGetc ();
1760 vStringTerminate (name);
1761 cppUngetc (c); /* unget non-package character */
1764 static void readPackageOrNamespace (statementInfo *const st, const declType declaration)
1766 st->declaration = declaration;
1768 if (declaration == DECL_NAMESPACE && !(isLanguage (Lang_csharp) || isLanguage (Lang_vala)))
1770 /* In C++ a namespace is specified one level at a time. */
1771 return;
1773 else
1775 /* In C#, a namespace can also be specified like a Java package name. */
1776 tokenInfo *const token = activeToken (st);
1777 Assert (isType (token, TOKEN_KEYWORD));
1778 readPackageName (token, skipToNonWhite ());
1779 token->type = TOKEN_NAME;
1780 st->gotName = TRUE;
1781 st->haveQualifyingName = TRUE;
1785 static void readPackage (statementInfo *const st)
1787 tokenInfo *const token = activeToken (st);
1788 Assert (isType (token, TOKEN_KEYWORD));
1789 readPackageName (token, skipToNonWhite ());
1790 token->type = TOKEN_NAME;
1791 if (isLanguage (Lang_d))
1792 st->declaration = DECL_MODULE;
1793 else
1794 st->declaration = DECL_PACKAGE;
1795 st->gotName = TRUE;
1796 st->haveQualifyingName = TRUE;
1799 static void processName (statementInfo *const st)
1801 Assert (isType (activeToken (st), TOKEN_NAME));
1802 if (st->gotName && st->declaration == DECL_NONE)
1803 st->declaration = DECL_BASE;
1804 st->gotName = TRUE;
1805 st->haveQualifyingName = TRUE;
1808 static void readOperator (statementInfo *const st)
1810 const char *const acceptable = "+-*/%^&|~!=<>,[]";
1811 const tokenInfo* const prev = prevToken (st,1);
1812 tokenInfo *const token = activeToken (st);
1813 vString *const name = token->name;
1814 int c = skipToNonWhite ();
1816 /* When we arrive here, we have the keyword "operator" in 'name'.
1818 if (isType (prev, TOKEN_KEYWORD) && (prev->keyword == KEYWORD_ENUM ||
1819 prev->keyword == KEYWORD_STRUCT || prev->keyword == KEYWORD_UNION))
1820 ; /* ignore "operator" keyword if preceded by these keywords */
1821 else if (c == '(')
1823 /* Verify whether this is a valid function call (i.e. "()") operator.
1825 if (cppGetc () == ')')
1827 vStringPut (name, ' '); /* always separate operator from keyword */
1828 c = skipToNonWhite ();
1829 if (c == '(')
1830 vStringCatS (name, "()");
1832 else
1834 skipToMatch ("()");
1835 c = cppGetc ();
1838 else if (isident1 (c))
1840 /* Handle "new" and "delete" operators, and conversion functions
1841 * (per 13.3.1.1.2 [2] of the C++ spec).
1843 boolean whiteSpace = TRUE; /* default causes insertion of space */
1846 if (isspace (c))
1847 whiteSpace = TRUE;
1848 else
1850 if (whiteSpace)
1852 vStringPut (name, ' ');
1853 whiteSpace = FALSE;
1855 vStringPut (name, c);
1857 c = cppGetc ();
1858 } while (! isOneOf (c, "(;") && c != EOF);
1859 vStringTerminate (name);
1861 else if (isOneOf (c, acceptable))
1863 vStringPut (name, ' '); /* always separate operator from keyword */
1866 vStringPut (name, c);
1867 c = cppGetc ();
1868 } while (isOneOf (c, acceptable));
1869 vStringTerminate (name);
1872 cppUngetc (c);
1874 token->type = TOKEN_NAME;
1875 token->keyword = KEYWORD_NONE;
1876 processName (st);
1879 static void copyToken (tokenInfo *const dest, const tokenInfo *const src)
1881 dest->type = src->type;
1882 dest->keyword = src->keyword;
1883 dest->filePosition = src->filePosition;
1884 dest->lineNumber = src->lineNumber;
1885 vStringCopy (dest->name, src->name);
1888 static void setAccess (statementInfo *const st, const accessType laccess)
1890 if (isMember (st))
1892 if (isLanguage (Lang_cpp) || isLanguage (Lang_d) || isLanguage (Lang_ferite))
1894 int c = skipToNonWhite ();
1896 if (c == ':')
1897 reinitStatementWithToken (st, prevToken (st, 1), FALSE);
1898 else
1899 cppUngetc (c);
1901 st->member.accessDefault = laccess;
1903 st->member.access = laccess;
1907 static void discardTypeList (tokenInfo *const token)
1909 int c = skipToNonWhite ();
1910 while (isident1 (c))
1912 readIdentifier (token, c);
1913 c = skipToNonWhite ();
1914 if (c == '.' || c == ',')
1915 c = skipToNonWhite ();
1917 cppUngetc (c);
1920 static void addParentClass (statementInfo *const st, tokenInfo *const token)
1922 if (vStringLength (token->name) > 0 &&
1923 vStringLength (st->parentClasses) > 0)
1925 vStringPut (st->parentClasses, ',');
1927 vStringCat (st->parentClasses, token->name);
1930 static void readParents (statementInfo *const st, const int qualifier)
1932 tokenInfo *const token = newToken ();
1933 tokenInfo *const parent = newToken ();
1934 int c;
1938 c = skipToNonWhite ();
1939 if (isident1 (c))
1941 readIdentifier (token, c);
1942 if (isType (token, TOKEN_NAME))
1943 vStringCat (parent->name, token->name);
1944 else
1946 addParentClass (st, parent);
1947 initToken (parent);
1950 else if (c == qualifier)
1951 vStringPut (parent->name, c);
1952 else if (c == '<')
1953 skipToMatch ("<>");
1954 else if (isType (token, TOKEN_NAME))
1956 addParentClass (st, parent);
1957 initToken (parent);
1959 } while (c != '{' && c != EOF);
1960 cppUngetc (c);
1961 deleteToken (parent);
1962 deleteToken (token);
1965 static void processToken (tokenInfo *const token, statementInfo *const st)
1967 switch (token->keyword) /* is it a reserved word? */
1969 default: break;
1971 case KEYWORD_NONE: processName (st); break;
1972 case KEYWORD_ABSTRACT: st->implementation = IMP_ABSTRACT; break;
1973 case KEYWORD_ATTRIBUTE: skipParens (); initToken (token); break;
1974 case KEYWORD_CATCH: skipParens (); skipBraces (); break;
1975 case KEYWORD_CHAR: st->declaration = DECL_BASE; break;
1976 case KEYWORD_CLASS: st->declaration = DECL_CLASS; break;
1977 case KEYWORD_CONST: st->declaration = DECL_BASE; break;
1978 case KEYWORD_DOUBLE: st->declaration = DECL_BASE; break;
1979 case KEYWORD_ENUM: st->declaration = DECL_ENUM; break;
1980 case KEYWORD_EXTENDS: readParents (st, '.');
1981 setToken (st, TOKEN_NONE); break;
1982 case KEYWORD_FLOAT: st->declaration = DECL_BASE; break;
1983 case KEYWORD_FRIEND: st->scope = SCOPE_FRIEND; break;
1984 case KEYWORD_IMPLEMENTS:readParents (st, '.');
1985 setToken (st, TOKEN_NONE); break;
1986 case KEYWORD_IMPORT: st->declaration = DECL_IGNORE; break;
1987 case KEYWORD_INT: st->declaration = DECL_BASE; break;
1988 case KEYWORD_BOOLEAN: st->declaration = DECL_BASE; break;
1989 case KEYWORD_WCHAR_T: st->declaration = DECL_BASE; break;
1990 case KEYWORD_SIZE_T: st->declaration = DECL_BASE; break;
1991 case KEYWORD_INTERFACE: st->declaration = DECL_INTERFACE; break;
1992 case KEYWORD_LONG: st->declaration = DECL_BASE; break;
1993 case KEYWORD_OPERATOR: readOperator (st); break;
1994 case KEYWORD_MODULE: readPackage (st); break;
1995 case KEYWORD_PRIVATE: setAccess (st, ACCESS_PRIVATE); break;
1996 case KEYWORD_PROTECTED: setAccess (st, ACCESS_PROTECTED); break;
1997 case KEYWORD_PUBLIC: setAccess (st, ACCESS_PUBLIC); break;
1998 case KEYWORD_SHORT: st->declaration = DECL_BASE; break;
1999 case KEYWORD_SIGNED: st->declaration = DECL_BASE; break;
2000 case KEYWORD_STRUCT: st->declaration = DECL_STRUCT; break;
2001 case KEYWORD_THROWS: discardTypeList (token); break;
2002 case KEYWORD_TYPEDEF: st->scope = SCOPE_TYPEDEF; break;
2003 case KEYWORD_UNION: st->declaration = DECL_UNION; break;
2004 case KEYWORD_UNSIGNED: st->declaration = DECL_BASE; break;
2005 case KEYWORD_USING: st->declaration = DECL_IGNORE; break;
2006 case KEYWORD_VOID: st->declaration = DECL_BASE; break;
2007 case KEYWORD_VOLATILE: st->declaration = DECL_BASE; break;
2008 case KEYWORD_VIRTUAL: st->implementation = IMP_VIRTUAL; break;
2010 case KEYWORD_NAMESPACE: readPackageOrNamespace (st, DECL_NAMESPACE); break;
2011 case KEYWORD_PACKAGE: readPackageOrNamespace (st, DECL_PACKAGE); break;
2012 case KEYWORD_EVENT:
2014 if (isLanguage (Lang_csharp))
2015 st->declaration = DECL_EVENT;
2016 break;
2018 case KEYWORD_SIGNAL:
2020 if (isLanguage (Lang_vala))
2021 st->declaration = DECL_SIGNAL;
2022 break;
2024 case KEYWORD_EXTERN:
2026 if (! isLanguage (Lang_csharp) || !st->gotName)
2028 /*reinitStatement (st, FALSE);*/
2029 st->scope = SCOPE_EXTERN;
2030 st->declaration = DECL_BASE;
2032 break;
2034 case KEYWORD_STATIC:
2036 if (! isLanguage (Lang_java) && ! isLanguage (Lang_csharp) && ! isLanguage (Lang_vala))
2038 /*reinitStatement (st, FALSE);*/
2039 st->scope = SCOPE_STATIC;
2040 st->declaration = DECL_BASE;
2042 break;
2048 * Parenthesis handling functions
2051 static void restartStatement (statementInfo *const st)
2053 tokenInfo *const save = newToken ();
2054 tokenInfo *token = activeToken (st);
2056 copyToken (save, token);
2057 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>");)
2058 reinitStatement (st, FALSE);
2059 token = activeToken (st);
2060 copyToken (token, save);
2061 deleteToken (save);
2062 processToken (token, st);
2065 /* Skips over a the mem-initializer-list of a ctor-initializer, defined as:
2067 * mem-initializer-list:
2068 * mem-initializer, mem-initializer-list
2070 * mem-initializer:
2071 * [::] [nested-name-spec] class-name (...)
2072 * identifier
2074 static void skipMemIntializerList (tokenInfo *const token)
2076 int c;
2080 c = skipToNonWhite ();
2081 while (isident1 (c) || c == ':')
2083 if (c != ':')
2084 readIdentifier (token, c);
2085 c = skipToNonWhite ();
2087 if (c == '<')
2089 skipToMatch ("<>");
2090 c = skipToNonWhite ();
2092 if (c == '(')
2094 skipToMatch ("()");
2095 c = skipToNonWhite ();
2097 } while (c == ',');
2098 cppUngetc (c);
2101 static void skipMacro (statementInfo *const st)
2103 tokenInfo *const prev2 = prevToken (st, 2);
2105 if (isType (prev2, TOKEN_NAME))
2106 retardToken (st);
2107 skipToMatch ("()");
2110 /* Skips over characters following the parameter list. This will be either
2111 * non-ANSI style function declarations or C++ stuff. Our choices:
2113 * C (K&R):
2114 * int func ();
2115 * int func (one, two) int one; float two; {...}
2116 * C (ANSI):
2117 * int func (int one, float two);
2118 * int func (int one, float two) {...}
2119 * C++:
2120 * int foo (...) [const|volatile] [throw (...)];
2121 * int foo (...) [const|volatile] [throw (...)] [ctor-initializer] {...}
2122 * int foo (...) [const|volatile] [throw (...)] try [ctor-initializer] {...}
2123 * catch (...) {...}
2125 static boolean skipPostArgumentStuff (statementInfo *const st,
2126 parenInfo *const info)
2128 tokenInfo *const token = activeToken (st);
2129 unsigned int parameters = info->parameterCount;
2130 unsigned int elementCount = 0;
2131 boolean restart = FALSE;
2132 boolean end = FALSE;
2133 int c = skipToNonWhite ();
2137 switch (c)
2139 case ')': break;
2140 case ':': skipMemIntializerList (token);break; /* ctor-initializer */
2141 case '[': skipToMatch ("[]"); break;
2142 case '=': cppUngetc (c); end = TRUE; break;
2143 case '{': cppUngetc (c); end = TRUE; break;
2144 case '}': cppUngetc (c); end = TRUE; break;
2146 case '(':
2148 if (elementCount > 0)
2149 ++elementCount;
2150 skipToMatch ("()");
2151 break;
2154 case ';':
2156 if (parameters == 0 || elementCount < 2)
2158 cppUngetc (c);
2159 end = TRUE;
2161 else if (--parameters == 0)
2162 end = TRUE;
2163 break;
2166 default:
2168 if (isident1 (c))
2170 readIdentifier (token, c);
2171 if (isLanguage(Lang_d))
2173 switch (token->keyword)
2175 /* template constraint */
2176 case KEYWORD_IF:
2177 /* contracts */
2178 case KEYWORD_IN:
2179 case KEYWORD_OUT:
2180 case KEYWORD_BODY:
2181 token->keyword = KEYWORD_CONST;
2182 default:
2183 break;
2186 switch (token->keyword)
2188 case KEYWORD_ATTRIBUTE: skipParens (); break;
2189 case KEYWORD_THROW: skipParens (); break;
2190 case KEYWORD_CONST: break;
2191 case KEYWORD_TRY: break;
2192 case KEYWORD_VOLATILE: break;
2194 case KEYWORD_CATCH: case KEYWORD_CLASS:
2195 case KEYWORD_EXPLICIT: case KEYWORD_EXTERN:
2196 case KEYWORD_FRIEND: case KEYWORD_INLINE:
2197 case KEYWORD_MUTABLE: case KEYWORD_NAMESPACE:
2198 case KEYWORD_NEW: case KEYWORD_OPERATOR:
2199 case KEYWORD_OVERLOAD: case KEYWORD_PRIVATE:
2200 case KEYWORD_PROTECTED: case KEYWORD_PUBLIC:
2201 case KEYWORD_STATIC: case KEYWORD_TEMPLATE:
2202 case KEYWORD_TYPEDEF: case KEYWORD_TYPENAME:
2203 case KEYWORD_USING: case KEYWORD_VIRTUAL:
2204 /* Never allowed within parameter declarations.
2206 restart = TRUE;
2207 end = TRUE;
2208 break;
2210 default:
2211 if (isType (token, TOKEN_NONE))
2213 else if (info->isKnrParamList && info->parameterCount > 0)
2214 ++elementCount;
2215 else
2217 /* If we encounter any other identifier immediately
2218 * following an empty parameter list, this is almost
2219 * certainly one of those Microsoft macro "thingies"
2220 * that the automatic source code generation sticks
2221 * in. Terminate the current statement.
2223 restart = TRUE;
2224 end = TRUE;
2226 break;
2231 if (! end)
2233 c = skipToNonWhite ();
2234 if (c == EOF)
2235 end = TRUE;
2237 } while (! end);
2239 if (restart)
2240 restartStatement (st);
2241 else
2242 setToken (st, TOKEN_NONE);
2243 return (boolean) (c != EOF);
2246 static void skipJavaThrows (statementInfo *const st)
2248 tokenInfo *const token = activeToken (st);
2249 int c = skipToNonWhite ();
2251 if (isident1 (c))
2253 readIdentifier (token, c);
2254 if (token->keyword == KEYWORD_THROWS)
2258 c = skipToNonWhite ();
2259 if (isident1 (c))
2261 readIdentifier (token, c);
2262 c = skipToNonWhite ();
2264 } while (c == '.' || c == ',');
2267 cppUngetc (c);
2268 setToken (st, TOKEN_NONE);
2271 static void skipValaPostParens (statementInfo *const st)
2273 tokenInfo *const token = activeToken (st);
2274 int c = skipToNonWhite ();
2276 while (isident1 (c))
2278 readIdentifier (token, c);
2279 if (token->keyword == KEYWORD_ATTRIBUTE)
2281 /* parse contracts */
2282 skipParens ();
2283 c = skipToNonWhite ();
2285 else if (token->keyword == KEYWORD_THROWS)
2289 c = skipToNonWhite ();
2290 if (isident1 (c))
2292 readIdentifier (token, c);
2293 c = skipToNonWhite ();
2295 } while (c == '.' || c == ',');
2297 else
2298 break;
2300 cppUngetc (c);
2301 setToken (st, TOKEN_NONE);
2304 static void analyzePostParens (statementInfo *const st, parenInfo *const info)
2306 const unsigned long inputLineNumber = getInputLineNumber ();
2307 int c = skipToNonWhite ();
2309 cppUngetc (c);
2310 if (isOneOf (c, "{;,="))
2312 else if (isLanguage (Lang_java))
2313 skipJavaThrows (st);
2314 else if (isLanguage (Lang_vala))
2315 skipValaPostParens(st);
2316 else
2318 if (! skipPostArgumentStuff (st, info))
2320 verbose (
2321 "%s: confusing argument declarations beginning at line %lu\n",
2322 getInputFileName (), inputLineNumber);
2323 longjmp (Exception, (int) ExceptionFormattingError);
2328 static int parseParens (statementInfo *const st, parenInfo *const info)
2330 tokenInfo *const token = activeToken (st);
2331 unsigned int identifierCount = 0;
2332 unsigned int depth = 1;
2333 boolean firstChar = TRUE;
2334 int nextChar = '\0';
2336 info->parameterCount = 1;
2339 int c = skipToNonWhite ();
2341 switch (c)
2343 case '&':
2344 case '*':
2346 /* DEBUG_PRINT("parseParens, po++\n"); */
2347 info->isKnrParamList = FALSE;
2348 if (identifierCount == 0)
2349 info->isParamList = FALSE;
2350 initToken (token);
2351 break;
2353 case ':':
2355 info->isKnrParamList = FALSE;
2356 break;
2358 case '.':
2360 info->isNameCandidate = FALSE;
2361 info->isKnrParamList = FALSE;
2362 break;
2364 case ',':
2366 info->isNameCandidate = FALSE;
2367 if (info->isKnrParamList)
2369 ++info->parameterCount;
2370 identifierCount = 0;
2372 break;
2374 case '=':
2376 info->isKnrParamList = FALSE;
2377 info->isNameCandidate = FALSE;
2378 if (firstChar)
2380 info->isParamList = FALSE;
2381 skipMacro (st);
2382 depth = 0;
2384 break;
2386 case '[':
2388 info->isKnrParamList = FALSE;
2389 skipToMatch ("[]");
2390 break;
2392 case '<':
2394 info->isKnrParamList = FALSE;
2395 skipToMatch ("<>");
2396 break;
2398 case ')':
2400 if (firstChar)
2401 info->parameterCount = 0;
2402 --depth;
2403 break;
2405 case '(':
2407 info->isKnrParamList = FALSE;
2408 if (firstChar)
2410 info->isNameCandidate = FALSE;
2411 cppUngetc (c);
2412 skipMacro (st);
2413 depth = 0;
2415 else if (isType (token, TOKEN_PAREN_NAME))
2417 c = skipToNonWhite ();
2418 if (c == '*') /* check for function pointer */
2420 skipToMatch ("()");
2421 c = skipToNonWhite ();
2422 if (c == '(')
2423 skipToMatch ("()");
2425 else
2427 cppUngetc (c);
2428 cppUngetc ('(');
2429 info->nestedArgs = TRUE;
2432 else
2433 ++depth;
2434 break;
2437 default:
2439 if (isident1 (c))
2441 if (++identifierCount > 1)
2442 info->isKnrParamList = FALSE;
2443 readIdentifier (token, c);
2444 if (isType (token, TOKEN_NAME) && info->isNameCandidate)
2445 token->type = TOKEN_PAREN_NAME;
2446 else if (isType (token, TOKEN_KEYWORD))
2448 info->isKnrParamList = FALSE;
2449 info->isNameCandidate = FALSE;
2452 else
2454 info->isParamList = FALSE;
2455 info->isKnrParamList = FALSE;
2456 info->isNameCandidate = FALSE;
2457 info->invalidContents = TRUE;
2459 break;
2462 firstChar = FALSE;
2463 } while (! info->nestedArgs && depth > 0 &&
2464 (info->isKnrParamList || info->isNameCandidate));
2466 if (! info->nestedArgs) while (depth > 0)
2468 skipToMatch ("()");
2469 --depth;
2471 if (st->argEndPosition == 0)
2472 st->argEndPosition = mio_tell (File.mio);
2474 if (! info->isNameCandidate)
2475 initToken (token);
2477 return nextChar;
2480 static void initParenInfo (parenInfo *const info)
2482 info->isParamList = TRUE;
2483 info->isKnrParamList = TRUE;
2484 info->isNameCandidate = TRUE;
2485 info->invalidContents = FALSE;
2486 info->nestedArgs = FALSE;
2487 info->parameterCount = 0;
2490 static void analyzeParens (statementInfo *const st)
2492 tokenInfo *const prev = prevToken (st, 1);
2494 if (! isType (prev, TOKEN_NONE)) /* in case of ignored enclosing macros */
2496 tokenInfo *const token = activeToken (st);
2497 parenInfo info;
2498 int c;
2500 initParenInfo (&info);
2501 parseParens (st, &info);
2503 c = skipToNonWhite ();
2505 cppUngetc (c);
2506 if (info.invalidContents)
2508 reinitStatement (st, FALSE);
2510 else if (info.isNameCandidate && isType (token, TOKEN_PAREN_NAME) &&
2511 ! st->gotParenName &&
2512 (! info.isParamList || ! st->haveQualifyingName ||
2513 c == '(' ||
2514 (c == '=' && st->implementation != IMP_VIRTUAL) ||
2515 (st->declaration == DECL_NONE && isOneOf (c, ",;"))))
2517 token->type = TOKEN_NAME;
2518 processName (st);
2519 st->gotParenName = TRUE;
2520 if (isLanguage(Lang_d) && c == '(' && isType (prev, TOKEN_NAME))
2522 st->declaration = DECL_FUNCTION_TEMPLATE;
2523 copyToken (st->blockName, prev);
2526 else if (! st->gotArgs && info.isParamList)
2528 st->gotArgs = TRUE;
2529 setToken (st, TOKEN_ARGS);
2530 advanceToken (st);
2531 analyzePostParens (st, &info);
2533 else
2535 setToken (st, TOKEN_NONE);
2541 * Token parsing functions
2544 static void addContext (statementInfo *const st, const tokenInfo* const token)
2546 if (isType (token, TOKEN_NAME))
2548 if (vStringLength (st->context->name) > 0)
2550 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
2551 vStringCatS (st->context->name, "::");
2552 else if (isLanguage (Lang_java) ||
2553 isLanguage (Lang_d) || isLanguage (Lang_ferite) ||
2554 isLanguage (Lang_csharp) || isLanguage (Lang_vala))
2555 vStringCatS (st->context->name, ".");
2557 vStringCat (st->context->name, token->name);
2558 st->context->type = TOKEN_NAME;
2562 static boolean inheritingDeclaration (declType decl)
2564 return (boolean) (decl == DECL_CLASS ||
2565 decl == DECL_STRUCT ||
2566 decl == DECL_INTERFACE);
2569 static void processColon (statementInfo *const st)
2571 int c = skipToNonWhite ();
2572 const boolean doubleColon = (boolean) (c == ':');
2574 if (doubleColon)
2576 setToken (st, TOKEN_DOUBLE_COLON);
2577 st->haveQualifyingName = FALSE;
2579 else
2581 cppUngetc (c);
2582 if ((((isLanguage (Lang_cpp) &&
2583 (st->declaration == DECL_CLASS || st->declaration == DECL_STRUCT)) ||
2584 isLanguage (Lang_csharp) || isLanguage (Lang_vala)) &&
2585 inheritingDeclaration (st->declaration)) ||
2586 isLanguage (Lang_d))
2588 readParents (st, ':');
2590 else if (parentDecl (st) == DECL_STRUCT || parentDecl (st) == DECL_CLASS)
2592 c = skipToOneOf (",;");
2593 if (c == ',')
2594 setToken (st, TOKEN_COMMA);
2595 else if (c == ';')
2596 setToken (st, TOKEN_SEMICOLON);
2598 else
2600 const tokenInfo *const prev = prevToken (st, 1);
2601 const tokenInfo *const prev2 = prevToken (st, 2);
2602 if (prev->keyword == KEYWORD_DEFAULT ||
2603 prev2->keyword == KEYWORD_CASE ||
2604 st->parent != NULL)
2606 reinitStatement (st, FALSE);
2612 /* Skips over any initializing value which may follow an '=' character in a
2613 * variable definition.
2615 static int skipInitializer (statementInfo *const st)
2617 boolean done = FALSE;
2618 int c;
2620 while (! done)
2622 c = skipToNonWhite ();
2624 if (c == EOF)
2625 longjmp (Exception, (int) ExceptionFormattingError);
2626 else switch (c)
2628 case ',':
2629 case ';': done = TRUE; break;
2631 case '0':
2632 if (st->implementation == IMP_VIRTUAL)
2633 st->implementation = IMP_PURE_VIRTUAL;
2634 break;
2636 case '[': skipToMatch ("[]"); break;
2637 case '(': skipToMatch ("()"); break;
2638 case '{': skipToMatch ("{}"); break;
2640 case '}':
2641 if (insideEnumBody (st))
2642 done = TRUE;
2643 else if (! isBraceFormat ())
2645 verbose ("%s: unexpected closing brace at line %lu\n",
2646 getInputFileName (), getInputLineNumber ());
2647 longjmp (Exception, (int) ExceptionBraceFormattingError);
2649 break;
2651 default: break;
2654 return c;
2657 static void processInitializer (statementInfo *const st)
2659 const boolean inEnumBody = insideEnumBody (st);
2660 const int c = skipInitializer (st);
2662 if (c == ';')
2663 setToken (st, TOKEN_SEMICOLON);
2664 else if (c == ',')
2665 setToken (st, TOKEN_COMMA);
2666 else if (c == '}' && inEnumBody)
2668 cppUngetc (c);
2669 setToken (st, TOKEN_COMMA);
2671 if (st->scope == SCOPE_EXTERN)
2672 st->scope = SCOPE_GLOBAL;
2675 static void parseIdentifier (statementInfo *const st, const int c)
2677 tokenInfo *const token = activeToken (st);
2679 readIdentifier (token, c);
2680 if (! isType (token, TOKEN_NONE))
2681 processToken (token, st);
2684 static void parseGeneralToken (statementInfo *const st, const int c)
2686 const tokenInfo *const prev = prevToken (st, 1);
2688 if (isident1(c))
2690 parseIdentifier (st, c);
2691 if (isType (st->context, TOKEN_NAME) &&
2692 isType (activeToken (st), TOKEN_NAME) && isType (prev, TOKEN_NAME))
2694 initToken (st->context);
2697 else if (isExternCDecl (st, c))
2699 st->declaration = DECL_NOMANGLE;
2700 st->scope = SCOPE_GLOBAL;
2704 /* Reads characters from the pre-processor and assembles tokens, setting
2705 * the current statement state.
2707 static void nextToken (statementInfo *const st)
2709 int c;
2710 tokenInfo *token = activeToken (st);
2713 c = skipToNonWhite();
2714 switch (c)
2716 case EOF: longjmp (Exception, (int) ExceptionEOF); break;
2717 case '(': analyzeParens (st); token = activeToken (st); break;
2718 case '*': setToken (st, TOKEN_STAR); break;
2719 case ',': setToken (st, TOKEN_COMMA); break;
2720 case ':': processColon (st); break;
2721 case ';': setToken (st, TOKEN_SEMICOLON); break;
2722 case '<': skipToMatch ("<>"); break;
2723 case '=': processInitializer (st); break;
2724 case '[':
2725 /* Hack for Vala: [..] can be a function attribute.
2726 * Seems not to have bad side effects, but have to test it more. */
2727 if (!isLanguage (Lang_vala))
2728 setToken (st, TOKEN_ARRAY);
2729 skipToMatch ("[]");
2730 break;
2731 case '{': setToken (st, TOKEN_BRACE_OPEN); break;
2732 case '}': setToken (st, TOKEN_BRACE_CLOSE); break;
2733 default: parseGeneralToken (st, c); break;
2735 } while (isType (token, TOKEN_NONE));
2737 /* We want to know about non-keyword variable types */
2738 if (TOKEN_NONE == st->firstToken->type)
2740 if ((TOKEN_NAME == token->type) || isDataTypeKeyword(token))
2741 copyToken(st->firstToken, token);
2746 * Scanning support functions
2748 static unsigned int contextual_fake_count = 0;
2749 static statementInfo *CurrentStatement = NULL;
2751 static statementInfo *newStatement (statementInfo *const parent)
2753 statementInfo *const st = xMalloc (1, statementInfo);
2754 unsigned int i;
2756 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2757 st->token [i] = newToken ();
2759 st->context = newToken ();
2760 st->blockName = newToken ();
2761 st->parentClasses = vStringNew ();
2762 st->firstToken = newToken();
2764 initStatement (st, parent);
2765 CurrentStatement = st;
2767 return st;
2770 static void deleteStatement (void)
2772 statementInfo *const st = CurrentStatement;
2773 statementInfo *const parent = st->parent;
2774 unsigned int i;
2776 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2778 deleteToken(st->token[i]); st->token[i] = NULL;
2780 deleteToken(st->blockName); st->blockName = NULL;
2781 deleteToken(st->context); st->context = NULL;
2782 vStringDelete(st->parentClasses); st->parentClasses = NULL;
2783 deleteToken(st->firstToken);
2784 eFree (st);
2785 CurrentStatement = parent;
2788 static void deleteAllStatements (void)
2790 while (CurrentStatement != NULL)
2791 deleteStatement ();
2794 static boolean isStatementEnd (const statementInfo *const st)
2796 const tokenInfo *const token = activeToken (st);
2797 boolean isEnd;
2799 if (isType (token, TOKEN_SEMICOLON))
2800 isEnd = TRUE;
2801 else if (isType (token, TOKEN_BRACE_CLOSE))
2802 /* Java, D, C#, Vala do not require semicolons to end a block. Neither do
2803 * C++ namespaces. All other blocks require a semicolon to terminate them.
2805 isEnd = (boolean) (isLanguage (Lang_java) || isLanguage (Lang_d) ||
2806 isLanguage (Lang_csharp) || isLanguage (Lang_vala) ||
2807 ! isContextualStatement (st));
2808 else
2809 isEnd = FALSE;
2811 return isEnd;
2814 static void checkStatementEnd (statementInfo *const st)
2816 const tokenInfo *const token = activeToken (st);
2817 boolean comma = isType (token, TOKEN_COMMA);
2819 if (comma || isStatementEnd (st))
2821 reinitStatementWithToken (st, activeToken (st), comma);
2823 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>"); )
2824 cppEndStatement ();
2826 else
2828 cppBeginStatement ();
2829 advanceToken (st);
2833 static void nest (statementInfo *const st, const unsigned int nestLevel)
2835 switch (st->declaration)
2837 case DECL_CLASS:
2838 case DECL_ENUM:
2839 case DECL_INTERFACE:
2840 case DECL_NAMESPACE:
2841 case DECL_NOMANGLE:
2842 case DECL_STRUCT:
2843 case DECL_UNION:
2844 createTags (nestLevel, st);
2845 break;
2846 default:
2847 skipToMatch ("{}");
2848 break;
2850 advanceToken (st);
2851 setToken (st, TOKEN_BRACE_CLOSE);
2854 static void tagCheck (statementInfo *const st)
2856 const tokenInfo *const token = activeToken (st);
2857 const tokenInfo *const prev = prevToken (st, 1);
2858 const tokenInfo *const prev2 = prevToken (st, 2);
2860 switch (token->type)
2862 case TOKEN_NAME:
2864 if (insideEnumBody (st))
2865 qualifyEnumeratorTag (st, token);
2866 break;
2868 #if 0
2869 case TOKEN_PACKAGE:
2871 if (st->haveQualifyingName)
2872 makeTag (token, st, FALSE, TAG_PACKAGE);
2873 break;
2875 #endif
2876 case TOKEN_BRACE_OPEN:
2878 if (isType (prev, TOKEN_ARGS))
2880 if (st->declaration == DECL_FUNCTION_TEMPLATE)
2881 qualifyFunctionTag (st, st->blockName);
2882 else if (st->haveQualifyingName)
2884 st->declaration = DECL_FUNCTION;
2885 if (isType (prev2, TOKEN_NAME))
2886 copyToken (st->blockName, prev2);
2887 qualifyFunctionTag (st, prev2);
2890 else if (isContextualStatement (st))
2892 tokenInfo *name_token = (tokenInfo *)prev;
2893 boolean free_name_token = FALSE;
2895 if (isType (name_token, TOKEN_NAME))
2897 if (!isLanguage (Lang_vala))
2898 copyToken (st->blockName, name_token);
2899 else
2901 switch (st->declaration)
2903 case DECL_CLASS:
2904 case DECL_ENUM:
2905 case DECL_INTERFACE:
2906 case DECL_NAMESPACE:
2907 case DECL_STRUCT:
2908 copyToken (st->blockName, name_token);
2909 break;
2911 /* anything else can be a property */
2912 default:
2913 /* makeTag (prev, st, FALSE, TAG_PROPERTY); */
2914 /* FIXME: temporary hack to get properties shown */
2915 makeTag (prev, st, FALSE, TAG_FIELD);
2916 break;
2920 else if (isLanguage (Lang_csharp))
2921 makeTag (prev, st, FALSE, TAG_PROPERTY);
2922 else
2924 tokenInfo *contextual_token = (tokenInfo *)prev;
2925 if(isContextualKeyword (contextual_token))
2927 char buffer[64];
2929 name_token = newToken ();
2930 free_name_token = TRUE;
2931 copyToken (name_token, contextual_token);
2933 sprintf(buffer, "anon_%s_%d", name_token->name->buffer, contextual_fake_count++);
2934 vStringClear(name_token->name);
2935 vStringCatS(name_token->name, buffer);
2937 name_token->type = TOKEN_NAME;
2938 name_token->keyword = KEYWORD_NONE;
2940 advanceToken (st);
2941 contextual_token = activeToken (st);
2942 copyToken (contextual_token, token);
2943 copyToken ((tokenInfo *const)token, name_token);
2944 copyToken (st->blockName, name_token);
2945 copyToken (st->firstToken, name_token);
2948 qualifyBlockTag (st, name_token);
2949 if (free_name_token)
2950 deleteToken (name_token);
2952 break;
2954 case TOKEN_ARRAY:
2955 case TOKEN_SEMICOLON:
2956 case TOKEN_COMMA:
2958 if (insideEnumBody (st))
2960 else if (isType (prev, TOKEN_NAME))
2962 if (isContextualKeyword (prev2))
2963 makeTag (prev, st, TRUE, TAG_EXTERN_VAR);
2964 else
2965 qualifyVariableTag (st, prev);
2967 else if (isType (prev, TOKEN_ARGS) && isType (prev2, TOKEN_NAME))
2969 qualifyFunctionDeclTag (st, prev2);
2971 break;
2973 default:
2974 break;
2978 /* Parses the current file and decides whether to write out and tags that
2979 * are discovered.
2981 static void createTags (const unsigned int nestLevel,
2982 statementInfo *const parent)
2984 statementInfo *const st = newStatement (parent);
2986 DebugStatement ( if (nestLevel > 0) debugParseNest (TRUE, nestLevel); )
2987 while (TRUE)
2989 tokenInfo *token;
2991 nextToken (st);
2993 token = activeToken (st);
2995 if (isType (token, TOKEN_BRACE_CLOSE))
2997 if (nestLevel > 0)
2998 break;
2999 else
3001 verbose ("%s: unexpected closing brace at line %lu\n",
3002 getInputFileName (), getInputLineNumber ());
3003 longjmp (Exception, (int) ExceptionBraceFormattingError);
3006 else if (isType (token, TOKEN_DOUBLE_COLON))
3008 addContext (st, prevToken (st, 1));
3009 advanceToken (st);
3011 else
3013 tagCheck (st);/* this can add new token */
3014 if (isType (activeToken (st), TOKEN_BRACE_OPEN))
3015 nest (st, nestLevel + 1);
3016 checkStatementEnd (st);
3019 deleteStatement ();
3020 DebugStatement ( if (nestLevel > 0) debugParseNest (FALSE, nestLevel - 1); )
3023 static boolean findCTags (const unsigned int passCount)
3025 exception_t exception;
3026 boolean retry;
3028 contextual_fake_count = 0;
3030 Assert (passCount < 3);
3031 cppInit ((boolean) (passCount > 1), isLanguage (Lang_csharp));
3033 exception = (exception_t) setjmp (Exception);
3034 retry = FALSE;
3036 if (exception == ExceptionNone)
3038 createTags (0, NULL);
3040 else
3042 deleteAllStatements ();
3043 if (exception == ExceptionBraceFormattingError && passCount == 1)
3045 retry = TRUE;
3046 verbose ("%s: retrying file with fallback brace matching algorithm\n",
3047 getInputFileName ());
3050 cppTerminate ();
3051 return retry;
3054 static void buildKeywordHash (const langType language, unsigned int idx)
3056 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
3057 size_t i;
3058 for (i = 0 ; i < count ; ++i)
3060 const keywordDesc* const p = &KeywordTable [i];
3061 if (p->isValid [idx])
3062 addKeyword (p->name, language, (int) p->id);
3066 static void initializeCParser (const langType language)
3068 Lang_c = language;
3069 buildKeywordHash (language, 0);
3072 static void initializeCppParser (const langType language)
3074 Lang_cpp = language;
3075 buildKeywordHash (language, 1);
3078 static void initializeJavaParser (const langType language)
3080 Lang_java = language;
3081 buildKeywordHash (language, 3);
3084 static void initializeDParser (const langType language)
3086 Lang_d = language;
3087 buildKeywordHash (language, 6);
3090 static void initializeGLSLParser (const langType language)
3092 Lang_glsl = language;
3093 buildKeywordHash (language, 0); /* C keywords */
3096 static void initializeFeriteParser (const langType language)
3098 Lang_ferite = language;
3099 buildKeywordHash (language, 1); /* C++ keywords */
3102 static void initializeCsharpParser (const langType language)
3104 Lang_csharp = language;
3105 buildKeywordHash (language, 2);
3108 static void initializeValaParser (const langType language)
3110 Lang_vala = language;
3111 buildKeywordHash (language, 5);
3114 extern parserDefinition* CParser (void)
3116 static const char *const extensions [] = { "c", "pc", "sc", NULL };
3117 parserDefinition* def = parserNew ("C");
3118 def->kinds = CKinds;
3119 def->kindCount = KIND_COUNT (CKinds);
3120 def->extensions = extensions;
3121 def->parser2 = findCTags;
3122 def->initialize = initializeCParser;
3123 return def;
3126 extern parserDefinition* CppParser (void)
3128 static const char *const extensions [] = {
3129 "c++", "cc", "cp", "cpp", "cxx", "h", "h++", "hh", "hp", "hpp", "hxx",
3130 "i",
3131 #ifndef CASE_INSENSITIVE_FILENAMES
3132 "C", "H",
3133 #endif
3134 NULL
3136 parserDefinition* def = parserNew ("C++");
3137 def->kinds = CKinds;
3138 def->kindCount = KIND_COUNT (CKinds);
3139 def->extensions = extensions;
3140 def->parser2 = findCTags;
3141 def->initialize = initializeCppParser;
3142 return def;
3145 extern parserDefinition* JavaParser (void)
3147 static const char *const extensions [] = { "java", NULL };
3148 parserDefinition* def = parserNew ("Java");
3149 def->kinds = JavaKinds;
3150 def->kindCount = KIND_COUNT (JavaKinds);
3151 def->extensions = extensions;
3152 def->parser2 = findCTags;
3153 def->initialize = initializeJavaParser;
3154 return def;
3157 extern parserDefinition* DParser (void)
3159 static const char *const extensions [] = { "d", "di", NULL };
3160 parserDefinition* def = parserNew ("D");
3161 def->kinds = DKinds;
3162 def->kindCount = KIND_COUNT (DKinds);
3163 def->extensions = extensions;
3164 def->parser2 = findCTags;
3165 def->initialize = initializeDParser;
3166 return def;
3169 extern parserDefinition* GLSLParser (void)
3171 static const char *const extensions [] = { "glsl", "frag", "vert", NULL };
3172 parserDefinition* def = parserNew ("GLSL");
3173 def->kinds = CKinds;
3174 def->kindCount = KIND_COUNT (CKinds);
3175 def->extensions = extensions;
3176 def->parser2 = findCTags;
3177 def->initialize = initializeGLSLParser;
3178 return def;
3181 extern parserDefinition* FeriteParser (void)
3183 static const char *const extensions [] = { "fe", NULL };
3184 parserDefinition* def = parserNew ("Ferite");
3185 def->kinds = CKinds;
3186 def->kindCount = KIND_COUNT (CKinds);
3187 def->extensions = extensions;
3188 def->parser2 = findCTags;
3189 def->initialize = initializeFeriteParser;
3190 return def;
3193 extern parserDefinition* CsharpParser (void)
3195 static const char *const extensions [] = { "cs", NULL };
3196 parserDefinition* def = parserNew ("C#");
3197 def->kinds = CsharpKinds;
3198 def->kindCount = KIND_COUNT (CsharpKinds);
3199 def->extensions = extensions;
3200 def->parser2 = findCTags;
3201 def->initialize = initializeCsharpParser;
3202 return def;
3205 extern parserDefinition* ValaParser (void)
3207 static const char *const extensions [] = { "vala", NULL };
3208 parserDefinition* def = parserNew ("Vala");
3209 def->kinds = ValaKinds;
3210 def->kindCount = KIND_COUNT (ValaKinds);
3211 def->extensions = extensions;
3212 def->parser2 = findCTags;
3213 def->initialize = initializeValaParser;
3214 return def;
3216 /* vi:set tabstop=8 shiftwidth=4: */