Making "inline" behave like an attribute. Fixes #1
[arduino-ctags.git] / c.c
blobaf46dc4864c05b43e9e39a316cff313918996976
1 /*
2 * $Id: c.c 689 2008-12-13 21:17:36Z elliotth $
4 * Copyright (c) 1996-2003, Darren Hiebert
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
9 * This module contains functions for parsing and scanning C, C++ and Java
10 * source files.
14 * INCLUDE FILES
16 #include "general.h" /* must always come first */
18 #include <string.h>
19 #include <setjmp.h>
21 #include "debug.h"
22 #include "entry.h"
23 #include "get.h"
24 #include "keyword.h"
25 #include "options.h"
26 #include "parse.h"
27 #include "read.h"
28 #include "routines.h"
31 * MACROS
34 #define activeToken(st) ((st)->token [(int) (st)->tokenIndex])
35 #define parentDecl(st) ((st)->parent == NULL ? \
36 DECL_NONE : (st)->parent->declaration)
37 #define isType(token,t) (boolean) ((token)->type == (t))
38 #define insideEnumBody(st) ((st)->parent == NULL ? FALSE : \
39 (boolean) ((st)->parent->declaration == DECL_ENUM))
40 #define isExternCDecl(st,c) (boolean) ((c) == STRING_SYMBOL && \
41 ! (st)->haveQualifyingName && (st)->scope == SCOPE_EXTERN)
43 #define isOneOf(c,s) (boolean) (strchr ((s), (c)) != NULL)
45 #define isHighChar(c) ((c) != EOF && (unsigned char)(c) >= 0xc0)
48 * DATA DECLARATIONS
51 enum { NumTokens = 15 };
53 typedef enum eException {
54 ExceptionNone, ExceptionEOF, ExceptionFormattingError,
55 ExceptionBraceFormattingError
56 } exception_t;
58 /* Used to specify type of keyword.
60 typedef enum eKeywordId {
61 KEYWORD_NONE = -1,
62 KEYWORD_ATTRIBUTE, KEYWORD_ABSTRACT,
63 KEYWORD_BOOLEAN, KEYWORD_BYTE, KEYWORD_BAD_STATE, KEYWORD_BAD_TRANS,
64 KEYWORD_BIND, KEYWORD_BIND_VAR, KEYWORD_BIT,
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_FLOAT, KEYWORD_FOR, KEYWORD_FOREACH,
72 KEYWORD_FRIEND, KEYWORD_FUNCTION,
73 KEYWORD_GOTO,
74 KEYWORD_IF, KEYWORD_IMPLEMENTS, KEYWORD_IMPORT, KEYWORD_INLINE, KEYWORD_INT,
75 KEYWORD_INOUT, KEYWORD_INPUT, KEYWORD_INTEGER, KEYWORD_INTERFACE,
76 KEYWORD_INTERNAL,
77 KEYWORD_LOCAL, KEYWORD_LONG,
78 KEYWORD_M_BAD_STATE, KEYWORD_M_BAD_TRANS, KEYWORD_M_STATE, KEYWORD_M_TRANS,
79 KEYWORD_MUTABLE,
80 KEYWORD_NAMESPACE, KEYWORD_NEW, KEYWORD_NEWCOV, KEYWORD_NATIVE,
81 KEYWORD_OPERATOR, KEYWORD_OUTPUT, KEYWORD_OVERLOAD, KEYWORD_OVERRIDE,
82 KEYWORD_PACKED, KEYWORD_PORT, KEYWORD_PACKAGE, KEYWORD_PRIVATE,
83 KEYWORD_PROGRAM, KEYWORD_PROTECTED, KEYWORD_PUBLIC,
84 KEYWORD_REGISTER, KEYWORD_RETURN,
85 KEYWORD_SHADOW, KEYWORD_STATE,
86 KEYWORD_SHORT, KEYWORD_SIGNED, KEYWORD_STATIC, KEYWORD_STRING,
87 KEYWORD_STRUCT, KEYWORD_SWITCH, KEYWORD_SYNCHRONIZED,
88 KEYWORD_TASK, KEYWORD_TEMPLATE, KEYWORD_THIS, KEYWORD_THROW,
89 KEYWORD_THROWS, KEYWORD_TRANSIENT, KEYWORD_TRANS, KEYWORD_TRANSITION,
90 KEYWORD_TRY, KEYWORD_TYPEDEF, KEYWORD_TYPENAME,
91 KEYWORD_UINT, KEYWORD_ULONG, KEYWORD_UNION, KEYWORD_UNSIGNED, KEYWORD_USHORT,
92 KEYWORD_USING,
93 KEYWORD_VIRTUAL, KEYWORD_VOID, KEYWORD_VOLATILE,
94 KEYWORD_WCHAR_T, KEYWORD_WHILE
95 } keywordId;
97 /* Used to determine whether keyword is valid for the current language and
98 * what its ID is.
100 typedef struct sKeywordDesc {
101 const char *name;
102 keywordId id;
103 short isValid [5]; /* indicates languages for which kw is valid */
104 } keywordDesc;
106 /* Used for reporting the type of object parsed by nextToken ().
108 typedef enum eTokenType {
109 TOKEN_NONE, /* none */
110 TOKEN_ARGS, /* a parenthetical pair and its contents */
111 TOKEN_BRACE_CLOSE,
112 TOKEN_BRACE_OPEN,
113 TOKEN_COLON, /* the colon character */
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_AMPERSAND, /* ampersand & detection */
124 TOKEN_COUNT
125 } tokenType;
127 /* This describes the scoping of the current statement.
129 typedef enum eTagScope {
130 SCOPE_GLOBAL, /* no storage class specified */
131 SCOPE_STATIC, /* static storage class */
132 SCOPE_EXTERN, /* external storage class */
133 SCOPE_FRIEND, /* declares access only */
134 SCOPE_TYPEDEF, /* scoping depends upon context */
135 SCOPE_COUNT
136 } tagScope;
138 typedef enum eDeclaration {
139 DECL_NONE,
140 DECL_BASE, /* base type (default) */
141 DECL_CLASS,
142 DECL_ENUM,
143 DECL_EVENT,
144 DECL_FUNCTION,
145 DECL_IGNORE, /* non-taggable "declaration" */
146 DECL_INTERFACE,
147 DECL_NAMESPACE,
148 DECL_NOMANGLE, /* C++ name demangling block */
149 DECL_PACKAGE,
150 DECL_PROGRAM, /* Vera program */
151 DECL_STRUCT,
152 DECL_TASK, /* Vera task */
153 DECL_UNION,
154 DECL_COUNT
155 } declType;
157 typedef enum eVisibilityType {
158 ACCESS_UNDEFINED,
159 ACCESS_LOCAL,
160 ACCESS_PRIVATE,
161 ACCESS_PROTECTED,
162 ACCESS_PUBLIC,
163 ACCESS_DEFAULT, /* Java-specific */
164 ACCESS_COUNT
165 } accessType;
167 /* Information about the parent class of a member (if any).
169 typedef struct sMemberInfo {
170 accessType access; /* access of current statement */
171 accessType accessDefault; /* access default for current statement */
172 } memberInfo;
174 typedef struct sTokenInfo {
175 tokenType type;
176 keywordId keyword;
177 vString* name; /* the name of the token */
178 unsigned long lineNumber; /* line number of tag */
179 fpos_t filePosition; /* file position of line containing name */
180 } tokenInfo;
182 typedef enum eImplementation {
183 IMP_DEFAULT,
184 IMP_ABSTRACT,
185 IMP_VIRTUAL,
186 IMP_PURE_VIRTUAL,
187 IMP_COUNT
188 } impType;
190 /* Describes the statement currently undergoing analysis.
192 typedef struct sStatementInfo {
193 tagScope scope;
194 declType declaration; /* specifier associated with TOKEN_SPEC */
195 boolean gotName; /* was a name parsed yet? */
196 boolean haveQualifyingName; /* do we have a name we are considering? */
197 boolean gotParenName; /* was a name inside parentheses parsed yet? */
198 boolean gotArgs; /* was a list of parameters parsed yet? */
199 boolean isPointer; /* is 'name' a pointer? */
200 boolean inFunction; /* are we inside of a function? */
201 boolean assignment; /* have we handled an '='? */
202 boolean notVariable; /* has a variable declaration been disqualified ? */
203 impType implementation; /* abstract or concrete implementation? */
204 unsigned int tokenIndex; /* currently active token */
205 tokenInfo* token [(int) NumTokens];
206 tokenInfo* context; /* accumulated scope of current statement */
207 tokenInfo* blockName; /* name of current block */
208 memberInfo member; /* information regarding parent class/struct */
209 vString* parentClasses; /* parent classes */
210 struct sStatementInfo *parent; /* statement we are nested within */
211 } statementInfo;
213 /* Describes the type of tag being generated.
215 typedef enum eTagType {
216 TAG_UNDEFINED,
217 TAG_CLASS, /* class name */
218 TAG_ENUM, /* enumeration name */
219 TAG_ENUMERATOR, /* enumerator (enumeration value) */
220 TAG_EVENT, /* event */
221 TAG_FIELD, /* field (Java) */
222 TAG_FUNCTION, /* function definition */
223 TAG_INTERFACE, /* interface declaration */
224 TAG_LOCAL, /* local variable definition */
225 TAG_MEMBER, /* structure, class or interface member */
226 TAG_METHOD, /* method declaration */
227 TAG_NAMESPACE, /* namespace name */
228 TAG_PACKAGE, /* package name */
229 TAG_PROGRAM, /* program name */
230 TAG_PROPERTY, /* property name */
231 TAG_PROTOTYPE, /* function prototype or declaration */
232 TAG_STRUCT, /* structure name */
233 TAG_TASK, /* task name */
234 TAG_TYPEDEF, /* typedef name */
235 TAG_UNION, /* union name */
236 TAG_VARIABLE, /* variable definition */
237 TAG_EXTERN_VAR, /* external variable declaration */
238 TAG_COUNT /* must be last */
239 } tagType;
241 typedef struct sParenInfo {
242 boolean isPointer;
243 boolean isParamList;
244 boolean isKnrParamList;
245 boolean isNameCandidate;
246 boolean invalidContents;
247 boolean nestedArgs;
248 unsigned int parameterCount;
249 } parenInfo;
252 * DATA DEFINITIONS
255 static jmp_buf Exception;
257 static langType Lang_c;
258 static langType Lang_cpp;
259 static langType Lang_csharp;
260 static langType Lang_java;
261 static langType Lang_vera;
262 static vString *Signature;
263 static boolean CollectingSignature;
264 static vString *ReturnType;
266 /* Number used to uniquely identify anonymous structs and unions. */
267 static int AnonymousID = 0;
269 /* Used to index into the CKinds table. */
270 typedef enum {
271 CK_UNDEFINED = -1,
272 CK_CLASS, CK_DEFINE, CK_ENUMERATOR, CK_FUNCTION,
273 CK_ENUMERATION, CK_LOCAL, CK_MEMBER, CK_NAMESPACE, CK_PROTOTYPE,
274 CK_STRUCT, CK_TYPEDEF, CK_UNION, CK_VARIABLE,
275 CK_EXTERN_VARIABLE
276 } cKind;
278 static kindOption CKinds [] = {
279 { TRUE, 'c', "class", "classes"},
280 { TRUE, 'd', "macro", "macro definitions"},
281 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
282 { TRUE, 'f', "function", "function definitions"},
283 { TRUE, 'g', "enum", "enumeration names"},
284 { FALSE, 'l', "local", "local variables"},
285 { TRUE, 'm', "member", "class, struct, and union members"},
286 { TRUE, 'n', "namespace", "namespaces"},
287 { FALSE, 'p', "prototype", "function prototypes"},
288 { TRUE, 's', "struct", "structure names"},
289 { TRUE, 't', "typedef", "typedefs"},
290 { TRUE, 'u', "union", "union names"},
291 { TRUE, 'v', "variable", "variable definitions"},
292 { FALSE, 'x', "externvar", "external and forward variable declarations"},
295 typedef enum {
296 CSK_UNDEFINED = -1,
297 CSK_CLASS, CSK_DEFINE, CSK_ENUMERATOR, CSK_EVENT, CSK_FIELD,
298 CSK_ENUMERATION, CSK_INTERFACE, CSK_LOCAL, CSK_METHOD,
299 CSK_NAMESPACE, CSK_PROPERTY, CSK_STRUCT, CSK_TYPEDEF
300 } csharpKind;
302 static kindOption CsharpKinds [] = {
303 { TRUE, 'c', "class", "classes"},
304 { TRUE, 'd', "macro", "macro definitions"},
305 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
306 { TRUE, 'E', "event", "events"},
307 { TRUE, 'f', "field", "fields"},
308 { TRUE, 'g', "enum", "enumeration names"},
309 { TRUE, 'i', "interface", "interfaces"},
310 { FALSE, 'l', "local", "local variables"},
311 { TRUE, 'm', "method", "methods"},
312 { TRUE, 'n', "namespace", "namespaces"},
313 { TRUE, 'p', "property", "properties"},
314 { TRUE, 's', "struct", "structure names"},
315 { TRUE, 't', "typedef", "typedefs"},
318 /* Used to index into the JavaKinds table. */
319 typedef enum {
320 JK_UNDEFINED = -1,
321 JK_CLASS, JK_ENUM_CONSTANT, JK_FIELD, JK_ENUM, JK_INTERFACE,
322 JK_LOCAL, JK_METHOD, JK_PACKAGE, JK_ACCESS, JK_CLASS_PREFIX
323 } javaKind;
325 static kindOption JavaKinds [] = {
326 { TRUE, 'c', "class", "classes"},
327 { TRUE, 'e', "enum constant", "enum constants"},
328 { TRUE, 'f', "field", "fields"},
329 { TRUE, 'g', "enum", "enum types"},
330 { TRUE, 'i', "interface", "interfaces"},
331 { FALSE, 'l', "local", "local variables"},
332 { TRUE, 'm', "method", "methods"},
333 { TRUE, 'p', "package", "packages"},
336 /* Used to index into the VeraKinds table. */
337 typedef enum {
338 VK_UNDEFINED = -1,
339 VK_CLASS, VK_DEFINE, VK_ENUMERATOR, VK_FUNCTION,
340 VK_ENUMERATION, VK_LOCAL, VK_MEMBER, VK_PROGRAM, VK_PROTOTYPE,
341 VK_TASK, VK_TYPEDEF, VK_VARIABLE,
342 VK_EXTERN_VARIABLE
343 } veraKind;
345 static kindOption VeraKinds [] = {
346 { TRUE, 'c', "class", "classes"},
347 { TRUE, 'd', "macro", "macro definitions"},
348 { TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
349 { TRUE, 'f', "function", "function definitions"},
350 { TRUE, 'g', "enum", "enumeration names"},
351 { FALSE, 'l', "local", "local variables"},
352 { TRUE, 'm', "member", "class, struct, and union members"},
353 { TRUE, 'p', "program", "programs"},
354 { FALSE, 'P', "prototype", "function prototypes"},
355 { TRUE, 't', "task", "tasks"},
356 { TRUE, 'T', "typedef", "typedefs"},
357 { TRUE, 'v', "variable", "variable definitions"},
358 { FALSE, 'x', "externvar", "external variable declarations"}
361 static const keywordDesc KeywordTable [] = {
362 /* C++ */
363 /* ANSI C | C# Java */
364 /* | | | | Vera */
365 /* keyword keyword ID | | | | | */
366 { "__attribute__", KEYWORD_ATTRIBUTE, { 1, 1, 1, 0, 0 } },
367 { "abstract", KEYWORD_ABSTRACT, { 0, 0, 1, 1, 0 } },
368 { "bad_state", KEYWORD_BAD_STATE, { 0, 0, 0, 0, 1 } },
369 { "bad_trans", KEYWORD_BAD_TRANS, { 0, 0, 0, 0, 1 } },
370 { "bind", KEYWORD_BIND, { 0, 0, 0, 0, 1 } },
371 { "bind_var", KEYWORD_BIND_VAR, { 0, 0, 0, 0, 1 } },
372 { "bit", KEYWORD_BIT, { 0, 0, 0, 0, 1 } },
373 { "boolean", KEYWORD_BOOLEAN, { 0, 0, 0, 1, 0 } },
374 { "byte", KEYWORD_BYTE, { 0, 0, 0, 1, 0 } },
375 { "case", KEYWORD_CASE, { 1, 1, 1, 1, 0 } },
376 { "catch", KEYWORD_CATCH, { 0, 1, 1, 0, 0 } },
377 { "char", KEYWORD_CHAR, { 1, 1, 1, 1, 0 } },
378 { "class", KEYWORD_CLASS, { 0, 1, 1, 1, 1 } },
379 { "const", KEYWORD_CONST, { 1, 1, 1, 1, 0 } },
380 { "constraint", KEYWORD_CONSTRAINT, { 0, 0, 0, 0, 1 } },
381 { "coverage_block", KEYWORD_COVERAGE_BLOCK, { 0, 0, 0, 0, 1 } },
382 { "coverage_def", KEYWORD_COVERAGE_DEF, { 0, 0, 0, 0, 1 } },
383 { "do", KEYWORD_DO, { 1, 1, 1, 1, 0 } },
384 { "default", KEYWORD_DEFAULT, { 1, 1, 1, 1, 0 } },
385 { "delegate", KEYWORD_DELEGATE, { 0, 0, 1, 0, 0 } },
386 { "delete", KEYWORD_DELETE, { 0, 1, 0, 0, 0 } },
387 { "double", KEYWORD_DOUBLE, { 1, 1, 1, 1, 0 } },
388 { "else", KEYWORD_ELSE, { 1, 1, 1, 1, 0 } },
389 { "enum", KEYWORD_ENUM, { 1, 1, 1, 1, 1 } },
390 { "event", KEYWORD_EVENT, { 0, 0, 1, 0, 1 } },
391 { "explicit", KEYWORD_EXPLICIT, { 0, 1, 1, 0, 0 } },
392 { "extends", KEYWORD_EXTENDS, { 0, 0, 0, 1, 1 } },
393 { "extern", KEYWORD_EXTERN, { 1, 1, 1, 0, 1 } },
394 { "final", KEYWORD_FINAL, { 0, 0, 0, 1, 0 } },
395 { "float", KEYWORD_FLOAT, { 1, 1, 1, 1, 0 } },
396 { "for", KEYWORD_FOR, { 1, 1, 1, 1, 0 } },
397 { "foreach", KEYWORD_FOREACH, { 0, 0, 1, 0, 0 } },
398 { "friend", KEYWORD_FRIEND, { 0, 1, 0, 0, 0 } },
399 { "function", KEYWORD_FUNCTION, { 0, 0, 0, 0, 1 } },
400 { "goto", KEYWORD_GOTO, { 1, 1, 1, 1, 0 } },
401 { "if", KEYWORD_IF, { 1, 1, 1, 1, 0 } },
402 { "implements", KEYWORD_IMPLEMENTS, { 0, 0, 0, 1, 0 } },
403 { "import", KEYWORD_IMPORT, { 0, 0, 0, 1, 0 } },
404 { "inline", KEYWORD_INLINE, { 0, 1, 0, 0, 0 } },
405 { "inout", KEYWORD_INOUT, { 0, 0, 0, 0, 1 } },
406 { "input", KEYWORD_INPUT, { 0, 0, 0, 0, 1 } },
407 { "int", KEYWORD_INT, { 1, 1, 1, 1, 0 } },
408 { "integer", KEYWORD_INTEGER, { 0, 0, 0, 0, 1 } },
409 { "interface", KEYWORD_INTERFACE, { 0, 0, 1, 1, 1 } },
410 { "internal", KEYWORD_INTERNAL, { 0, 0, 1, 0, 0 } },
411 { "local", KEYWORD_LOCAL, { 0, 0, 0, 0, 1 } },
412 { "long", KEYWORD_LONG, { 1, 1, 1, 1, 0 } },
413 { "m_bad_state", KEYWORD_M_BAD_STATE, { 0, 0, 0, 0, 1 } },
414 { "m_bad_trans", KEYWORD_M_BAD_TRANS, { 0, 0, 0, 0, 1 } },
415 { "m_state", KEYWORD_M_STATE, { 0, 0, 0, 0, 1 } },
416 { "m_trans", KEYWORD_M_TRANS, { 0, 0, 0, 0, 1 } },
417 { "mutable", KEYWORD_MUTABLE, { 0, 1, 0, 0, 0 } },
418 { "namespace", KEYWORD_NAMESPACE, { 0, 1, 1, 0, 0 } },
419 { "native", KEYWORD_NATIVE, { 0, 0, 0, 1, 0 } },
420 { "new", KEYWORD_NEW, { 0, 1, 1, 1, 0 } },
421 { "newcov", KEYWORD_NEWCOV, { 0, 0, 0, 0, 1 } },
422 { "operator", KEYWORD_OPERATOR, { 0, 1, 1, 0, 0 } },
423 { "output", KEYWORD_OUTPUT, { 0, 0, 0, 0, 1 } },
424 { "overload", KEYWORD_OVERLOAD, { 0, 1, 0, 0, 0 } },
425 { "override", KEYWORD_OVERRIDE, { 0, 0, 1, 0, 0 } },
426 { "package", KEYWORD_PACKAGE, { 0, 0, 0, 1, 0 } },
427 { "packed", KEYWORD_PACKED, { 0, 0, 0, 0, 1 } },
428 { "port", KEYWORD_PORT, { 0, 0, 0, 0, 1 } },
429 { "private", KEYWORD_PRIVATE, { 0, 1, 1, 1, 0 } },
430 { "program", KEYWORD_PROGRAM, { 0, 0, 0, 0, 1 } },
431 { "protected", KEYWORD_PROTECTED, { 0, 1, 1, 1, 1 } },
432 { "public", KEYWORD_PUBLIC, { 0, 1, 1, 1, 1 } },
433 { "register", KEYWORD_REGISTER, { 1, 1, 0, 0, 0 } },
434 { "return", KEYWORD_RETURN, { 1, 1, 1, 1, 0 } },
435 { "shadow", KEYWORD_SHADOW, { 0, 0, 0, 0, 1 } },
436 { "short", KEYWORD_SHORT, { 1, 1, 1, 1, 0 } },
437 { "signed", KEYWORD_SIGNED, { 1, 1, 0, 0, 0 } },
438 { "state", KEYWORD_STATE, { 0, 0, 0, 0, 1 } },
439 { "static", KEYWORD_STATIC, { 1, 1, 1, 1, 1 } },
440 { "string", KEYWORD_STRING, { 0, 0, 1, 0, 1 } },
441 { "struct", KEYWORD_STRUCT, { 1, 1, 1, 0, 0 } },
442 { "switch", KEYWORD_SWITCH, { 1, 1, 1, 1, 0 } },
443 { "synchronized", KEYWORD_SYNCHRONIZED, { 0, 0, 0, 1, 0 } },
444 { "task", KEYWORD_TASK, { 0, 0, 0, 0, 1 } },
445 { "template", KEYWORD_TEMPLATE, { 0, 1, 0, 0, 0 } },
446 { "this", KEYWORD_THIS, { 0, 1, 1, 1, 0 } },
447 { "throw", KEYWORD_THROW, { 0, 1, 1, 1, 0 } },
448 { "throws", KEYWORD_THROWS, { 0, 0, 0, 1, 0 } },
449 { "trans", KEYWORD_TRANS, { 0, 0, 0, 0, 1 } },
450 { "transition", KEYWORD_TRANSITION, { 0, 0, 0, 0, 1 } },
451 { "transient", KEYWORD_TRANSIENT, { 0, 0, 0, 1, 0 } },
452 { "try", KEYWORD_TRY, { 0, 1, 1, 0, 0 } },
453 { "typedef", KEYWORD_TYPEDEF, { 1, 1, 1, 0, 1 } },
454 { "typename", KEYWORD_TYPENAME, { 0, 1, 0, 0, 0 } },
455 { "uint", KEYWORD_UINT, { 0, 0, 1, 0, 0 } },
456 { "ulong", KEYWORD_ULONG, { 0, 0, 1, 0, 0 } },
457 { "union", KEYWORD_UNION, { 1, 1, 0, 0, 0 } },
458 { "unsigned", KEYWORD_UNSIGNED, { 1, 1, 1, 0, 0 } },
459 { "ushort", KEYWORD_USHORT, { 0, 0, 1, 0, 0 } },
460 { "using", KEYWORD_USING, { 0, 1, 1, 0, 0 } },
461 { "virtual", KEYWORD_VIRTUAL, { 0, 1, 1, 0, 1 } },
462 { "void", KEYWORD_VOID, { 1, 1, 1, 1, 1 } },
463 { "volatile", KEYWORD_VOLATILE, { 1, 1, 1, 1, 0 } },
464 { "wchar_t", KEYWORD_WCHAR_T, { 1, 1, 1, 0, 0 } },
465 { "while", KEYWORD_WHILE, { 1, 1, 1, 1, 0 } }
469 * FUNCTION PROTOTYPES
471 static void createTags (const unsigned int nestLevel, statementInfo *const parent);
474 * FUNCTION DEFINITIONS
477 extern boolean includingDefineTags (void)
479 return CKinds [CK_DEFINE].enabled;
483 * Token management
486 static void initToken (tokenInfo* const token)
488 token->type = TOKEN_NONE;
489 token->keyword = KEYWORD_NONE;
490 token->lineNumber = getSourceLineNumber ();
491 token->filePosition = getInputFilePosition ();
492 vStringClear (token->name);
495 static void advanceToken (statementInfo* const st)
497 if (st->tokenIndex >= (unsigned int) NumTokens - 1)
498 st->tokenIndex = 0;
499 else
500 ++st->tokenIndex;
501 initToken (st->token [st->tokenIndex]);
504 static tokenInfo *prevToken (const statementInfo *const st, unsigned int n)
506 unsigned int tokenIndex;
507 unsigned int num = (unsigned int) NumTokens;
508 Assert (n < num);
509 tokenIndex = (st->tokenIndex + num - n) % num;
510 return st->token [tokenIndex];
513 static void setToken (statementInfo *const st, const tokenType type)
515 tokenInfo *token;
516 token = activeToken (st);
517 initToken (token);
518 token->type = type;
521 static void retardToken (statementInfo *const st)
523 if (st->tokenIndex == 0)
524 st->tokenIndex = (unsigned int) NumTokens - 1;
525 else
526 --st->tokenIndex;
527 setToken (st, TOKEN_NONE);
530 static tokenInfo *newToken (void)
532 tokenInfo *const token = xMalloc (1, tokenInfo);
533 token->name = vStringNew ();
534 initToken (token);
535 return token;
538 static void deleteToken (tokenInfo *const token)
540 if (token != NULL)
542 vStringDelete (token->name);
543 eFree (token);
547 static const char *accessString (const accessType access)
549 static const char *const names [] = {
550 "?", "local", "private", "protected", "public", "default"
552 Assert (sizeof (names) / sizeof (names [0]) == ACCESS_COUNT);
553 Assert ((int) access < ACCESS_COUNT);
554 return names [(int) access];
557 static const char *implementationString (const impType imp)
559 static const char *const names [] ={
560 "?", "abstract", "virtual", "pure virtual"
562 Assert (sizeof (names) / sizeof (names [0]) == IMP_COUNT);
563 Assert ((int) imp < IMP_COUNT);
564 return names [(int) imp];
568 * Debugging functions
570 #define DEBUG
571 #ifdef DEBUG
573 #define boolString(c) ((c) ? "TRUE" : "FALSE")
575 static const char *tokenString (const tokenType type)
577 static const char *const names [] = {
578 "none", "args", "}", "{", "colon", "comma", "double colon", "keyword",
579 "name", "package", "paren-name", "semicolon", "specifier", "star", "ampersand"
581 Assert (sizeof (names) / sizeof (names [0]) == TOKEN_COUNT);
582 Assert ((int) type < TOKEN_COUNT);
583 return names [(int) type];
586 static const char *scopeString (const tagScope scope)
588 static const char *const names [] = {
589 "global", "static", "extern", "friend", "typedef"
591 Assert (sizeof (names) / sizeof (names [0]) == SCOPE_COUNT);
592 Assert ((int) scope < SCOPE_COUNT);
593 return names [(int) scope];
596 static const char *declString (const declType declaration)
598 static const char *const names [] = {
599 "?", "base", "class", "enum", "event", "function", "ignore",
600 "interface", "namespace", "no mangle", "package", "program",
601 "struct", "task", "union",
603 Assert (sizeof (names) / sizeof (names [0]) == DECL_COUNT);
604 Assert ((int) declaration < DECL_COUNT);
605 return names [(int) declaration];
608 static const char *keywordString (const keywordId keyword)
610 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
611 const char *name = "none";
612 size_t i;
613 for (i = 0 ; i < count ; ++i)
615 const keywordDesc *p = &KeywordTable [i];
616 if (p->id == keyword)
618 name = p->name;
619 break;
622 return name;
625 static void __unused__ pt (tokenInfo *const token)
627 if (isType (token, TOKEN_NAME))
628 printf ("type: %-12s: %-13s line: %lu\n",
629 tokenString (token->type), vStringValue (token->name),
630 token->lineNumber);
631 else if (isType (token, TOKEN_KEYWORD))
632 printf ("type: %-12s: %-13s line: %lu\n",
633 tokenString (token->type), keywordString (token->keyword),
634 token->lineNumber);
635 else
636 printf ("type: %-12s line: %lu\n",
637 tokenString (token->type), token->lineNumber);
640 static void __unused__ ps (statementInfo *const st)
642 unsigned int i;
643 printf ("scope: %s decl: %s gotName: %s gotParenName: %s isPointer: %s\n",
644 scopeString (st->scope), declString (st->declaration),
645 boolString (st->gotName), boolString (st->gotParenName), boolString (st->isPointer));
646 printf ("haveQualifyingName: %s\n", boolString (st->haveQualifyingName));
647 printf ("access: %s default: %s\n", accessString (st->member.access),
648 accessString (st->member.accessDefault));
649 printf ("active token : ");
650 pt (activeToken (st));
651 for (i = 1 ; i < (unsigned int) NumTokens ; ++i)
653 printf ("prev %u : ", i);
654 pt (prevToken (st, i));
656 printf ("context: ");
657 pt (st->context);
660 #endif
663 * Statement management
666 static boolean isContextualKeyword (const tokenInfo *const token)
668 boolean result;
669 switch (token->keyword)
671 case KEYWORD_CLASS:
672 case KEYWORD_ENUM:
673 case KEYWORD_INTERFACE:
674 case KEYWORD_NAMESPACE:
675 case KEYWORD_STRUCT:
676 case KEYWORD_UNION:
677 result = TRUE;
678 break;
680 default: result = FALSE; break;
682 return result;
685 static boolean isContextualStatement (const statementInfo *const st)
687 boolean result = FALSE;
688 if (st != NULL) switch (st->declaration)
690 case DECL_CLASS:
691 case DECL_ENUM:
692 case DECL_INTERFACE:
693 case DECL_NAMESPACE:
694 case DECL_STRUCT:
695 case DECL_UNION:
696 result = TRUE;
697 break;
699 default: result = FALSE; break;
701 return result;
704 static boolean isMember (const statementInfo *const st)
706 boolean result;
707 if (isType (st->context, TOKEN_NAME))
708 result = TRUE;
709 else
710 result = (boolean)
711 (st->parent != NULL && isContextualStatement (st->parent));
712 return result;
715 static void initMemberInfo (statementInfo *const st)
717 accessType accessDefault = ACCESS_UNDEFINED;
719 if (st->parent != NULL) switch (st->parent->declaration)
721 case DECL_ENUM:
722 accessDefault = (isLanguage (Lang_java) ? ACCESS_PUBLIC : ACCESS_UNDEFINED);
723 break;
724 case DECL_NAMESPACE:
725 accessDefault = ACCESS_UNDEFINED;
726 break;
728 case DECL_CLASS:
729 if (isLanguage (Lang_java))
730 accessDefault = ACCESS_DEFAULT;
731 else
732 accessDefault = ACCESS_PRIVATE;
733 break;
735 case DECL_INTERFACE:
736 case DECL_STRUCT:
737 case DECL_UNION:
738 accessDefault = ACCESS_PUBLIC;
739 break;
741 default: break;
743 st->member.accessDefault = accessDefault;
744 st->member.access = accessDefault;
747 static void reinitStatement (statementInfo *const st, const boolean partial)
749 unsigned int i;
751 if (! partial)
753 st->scope = SCOPE_GLOBAL;
754 if (isContextualStatement (st->parent))
755 st->declaration = DECL_BASE;
756 else
757 st->declaration = DECL_NONE;
759 st->gotParenName = FALSE;
760 st->isPointer = FALSE;
761 st->inFunction = FALSE;
762 st->assignment = FALSE;
763 st->notVariable = FALSE;
764 st->implementation = IMP_DEFAULT;
765 st->gotArgs = FALSE;
766 st->gotName = FALSE;
767 st->haveQualifyingName = FALSE;
768 st->tokenIndex = 0;
770 if (st->parent != NULL)
771 st->inFunction = st->parent->inFunction;
773 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
774 initToken (st->token [i]);
776 initToken (st->context);
778 /* Keep the block name, so that a variable following after a comma will
779 * still have the structure name.
781 if (! partial)
782 initToken (st->blockName);
784 vStringClear (st->parentClasses);
786 /* Init member info.
788 if (! partial)
789 st->member.access = st->member.accessDefault;
792 static void initStatement (statementInfo *const st, statementInfo *const parent)
794 st->parent = parent;
795 initMemberInfo (st);
796 reinitStatement (st, FALSE);
800 * Tag generation functions
802 static cKind cTagKind (const tagType type)
804 cKind result = CK_UNDEFINED;
805 switch (type)
807 case TAG_CLASS: result = CK_CLASS; break;
808 case TAG_ENUM: result = CK_ENUMERATION; break;
809 case TAG_ENUMERATOR: result = CK_ENUMERATOR; break;
810 case TAG_FUNCTION: result = CK_FUNCTION; break;
811 case TAG_LOCAL: result = CK_LOCAL; break;
812 case TAG_MEMBER: result = CK_MEMBER; break;
813 case TAG_NAMESPACE: result = CK_NAMESPACE; break;
814 case TAG_PROTOTYPE: result = CK_PROTOTYPE; break;
815 case TAG_STRUCT: result = CK_STRUCT; break;
816 case TAG_TYPEDEF: result = CK_TYPEDEF; break;
817 case TAG_UNION: result = CK_UNION; break;
818 case TAG_VARIABLE: result = CK_VARIABLE; break;
819 case TAG_EXTERN_VAR: result = CK_EXTERN_VARIABLE; break;
821 default: Assert ("Bad C tag type" == NULL); break;
823 return result;
826 static csharpKind csharpTagKind (const tagType type)
828 csharpKind result = CSK_UNDEFINED;
829 switch (type)
831 case TAG_CLASS: result = CSK_CLASS; break;
832 case TAG_ENUM: result = CSK_ENUMERATION; break;
833 case TAG_ENUMERATOR: result = CSK_ENUMERATOR; break;
834 case TAG_EVENT: result = CSK_EVENT; break;
835 case TAG_FIELD: result = CSK_FIELD ; break;
836 case TAG_INTERFACE: result = CSK_INTERFACE; break;
837 case TAG_LOCAL: result = CSK_LOCAL; break;
838 case TAG_METHOD: result = CSK_METHOD; break;
839 case TAG_NAMESPACE: result = CSK_NAMESPACE; break;
840 case TAG_PROPERTY: result = CSK_PROPERTY; break;
841 case TAG_STRUCT: result = CSK_STRUCT; break;
842 case TAG_TYPEDEF: result = CSK_TYPEDEF; break;
844 default: Assert ("Bad C# tag type" == NULL); break;
846 return result;
849 static javaKind javaTagKind (const tagType type)
851 javaKind result = JK_UNDEFINED;
852 switch (type)
854 case TAG_CLASS: result = JK_CLASS; break;
855 case TAG_ENUM: result = JK_ENUM; break;
856 case TAG_ENUMERATOR: result = JK_ENUM_CONSTANT; break;
857 case TAG_FIELD: result = JK_FIELD; break;
858 case TAG_INTERFACE: result = JK_INTERFACE; break;
859 case TAG_LOCAL: result = JK_LOCAL; break;
860 case TAG_METHOD: result = JK_METHOD; break;
861 case TAG_PACKAGE: result = JK_PACKAGE; break;
863 default: Assert ("Bad Java tag type" == NULL); break;
865 return result;
868 static veraKind veraTagKind (const tagType type) {
869 veraKind result = VK_UNDEFINED;
870 switch (type)
872 case TAG_CLASS: result = VK_CLASS; break;
873 case TAG_ENUM: result = VK_ENUMERATION; break;
874 case TAG_ENUMERATOR: result = VK_ENUMERATOR; break;
875 case TAG_FUNCTION: result = VK_FUNCTION; break;
876 case TAG_LOCAL: result = VK_LOCAL; break;
877 case TAG_MEMBER: result = VK_MEMBER; break;
878 case TAG_PROGRAM: result = VK_PROGRAM; break;
879 case TAG_PROTOTYPE: result = VK_PROTOTYPE; break;
880 case TAG_TASK: result = VK_TASK; break;
881 case TAG_TYPEDEF: result = VK_TYPEDEF; break;
882 case TAG_VARIABLE: result = VK_VARIABLE; break;
883 case TAG_EXTERN_VAR: result = VK_EXTERN_VARIABLE; break;
885 default: Assert ("Bad Vera tag type" == NULL); break;
887 return result;
890 static const char *tagName (const tagType type)
892 const char* result;
893 if (isLanguage (Lang_csharp))
894 result = CsharpKinds [csharpTagKind (type)].name;
895 else if (isLanguage (Lang_java))
896 result = JavaKinds [javaTagKind (type)].name;
897 else if (isLanguage (Lang_vera))
898 result = VeraKinds [veraTagKind (type)].name;
899 else
900 result = CKinds [cTagKind (type)].name;
901 return result;
904 static int tagLetter (const tagType type)
906 int result;
907 if (isLanguage (Lang_csharp))
908 result = CsharpKinds [csharpTagKind (type)].letter;
909 else if (isLanguage (Lang_java))
910 result = JavaKinds [javaTagKind (type)].letter;
911 else if (isLanguage (Lang_vera))
912 result = VeraKinds [veraTagKind (type)].letter;
913 else
914 result = CKinds [cTagKind (type)].letter;
915 return result;
918 static boolean includeTag (const tagType type, const boolean isFileScope)
920 boolean result;
921 if (isFileScope && ! Option.include.fileScope)
922 result = FALSE;
923 else if (isLanguage (Lang_csharp))
924 result = CsharpKinds [csharpTagKind (type)].enabled;
925 else if (isLanguage (Lang_java))
926 result = JavaKinds [javaTagKind (type)].enabled;
927 else if (isLanguage (Lang_vera))
928 result = VeraKinds [veraTagKind (type)].enabled;
929 else
930 result = CKinds [cTagKind (type)].enabled;
931 return result;
934 static tagType declToTagType (const declType declaration)
936 tagType type = TAG_UNDEFINED;
938 switch (declaration)
940 case DECL_CLASS: type = TAG_CLASS; break;
941 case DECL_ENUM: type = TAG_ENUM; break;
942 case DECL_EVENT: type = TAG_EVENT; break;
943 case DECL_FUNCTION: type = TAG_FUNCTION; break;
944 case DECL_INTERFACE: type = TAG_INTERFACE; break;
945 case DECL_NAMESPACE: type = TAG_NAMESPACE; break;
946 case DECL_PROGRAM: type = TAG_PROGRAM; break;
947 case DECL_TASK: type = TAG_TASK; break;
948 case DECL_STRUCT: type = TAG_STRUCT; break;
949 case DECL_UNION: type = TAG_UNION; break;
951 default: Assert ("Unexpected declaration" == NULL); break;
953 return type;
956 static const char* accessField (const statementInfo *const st)
958 const char* result = NULL;
959 if (isLanguage (Lang_cpp) && st->scope == SCOPE_FRIEND)
960 result = "friend";
961 else if (st->member.access != ACCESS_UNDEFINED)
962 result = accessString (st->member.access);
963 return result;
966 static void addContextSeparator (vString *const scope)
968 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
969 vStringCatS (scope, "::");
970 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
971 vStringCatS (scope, ".");
974 static void addOtherFields (tagEntryInfo* const tag, const tagType type,
975 const statementInfo *const st,
976 vString *const scope, vString *const typeRef)
978 /* For selected tag types, append an extension flag designating the
979 * parent object in which the tag is defined.
981 switch (type)
983 default: break;
985 case TAG_FUNCTION:
986 case TAG_METHOD:
987 case TAG_PROTOTYPE:
988 if (vStringLength (Signature) > 0)
990 tag->extensionFields.signature = vStringValue (Signature);
993 if (vStringLength (ReturnType) > 0)
995 tag->extensionFields.returnType = vStringValue (ReturnType);
997 case TAG_CLASS:
998 case TAG_ENUM:
999 case TAG_ENUMERATOR:
1000 case TAG_EVENT:
1001 case TAG_FIELD:
1002 case TAG_INTERFACE:
1003 case TAG_MEMBER:
1004 case TAG_NAMESPACE:
1005 case TAG_PROPERTY:
1006 case TAG_STRUCT:
1007 case TAG_TASK:
1008 case TAG_TYPEDEF:
1009 case TAG_UNION:
1010 if (vStringLength (scope) > 0 &&
1011 (isMember (st) || st->parent->declaration == DECL_NAMESPACE))
1013 if (isType (st->context, TOKEN_NAME))
1014 tag->extensionFields.scope [0] = tagName (TAG_CLASS);
1015 else
1016 tag->extensionFields.scope [0] =
1017 tagName (declToTagType (parentDecl (st)));
1018 tag->extensionFields.scope [1] = vStringValue (scope);
1020 if ((type == TAG_CLASS || type == TAG_INTERFACE ||
1021 type == TAG_STRUCT) && vStringLength (st->parentClasses) > 0)
1024 tag->extensionFields.inheritance =
1025 vStringValue (st->parentClasses);
1027 if (st->implementation != IMP_DEFAULT &&
1028 (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) ||
1029 isLanguage (Lang_java)))
1031 tag->extensionFields.implementation =
1032 implementationString (st->implementation);
1034 if (isMember (st))
1036 tag->extensionFields.access = accessField (st);
1038 break;
1041 /* Add typename info, type of the tag and name of struct/union/etc. */
1042 if ((type == TAG_TYPEDEF || type == TAG_VARIABLE || type == TAG_MEMBER)
1043 && isContextualStatement(st))
1045 char *p;
1047 tag->extensionFields.typeRef [0] =
1048 tagName (declToTagType (st->declaration));
1049 p = vStringValue (st->blockName->name);
1051 /* If there was no {} block get the name from the token before the
1052 * name (current token is ';' or ',', previous token is the name).
1054 if (p == NULL || *p == '\0')
1056 tokenInfo *const prev2 = prevToken (st, 2);
1057 if (isType (prev2, TOKEN_NAME))
1058 p = vStringValue (prev2->name);
1061 /* Prepend the scope name if there is one. */
1062 if (vStringLength (scope) > 0)
1064 vStringCopy(typeRef, scope);
1065 addContextSeparator (typeRef);
1066 vStringCatS(typeRef, p);
1067 p = vStringValue (typeRef);
1069 tag->extensionFields.typeRef [1] = p;
1073 static void findScopeHierarchy (vString *const string,
1074 const statementInfo *const st)
1076 vStringClear (string);
1077 if (isType (st->context, TOKEN_NAME))
1078 vStringCopy (string, st->context->name);
1079 if (st->parent != NULL)
1081 vString *temp = vStringNew ();
1082 const statementInfo *s;
1083 for (s = st->parent ; s != NULL ; s = s->parent)
1085 if (isContextualStatement (s) ||
1086 s->declaration == DECL_NAMESPACE ||
1087 s->declaration == DECL_PROGRAM)
1089 vStringCopy (temp, string);
1090 vStringClear (string);
1091 Assert (isType (s->blockName, TOKEN_NAME));
1092 if (isType (s->context, TOKEN_NAME) &&
1093 vStringLength (s->context->name) > 0)
1095 vStringCat (string, s->context->name);
1096 addContextSeparator (string);
1098 vStringCat (string, s->blockName->name);
1099 if (vStringLength (temp) > 0)
1100 addContextSeparator (string);
1101 vStringCat (string, temp);
1104 vStringDelete (temp);
1108 static void makeExtraTagEntry (const tagType type, tagEntryInfo *const e,
1109 vString *const scope)
1111 if (Option.include.qualifiedTags &&
1112 scope != NULL && vStringLength (scope) > 0)
1114 vString *const scopedName = vStringNew ();
1116 if (type != TAG_ENUMERATOR)
1117 vStringCopy (scopedName, scope);
1118 else
1120 /* remove last component (i.e. enumeration name) from scope */
1121 const char* const sc = vStringValue (scope);
1122 const char* colon = strrchr (sc, ':');
1123 if (colon != NULL)
1125 while (*colon == ':' && colon > sc)
1126 --colon;
1127 vStringNCopy (scopedName, scope, colon + 1 - sc);
1130 if (vStringLength (scopedName) > 0)
1132 addContextSeparator (scopedName);
1133 vStringCatS (scopedName, e->name);
1134 e->name = vStringValue (scopedName);
1135 makeTagEntry (e);
1137 vStringDelete (scopedName);
1141 static void makeTag (const tokenInfo *const token,
1142 const statementInfo *const st,
1143 boolean isFileScope, const tagType type)
1145 /* Nothing is really of file scope when it appears in a header file.
1147 isFileScope = (boolean) (isFileScope && ! isHeaderFile ());
1149 if (isType (token, TOKEN_NAME) && vStringLength (token->name) > 0 &&
1150 includeTag (type, isFileScope))
1152 vString *scope = vStringNew ();
1153 /* Use "typeRef" to store the typename from addOtherFields() until
1154 * it's used in makeTagEntry().
1156 vString *typeRef = vStringNew ();
1157 tagEntryInfo e;
1159 initTagEntry (&e, vStringValue (token->name));
1161 e.lineNumber = token->lineNumber;
1162 e.filePosition = token->filePosition;
1163 e.isFileScope = isFileScope;
1164 e.kindName = tagName (type);
1165 e.kind = tagLetter (type);
1167 findScopeHierarchy (scope, st);
1168 addOtherFields (&e, type, st, scope, typeRef);
1170 makeTagEntry (&e);
1171 makeExtraTagEntry (type, &e, scope);
1172 vStringDelete (scope);
1173 vStringDelete (typeRef);
1177 static boolean isValidTypeSpecifier (const declType declaration)
1179 boolean result;
1180 switch (declaration)
1182 case DECL_BASE:
1183 case DECL_CLASS:
1184 case DECL_ENUM:
1185 case DECL_EVENT:
1186 case DECL_STRUCT:
1187 case DECL_UNION:
1188 result = TRUE;
1189 break;
1191 default:
1192 result = FALSE;
1193 break;
1195 return result;
1198 static void qualifyEnumeratorTag (const statementInfo *const st,
1199 const tokenInfo *const nameToken)
1201 if (isType (nameToken, TOKEN_NAME))
1202 makeTag (nameToken, st, TRUE, TAG_ENUMERATOR);
1205 static void qualifyFunctionTag (const statementInfo *const st,
1206 const tokenInfo *const nameToken)
1208 if (isType (nameToken, TOKEN_NAME))
1210 tagType type;
1211 const boolean isFileScope =
1212 (boolean) (st->member.access == ACCESS_PRIVATE ||
1213 (!isMember (st) && st->scope == SCOPE_STATIC));
1214 if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
1215 type = TAG_METHOD;
1216 else if (isLanguage (Lang_vera) && st->declaration == DECL_TASK)
1217 type = TAG_TASK;
1218 else
1219 type = TAG_FUNCTION;
1220 makeTag (nameToken, st, isFileScope, type);
1224 static void qualifyFunctionDeclTag (const statementInfo *const st,
1225 const tokenInfo *const nameToken)
1227 if (! isType (nameToken, TOKEN_NAME))
1229 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
1230 qualifyFunctionTag (st, nameToken);
1231 else if (st->scope == SCOPE_TYPEDEF)
1232 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1233 else if (isValidTypeSpecifier (st->declaration) && ! isLanguage (Lang_csharp))
1234 makeTag (nameToken, st, TRUE, TAG_PROTOTYPE);
1237 static void qualifyCompoundTag (const statementInfo *const st,
1238 const tokenInfo *const nameToken)
1240 if (isType (nameToken, TOKEN_NAME))
1242 const tagType type = declToTagType (st->declaration);
1243 const boolean fileScoped = (boolean)
1244 (!(isLanguage (Lang_java) ||
1245 isLanguage (Lang_csharp) ||
1246 isLanguage (Lang_vera)));
1248 if (type != TAG_UNDEFINED)
1249 makeTag (nameToken, st, fileScoped, type);
1253 static void qualifyBlockTag (statementInfo *const st,
1254 const tokenInfo *const nameToken)
1256 switch (st->declaration)
1258 case DECL_CLASS:
1259 case DECL_ENUM:
1260 case DECL_INTERFACE:
1261 case DECL_NAMESPACE:
1262 case DECL_PROGRAM:
1263 case DECL_STRUCT:
1264 case DECL_UNION:
1265 qualifyCompoundTag (st, nameToken);
1266 break;
1267 default: break;
1271 static void qualifyVariableTag (const statementInfo *const st,
1272 const tokenInfo *const nameToken)
1274 /* We have to watch that we do not interpret a declaration of the
1275 * form "struct tag;" as a variable definition. In such a case, the
1276 * token preceding the name will be a keyword.
1278 if (! isType (nameToken, TOKEN_NAME))
1280 else if (st->scope == SCOPE_TYPEDEF)
1281 makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
1282 else if (st->declaration == DECL_EVENT)
1283 makeTag (nameToken, st, (boolean) (st->member.access == ACCESS_PRIVATE),
1284 TAG_EVENT);
1285 else if (st->declaration == DECL_PACKAGE)
1286 makeTag (nameToken, st, FALSE, TAG_PACKAGE);
1287 else if (isValidTypeSpecifier (st->declaration))
1289 if (st->notVariable)
1291 else if (isMember (st))
1293 if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
1294 makeTag (nameToken, st,
1295 (boolean) (st->member.access == ACCESS_PRIVATE), TAG_FIELD);
1296 else if (st->scope == SCOPE_GLOBAL || st->scope == SCOPE_STATIC)
1297 makeTag (nameToken, st, TRUE, TAG_MEMBER);
1299 else
1301 if (st->scope == SCOPE_EXTERN || ! st->haveQualifyingName)
1302 makeTag (nameToken, st, FALSE, TAG_EXTERN_VAR);
1303 else if (st->inFunction)
1304 makeTag (nameToken, st, (boolean) (st->scope == SCOPE_STATIC),
1305 TAG_LOCAL);
1306 else
1307 makeTag (nameToken, st, (boolean) (st->scope == SCOPE_STATIC),
1308 TAG_VARIABLE);
1314 * Parsing functions
1317 static int skipToOneOf (const char *const chars)
1319 int c;
1321 c = cppGetc ();
1322 while (c != EOF && c != '\0' && strchr (chars, c) == NULL);
1323 return c;
1326 /* Skip to the next non-white character.
1328 static int skipToNonWhite (void)
1330 boolean found = FALSE;
1331 int c;
1333 #if 0
1335 c = cppGetc ();
1336 while (isspace (c));
1337 #else
1338 while (1)
1340 c = cppGetc ();
1341 if (isspace (c))
1342 found = TRUE;
1343 else
1344 break;
1346 if (CollectingSignature && found)
1347 vStringPut (Signature, ' ');
1348 #endif
1350 return c;
1353 /* Skips to the next brace in column 1. This is intended for cases where
1354 * preprocessor constructs result in unbalanced braces.
1356 static void skipToFormattedBraceMatch (void)
1358 int c, next;
1360 c = cppGetc ();
1361 next = cppGetc ();
1362 while (c != EOF && (c != '\n' || next != '}'))
1364 c = next;
1365 next = cppGetc ();
1369 /* Skip to the matching character indicated by the pair string. If skipping
1370 * to a matching brace and any brace is found within a different level of a
1371 * #if conditional statement while brace formatting is in effect, we skip to
1372 * the brace matched by its formatting. It is assumed that we have already
1373 * read the character which starts the group (i.e. the first character of
1374 * "pair").
1376 static void skipToMatch (const char *const pair)
1378 const boolean braceMatching = (boolean) (strcmp ("{}", pair) == 0);
1379 const boolean braceFormatting = (boolean) (isBraceFormat () && braceMatching);
1380 const unsigned int initialLevel = getDirectiveNestLevel ();
1381 const int begin = pair [0], end = pair [1];
1382 const unsigned long inputLineNumber = getInputLineNumber ();
1383 int matchLevel = 1;
1384 int c = '\0';
1386 while (matchLevel > 0 && (c = skipToNonWhite ()) != EOF)
1388 if (CollectingSignature)
1389 vStringPut (Signature, c);
1392 if (c == begin)
1394 ++matchLevel;
1395 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1397 skipToFormattedBraceMatch ();
1398 break;
1401 else if (c == end)
1403 --matchLevel;
1404 if (braceFormatting && getDirectiveNestLevel () != initialLevel)
1406 skipToFormattedBraceMatch ();
1407 break;
1411 if (c == EOF)
1413 verbose ("%s: failed to find match for '%c' at line %lu\n",
1414 getInputFileName (), begin, inputLineNumber);
1415 if (braceMatching)
1416 longjmp (Exception, (int) ExceptionBraceFormattingError);
1417 else
1418 longjmp (Exception, (int) ExceptionFormattingError);
1422 static void skipParens (void)
1424 const int c = skipToNonWhite ();
1426 if (c == '(')
1427 skipToMatch ("()");
1428 else
1429 cppUngetc (c);
1432 static void skipBraces (void)
1434 const int c = skipToNonWhite ();
1436 if (c == '{')
1437 skipToMatch ("{}");
1438 else
1439 cppUngetc (c);
1442 static keywordId analyzeKeyword (const char *const name)
1444 const keywordId id = (keywordId) lookupKeyword (name, getSourceLanguage ());
1445 return id;
1448 static void analyzeIdentifier (tokenInfo *const token)
1450 char *const name = vStringValue (token->name);
1451 const char *replacement = NULL;
1452 boolean parensToo = FALSE;
1454 if (isLanguage (Lang_java) ||
1455 ! isIgnoreToken (name, &parensToo, &replacement))
1457 if (replacement != NULL)
1458 token->keyword = analyzeKeyword (replacement);
1459 else
1460 token->keyword = analyzeKeyword (vStringValue (token->name));
1462 if (token->keyword == KEYWORD_NONE)
1463 token->type = TOKEN_NAME;
1464 else
1465 token->type = TOKEN_KEYWORD;
1467 else
1469 initToken (token);
1470 if (parensToo)
1472 int c = skipToNonWhite ();
1474 if (c == '(')
1475 skipToMatch ("()");
1480 static void readIdentifier (tokenInfo *const token, const int firstChar)
1482 vString *const name = token->name;
1483 int c = firstChar;
1484 boolean first = TRUE;
1486 initToken (token);
1488 /* Bug #1585745: strangely, C++ destructors allow whitespace between
1489 * the ~ and the class name. */
1490 if (isLanguage (Lang_cpp) && firstChar == '~')
1492 vStringPut (name, c);
1493 c = skipToNonWhite ();
1498 vStringPut (name, c);
1499 if (CollectingSignature)
1501 if (!first)
1502 vStringPut (Signature, c);
1503 first = FALSE;
1505 c = cppGetc ();
1506 } while (isident (c) || ((isLanguage (Lang_java) || isLanguage (Lang_csharp)) && (isHighChar (c) || c == '.')));
1507 vStringTerminate (name);
1508 cppUngetc (c); /* unget non-identifier character */
1510 analyzeIdentifier (token);
1513 static void readPackageName (tokenInfo *const token, const int firstChar)
1515 vString *const name = token->name;
1516 int c = firstChar;
1518 initToken (token);
1520 while (isident (c) || c == '.')
1522 vStringPut (name, c);
1523 c = cppGetc ();
1525 vStringTerminate (name);
1526 cppUngetc (c); /* unget non-package character */
1529 static void readPackageOrNamespace (statementInfo *const st, const declType declaration)
1531 st->declaration = declaration;
1533 if (declaration == DECL_NAMESPACE && !isLanguage (Lang_csharp))
1535 /* In C++ a namespace is specified one level at a time. */
1536 return;
1538 else
1540 /* In C#, a namespace can also be specified like a Java package name. */
1541 tokenInfo *const token = activeToken (st);
1542 Assert (isType (token, TOKEN_KEYWORD));
1543 readPackageName (token, skipToNonWhite ());
1544 token->type = TOKEN_NAME;
1545 st->gotName = TRUE;
1546 st->haveQualifyingName = TRUE;
1550 static void processName (statementInfo *const st)
1552 Assert (isType (activeToken (st), TOKEN_NAME));
1553 if (st->gotName && st->declaration == DECL_NONE)
1554 st->declaration = DECL_BASE;
1555 st->gotName = TRUE;
1556 st->haveQualifyingName = TRUE;
1559 static void readOperator (statementInfo *const st)
1561 const char *const acceptable = "+-*/%^&|~!=<>,[]";
1562 const tokenInfo* const prev = prevToken (st,1);
1563 tokenInfo *const token = activeToken (st);
1564 vString *const name = token->name;
1565 int c = skipToNonWhite ();
1567 /* When we arrive here, we have the keyword "operator" in 'name'.
1569 if (isType (prev, TOKEN_KEYWORD) && (prev->keyword == KEYWORD_ENUM ||
1570 prev->keyword == KEYWORD_STRUCT || prev->keyword == KEYWORD_UNION))
1571 ; /* ignore "operator" keyword if preceded by these keywords */
1572 else if (c == '(')
1574 /* Verify whether this is a valid function call (i.e. "()") operator.
1576 if (cppGetc () == ')')
1578 vStringPut (name, ' '); /* always separate operator from keyword */
1579 c = skipToNonWhite ();
1580 if (c == '(')
1581 vStringCatS (name, "()");
1583 else
1585 skipToMatch ("()");
1586 c = cppGetc ();
1589 else if (isident1 (c))
1591 /* Handle "new" and "delete" operators, and conversion functions
1592 * (per 13.3.1.1.2 [2] of the C++ spec).
1594 boolean whiteSpace = TRUE; /* default causes insertion of space */
1597 if (isspace (c))
1598 whiteSpace = TRUE;
1599 else
1601 if (whiteSpace)
1603 vStringPut (name, ' ');
1604 whiteSpace = FALSE;
1606 vStringPut (name, c);
1608 c = cppGetc ();
1609 } while (! isOneOf (c, "(;") && c != EOF);
1610 vStringTerminate (name);
1612 else if (isOneOf (c, acceptable))
1614 vStringPut (name, ' '); /* always separate operator from keyword */
1617 vStringPut (name, c);
1618 c = cppGetc ();
1619 } while (isOneOf (c, acceptable));
1620 vStringTerminate (name);
1623 cppUngetc (c);
1625 token->type = TOKEN_NAME;
1626 token->keyword = KEYWORD_NONE;
1627 processName (st);
1630 static void copyToken (tokenInfo *const dest, const tokenInfo *const src)
1632 dest->type = src->type;
1633 dest->keyword = src->keyword;
1634 dest->filePosition = src->filePosition;
1635 dest->lineNumber = src->lineNumber;
1636 vStringCopy (dest->name, src->name);
1639 static void setAccess (statementInfo *const st, const accessType access)
1641 if (isMember (st))
1643 if (isLanguage (Lang_cpp))
1645 int c = skipToNonWhite ();
1647 if (c == ':')
1648 reinitStatement (st, FALSE);
1649 else
1650 cppUngetc (c);
1652 st->member.accessDefault = access;
1654 st->member.access = access;
1658 static void discardTypeList (tokenInfo *const token)
1660 int c = skipToNonWhite ();
1661 while (isident1 (c))
1663 readIdentifier (token, c);
1664 c = skipToNonWhite ();
1665 if (c == '.' || c == ',')
1666 c = skipToNonWhite ();
1668 cppUngetc (c);
1671 static void addParentClass (statementInfo *const st, tokenInfo *const token)
1673 if (vStringLength (token->name) > 0 &&
1674 vStringLength (st->parentClasses) > 0)
1676 vStringPut (st->parentClasses, ',');
1678 vStringCat (st->parentClasses, token->name);
1681 static void readParents (statementInfo *const st, const int qualifier)
1683 tokenInfo *const token = newToken ();
1684 tokenInfo *const parent = newToken ();
1685 int c;
1689 c = skipToNonWhite ();
1690 if (isident1 (c))
1692 readIdentifier (token, c);
1693 if (isType (token, TOKEN_NAME))
1694 vStringCat (parent->name, token->name);
1695 else
1697 addParentClass (st, parent);
1698 initToken (parent);
1701 else if (c == qualifier)
1702 vStringPut (parent->name, c);
1703 else if (c == '<')
1704 skipToMatch ("<>");
1705 else if (isType (token, TOKEN_NAME))
1707 addParentClass (st, parent);
1708 initToken (parent);
1710 } while (c != '{' && c != EOF);
1711 cppUngetc (c);
1712 deleteToken (parent);
1713 deleteToken (token);
1716 static void skipStatement (statementInfo *const st)
1718 st->declaration = DECL_IGNORE;
1719 skipToOneOf (";");
1722 static void processInterface (statementInfo *const st)
1724 st->declaration = DECL_INTERFACE;
1727 static void processToken (tokenInfo *const token, statementInfo *const st)
1729 switch (token->keyword) /* is it a reserved word? */
1731 default: break;
1733 case KEYWORD_NONE: processName (st); break;
1734 case KEYWORD_ABSTRACT: st->implementation = IMP_ABSTRACT; break;
1735 case KEYWORD_ATTRIBUTE:
1736 case KEYWORD_INLINE: skipParens (); initToken (token); break;
1737 case KEYWORD_BIND: st->declaration = DECL_BASE; break;
1738 case KEYWORD_BIT: st->declaration = DECL_BASE; break;
1739 case KEYWORD_CATCH: skipParens (); skipBraces (); break;
1740 case KEYWORD_CHAR: st->declaration = DECL_BASE; break;
1741 case KEYWORD_CLASS: st->declaration = DECL_CLASS; break;
1742 case KEYWORD_CONST: st->declaration = DECL_BASE; break;
1743 case KEYWORD_DOUBLE: st->declaration = DECL_BASE; break;
1744 case KEYWORD_ENUM: st->declaration = DECL_ENUM; break;
1745 case KEYWORD_EXTENDS: readParents (st, '.');
1746 setToken (st, TOKEN_NONE); break;
1747 case KEYWORD_FLOAT: st->declaration = DECL_BASE; break;
1748 case KEYWORD_FUNCTION: st->declaration = DECL_BASE; break;
1749 case KEYWORD_FRIEND: st->scope = SCOPE_FRIEND; break;
1750 case KEYWORD_GOTO: skipStatement (st); break;
1751 case KEYWORD_IMPLEMENTS:readParents (st, '.');
1752 setToken (st, TOKEN_NONE); break;
1753 case KEYWORD_IMPORT: skipStatement (st); break;
1754 case KEYWORD_INT: st->declaration = DECL_BASE; break;
1755 case KEYWORD_INTEGER: st->declaration = DECL_BASE; break;
1756 case KEYWORD_INTERFACE: processInterface (st); break;
1757 case KEYWORD_LOCAL: setAccess (st, ACCESS_LOCAL); break;
1758 case KEYWORD_LONG: st->declaration = DECL_BASE; break;
1759 case KEYWORD_OPERATOR: readOperator (st); break;
1760 case KEYWORD_PRIVATE: setAccess (st, ACCESS_PRIVATE); break;
1761 case KEYWORD_PROGRAM: st->declaration = DECL_PROGRAM; break;
1762 case KEYWORD_PROTECTED: setAccess (st, ACCESS_PROTECTED); break;
1763 case KEYWORD_PUBLIC: setAccess (st, ACCESS_PUBLIC); break;
1764 case KEYWORD_RETURN: skipStatement (st); break;
1765 case KEYWORD_SHORT: st->declaration = DECL_BASE; break;
1766 case KEYWORD_SIGNED: st->declaration = DECL_BASE; break;
1767 case KEYWORD_STRING: st->declaration = DECL_BASE; break;
1768 case KEYWORD_STRUCT: st->declaration = DECL_STRUCT; break;
1769 case KEYWORD_TASK: st->declaration = DECL_TASK; break;
1770 case KEYWORD_THROWS: discardTypeList (token); break;
1771 case KEYWORD_UNION: st->declaration = DECL_UNION; break;
1772 case KEYWORD_UNSIGNED: st->declaration = DECL_BASE; break;
1773 case KEYWORD_USING: skipStatement (st); break;
1774 case KEYWORD_VOID: st->declaration = DECL_BASE; break;
1775 case KEYWORD_VOLATILE: st->declaration = DECL_BASE; break;
1776 case KEYWORD_VIRTUAL: st->implementation = IMP_VIRTUAL; break;
1777 case KEYWORD_WCHAR_T: st->declaration = DECL_BASE; break;
1779 case KEYWORD_NAMESPACE: readPackageOrNamespace (st, DECL_NAMESPACE); break;
1780 case KEYWORD_PACKAGE: readPackageOrNamespace (st, DECL_PACKAGE); break;
1782 case KEYWORD_EVENT:
1783 if (isLanguage (Lang_csharp))
1784 st->declaration = DECL_EVENT;
1785 break;
1787 case KEYWORD_TYPEDEF:
1788 reinitStatement (st, FALSE);
1789 st->scope = SCOPE_TYPEDEF;
1790 break;
1792 case KEYWORD_EXTERN:
1793 if (! isLanguage (Lang_csharp) || !st->gotName)
1795 reinitStatement (st, FALSE);
1796 st->scope = SCOPE_EXTERN;
1797 st->declaration = DECL_BASE;
1799 break;
1801 case KEYWORD_STATIC:
1802 if (! (isLanguage (Lang_java) || isLanguage (Lang_csharp)))
1804 reinitStatement (st, FALSE);
1805 st->scope = SCOPE_STATIC;
1806 st->declaration = DECL_BASE;
1808 break;
1810 case KEYWORD_FOR:
1811 case KEYWORD_FOREACH:
1812 case KEYWORD_IF:
1813 case KEYWORD_SWITCH:
1814 case KEYWORD_WHILE:
1816 int c = skipToNonWhite ();
1817 if (c == '(')
1818 skipToMatch ("()");
1819 break;
1825 * Parenthesis handling functions
1828 static void restartStatement (statementInfo *const st)
1830 tokenInfo *const save = newToken ();
1831 tokenInfo *token = activeToken (st);
1833 copyToken (save, token);
1834 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>");)
1835 reinitStatement (st, FALSE);
1836 token = activeToken (st);
1837 copyToken (token, save);
1838 deleteToken (save);
1839 processToken (token, st);
1842 /* Skips over a the mem-initializer-list of a ctor-initializer, defined as:
1844 * mem-initializer-list:
1845 * mem-initializer, mem-initializer-list
1847 * mem-initializer:
1848 * [::] [nested-name-spec] class-name (...)
1849 * identifier
1851 static void skipMemIntializerList (tokenInfo *const token)
1853 int c;
1857 c = skipToNonWhite ();
1858 while (isident1 (c) || c == ':')
1860 if (c != ':')
1861 readIdentifier (token, c);
1862 c = skipToNonWhite ();
1864 if (c == '<')
1866 skipToMatch ("<>");
1867 c = skipToNonWhite ();
1869 if (c == '(')
1871 skipToMatch ("()");
1872 c = skipToNonWhite ();
1874 } while (c == ',');
1875 cppUngetc (c);
1878 static void skipMacro (statementInfo *const st)
1880 tokenInfo *const prev2 = prevToken (st, 2);
1882 if (isType (prev2, TOKEN_NAME))
1883 retardToken (st);
1884 skipToMatch ("()");
1887 /* Skips over characters following the parameter list. This will be either
1888 * non-ANSI style function declarations or C++ stuff. Our choices:
1890 * C (K&R):
1891 * int func ();
1892 * int func (one, two) int one; float two; {...}
1893 * C (ANSI):
1894 * int func (int one, float two);
1895 * int func (int one, float two) {...}
1896 * C++:
1897 * int foo (...) [const|volatile] [throw (...)];
1898 * int foo (...) [const|volatile] [throw (...)] [ctor-initializer] {...}
1899 * int foo (...) [const|volatile] [throw (...)] try [ctor-initializer] {...}
1900 * catch (...) {...}
1902 static boolean skipPostArgumentStuff (
1903 statementInfo *const st, parenInfo *const info)
1905 tokenInfo *const token = activeToken (st);
1906 unsigned int parameters = info->parameterCount;
1907 unsigned int elementCount = 0;
1908 boolean restart = FALSE;
1909 boolean end = FALSE;
1910 int c = skipToNonWhite ();
1914 switch (c)
1916 case ')': break;
1917 case ':': skipMemIntializerList (token);break; /* ctor-initializer */
1918 case '[': skipToMatch ("[]"); break;
1919 case '=': cppUngetc (c); end = TRUE; break;
1920 case '{': cppUngetc (c); end = TRUE; break;
1921 case '}': cppUngetc (c); end = TRUE; break;
1923 case '(':
1924 if (elementCount > 0)
1925 ++elementCount;
1926 skipToMatch ("()");
1927 break;
1929 case ';':
1930 if (parameters == 0 || elementCount < 2)
1932 cppUngetc (c);
1933 end = TRUE;
1935 else if (--parameters == 0)
1936 end = TRUE;
1937 break;
1939 default:
1940 if (isident1 (c))
1942 readIdentifier (token, c);
1943 switch (token->keyword)
1945 case KEYWORD_ATTRIBUTE: skipParens (); break;
1946 case KEYWORD_THROW: skipParens (); break;
1947 case KEYWORD_TRY: break;
1949 case KEYWORD_CONST:
1950 case KEYWORD_VOLATILE:
1951 if (vStringLength (Signature) > 0)
1953 vStringPut (Signature, ' ');
1954 vStringCat (Signature, token->name);
1956 break;
1958 case KEYWORD_CATCH:
1959 case KEYWORD_CLASS:
1960 case KEYWORD_EXPLICIT:
1961 case KEYWORD_EXTERN:
1962 case KEYWORD_FRIEND:
1963 case KEYWORD_INLINE:
1964 case KEYWORD_MUTABLE:
1965 case KEYWORD_NAMESPACE:
1966 case KEYWORD_NEW:
1967 case KEYWORD_NEWCOV:
1968 case KEYWORD_OPERATOR:
1969 case KEYWORD_OVERLOAD:
1970 case KEYWORD_PRIVATE:
1971 case KEYWORD_PROTECTED:
1972 case KEYWORD_PUBLIC:
1973 case KEYWORD_STATIC:
1974 case KEYWORD_TEMPLATE:
1975 case KEYWORD_TYPEDEF:
1976 case KEYWORD_TYPENAME:
1977 case KEYWORD_USING:
1978 case KEYWORD_VIRTUAL:
1979 /* Never allowed within parameter declarations. */
1980 restart = TRUE;
1981 end = TRUE;
1982 break;
1984 default:
1985 if (isType (token, TOKEN_NONE))
1987 else if (info->isKnrParamList && info->parameterCount > 0)
1988 ++elementCount;
1989 else
1991 /* If we encounter any other identifier immediately
1992 * following an empty parameter list, this is almost
1993 * certainly one of those Microsoft macro "thingies"
1994 * that the automatic source code generation sticks
1995 * in. Terminate the current statement.
1997 restart = TRUE;
1998 end = TRUE;
2000 break;
2004 if (! end)
2006 c = skipToNonWhite ();
2007 if (c == EOF)
2008 end = TRUE;
2010 } while (! end);
2012 if (restart)
2013 restartStatement (st);
2014 else
2015 setToken (st, TOKEN_NONE);
2017 return (boolean) (c != EOF);
2020 static void skipJavaThrows (statementInfo *const st)
2022 tokenInfo *const token = activeToken (st);
2023 int c = skipToNonWhite ();
2025 if (isident1 (c))
2027 readIdentifier (token, c);
2028 if (token->keyword == KEYWORD_THROWS)
2032 c = skipToNonWhite ();
2033 if (isident1 (c))
2035 readIdentifier (token, c);
2036 c = skipToNonWhite ();
2038 } while (c == '.' || c == ',');
2041 cppUngetc (c);
2042 setToken (st, TOKEN_NONE);
2045 static void analyzePostParens (statementInfo *const st, parenInfo *const info)
2047 const unsigned long inputLineNumber = getInputLineNumber ();
2048 int c = skipToNonWhite ();
2050 cppUngetc (c);
2051 if (isOneOf (c, "{;,="))
2053 else if (isLanguage (Lang_java))
2054 skipJavaThrows (st);
2055 else
2057 if (! skipPostArgumentStuff (st, info))
2059 verbose (
2060 "%s: confusing argument declarations beginning at line %lu\n",
2061 getInputFileName (), inputLineNumber);
2062 longjmp (Exception, (int) ExceptionFormattingError);
2067 static boolean languageSupportsGenerics (void)
2069 return (boolean) (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) ||
2070 isLanguage (Lang_java));
2073 static void processAngleBracket (void)
2075 int c = cppGetc ();
2076 if (c == '>') {
2077 /* already found match for template */
2078 } else if (languageSupportsGenerics () && c != '<' && c != '=') {
2079 /* this is a template */
2080 cppUngetc (c);
2081 skipToMatch ("<>");
2082 } else if (c == '<') {
2083 /* skip "<<" or "<<=". */
2084 c = cppGetc ();
2085 if (c != '=') {
2086 cppUngetc (c);
2088 } else {
2089 cppUngetc (c);
2093 static void parseJavaAnnotation (statementInfo *const st)
2096 * @Override
2097 * @Target(ElementType.METHOD)
2098 * @SuppressWarnings(value = "unchecked")
2100 * But watch out for "@interface"!
2102 tokenInfo *const token = activeToken (st);
2104 int c = skipToNonWhite ();
2105 readIdentifier (token, c);
2106 if (token->keyword == KEYWORD_INTERFACE)
2108 /* Oops. This was actually "@interface" defining a new annotation. */
2109 processInterface (st);
2111 else
2113 /* Bug #1691412: skip any annotation arguments. */
2114 skipParens ();
2118 static void parseReturnType (statementInfo *const st)
2120 int i;
2121 int lower_bound;
2122 tokenInfo * finding_tok;
2124 /* FIXME TODO: if java language must be supported then impement this here
2125 * removing the current FIXME */
2126 if (!isLanguage (Lang_c) && !isLanguage (Lang_cpp))
2128 return;
2131 vStringClear (ReturnType);
2133 finding_tok = prevToken (st, 1);
2135 if (isType (finding_tok, TOKEN_NONE))
2136 return;
2138 finding_tok = prevToken (st, 2);
2140 if (finding_tok->type == TOKEN_DOUBLE_COLON)
2142 /* get the total number of double colons */
2143 int j;
2144 int num_colons = 0;
2146 /* we already are at 2nd token */
2147 /* the +=2 means that colons are usually found at even places */
2148 for (j = 2; j < NumTokens; j+=2)
2150 tokenInfo *curr_tok;
2151 curr_tok = prevToken (st, j);
2152 if (curr_tok->type == TOKEN_DOUBLE_COLON)
2153 num_colons++;
2154 else
2155 break;
2158 /*printf ("FOUND colons %d\n", num_colons);*/
2159 lower_bound = 2 * num_colons + 1;
2161 else
2162 lower_bound = 1;
2164 for (i = (unsigned int) NumTokens; i > lower_bound; i--)
2166 tokenInfo * curr_tok;
2167 curr_tok = prevToken (st, i);
2169 switch (curr_tok->type)
2171 case TOKEN_PAREN_NAME:
2172 case TOKEN_NONE:
2173 continue;
2174 break;
2176 case TOKEN_DOUBLE_COLON:
2177 /* usually C++ class scope */
2178 vStringCatS (ReturnType, "::");
2179 break;
2181 case TOKEN_STAR:
2182 /* pointers */
2183 vStringPut (ReturnType, '*');
2184 break;
2186 case TOKEN_AMPERSAND:
2187 /* references */
2188 vStringPut (ReturnType, '&');
2189 break;
2191 case TOKEN_KEYWORD:
2192 vStringPut (ReturnType, ' ');
2194 default:
2195 vStringCat (ReturnType, curr_tok->name);
2196 break;
2200 /* clear any white space from the front */
2201 vStringStripLeading (ReturnType);
2203 /* .. and from the tail too */
2204 vStringStripTrailing (ReturnType);
2206 /* put and end marker */
2207 vStringTerminate (ReturnType);
2210 printf ("~~~~~ statement ---->\n");
2211 ps (st);
2212 printf ("NumTokens: %d\n", NumTokens);
2213 printf ("FOUND ReturnType: %s\n", vStringValue (ReturnType));
2214 printf ("<~~~~~\n");
2215 //*/
2218 static int parseParens (statementInfo *const st, parenInfo *const info)
2220 tokenInfo *const token = activeToken (st);
2221 unsigned int identifierCount = 0;
2222 unsigned int depth = 1;
2223 boolean firstChar = TRUE;
2224 int nextChar = '\0';
2226 CollectingSignature = TRUE;
2227 vStringClear (Signature);
2228 vStringPut (Signature, '(');
2229 info->parameterCount = 1;
2232 int c = skipToNonWhite ();
2233 vStringPut (Signature, c);
2235 switch (c)
2237 case '&':
2238 case '*':
2239 info->isPointer = TRUE;
2240 info->isKnrParamList = FALSE;
2241 if (identifierCount == 0)
2242 info->isParamList = FALSE;
2243 initToken (token);
2244 break;
2246 case ':':
2247 info->isKnrParamList = FALSE;
2248 break;
2250 case '.':
2251 info->isNameCandidate = FALSE;
2252 c = cppGetc ();
2253 if (c != '.')
2255 cppUngetc (c);
2256 info->isKnrParamList = FALSE;
2258 else
2260 c = cppGetc ();
2261 if (c != '.')
2263 cppUngetc (c);
2264 info->isKnrParamList = FALSE;
2266 else
2267 vStringCatS (Signature, "..."); /* variable arg list */
2269 break;
2271 case ',':
2272 info->isNameCandidate = FALSE;
2273 if (info->isKnrParamList)
2275 ++info->parameterCount;
2276 identifierCount = 0;
2278 break;
2280 case '=':
2281 info->isKnrParamList = FALSE;
2282 info->isNameCandidate = FALSE;
2283 if (firstChar)
2285 info->isParamList = FALSE;
2286 skipMacro (st);
2287 depth = 0;
2289 break;
2291 case '[':
2292 info->isKnrParamList = FALSE;
2293 skipToMatch ("[]");
2294 break;
2296 case '<':
2297 info->isKnrParamList = FALSE;
2298 processAngleBracket ();
2299 break;
2301 case ')':
2302 if (firstChar)
2303 info->parameterCount = 0;
2304 --depth;
2305 break;
2307 case '(':
2308 info->isKnrParamList = FALSE;
2309 if (firstChar)
2311 info->isNameCandidate = FALSE;
2312 cppUngetc (c);
2313 vStringClear (Signature);
2314 skipMacro (st);
2315 depth = 0;
2316 vStringChop (Signature);
2318 else if (isType (token, TOKEN_PAREN_NAME))
2320 c = skipToNonWhite ();
2321 if (c == '*') /* check for function pointer */
2323 skipToMatch ("()");
2324 c = skipToNonWhite ();
2325 if (c == '(')
2326 skipToMatch ("()");
2327 else
2328 cppUngetc (c);
2330 else
2332 cppUngetc (c);
2333 cppUngetc ('(');
2334 info->nestedArgs = TRUE;
2337 else
2338 ++depth;
2339 break;
2341 default:
2342 if (c == '@' && isLanguage (Lang_java))
2344 parseJavaAnnotation(st);
2346 else if (isident1 (c))
2348 if (++identifierCount > 1)
2349 info->isKnrParamList = FALSE;
2350 readIdentifier (token, c);
2351 if (isType (token, TOKEN_NAME) && info->isNameCandidate)
2352 token->type = TOKEN_PAREN_NAME;
2353 else if (isType (token, TOKEN_KEYWORD))
2355 if (token->keyword != KEYWORD_CONST &&
2356 token->keyword != KEYWORD_VOLATILE)
2358 info->isKnrParamList = FALSE;
2359 info->isNameCandidate = FALSE;
2363 else
2365 info->isParamList = FALSE;
2366 info->isKnrParamList = FALSE;
2367 info->isNameCandidate = FALSE;
2368 info->invalidContents = TRUE;
2370 break;
2372 firstChar = FALSE;
2373 } while (! info->nestedArgs && depth > 0 &&
2374 (info->isKnrParamList || info->isNameCandidate));
2376 if (! info->nestedArgs) while (depth > 0)
2378 skipToMatch ("()");
2379 --depth;
2382 if (! info->isNameCandidate)
2383 initToken (token);
2385 vStringTerminate (Signature);
2386 if (info->isKnrParamList)
2387 vStringClear (Signature);
2388 CollectingSignature = FALSE;
2389 return nextChar;
2392 static void initParenInfo (parenInfo *const info)
2394 info->isPointer = FALSE;
2395 info->isParamList = TRUE;
2396 info->isKnrParamList = isLanguage (Lang_c);
2397 info->isNameCandidate = TRUE;
2398 info->invalidContents = FALSE;
2399 info->nestedArgs = FALSE;
2400 info->parameterCount = 0;
2403 static void analyzeParens (statementInfo *const st)
2405 tokenInfo *const prev = prevToken (st, 1);
2407 if (st->inFunction && ! st->assignment)
2408 st->notVariable = TRUE;
2409 if (! isType (prev, TOKEN_NONE)) /* in case of ignored enclosing macros */
2411 tokenInfo *const token = activeToken (st);
2412 parenInfo info;
2413 int c;
2415 initParenInfo (&info);
2416 parseParens (st, &info);
2417 parseReturnType (st);
2418 c = skipToNonWhite ();
2419 cppUngetc (c);
2420 if (info.invalidContents)
2421 reinitStatement (st, FALSE);
2422 else if (info.isNameCandidate && isType (token, TOKEN_PAREN_NAME) &&
2423 ! st->gotParenName &&
2424 (! info.isParamList || ! st->haveQualifyingName ||
2425 c == '(' ||
2426 (c == '=' && st->implementation != IMP_VIRTUAL) ||
2427 (st->declaration == DECL_NONE && isOneOf (c, ",;"))))
2429 token->type = TOKEN_NAME;
2430 processName (st);
2431 st->gotParenName = TRUE;
2432 if (! (c == '(' && info.nestedArgs))
2433 st->isPointer = info.isPointer;
2435 else if (! st->gotArgs && info.isParamList)
2437 st->gotArgs = TRUE;
2438 setToken (st, TOKEN_ARGS);
2439 advanceToken (st);
2440 if (st->scope != SCOPE_TYPEDEF)
2441 analyzePostParens (st, &info);
2443 else
2444 setToken (st, TOKEN_NONE);
2449 * Token parsing functions
2452 static void addContext (statementInfo *const st, const tokenInfo* const token)
2454 if (isType (token, TOKEN_NAME))
2456 if (vStringLength (st->context->name) > 0)
2458 if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
2459 vStringCatS (st->context->name, "::");
2460 else if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
2461 vStringCatS (st->context->name, ".");
2463 vStringCat (st->context->name, token->name);
2464 st->context->type = TOKEN_NAME;
2468 static boolean inheritingDeclaration (declType decl)
2470 /* C# supports inheritance for enums. C++0x will too, but not yet. */
2471 if (decl == DECL_ENUM)
2473 return (boolean) (isLanguage (Lang_csharp));
2475 return (boolean) (
2476 decl == DECL_CLASS ||
2477 decl == DECL_STRUCT ||
2478 decl == DECL_INTERFACE);
2481 static void processColon (statementInfo *const st)
2483 int c = (isLanguage (Lang_cpp) ? cppGetc () : skipToNonWhite ());
2484 const boolean doubleColon = (boolean) (c == ':');
2486 if (doubleColon)
2488 setToken (st, TOKEN_DOUBLE_COLON);
2489 st->haveQualifyingName = FALSE;
2491 else
2493 cppUngetc (c);
2494 if ((isLanguage (Lang_cpp) || isLanguage (Lang_csharp)) &&
2495 inheritingDeclaration (st->declaration))
2497 readParents (st, ':');
2499 else if (parentDecl (st) == DECL_STRUCT)
2501 c = skipToOneOf (",;");
2502 if (c == ',')
2503 setToken (st, TOKEN_COMMA);
2504 else if (c == ';')
2505 setToken (st, TOKEN_SEMICOLON);
2507 else
2509 const tokenInfo *const prev = prevToken (st, 1);
2510 const tokenInfo *const prev2 = prevToken (st, 2);
2511 if (prev->keyword == KEYWORD_DEFAULT ||
2512 prev2->keyword == KEYWORD_CASE ||
2513 st->parent != NULL)
2515 reinitStatement (st, FALSE);
2521 /* Skips over any initializing value which may follow an '=' character in a
2522 * variable definition.
2524 static int skipInitializer (statementInfo *const st)
2526 boolean done = FALSE;
2527 int c;
2529 while (! done)
2531 c = skipToNonWhite ();
2533 if (c == EOF)
2534 longjmp (Exception, (int) ExceptionFormattingError);
2535 else switch (c)
2537 case ',':
2538 case ';': done = TRUE; break;
2540 case '0':
2541 if (st->implementation == IMP_VIRTUAL)
2542 st->implementation = IMP_PURE_VIRTUAL;
2543 break;
2545 case '[': skipToMatch ("[]"); break;
2546 case '(': skipToMatch ("()"); break;
2547 case '{': skipToMatch ("{}"); break;
2548 case '<': processAngleBracket(); break;
2550 case '}':
2551 if (insideEnumBody (st))
2552 done = TRUE;
2553 else if (! isBraceFormat ())
2555 verbose ("%s: unexpected closing brace at line %lu\n",
2556 getInputFileName (), getInputLineNumber ());
2557 longjmp (Exception, (int) ExceptionBraceFormattingError);
2559 break;
2561 default: break;
2564 return c;
2567 static void processInitializer (statementInfo *const st)
2569 const boolean inEnumBody = insideEnumBody (st);
2570 int c = cppGetc ();
2572 if (c != '=')
2574 cppUngetc (c);
2575 c = skipInitializer (st);
2576 st->assignment = TRUE;
2577 if (c == ';')
2578 setToken (st, TOKEN_SEMICOLON);
2579 else if (c == ',')
2580 setToken (st, TOKEN_COMMA);
2581 else if (c == '}' && inEnumBody)
2583 cppUngetc (c);
2584 setToken (st, TOKEN_COMMA);
2586 if (st->scope == SCOPE_EXTERN)
2587 st->scope = SCOPE_GLOBAL;
2591 static void parseIdentifier (statementInfo *const st, const int c)
2593 tokenInfo *const token = activeToken (st);
2595 readIdentifier (token, c);
2596 if (! isType (token, TOKEN_NONE))
2597 processToken (token, st);
2600 static void parseGeneralToken (statementInfo *const st, const int c)
2602 const tokenInfo *const prev = prevToken (st, 1);
2604 if (isident1 (c) || (isLanguage (Lang_java) && isHighChar (c)))
2606 parseIdentifier (st, c);
2607 if (isType (st->context, TOKEN_NAME) &&
2608 isType (activeToken (st), TOKEN_NAME) && isType (prev, TOKEN_NAME))
2610 initToken (st->context);
2613 else if (c == '.' || c == '-')
2615 if (! st->assignment)
2616 st->notVariable = TRUE;
2617 if (c == '-')
2619 int c2 = cppGetc ();
2620 if (c2 != '>')
2621 cppUngetc (c2);
2624 else if (c == '!' || c == '>')
2626 int c2 = cppGetc ();
2627 if (c2 != '=')
2628 cppUngetc (c2);
2630 else if (c == '@' && isLanguage (Lang_java))
2632 parseJavaAnnotation (st);
2634 else if (isExternCDecl (st, c))
2636 st->declaration = DECL_NOMANGLE;
2637 st->scope = SCOPE_GLOBAL;
2641 /* Reads characters from the pre-processor and assembles tokens, setting
2642 * the current statement state.
2644 static void nextToken (statementInfo *const st)
2646 tokenInfo *token;
2649 int c = skipToNonWhite ();
2650 switch (c)
2652 case EOF: longjmp (Exception, (int) ExceptionEOF); break;
2653 /* analyze functions and co */
2654 case '(': analyzeParens (st); break;
2655 case '<': processAngleBracket (); break;
2656 case '*':
2657 st->haveQualifyingName = FALSE;
2658 setToken (st, TOKEN_STAR);
2659 break;
2660 case '&': setToken (st, TOKEN_AMPERSAND); break;
2662 case ',': setToken (st, TOKEN_COMMA); break;
2663 case ':': processColon (st); break;
2664 case ';': setToken (st, TOKEN_SEMICOLON); break;
2665 case '=': processInitializer (st); break;
2666 case '[': skipToMatch ("[]"); break;
2667 case '{': setToken (st, TOKEN_BRACE_OPEN); break;
2668 case '}': setToken (st, TOKEN_BRACE_CLOSE); break;
2669 default: parseGeneralToken (st, c); break;
2671 token = activeToken (st);
2672 } while (isType (token, TOKEN_NONE));
2676 * Scanning support functions
2679 static statementInfo *CurrentStatement = NULL;
2681 static statementInfo *newStatement (statementInfo *const parent)
2683 statementInfo *const st = xMalloc (1, statementInfo);
2684 unsigned int i;
2686 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2687 st->token [i] = newToken ();
2689 st->context = newToken ();
2690 st->blockName = newToken ();
2691 st->parentClasses = vStringNew ();
2693 initStatement (st, parent);
2694 CurrentStatement = st;
2696 return st;
2699 static void deleteStatement (void)
2701 statementInfo *const st = CurrentStatement;
2702 statementInfo *const parent = st->parent;
2703 unsigned int i;
2705 for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
2707 deleteToken (st->token [i]); st->token [i] = NULL;
2709 deleteToken (st->blockName); st->blockName = NULL;
2710 deleteToken (st->context); st->context = NULL;
2711 vStringDelete (st->parentClasses); st->parentClasses = NULL;
2712 eFree (st);
2713 CurrentStatement = parent;
2716 static void deleteAllStatements (void)
2718 while (CurrentStatement != NULL)
2719 deleteStatement ();
2722 static boolean isStatementEnd (const statementInfo *const st)
2724 const tokenInfo *const token = activeToken (st);
2725 boolean isEnd;
2727 if (isType (token, TOKEN_SEMICOLON))
2728 isEnd = TRUE;
2729 else if (isType (token, TOKEN_BRACE_CLOSE))
2730 /* Java and C# do not require semicolons to end a block. Neither do C++
2731 * namespaces. All other blocks require a semicolon to terminate them.
2733 isEnd = (boolean) (isLanguage (Lang_java) || isLanguage (Lang_csharp) ||
2734 ! isContextualStatement (st));
2735 else
2736 isEnd = FALSE;
2738 return isEnd;
2741 static void checkStatementEnd (statementInfo *const st)
2743 const tokenInfo *const token = activeToken (st);
2745 if (isType (token, TOKEN_COMMA))
2746 reinitStatement (st, TRUE);
2747 else if (isStatementEnd (st))
2749 DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>"); )
2750 reinitStatement (st, FALSE);
2751 cppEndStatement ();
2753 else
2755 cppBeginStatement ();
2756 advanceToken (st);
2760 static void nest (statementInfo *const st, const unsigned int nestLevel)
2762 switch (st->declaration)
2764 case DECL_CLASS:
2765 case DECL_ENUM:
2766 case DECL_INTERFACE:
2767 case DECL_NAMESPACE:
2768 case DECL_NOMANGLE:
2769 case DECL_STRUCT:
2770 case DECL_UNION:
2771 createTags (nestLevel, st);
2772 break;
2774 case DECL_FUNCTION:
2775 case DECL_TASK:
2776 st->inFunction = TRUE;
2778 /* fall through */
2779 default:
2780 if (includeTag (TAG_LOCAL, FALSE))
2781 createTags (nestLevel, st);
2782 else
2783 skipToMatch ("{}");
2784 break;
2786 advanceToken (st);
2787 setToken (st, TOKEN_BRACE_CLOSE);
2790 static void tagCheck (statementInfo *const st)
2792 const tokenInfo *const token = activeToken (st);
2793 const tokenInfo *const prev = prevToken (st, 1);
2794 const tokenInfo *const prev2 = prevToken (st, 2);
2796 switch (token->type)
2798 case TOKEN_NAME:
2799 if (insideEnumBody (st))
2800 qualifyEnumeratorTag (st, token);
2801 break;
2802 #if 0
2803 case TOKEN_PACKAGE:
2804 if (st->haveQualifyingName)
2805 makeTag (token, st, FALSE, TAG_PACKAGE);
2806 break;
2807 #endif
2808 case TOKEN_BRACE_OPEN:
2809 if (isType (prev, TOKEN_ARGS))
2811 if (st->haveQualifyingName)
2813 if (! isLanguage (Lang_vera))
2814 st->declaration = DECL_FUNCTION;
2815 if (isType (prev2, TOKEN_NAME))
2816 copyToken (st->blockName, prev2);
2817 qualifyFunctionTag (st, prev2);
2820 else if (isContextualStatement (st) ||
2821 st->declaration == DECL_NAMESPACE ||
2822 st->declaration == DECL_PROGRAM)
2824 if (isType (prev, TOKEN_NAME))
2825 copyToken (st->blockName, prev);
2826 else
2828 /* For an anonymous struct or union we use a unique ID
2829 * a number, so that the members can be found.
2831 char buf [20]; /* length of "_anon" + digits + null */
2832 sprintf (buf, "__anon%d", ++AnonymousID);
2833 vStringCopyS (st->blockName->name, buf);
2834 st->blockName->type = TOKEN_NAME;
2835 st->blockName->keyword = KEYWORD_NONE;
2837 qualifyBlockTag (st, prev);
2839 else if (isLanguage (Lang_csharp))
2840 makeTag (prev, st, FALSE, TAG_PROPERTY);
2841 break;
2843 case TOKEN_SEMICOLON:
2844 case TOKEN_COMMA:
2845 if (insideEnumBody (st))
2847 else if (isType (prev, TOKEN_NAME))
2849 if (isContextualKeyword (prev2))
2850 makeTag (prev, st, TRUE, TAG_EXTERN_VAR);
2851 else
2852 qualifyVariableTag (st, prev);
2854 else if (isType (prev, TOKEN_ARGS) && isType (prev2, TOKEN_NAME))
2856 if (st->isPointer)
2857 qualifyVariableTag (st, prev2);
2858 else
2859 qualifyFunctionDeclTag (st, prev2);
2861 if (isLanguage (Lang_java) && token->type == TOKEN_SEMICOLON && insideEnumBody (st))
2863 /* In Java, after an initial enum-like part,
2864 * a semicolon introduces a class-like part.
2865 * See Bug #1730485 for the full rationale. */
2866 st->parent->declaration = DECL_CLASS;
2868 break;
2870 default: break;
2874 /* Parses the current file and decides whether to write out and tags that
2875 * are discovered.
2877 static void createTags (const unsigned int nestLevel,
2878 statementInfo *const parent)
2880 statementInfo *const st = newStatement (parent);
2882 DebugStatement ( if (nestLevel > 0) debugParseNest (TRUE, nestLevel); )
2883 while (TRUE)
2885 tokenInfo *token;
2887 nextToken (st);
2888 token = activeToken (st);
2890 if (isType (token, TOKEN_BRACE_CLOSE))
2892 if (nestLevel > 0)
2893 break;
2894 else
2896 verbose ("%s: unexpected closing brace at line %lu\n",
2897 getInputFileName (), getInputLineNumber ());
2898 longjmp (Exception, (int) ExceptionBraceFormattingError);
2901 else if (isType (token, TOKEN_DOUBLE_COLON))
2903 addContext (st, prevToken (st, 1));
2904 advanceToken (st);
2906 else
2908 tagCheck (st);
2909 if (isType (token, TOKEN_BRACE_OPEN))
2910 nest (st, nestLevel + 1);
2911 checkStatementEnd (st);
2914 deleteStatement ();
2915 DebugStatement ( if (nestLevel > 0) debugParseNest (FALSE, nestLevel - 1); )
2918 static boolean findCTags (const unsigned int passCount)
2920 exception_t exception;
2921 boolean retry;
2923 Assert (passCount < 3);
2924 cppInit ((boolean) (passCount > 1), isLanguage (Lang_csharp));
2925 Signature = vStringNew ();
2926 ReturnType = vStringNew ();
2928 exception = (exception_t) setjmp (Exception);
2929 retry = FALSE;
2930 if (exception == ExceptionNone)
2931 createTags (0, NULL);
2932 else
2934 deleteAllStatements ();
2935 if (exception == ExceptionBraceFormattingError && passCount == 1)
2937 retry = TRUE;
2938 verbose ("%s: retrying file with fallback brace matching algorithm\n",
2939 getInputFileName ());
2942 vStringDelete (Signature);
2943 vStringDelete (ReturnType);
2944 cppTerminate ();
2945 return retry;
2948 static void buildKeywordHash (const langType language, unsigned int idx)
2950 const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
2951 size_t i;
2952 for (i = 0 ; i < count ; ++i)
2954 const keywordDesc* const p = &KeywordTable [i];
2955 if (p->isValid [idx])
2956 addKeyword (p->name, language, (int) p->id);
2960 static void initializeCParser (const langType language)
2962 Lang_c = language;
2963 buildKeywordHash (language, 0);
2966 static void initializeCppParser (const langType language)
2968 Lang_cpp = language;
2969 buildKeywordHash (language, 1);
2972 static void initializeCsharpParser (const langType language)
2974 Lang_csharp = language;
2975 buildKeywordHash (language, 2);
2978 static void initializeJavaParser (const langType language)
2980 Lang_java = language;
2981 buildKeywordHash (language, 3);
2984 static void initializeVeraParser (const langType language)
2986 Lang_vera = language;
2987 buildKeywordHash (language, 4);
2990 extern parserDefinition* CParser (void)
2992 static const char *const extensions [] = { "c", NULL };
2993 parserDefinition* def = parserNew ("C");
2994 def->kinds = CKinds;
2995 def->kindCount = KIND_COUNT (CKinds);
2996 def->extensions = extensions;
2997 def->parser2 = findCTags;
2998 def->initialize = initializeCParser;
2999 return def;
3002 extern parserDefinition* CppParser (void)
3004 static const char *const extensions [] = {
3005 "c++", "cc", "cp", "cpp", "cxx", "h", "h++", "hh", "hp", "hpp", "hxx",
3006 #ifndef CASE_INSENSITIVE_FILENAMES
3007 "C", "H",
3008 #endif
3009 NULL
3011 parserDefinition* def = parserNew ("C++");
3012 def->kinds = CKinds;
3013 def->kindCount = KIND_COUNT (CKinds);
3014 def->extensions = extensions;
3015 def->parser2 = findCTags;
3016 def->initialize = initializeCppParser;
3017 return def;
3020 extern parserDefinition* CsharpParser (void)
3022 static const char *const extensions [] = { "cs", NULL };
3023 parserDefinition* def = parserNew ("C#");
3024 def->kinds = CsharpKinds;
3025 def->kindCount = KIND_COUNT (CsharpKinds);
3026 def->extensions = extensions;
3027 def->parser2 = findCTags;
3028 def->initialize = initializeCsharpParser;
3029 return def;
3032 extern parserDefinition* JavaParser (void)
3034 static const char *const extensions [] = { "java", NULL };
3035 parserDefinition* def = parserNew ("Java");
3036 def->kinds = JavaKinds;
3037 def->kindCount = KIND_COUNT (JavaKinds);
3038 def->extensions = extensions;
3039 def->parser2 = findCTags;
3040 def->initialize = initializeJavaParser;
3041 return def;
3044 extern parserDefinition* VeraParser (void)
3046 static const char *const extensions [] = { "vr", "vri", "vrh", NULL };
3047 parserDefinition* def = parserNew ("Vera");
3048 def->kinds = VeraKinds;
3049 def->kindCount = KIND_COUNT (VeraKinds);
3050 def->extensions = extensions;
3051 def->parser2 = findCTags;
3052 def->initialize = initializeVeraParser;
3053 return def;
3056 /* vi:set tabstop=4 shiftwidth=4 noexpandtab: */